votingPhase can now be toggled from jar

Use -DvotingPhase={boolean} to set value (Defaults to false)
This commit is contained in:
2020-12-08 23:32:20 +01:00
parent 9bf9f8acdc
commit 5204322f40

View File

@@ -26,7 +26,8 @@ import java.util.*;
@Controller @Controller
public class VotingController { public class VotingController {
private Boolean candidatesAdded = true;
private boolean votingPhase = false;
private static final Logger LOGGER = LogManager.getLogger(VotingController.class); private static final Logger LOGGER = LogManager.getLogger(VotingController.class);
private TableAction tableAction = new TableAction(); private TableAction tableAction = new TableAction();
@@ -52,20 +53,32 @@ public class VotingController {
@PostConstruct @PostConstruct
public void init() { public void init() {
try {
String mode = System.getProperty("votingPhase");
if (mode.equalsIgnoreCase("true")) {
votingPhase = true;
} else {
votingPhase = false;
}
} catch (Exception e){
votingPhase = false;
}
if (voterRepository.findAll().size() == 0) { if (voterRepository.findAll().size() == 0) {
tableAction.setUpVoters(voterRepository); tableAction.setUpVoters(voterRepository);
LOGGER.info("Voters successfully set up"); LOGGER.info("Voters successfully set up");
} }
if (categoryRepository.findAll().size() == 0){ if (categoryRepository.findAll().size() == 0) {
tableAction.setUpCategories(categoryRepository); tableAction.setUpCategories(categoryRepository);
LOGGER.info("Categories successfully set up"); LOGGER.info("Categories successfully set up");
} }
if (candidateRepository.findAll().size() == 0 && candidatesAdded == true && possibleCandidateRepository.findAll().size()!=0) { if (candidateRepository.findAll().size() == 0 && votingPhase == true && possibleCandidateRepository.findAll().size() != 0) {
tableAction.setUpCandidates(possibleCandidateRepository, candidateRepository); tableAction.setUpCandidates(possibleCandidateRepository, candidateRepository);
LOGGER.info("Candidates successfully set up"); LOGGER.info("Candidates successfully set up");
} }
LOGGER.info(votingPhase);
} }
@RequestMapping("/") @RequestMapping("/")
@@ -91,12 +104,12 @@ public class VotingController {
if (voter.getVote_status()) { if (voter.getVote_status()) {
LOGGER.warn(name + " has already voted"); LOGGER.warn(name + " has already voted");
return "errors/alreadyVoted.html"; return "errors/alreadyVoted.html";
} else if (voter.getCandidatesubmit_status() && candidatesAdded == false) { } else if (voter.getCandidatesubmit_status() && votingPhase == false) {
LOGGER.warn(name + " has already submitted its candidates"); LOGGER.warn(name + " has already submitted its candidates");
return "errors/alreadysubmittedcandidates.html"; return "errors/alreadysubmittedcandidates.html";
} else { } else {
AuthCode authCode = tableAction.generateToken(name, RandomNumber.getRandomNumberString(), authCodesRepository); AuthCode authCode = tableAction.generateToken(name, RandomNumber.getRandomNumberString(), authCodesRepository);
sendSimpleMessage(name,"Code zur Authentifizierung", "Dein Code lautet: " + authCode.getCode()); sendSimpleMessage(name, "Code zur Authentifizierung", "Dein Code lautet: " + authCode.getCode());
model.addAttribute("name", name); model.addAttribute("name", name);
model.addAttribute("codeExpired", false); model.addAttribute("codeExpired", false);
model.addAttribute("codeFalse", false); model.addAttribute("codeFalse", false);
@@ -113,11 +126,11 @@ public class VotingController {
} }
@RequestMapping("/vote") @RequestMapping("/vote")
public String voting_adding(@RequestParam String code,@RequestParam String name, Model model){ public String voting_adding(@RequestParam String code, @RequestParam String name, Model model) {
switch (tableAction.checkToken(name, code, authCodesRepository)){ switch (tableAction.checkToken(name, code, authCodesRepository)) {
case "matched": case "matched":
LOGGER.warn("matched"); LOGGER.warn("matched");
if(candidatesAdded) { if (votingPhase) {
List<Category> categories = categoryRepository.findAll(); List<Category> categories = categoryRepository.findAll();
model.addAttribute("categories", categories); model.addAttribute("categories", categories);
model.addAttribute("name", name); model.addAttribute("name", name);
@@ -125,7 +138,7 @@ public class VotingController {
} else { } else {
PossibleCandidateWrapper possibleCandidates = new PossibleCandidateWrapper(); PossibleCandidateWrapper possibleCandidates = new PossibleCandidateWrapper();
List<Category> categories = categoryRepository.findAll(); List<Category> categories = categoryRepository.findAll();
for (int i = 0; i < categories.size(); i++){ for (int i = 0; i < categories.size(); i++) {
possibleCandidates.addPossibleCandidate(new PossibleCandidate()); possibleCandidates.addPossibleCandidate(new PossibleCandidate());
} }
model.addAttribute("categories", categories); model.addAttribute("categories", categories);
@@ -150,13 +163,13 @@ public class VotingController {
} }
@RequestMapping("/saveCandidates") @RequestMapping("/saveCandidates")
public String candidateSaving(@ModelAttribute PossibleCandidateWrapper possibleCandidates, @RequestParam String name){ public String candidateSaving(@ModelAttribute PossibleCandidateWrapper possibleCandidates, @RequestParam String name) {
if (voterRepository.findByEmail(name).getVote_status()){ if (voterRepository.findByEmail(name).getVote_status()) {
return "errors/alreadyVoted.html"; return "errors/alreadyVoted.html";
} else { } else {
LinkedList<PossibleCandidate> posCandidates = possibleCandidates.getPossibleCandidates(); LinkedList<PossibleCandidate> posCandidates = possibleCandidates.getPossibleCandidates();
long index = 1; long index = 1;
for (PossibleCandidate posCandidate : posCandidates){ for (PossibleCandidate posCandidate : posCandidates) {
if (posCandidate.getName() != "") { if (posCandidate.getName() != "") {
if (possibleCandidateRepository.findByNameAndCategory(posCandidate.getName(), categoryRepository.findById(index).get()) != null) { if (possibleCandidateRepository.findByNameAndCategory(posCandidate.getName(), categoryRepository.findById(index).get()) != null) {
PossibleCandidate p = possibleCandidateRepository.findByNameAndCategory(posCandidate.getName(), categoryRepository.findById(index).get()); PossibleCandidate p = possibleCandidateRepository.findByNameAndCategory(posCandidate.getName(), categoryRepository.findById(index).get());
@@ -176,7 +189,7 @@ public class VotingController {
@RequestMapping("/processVote") @RequestMapping("/processVote")
public String ProcessVote(@RequestParam String name, @RequestParam String voteValues) { public String ProcessVote(@RequestParam String name, @RequestParam String voteValues) {
if (voterRepository.findByEmail(name).getCandidatesubmit_status()){ if (voterRepository.findByEmail(name).getCandidatesubmit_status()) {
return "errors/alreadySubmitted.html"; return "errors/alreadySubmitted.html";
} else { } else {
String[] partVoteValues = voteValues.split(","); String[] partVoteValues = voteValues.split(",");
@@ -190,17 +203,17 @@ public class VotingController {
} }
@RequestMapping("/dashboard") @RequestMapping("/dashboard")
public String AccessDashboard(@RequestParam String password, Model model){ public String AccessDashboard(@RequestParam String password, Model model) {
try { try {
if (password.equals("admin")) { if (password.equals("admin")) {
List<Voter> voters = voterRepository.findAll(); List<Voter> voters = voterRepository.findAll();
List<Category> categories = categoryRepository.findAll(); List<Category> categories = categoryRepository.findAll();
model.addAttribute("voters", voters); model.addAttribute("voters", voters);
model.addAttribute("categories", categories); model.addAttribute("categories", categories);
return "dashboard.html"; return "dashboard.html";
} else { } else {
LOGGER.error("Wrong Password"); LOGGER.error("Wrong Password");
} }
} catch (Exception e) { } catch (Exception e) {
LOGGER.fatal("voters table is not existing!"); LOGGER.fatal("voters table is not existing!");
} }