Skip to content

Commit 64f5276

Browse files
committed
refactoring
1 parent 2af419b commit 64f5276

30 files changed

+370
-406
lines changed

pmd_rules.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<exclude name="CyclomaticComplexity"/>
3232
<exclude name="LoosePackageCoupling"/>
3333
<exclude name="UseUtilityClass"/>
34+
<exclude name="DataClass"/>
3435
</rule>
3536
<rule ref="category/java/design.xml/CyclomaticComplexity">
3637
<properties>

src/main/java/org/rulez/demokracia/pdengine/ChoiceManager.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import javax.xml.ws.WebServiceContext;
44

5+
import org.rulez.demokracia.pdengine.dataobjects.VoteAdminInfo;
56
import org.rulez.demokracia.pdengine.exception.ReportedException;
67

78
public class ChoiceManager extends VoteManager {
@@ -10,28 +11,33 @@ public ChoiceManager(final WebServiceContext wsContext) {
1011
super(wsContext);
1112
}
1213

13-
public String addChoice(final String adminKey, final String voteId, final String choiceName, final String user) {
14-
Vote vote = getVote(voteId);
15-
vote.checkAdminKey(adminKey);
16-
if(vote.hasIssuedBallots())
17-
throw new ReportedException("No choice can be added because there are ballots issued for the vote.");
14+
public String addChoice(final VoteAdminInfo voteAdminInfo, final String choiceName, final String user) {
15+
Vote vote = getVoteIfModifiable(voteAdminInfo.voteId, voteAdminInfo.adminKey);
1816

19-
return getVote(voteId).addChoice(choiceName, user);
17+
return vote.addChoice(choiceName, user);
2018
}
2119

2220
public Choice getChoice(final String voteId, final String choiceId) {
2321
return getVote(voteId).getChoice(choiceId);
2422
}
2523

26-
public void endorseChoice(final String adminKey, final String voteId, final String choiceId, final String givenUserName) {
24+
public void endorseChoice(final VoteAdminInfo voteAdminInfo, final String choiceId, final String givenUserName) {
2725
String userName = givenUserName;
28-
if("user".equals(adminKey)) {
29-
checkIfVoteIsEndorseable(voteId);
26+
if("user".equals(voteAdminInfo.adminKey)) {
27+
checkIfVoteIsEndorseable(voteAdminInfo.voteId);
3028
userName = getWsUserName();
3129
}
30+
Vote vote = getVote(voteAdminInfo.voteId);
31+
vote.checkAdminKey(voteAdminInfo.adminKey);
32+
vote.getChoice(choiceId).endorse(userName);
33+
}
34+
35+
protected Vote getVoteIfModifiable(final String voteId, final String adminKey) {
3236
Vote vote = getVote(voteId);
3337
vote.checkAdminKey(adminKey);
34-
vote.getChoice(choiceId).endorse(userName);
38+
if (vote.hasIssuedBallots())
39+
throw new ReportedException("Vote modification disallowed: ballots already issued");
40+
return vote;
3541
}
3642

3743
}

src/main/java/org/rulez/demokracia/pdengine/IVoteManager.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,45 @@
88
import org.json.JSONObject;
99
import org.rulez.demokracia.pdengine.dataobjects.ChoiceEntity;
1010
import org.rulez.demokracia.pdengine.dataobjects.VoteAdminInfo;
11-
import org.rulez.demokracia.pdengine.exception.ReportedException;
11+
import org.rulez.demokracia.pdengine.dataobjects.VoteParameters;
1212

1313
public interface IVoteManager {
1414

15-
static IVoteManager getVoteManager(WebServiceContext wsContext) {
15+
static IVoteManager getVoteManager(final WebServiceContext wsContext) {
1616
return VoteManagerUtils.getVoteManager(wsContext);
1717
}
1818

1919
WebServiceContext getWsContext();
2020

21-
VoteAdminInfo createVote(String voteName, Set<String> neededAssurances, Set<String> countedAssurances,
22-
boolean isPrivate, int minEndorsements) throws ReportedException;
21+
VoteAdminInfo createVote(final String voteName, final Set<String> neededAssurances, final Set<String> countedAssurances,
22+
final boolean isPrivate, final int minEndorsements);
2323

24-
Vote getVote(String voteId);
24+
Vote getVote(final String voteId);
2525

26-
String addChoice(String adminKey, String voteId, String choiceName, String user) throws ReportedException;
26+
String addChoice(final VoteAdminInfo voteAdminInfo, final String choiceName, final String user);
2727

28-
String deleteChoice(String voteId, String choiceId, String adminKey) throws ReportedException;
28+
String deleteChoice(final VoteAdminInfo voteAdminInfo, final String choiceId);
2929

30-
void modifyChoice(String voteId, String choiceId, String adminKey, String choice) throws ReportedException;
30+
void modifyChoice(final VoteAdminInfo adminInfo, final String choiceId, final String choiceName);
3131

32-
ChoiceEntity getChoice(String voteId, String choiceId) throws ReportedException;
32+
ChoiceEntity getChoice(final String voteId, final String choiceId);
3333

34-
void endorseChoice(String adminKey, String voteId, String choiceId, String statedUserName) throws ReportedException;
34+
void endorseChoice(final VoteAdminInfo voteAdminInfo, final String choiceId, final String statedUserName);
3535

36-
String obtainBallot(String identifier, String adminKey) throws ReportedException;
36+
String obtainBallot(final String identifier, final String adminKey);
3737

38-
void castVote(String voteId, String ballot, List<RankedChoice> theVote);
38+
void castVote(final String voteId, final String ballot, final List<RankedChoice> theVote);
3939

4040
String getWsUserName();
4141

42-
boolean hasAssurance(String role);
42+
boolean hasAssurance(final String role);
4343

44-
void modifyVote(String voteId, String adminKey, String votename) throws ReportedException;
44+
void modifyVote(final VoteAdminInfo voteAdminInfo, final String voteName);
4545

46-
void deleteVote(String voteId, String adminKey) throws ReportedException;
46+
void deleteVote(final VoteAdminInfo adminInfo);
4747

48-
JSONObject showVote(String voteId, String adminKey) throws ReportedException;
48+
JSONObject showVote(final VoteAdminInfo adminInfo);
4949

50-
void setVoteParameters(String voteId, String adminKey, int minEndorsements, boolean canAddin, boolean canEndorse,
51-
boolean canVote, boolean canView) throws ReportedException;
50+
void setVoteParameters(final VoteAdminInfo adminInfo, final VoteParameters voteParameters);
5251

5352
}

src/main/java/org/rulez/demokracia/pdengine/SessionFactoryManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
public class SessionFactoryManager {
88

99
protected Session session;
10-
private WebServiceContext wsContext;
10+
private final WebServiceContext wsContext;
1111

1212
public SessionFactoryManager(final WebServiceContext wsContext) {
1313
super();

src/main/java/org/rulez/demokracia/pdengine/ValidationUtil.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import org.rulez.demokracia.pdengine.exception.IsNullException;
99
import org.rulez.demokracia.pdengine.exception.NotValidCharsException;
10-
import org.rulez.demokracia.pdengine.exception.ReportedException;
1110
import org.rulez.demokracia.pdengine.exception.TooLongException;
1211
import org.rulez.demokracia.pdengine.exception.TooShortException;
1312

@@ -22,7 +21,7 @@ public static void checkVoteName(final String voteName) {
2221
checkStringWithSpaces(voteName, "vote name");
2322
}
2423

25-
public static Set<String> checkAssurances(final Set<String> neededAssurances, final String type) throws ReportedException{
24+
public static Set<String> checkAssurances(final Set<String> neededAssurances, final String type) {
2625

2726
Set<String> uniques = new HashSet<>();
2827

src/main/java/org/rulez/demokracia/pdengine/VoteRegistry.java

Lines changed: 59 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import javax.xml.ws.WebServiceContext;
66

77
import org.json.JSONObject;
8+
import org.rulez.demokracia.pdengine.dataobjects.VoteAdminInfo;
9+
import org.rulez.demokracia.pdengine.dataobjects.VoteParameters;
810
import org.rulez.demokracia.pdengine.exception.ReportedException;
911

1012
public class VoteRegistry extends ChoiceManager implements IVoteManager {
@@ -48,21 +50,11 @@ public boolean userHasAllAssurance(final List<String> neededAssuranceList) {
4850
@Override
4951
public void castVote(final String voteId, final String ballot, final List<RankedChoice> theVote) {
5052
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");
5653

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);
6658

6759
if (vote.canUpdate)
6860
vote.addCastVote(getWsUserName(), theVote);
@@ -72,45 +64,69 @@ public void castVote(final String voteId, final String ballot, final List<Ranked
7264
vote.ballots.remove(ballot);
7365
}
7466

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+
7595
@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);
80100

81101
if (vote.hasIssuedBallots())
82102
throw new IllegalArgumentException("The vote cannot be modified there are ballots issued.");
83103

84-
vote.name = votename;
104+
vote.name = voteName;
85105
}
86106

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);
90110

91111
if (vote.hasIssuedBallots())
92112
throw new IllegalArgumentException("This vote cannot be deleted it has issued ballots.");
93113

94114
session.remove(vote);
95115
}
96116

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);
100120

101-
return vote.toJson(voteId);
121+
return vote.toJson(adminInfo.voteId);
102122
}
103123

104124
@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);
111127

112128
Choice votesChoice = vote.getChoice(choiceId);
113-
if ("user".equals(adminKey))
129+
if ("user".equals(voteAdminInfo.adminKey))
114130
if (votesChoice.userName.equals(getWsUserName()))
115131
if (vote.canAddin)
116132
vote.choices.remove(votesChoice.id);
@@ -124,15 +140,11 @@ public String deleteChoice(final String voteId, final String choiceId, final Str
124140
return "OK";
125141
}
126142

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);
133145

134146
Choice votesChoice = vote.getChoice(choiceId);
135-
if ("user".equals(adminKey)) {
147+
if ("user".equals(adminInfo.adminKey)) {
136148
if (!vote.canAddin)
137149
throw new ReportedException(
138150
"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
144156
votesChoice.userName);
145157
}
146158

147-
votesChoice.name = choice;
159+
votesChoice.name = choiceName;
148160
}
149161

150162
@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);
155166

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);
158169
else
159-
throw new IllegalArgumentException(String.format("Illegal minEndorsements: %s", minEndorsements));
170+
throw new ReportedException("Illegal minEndorsements", Integer.toString(voteParameters.minEndorsements));
160171
}
161172
}

src/main/java/org/rulez/demokracia/pdengine/dataobjects/VoteAdminInfo.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,13 @@
33
public class VoteAdminInfo {
44
public String adminKey;
55
public String voteId;
6+
7+
public VoteAdminInfo(final String voteId, final String adminKey) {
8+
this.voteId = voteId;
9+
this.adminKey = adminKey;
10+
}
11+
12+
public VoteAdminInfo() {
13+
}
614
}
715

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.rulez.demokracia.pdengine.dataobjects;
2+
3+
public class VoteParameters {
4+
public int minEndorsements;
5+
public boolean canAddin;
6+
public boolean canEndorse;
7+
public boolean canVote;
8+
public boolean canView;
9+
10+
public VoteParameters(
11+
final int minEndorsements,
12+
final boolean canAddin,
13+
final boolean canEndorse,
14+
final boolean canVote,
15+
final boolean canView) {
16+
this.minEndorsements = minEndorsements;
17+
this.canAddin = canAddin;
18+
this.canEndorse = canEndorse;
19+
this.canVote = canVote;
20+
this.canView = canView;
21+
}
22+
}

0 commit comments

Comments
 (0)