|
4 | 4 | import java.util.ArrayList;
|
5 | 5 | import java.util.Collection;
|
6 | 6 | import java.util.HashMap;
|
7 |
| -import java.util.Iterator; |
8 | 7 | import java.util.List;
|
9 | 8 | import java.util.Map;
|
10 | 9 | import java.util.Map.Entry;
|
|
15 | 14 | import org.json.JSONObject;
|
16 | 15 | import org.rulez.demokracia.pdengine.dataobjects.CastVote;
|
17 | 16 | import org.rulez.demokracia.pdengine.dataobjects.VoteEntity;
|
18 |
| -import org.rulez.demokracia.pdengine.exception.ReportedException; |
| 17 | +import org.rulez.demokracia.pdengine.dataobjects.VoteParameters; |
19 | 18 |
|
20 | 19 | @Entity
|
21 |
| -public class Vote extends VoteEntity { |
| 20 | +public class Vote extends VoteEntity implements VoteInterface, Admnistrable, HasChoices, HasBallots, Endorseable, Voteable { |
22 | 21 |
|
23 | 22 | private static final long serialVersionUID = 1L;
|
24 | 23 |
|
25 | 24 | public Vote(final String voteName, final Collection<String> neededAssurances, final Collection<String> countedAssurances,
|
26 | 25 | final boolean isClosed, final int minEndorsements) {
|
27 | 26 | super();
|
28 |
| - name = voteName; |
29 |
| - adminKey = RandomUtils.createRandomKey(); |
| 27 | + this.name = voteName; |
| 28 | + this.adminKey = RandomUtils.createRandomKey(); |
30 | 29 | this.neededAssurances = new ArrayList<>(neededAssurances);
|
31 | 30 | 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<>(); |
39 | 39 | }
|
40 | 40 |
|
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() { |
82 | 42 | JSONObject obj = new JSONObject();
|
83 | 43 | obj.put("name", this.name);
|
84 |
| - obj.put("canAddIn", this.canAddin); |
| 44 | + obj.put("canAddIn", this.parameters.canAddin); |
85 | 45 | obj.put("creationTime", this.creationTime);
|
86 | 46 | obj.put("choices", createChoicesJson(this.choices));
|
87 |
| - obj.put("canEndorse", this.canEndorse); |
| 47 | + obj.put("canEndorse", this.parameters.canEndorse); |
88 | 48 | obj.put("countedAssurances", this.countedAssurances);
|
89 | 49 | 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); |
94 | 54 | return obj;
|
95 | 55 | }
|
96 | 56 |
|
97 | 57 | public JSONArray createChoicesJson(final Map<String, Choice> choices) {
|
98 | 58 | JSONArray array = new JSONArray();
|
99 | 59 |
|
100 | 60 | for (Entry<String, Choice> entry : choices.entrySet()) {
|
101 |
| - String key = entry.getKey(); |
102 | 61 | Choice value = entry.getValue();
|
103 | 62 |
|
104 |
| - JSONObject obj = choiceAsJson(key, value); |
| 63 | + JSONObject obj = choiceAsJson(value); |
105 | 64 |
|
106 | 65 | array.put(obj);
|
107 | 66 | }
|
108 | 67 |
|
109 | 68 | return array;
|
110 | 69 | }
|
111 | 70 |
|
112 |
| - private JSONObject choiceAsJson(final String key, final Choice choice) { |
| 71 | + private JSONObject choiceAsJson(final Choice choice) { |
113 | 72 | JSONObject obj = new JSONObject();
|
114 | 73 | obj.put("initiator", choice.userName);
|
115 | 74 | obj.put("endorsers", choice.endorsers);
|
116 | 75 | obj.put("name", choice.name);
|
117 |
| - obj.put("id", key); |
| 76 | + obj.put("id", choice.id); |
118 | 77 | return obj;
|
119 | 78 | }
|
120 | 79 |
|
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 | + } |
125 | 84 |
|
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 | + } |
129 | 114 |
|
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); |
133 | 118 | }
|
134 | 119 | }
|
0 commit comments