This repository was archived by the owner on Oct 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathVoteRegistry.java
More file actions
236 lines (194 loc) · 7.08 KB
/
VoteRegistry.java
File metadata and controls
236 lines (194 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package org.rulez.demokracia.pdengine;
import java.util.List;
import javax.xml.ws.WebServiceContext;
import org.rulez.demokracia.pdengine.dataobjects.VoteAdminInfo;
import org.rulez.demokracia.pdengine.dataobjects.VoteParameters;
import org.rulez.demokracia.pdengine.exception.ReportedException;
import com.google.gson.JsonObject;
public class VoteRegistry extends ChoiceManager implements IVoteManager {
private final AssuranceManager assuranceManager;
public VoteRegistry(
final WebServiceContext wsContext, final AssuranceManager assuranceManager
) {
super(wsContext);
this.assuranceManager = assuranceManager;
}
@Override
public String obtainBallot(final String identifier, final String adminKey) {
final Vote vote = getVote(identifier);
vote.checkAdminKey(adminKey);
if (adminKey.equals(vote.getAdminKey()))
vote.increaseRecordedBallots("admin");
else {
if (getWsContext().getUserPrincipal() == null)
throw new IllegalArgumentException(
"Simple user is not authenticated, cannot issue any ballot."
);
if (!userHasAllAssurance(vote.getNeededAssurances()))
throw new IllegalArgumentException(
"The user does not have all of the needed assurances."
);
if (vote.getRecordedBallotsCount(getWsUserName()).intValue() > 0)
throw new IllegalArgumentException("This user already have a ballot.");
vote.increaseRecordedBallots(getWsUserName());
}
final String ballot = RandomUtils.createRandomKey();
vote.addBallot(ballot);
return ballot;
}
public boolean userHasAllAssurance(final List<String> neededAssuranceList) {
for (final String neededAssurance : neededAssuranceList)
if (!hasAssurance(neededAssurance))
return false;
return true;
}
@Override
public CastVote castVote(
final String voteId, final String ballot, final List<RankedChoice> theVote
) {
final Vote vote = getVote(voteId);
checkIfVotingEnabled(vote);
checkIfUpdateConditionsAreConsistent(vote);
validateBallot(ballot, vote);
validatePreferences(theVote, vote);
CastVote receipt;
if (vote.getParameters().canUpdate) {
final String proxyId = getWsUserName();
receipt = vote.addCastVote(proxyId, theVote, getAssurances());
} else
receipt = vote.addCastVote(null, theVote);
vote.removeBallot(ballot);
return receipt;
}
private void
validatePreferences(final List<RankedChoice> theVote, final Vote vote) {
for (final RankedChoice choice : theVote)
validateOnePreference(vote, choice);
}
private void
validateOnePreference(final Vote vote, final RankedChoice choice) {
if (!vote.getChoices().containsKey(choice.choiceId))
throw new ReportedException("Invalid choiceId");
if (choice.rank < 0)
throw new ReportedException("Invalid rank");
}
private void validateBallot(final String ballot, final Vote vote) {
if (!vote.getBallots().contains(ballot))
throw new ReportedException("Illegal ballot");
}
private void checkIfUpdateConditionsAreConsistent(final Vote vote) {
if (
vote.getParameters().canUpdate &&
getWsContext().getUserPrincipal() == null
)
throw new ReportedException(
"canUpdate is true but the user is not authenticated"
);
}
private void checkIfVotingEnabled(final Vote vote) {
if (!vote.parameters.canVote)
throw new ReportedException("This issue cannot be voted on yet");
}
@Override
public void
modifyVote(final VoteAdminInfo voteAdminInfo, final String voteName) {
ValidationUtil.checkVoteName(voteName);
final Vote vote = checkIfVoteCanBeModified(voteAdminInfo);
vote.name = voteName;
}
@Override
public void deleteVote(final VoteAdminInfo adminInfo) {
final Vote vote = checkIfVoteCanBeModified(adminInfo);
session.remove(vote);
}
private Vote checkIfVoteCanBeModified(final VoteAdminInfo adminInfo) {
final Vote vote = checkAdminInfo(adminInfo);
if (vote.hasIssuedBallots())
throw new IllegalArgumentException(
"This vote cannot be modified it has issued ballots."
);
return vote;
}
private Vote checkAdminInfo(final VoteAdminInfo adminInfo) {
final Vote vote = getVote(adminInfo.voteId);
vote.checkAdminKey(adminInfo.adminKey);
return vote;
}
@Override
public JsonObject showVote(final VoteAdminInfo adminInfo) {
final Vote vote = checkAdminInfo(adminInfo);
if (!adminInfo.adminKey.equals(vote.adminKey))
checkAssurances(vote);
return vote.toJson();
}
private void checkAssurances(final Vote vote) {
for (final String assurance : vote.countedAssurances)
if (!hasAssurance(assurance))
throw new ReportedException("missing assurances", assurance);
}
@Override
public String
deleteChoice(final VoteAdminInfo voteAdminInfo, final String choiceId) {
final Vote vote =
getVoteIfModifiable(voteAdminInfo.voteId, voteAdminInfo.adminKey);
final Choice votesChoice = vote.getChoice(choiceId);
if ("user".equals(voteAdminInfo.adminKey)) {
if (votesChoice.userName.equals(getWsUserName())) {
if (vote.parameters.canAddin)
vote.choices.remove(votesChoice.id);
else
throw new IllegalArgumentException(
"The adminKey is \"user\" but canAddin is false."
);
} else
throw new IllegalArgumentException(
"The adminKey is \"user\" but the user is not same with that user who added the choice."
);
} else
vote.choices.remove(votesChoice.id);
return "OK";
}
@Override
public void modifyChoice(
final VoteAdminInfo adminInfo, final String choiceId,
final String choiceName
) {
final Vote vote = getVoteIfModifiable(adminInfo.voteId, adminInfo.adminKey);
final Choice votesChoice = vote.getChoice(choiceId);
if ("user".equals(adminInfo.adminKey)) {
if (!vote.parameters.canAddin)
throw new ReportedException(
"Choice modification disallowed: adminKey is user, but canAddin is false"
);
if (!votesChoice.userName.equals(getWsUserName()))
throw new ReportedException(
"Choice modification disallowed: adminKey is user, " +
"and the choice was added by a different user",
votesChoice.userName
);
}
votesChoice.name = choiceName;
}
@Override
public void setVoteParameters(
final VoteAdminInfo adminInfo, final VoteParameters voteParameters
) {
final Vote vote = checkAdminInfo(adminInfo);
if (voteParameters.minEndorsements >= 0)
vote.setParameters(
voteParameters.minEndorsements, voteParameters.canAddin,
voteParameters.canEndorse,
voteParameters.canVote, voteParameters.canView
);
else
throw new ReportedException(
"Illegal minEndorsements",
Integer.toString(voteParameters.minEndorsements)
);
}
@Override
public List<String> getAssurances() {
final String wsUserName = getWsUserName();
return assuranceManager.getAssurances(wsUserName);
}
}