5
5
import javax .xml .ws .WebServiceContext ;
6
6
7
7
import org .json .JSONObject ;
8
+ import org .rulez .demokracia .pdengine .dataobjects .VoteAdminInfo ;
9
+ import org .rulez .demokracia .pdengine .dataobjects .VoteParameters ;
8
10
import org .rulez .demokracia .pdengine .exception .ReportedException ;
9
11
10
12
public class VoteRegistry extends ChoiceManager implements IVoteManager {
@@ -48,21 +50,11 @@ public boolean userHasAllAssurance(final List<String> neededAssuranceList) {
48
50
@ Override
49
51
public void castVote (final String voteId , final String ballot , final List <RankedChoice > theVote ) {
50
52
Vote vote = getVote (voteId );
51
- if (!vote .canVote )
52
- throw new ReportedException ("This issue cannot be voted on yet" );
53
-
54
- if (vote .canUpdate && getWsContext ().getUserPrincipal () == null )
55
- throw new ReportedException ("canUpdate is true but the user is not authenticated" );
56
53
57
- if (!vote .ballots .contains (ballot ))
58
- throw new ReportedException ("Illegal ballot" );
59
-
60
- for (RankedChoice choice : theVote ) {
61
- if (!vote .choices .containsKey (choice .choiceId ))
62
- throw new ReportedException ("Invalid choiceId" );
63
- if (choice .rank < 0 )
64
- throw new ReportedException ("Invalid rank" );
65
- }
54
+ checkIfVotingEnabled (vote );
55
+ checkIfUpdateConditionsAreConsistent (vote );
56
+ validateBallot (ballot , vote );
57
+ validatePreferences (theVote , vote );
66
58
67
59
if (vote .canUpdate )
68
60
vote .addCastVote (getWsUserName (), theVote );
@@ -72,45 +64,69 @@ public void castVote(final String voteId, final String ballot, final List<Ranked
72
64
vote .ballots .remove (ballot );
73
65
}
74
66
67
+ private void validatePreferences (final List <RankedChoice > theVote , Vote vote ) {
68
+ for (RankedChoice choice : theVote ) {
69
+ validateOnePreference (vote , choice );
70
+ }
71
+ }
72
+
73
+ private void validateOnePreference (final Vote vote , final RankedChoice choice ) {
74
+ if (!vote .choices .containsKey (choice .choiceId ))
75
+ throw new ReportedException ("Invalid choiceId" );
76
+ if (choice .rank < 0 )
77
+ throw new ReportedException ("Invalid rank" );
78
+ }
79
+
80
+ private void validateBallot (final String ballot , final Vote vote ) {
81
+ if (!vote .ballots .contains (ballot ))
82
+ throw new ReportedException ("Illegal ballot" );
83
+ }
84
+
85
+ private void checkIfUpdateConditionsAreConsistent (final Vote vote ) {
86
+ if (vote .canUpdate && getWsContext ().getUserPrincipal () == null )
87
+ throw new ReportedException ("canUpdate is true but the user is not authenticated" );
88
+ }
89
+
90
+ private void checkIfVotingEnabled (final Vote vote ) {
91
+ if (!vote .canVote )
92
+ throw new ReportedException ("This issue cannot be voted on yet" );
93
+ }
94
+
75
95
@ Override
76
- public void modifyVote (final String voteId , final String adminKey , final String votename ) {
77
- ValidationUtil . checkVoteName ( votename );
78
- Vote vote = getVote ( voteId );
79
- vote . checkAdminKey ( adminKey );
96
+ public void modifyVote (final VoteAdminInfo voteAdminInfo , final String voteName ) {
97
+ Vote vote = getVote ( voteAdminInfo . voteId );
98
+ vote . checkAdminKey ( voteAdminInfo . adminKey );
99
+ ValidationUtil . checkVoteName ( voteName );
80
100
81
101
if (vote .hasIssuedBallots ())
82
102
throw new IllegalArgumentException ("The vote cannot be modified there are ballots issued." );
83
103
84
- vote .name = votename ;
104
+ vote .name = voteName ;
85
105
}
86
106
87
- public void deleteVote (final String voteId , final String adminKey ) {
88
- Vote vote = getVote (voteId );
89
- vote .checkAdminKey (adminKey );
107
+ public void deleteVote (final VoteAdminInfo adminInfo ) {
108
+ Vote vote = getVote (adminInfo . voteId );
109
+ vote .checkAdminKey (adminInfo . adminKey );
90
110
91
111
if (vote .hasIssuedBallots ())
92
112
throw new IllegalArgumentException ("This vote cannot be deleted it has issued ballots." );
93
113
94
114
session .remove (vote );
95
115
}
96
116
97
- public JSONObject showVote (final String voteId , final String adminKey ) {
98
- Vote vote = getVote (voteId );
99
- vote .checkAdminKey (adminKey );
117
+ public JSONObject showVote (final VoteAdminInfo adminInfo ) {
118
+ Vote vote = getVote (adminInfo . voteId );
119
+ vote .checkAdminKey (adminInfo . adminKey );
100
120
101
- return vote .toJson (voteId );
121
+ return vote .toJson (adminInfo . voteId );
102
122
}
103
123
104
124
@ Override
105
- public String deleteChoice (final String voteId , final String choiceId , final String adminKey ) {
106
- Vote vote = getVote (voteId );
107
- vote .checkAdminKey (adminKey );
108
-
109
- if (vote .hasIssuedBallots ())
110
- throw new IllegalArgumentException ("This choice cannot be deleted the vote has issued ballots." );
125
+ public String deleteChoice (final VoteAdminInfo voteAdminInfo , final String choiceId ) {
126
+ Vote vote = getVoteIfModifiable (voteAdminInfo .voteId , voteAdminInfo .adminKey );
111
127
112
128
Choice votesChoice = vote .getChoice (choiceId );
113
- if ("user" .equals (adminKey ))
129
+ if ("user" .equals (voteAdminInfo . adminKey ))
114
130
if (votesChoice .userName .equals (getWsUserName ()))
115
131
if (vote .canAddin )
116
132
vote .choices .remove (votesChoice .id );
@@ -124,15 +140,11 @@ public String deleteChoice(final String voteId, final String choiceId, final Str
124
140
return "OK" ;
125
141
}
126
142
127
- public void modifyChoice (final String voteId , final String choiceId , final String adminKey , final String choice ) {
128
- Vote vote = getVote (voteId );
129
- vote .checkAdminKey (adminKey );
130
-
131
- if (vote .hasIssuedBallots ())
132
- throw new ReportedException ("Choice modification disallowed: ballots already issued" );
143
+ public void modifyChoice (final VoteAdminInfo adminInfo , final String choiceId , final String choiceName ) {
144
+ Vote vote = getVoteIfModifiable (adminInfo .voteId , adminInfo .adminKey );
133
145
134
146
Choice votesChoice = vote .getChoice (choiceId );
135
- if ("user" .equals (adminKey )) {
147
+ if ("user" .equals (adminInfo . adminKey )) {
136
148
if (!vote .canAddin )
137
149
throw new ReportedException (
138
150
"Choice modification disallowed: adminKey is user, but canAddin is false" );
@@ -144,18 +156,17 @@ public void modifyChoice(final String voteId, final String choiceId, final Strin
144
156
votesChoice .userName );
145
157
}
146
158
147
- votesChoice .name = choice ;
159
+ votesChoice .name = choiceName ;
148
160
}
149
161
150
162
@ Override
151
- public void setVoteParameters (final String voteId , final String adminKey , final int minEndorsements , final boolean canAddin ,
152
- final boolean canEndorse , final boolean canVote , final boolean canView ) {
153
- Vote vote = getVote (voteId );
154
- vote .checkAdminKey (adminKey );
163
+ public void setVoteParameters (final VoteAdminInfo adminInfo , final VoteParameters voteParameters ) {
164
+ Vote vote = getVote (adminInfo .voteId );
165
+ vote .checkAdminKey (adminInfo .adminKey );
155
166
156
- if (minEndorsements >= 0 )
157
- vote .setParameters (minEndorsements , canAddin , canEndorse , canVote , canView );
167
+ if (voteParameters . minEndorsements >= 0 )
168
+ vote .setParameters (voteParameters . minEndorsements , voteParameters . canAddin , voteParameters . canEndorse , voteParameters . canVote , voteParameters . canView );
158
169
else
159
- throw new IllegalArgumentException ( String . format ( "Illegal minEndorsements: %s " , minEndorsements ));
170
+ throw new ReportedException ( "Illegal minEndorsements" , Integer . toString ( voteParameters . minEndorsements ));
160
171
}
161
172
}
0 commit comments