1
2
3
4
5
6 package com.hack23.cia.model.sweden.impl;
7
8 import java.util.ArrayList;
9 import java.util.Date;
10 import java.util.List;
11
12 import javax.persistence.CascadeType;
13 import javax.persistence.Entity;
14 import javax.persistence.EnumType;
15 import javax.persistence.Enumerated;
16 import javax.persistence.FetchType;
17 import javax.persistence.GeneratedValue;
18 import javax.persistence.GenerationType;
19 import javax.persistence.Id;
20 import javax.persistence.JoinTable;
21 import javax.persistence.ManyToOne;
22 import javax.persistence.OneToMany;
23 import javax.persistence.Table;
24 import javax.persistence.Temporal;
25 import javax.persistence.TemporalType;
26 import javax.persistence.Transient;
27 import javax.persistence.UniqueConstraint;
28 import javax.persistence.Version;
29
30 import org.hibernate.annotations.Cache;
31 import org.hibernate.annotations.CacheConcurrencyStrategy;
32
33 import com.hack23.cia.model.core.impl.BaseEntity;
34 import com.hack23.cia.model.sweden.impl.Vote.Position;
35
36 /***
37 * The Class Ballot.
38 */
39 @Entity
40 @Table(uniqueConstraints = { @UniqueConstraint(columnNames = { "voteResultsHref" }) })
41 @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
42 public class Ballot extends BaseEntity {
43
44 /***
45 * The Enum ImportStatus.
46 */
47 public enum ImportStatus {
48
49 /*** The Completed. */
50 Completed,
51
52 /*** The Created. */
53 Created
54 }
55
56 /*** The Constant serialVersionUID. */
57 private static final long serialVersionUID = 1L;
58
59 /*** The Constant TOTAL_VOTES. */
60 public static final int TOTAL_VOTES = 349;
61
62 /*** The ballot result. */
63 private BallotResult ballotResult;
64
65 /*** The commitee report. */
66 private CommitteeReport commiteeReport;
67
68 /*** The datum. */
69 private Date datum;
70
71 /*** The description. */
72 private String description;
73
74 /*** The id. */
75 private Long id;
76
77 /*** The import status. */
78 private ImportStatus importStatus = ImportStatus.Created;
79
80 /*** The overview href. */
81 private String overviewHref;
82
83 /*** The party ballot result. */
84 private List<PartyBallotResult> partyBallotResult = new ArrayList<PartyBallotResult>();
85
86 /*** The version. */
87 private Long version=1L;
88
89 /*** The vote results href. */
90 private String voteResultsHref;
91
92 /*** The votes. */
93 private List<Vote> votes = new ArrayList<Vote>();
94
95 /***
96 * Gets the ballot result.
97 *
98 * @return the ballot result
99 */
100 @ManyToOne(cascade=CascadeType.ALL)
101 public BallotResult getBallotResult() {
102 return ballotResult;
103 }
104
105 /***
106 * Gets the commitee report.
107 *
108 * @return the commitee report
109 */
110 @ManyToOne
111 public CommitteeReport getCommiteeReport() {
112 return commiteeReport;
113 }
114
115 /***
116 * Gets the datum.
117 *
118 * @return the datum
119 */
120 @Temporal(TemporalType.DATE)
121 public Date getDatum() {
122 return datum;
123 }
124
125 /***
126 * Gets the description.
127 *
128 * @return the description
129 */
130 public String getDescription() {
131 return description;
132 }
133
134
135
136
137
138
139 @Override
140 @Id
141 @GeneratedValue(strategy = GenerationType.AUTO)
142 public Long getId() {
143 return id;
144 }
145
146 /***
147 * Gets the import status.
148 *
149 * @return the import status
150 */
151 @Enumerated(EnumType.STRING)
152 public ImportStatus getImportStatus() {
153 return importStatus;
154 }
155
156 /***
157 * Gets the overview href.
158 *
159 * @return the overview href
160 */
161 public String getOverviewHref() {
162 return overviewHref;
163 }
164
165 /***
166 * Gets the party ballot result.
167 *
168 * @return the party ballot result
169 */
170 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
171 @JoinTable(name = "BallotPartyVoteResult")
172 public List<PartyBallotResult> getPartyBallotResult() {
173 return partyBallotResult;
174 }
175
176 /***
177 * Gets the party ballot result.
178 *
179 * @param party the party
180 * @return the party ballot result
181 */
182 @Transient
183 public PartyBallotResult getPartyBallotResult(final String party) {
184 for(final PartyBallotResult partyBallotResult : getPartyBallotResult()) {
185 if (partyBallotResult.getPoliticalParty().getShortCode().equals(party)) {
186 return partyBallotResult;
187 }
188 }
189 return null;
190 }
191
192
193
194
195 @Override
196 @Version
197 public Long getVersion() {
198 return version;
199 }
200
201 /***
202 * Gets the vote for parliament member.
203 *
204 * @param parliamentMember the parliament member
205 * @return the vote for parliament member
206 */
207 @Transient
208 public Vote getVoteForParliamentMember(final ParliamentMember parliamentMember) {
209 final Vote result = null;
210 for (final Vote vote : getVotes()) {
211 if (vote.getParliamentMember().getId().equals(
212 parliamentMember.getId())) {
213 return vote;
214 }
215 }
216 return result;
217 }
218
219 /***
220 * Gets the vote results href.
221 *
222 * @return the vote results href
223 */
224 public String getVoteResultsHref() {
225 return voteResultsHref;
226 }
227
228 /***
229 * Gets the votes.
230 *
231 * @return the votes
232 */
233 @OneToMany(mappedBy = "ballot", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
234 @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
235 public List<Vote> getVotes() {
236 return votes;
237 }
238
239 /***
240 * Checks if is rebel vote.
241 *
242 * @param vote the vote
243 * @return true, if is rebel vote
244 */
245 @Transient
246 public boolean isRebelVote(final Vote vote) {
247 return getPartyBallotResult(vote.getParliamentMember().getParty()).isRebelVote(vote);
248 }
249
250 /***
251 * Checks if is winning position.
252 *
253 * @param position the position
254 * @return true, if is winning position
255 */
256 @Transient
257 public boolean isWinningPosition(final Position position) {
258 return getBallotResult().getWinningPosition().equals(position);
259 }
260
261 /***
262 * Sets the ballot result.
263 *
264 * @param ballotResult the new ballot result
265 */
266 public void setBallotResult(final BallotResult ballotResult) {
267 this.ballotResult = ballotResult;
268 }
269
270 /***
271 * Sets the commitee report.
272 *
273 * @param commiteeReport the new commitee report
274 */
275 public void setCommiteeReport(final CommitteeReport commiteeReport) {
276 this.commiteeReport = commiteeReport;
277 }
278
279 /***
280 * Sets the datum.
281 *
282 * @param datum the new datum
283 */
284 public void setDatum(final Date datum) {
285 this.datum = datum;
286 }
287
288 /***
289 * Sets the description.
290 *
291 * @param description the new description
292 */
293 public void setDescription(final String description) {
294 this.description = description;
295 }
296
297 /***
298 * Sets the id.
299 *
300 * @param id the new id
301 */
302 public void setId(final Long id) {
303 this.id = id;
304 }
305
306 /***
307 * Sets the import status.
308 *
309 * @param importStatus the new import status
310 */
311 public void setImportStatus(final ImportStatus importStatus) {
312 this.importStatus = importStatus;
313 }
314
315 /***
316 * Sets the overview href.
317 *
318 * @param overviewHref the new overview href
319 */
320 public void setOverviewHref(final String overviewHref) {
321 this.overviewHref = overviewHref;
322 }
323
324 /***
325 * Sets the party ballot result.
326 *
327 * @param partyBallotResult the new party ballot result
328 */
329 public void setPartyBallotResult(final List<PartyBallotResult> partyBallotResult) {
330 this.partyBallotResult = partyBallotResult;
331 }
332
333 /***
334 * Sets the version.
335 *
336 * @param version the new version
337 */
338 public void setVersion(final Long version) {
339 this.version = version;
340 }
341
342 /***
343 * Sets the vote results href.
344 *
345 * @param voteResultsHref the new vote results href
346 */
347 public void setVoteResultsHref(final String voteResultsHref) {
348 this.voteResultsHref = voteResultsHref;
349 }
350
351 /***
352 * Sets the votes.
353 *
354 * @param votes the new votes
355 */
356 public void setVotes(final List<Vote> votes) {
357 this.votes = votes;
358 }
359
360 }