This repository was archived by the owner on Oct 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathChoiceServiceImpl.java
More file actions
95 lines (76 loc) · 2.84 KB
/
ChoiceServiceImpl.java
File metadata and controls
95 lines (76 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package org.rulez.demokracia.pdengine.choice;
import org.rulez.demokracia.pdengine.authentication.AuthenticatedUserService;
import org.rulez.demokracia.pdengine.dataobjects.VoteAdminInfo;
import org.rulez.demokracia.pdengine.exception.ReportedException;
import org.rulez.demokracia.pdengine.vote.Vote;
import org.rulez.demokracia.pdengine.vote.VoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ChoiceServiceImpl implements ChoiceService {
private static final String USER = "user";
@Autowired
private ChoiceModificationValidatorService choiceModificationValidatorService;
@Autowired
private VoteService voteService;
@Autowired
private AuthenticatedUserService authenticatedUserService;
@Override
public Choice addChoice(
final VoteAdminInfo voteAdminInfo, final String choiceName,
final String user
) {
final Vote vote = getModifiableVote(voteAdminInfo, voteService);
final Choice choice = new Choice(choiceName, user);
vote.addChoice(choice);
voteService.saveVote(vote);
return choice;
}
@Override
public Choice getChoice(final String voteId, final String choiceId) {
return voteService.getVote(voteId).getChoice(choiceId);
}
@Override
public void endorseChoice(
final VoteAdminInfo voteAdminInfo, final String choiceId,
final String givenUserName
) {
String userName = givenUserName;
final Vote vote = voteService.getVote(voteAdminInfo.voteId);
if (USER.equals(voteAdminInfo.adminKey)) {
checkIfVoteIsEndorseable(vote);
userName = getUserName();
}
vote.checkAdminKey(voteAdminInfo.adminKey);
vote.getChoice(choiceId).endorse(userName);
}
@Override
public void
deleteChoice(final VoteAdminInfo voteAdminInfo, final String choiceId) {
final Vote vote = getModifiableVote(voteAdminInfo, voteService);
final Choice votesChoice = getChoice(voteAdminInfo.getVoteId(), choiceId);
choiceModificationValidatorService
.validateDeletion(voteAdminInfo, vote, votesChoice);
vote.getChoices().remove(votesChoice.getId());
voteService.saveVote(vote);
}
@Override
public void modifyChoice(
final VoteAdminInfo adminInfo, final String choiceId,
final String choiceName
) {
final Vote vote = getModifiableVote(adminInfo, voteService);
final Choice votesChoice = vote.getChoice(choiceId);
choiceModificationValidatorService
.validateModification(adminInfo, vote, votesChoice);
votesChoice.setName(choiceName);
voteService.saveVote(vote);
}
private String getUserName() {
return authenticatedUserService.getAuthenticatedUserName();
}
private void checkIfVoteIsEndorseable(final Vote vote) {
if (!vote.isEndorsable())
throw new ReportedException("user cannot endorse this vote");
}
}