Skip to content

Commit dfc50dc

Browse files
committed
a step towards traits pattern (#296)
* a step towards traits pattern
1 parent 74b7b32 commit dfc50dc

27 files changed

+281
-176
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.rulez.demokracia.pdengine;
2+
3+
import org.rulez.demokracia.pdengine.exception.ReportedException;
4+
5+
public interface Admnistrable extends VoteInterface {
6+
7+
default void setParameters(final int minEndorsements, final boolean canAddin, final boolean canEndorse, final boolean canVote,
8+
final boolean canView) {
9+
getParameters().minEndorsements = minEndorsements;
10+
getParameters().canAddin = canAddin;
11+
getParameters().canEndorse = canEndorse;
12+
getParameters().canVote = canVote;
13+
getParameters().canView = canView;
14+
}
15+
16+
default void checkAdminKey(final String providedAdminKey) {
17+
if (!(getAdminKey().equals(providedAdminKey) || "user".equals(providedAdminKey))) {
18+
throw new ReportedException("Illegal adminKey", providedAdminKey);
19+
}
20+
}
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.rulez.demokracia.pdengine;
2+
3+
public interface Endorseable extends VoteInterface {
4+
5+
default boolean isEndorseable() {
6+
return getParameters().canEndorse;
7+
}
8+
9+
10+
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.rulez.demokracia.pdengine;
2+
3+
public interface HasBallots extends VoteInterface {
4+
5+
default Integer getRecordedBallotsCount(final String userName) {
6+
return getRecordedBallots().containsKey(userName) ? getRecordedBallots().get(userName) : 0;
7+
}
8+
9+
default void increaseRecordedBallots(final String key) {
10+
getRecordedBallots().put(key, getRecordedBallotsCount(key) + 1);
11+
}
12+
13+
default boolean hasIssuedBallots() {
14+
return !getBallots().isEmpty();
15+
}
16+
17+
default void addBallot(String ballot) {
18+
getBallots().add(ballot);
19+
}
20+
21+
default void removeBallot(String ballot) {
22+
this.getBallots().remove(ballot);
23+
}
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.rulez.demokracia.pdengine;
2+
3+
import org.rulez.demokracia.pdengine.exception.ReportedException;
4+
5+
public interface HasChoices extends VoteInterface {
6+
7+
default String addChoice(final String choiceName, final String user) {
8+
Choice choice = new Choice(choiceName, user);
9+
getChoices().put(choice.id, choice);
10+
return choice.id;
11+
}
12+
13+
default Choice getChoice(final String choiceId) {
14+
if (!getChoices().containsKey(choiceId)) {
15+
throw new ReportedException("Illegal choiceId", choiceId);
16+
}
17+
return getChoices().get(choiceId);
18+
}
19+
}

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

+58-73
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.util.ArrayList;
55
import java.util.Collection;
66
import java.util.HashMap;
7-
import java.util.Iterator;
87
import java.util.List;
98
import java.util.Map;
109
import java.util.Map.Entry;
@@ -15,120 +14,106 @@
1514
import org.json.JSONObject;
1615
import org.rulez.demokracia.pdengine.dataobjects.CastVote;
1716
import org.rulez.demokracia.pdengine.dataobjects.VoteEntity;
18-
import org.rulez.demokracia.pdengine.exception.ReportedException;
17+
import org.rulez.demokracia.pdengine.dataobjects.VoteParameters;
1918

2019
@Entity
21-
public class Vote extends VoteEntity {
20+
public class Vote extends VoteEntity implements VoteInterface, Admnistrable, HasChoices, HasBallots, Endorseable, Voteable {
2221

2322
private static final long serialVersionUID = 1L;
2423

2524
public Vote(final String voteName, final Collection<String> neededAssurances, final Collection<String> countedAssurances,
2625
final boolean isClosed, final int minEndorsements) {
2726
super();
28-
name = voteName;
29-
adminKey = RandomUtils.createRandomKey();
27+
this.name = voteName;
28+
this.adminKey = RandomUtils.createRandomKey();
3029
this.neededAssurances = new ArrayList<>(neededAssurances);
3130
this.countedAssurances = new ArrayList<>(countedAssurances);
32-
isPrivate = isClosed;
33-
this.minEndorsements = minEndorsements;
34-
creationTime = Instant.now().getEpochSecond();
35-
choices = new HashMap<>();
36-
ballots = new ArrayList<>();
37-
votesCast = new ArrayList<>();
38-
recordedBallots = new HashMap<>();
31+
this.isPrivate = isClosed;
32+
this.parameters = new VoteParameters();
33+
this.parameters.minEndorsements = minEndorsements;
34+
this.creationTime = Instant.now().getEpochSecond();
35+
this.choices = new HashMap<>();
36+
this.ballots = new ArrayList<>();
37+
this.votesCast = new ArrayList<>();
38+
this.recordedBallots = new HashMap<>();
3939
}
4040

41-
public Integer getRecordedBallotsCount(final String userName) {
42-
return recordedBallots.containsKey(userName) ? recordedBallots.get(userName) : 0;
43-
}
44-
45-
public void increaseRecordedBallots(final String key) {
46-
recordedBallots.put(key, getRecordedBallotsCount(key) + 1);
47-
}
48-
49-
public String addChoice(final String choiceName, final String user) {
50-
Choice choice = new Choice(choiceName, user);
51-
choices.put(choice.id, choice);
52-
return choice.id;
53-
}
54-
55-
public Choice getChoice(final String choiceId) {
56-
if (!choices.containsKey(choiceId)) {
57-
throw new ReportedException("Illegal choiceId", choiceId);
58-
}
59-
return choices.get(choiceId);
60-
}
61-
62-
public boolean hasIssuedBallots() {
63-
return !ballots.isEmpty();
64-
}
65-
66-
public void setParameters(final int minEndorsements, final boolean canAddin, final boolean canEndorse, final boolean canVote,
67-
final boolean canView) {
68-
this.minEndorsements = minEndorsements;
69-
this.canAddin = canAddin;
70-
this.canEndorse = canEndorse;
71-
this.canVote = canVote;
72-
this.canView = canView;
73-
}
74-
75-
public void checkAdminKey(final String providedAdminKey) {
76-
if (!(adminKey.equals(providedAdminKey) || "user".equals(providedAdminKey))) {
77-
throw new ReportedException("Illegal adminKey", providedAdminKey);
78-
}
79-
}
80-
81-
public JSONObject toJson(final String voteId) {
41+
public JSONObject toJson() {
8242
JSONObject obj = new JSONObject();
8343
obj.put("name", this.name);
84-
obj.put("canAddIn", this.canAddin);
44+
obj.put("canAddIn", this.parameters.canAddin);
8545
obj.put("creationTime", this.creationTime);
8646
obj.put("choices", createChoicesJson(this.choices));
87-
obj.put("canEndorse", this.canEndorse);
47+
obj.put("canEndorse", this.parameters.canEndorse);
8848
obj.put("countedAssurances", this.countedAssurances);
8949
obj.put("neededAssurances", this.neededAssurances);
90-
obj.put("minEndorsements", this.minEndorsements);
91-
obj.put("id", voteId);
92-
obj.put("canView", this.canView);
93-
obj.put("canVote", this.canVote);
50+
obj.put("minEndorsements", this.parameters.minEndorsements);
51+
obj.put("id", this.id);
52+
obj.put("canView", this.parameters.canView);
53+
obj.put("canVote", this.parameters.canVote);
9454
return obj;
9555
}
9656

9757
public JSONArray createChoicesJson(final Map<String, Choice> choices) {
9858
JSONArray array = new JSONArray();
9959

10060
for (Entry<String, Choice> entry : choices.entrySet()) {
101-
String key = entry.getKey();
10261
Choice value = entry.getValue();
10362

104-
JSONObject obj = choiceAsJson(key, value);
63+
JSONObject obj = choiceAsJson(value);
10564

10665
array.put(obj);
10766
}
10867

10968
return array;
11069
}
11170

112-
private JSONObject choiceAsJson(final String key, final Choice choice) {
71+
private JSONObject choiceAsJson(final Choice choice) {
11372
JSONObject obj = new JSONObject();
11473
obj.put("initiator", choice.userName);
11574
obj.put("endorsers", choice.endorsers);
11675
obj.put("name", choice.name);
117-
obj.put("id", key);
76+
obj.put("id", choice.id);
11877
return obj;
11978
}
12079

121-
protected CastVote addCastVote(final String proxyId, final List<RankedChoice> theVote) {
122-
Iterator<CastVote> listIterator = votesCast.iterator();
123-
while (listIterator.hasNext()) {
124-
CastVote element = listIterator.next();
80+
@Override
81+
public VoteParameters getParameters() {
82+
return this.parameters;
83+
}
12584

126-
if (element.proxyId != null && element.proxyId.equals(proxyId))
127-
listIterator.remove();
128-
}
85+
@Override
86+
public String getAdminKey() {
87+
return this.adminKey;
88+
}
89+
90+
@Override
91+
public Map<String, Choice> getChoices() {
92+
return this.choices;
93+
}
94+
95+
@Override
96+
public Map<String, Integer> getRecordedBallots() {
97+
return this.recordedBallots;
98+
}
99+
100+
@Override
101+
public List<String> getBallots() {
102+
return this.ballots;
103+
}
104+
105+
@Override
106+
public List<CastVote> getVotesCast() {
107+
return this.votesCast;
108+
}
109+
110+
@Override
111+
public String getId() {
112+
return this.id;
113+
}
129114

130-
CastVote castVote = new CastVote(proxyId, theVote);
131-
votesCast.add(castVote);
132-
return castVote;
115+
@Override
116+
public List<String> getNeededAssurances() {
117+
return new ArrayList<>(this.neededAssurances);
133118
}
134119
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.rulez.demokracia.pdengine;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
import org.rulez.demokracia.pdengine.dataobjects.CastVote;
7+
import org.rulez.demokracia.pdengine.dataobjects.VoteParameters;
8+
9+
public interface VoteInterface {
10+
11+
VoteParameters getParameters();
12+
13+
String getAdminKey();
14+
15+
Map<String,Choice> getChoices();
16+
17+
Map<String,Integer> getRecordedBallots();
18+
19+
List<String> getBallots();
20+
21+
List<CastVote> getVotesCast();
22+
23+
String getId();
24+
25+
List<String> getNeededAssurances();
26+
27+
}

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import javax.xml.ws.WebServiceContext;
66

77
import org.rulez.demokracia.pdengine.dataobjects.VoteAdminInfo;
8-
import org.rulez.demokracia.pdengine.dataobjects.VoteEntity;
98
import org.rulez.demokracia.pdengine.exception.ReportedException;
109

1110
public class VoteManager extends SessionFactoryManager {
@@ -21,14 +20,14 @@ public VoteAdminInfo createVote(final String voteName, final Set<String> neededA
2120
ValidationUtil.checkVoteName(voteName);
2221

2322
VoteAdminInfo admininfo = new VoteAdminInfo();
24-
VoteEntity vote = new Vote (
23+
Vote vote = new Vote (
2524
voteName,
2625
ValidationUtil.checkAssurances(neededAssurances, "needed"),
2726
ValidationUtil.checkAssurances(countedAssurances, "counted"),
2827
isClosed,
2928
minEndorsements);
30-
admininfo.adminKey = vote.adminKey;
31-
admininfo.voteId = vote.id;
29+
admininfo.adminKey = vote.getAdminKey();
30+
admininfo.voteId = vote.getId();
3231
session.save(vote);
3332
return admininfo;
3433
}
@@ -48,7 +47,7 @@ private void validateVoteId(final String voteId, final Vote vote) {
4847

4948
protected void checkIfVoteIsEndorseable(final String voteId) {
5049
Vote vote = getVote(voteId);
51-
if(! vote.canEndorse) {
50+
if(! vote.isEndorseable()) {
5251
throw new ReportedException("user cannot endorse this vote");
5352
}
5453
}

0 commit comments

Comments
 (0)