From 149a0dece33593b034824c10b78e01d06430adf4 Mon Sep 17 00:00:00 2001 From: Simon Date: Mon, 4 Jan 2021 02:22:00 +0100 Subject: [PATCH] Made final changes --- .../controller/VotingController.java | 73 ++++++++----------- .../entities/Motto.java | 58 --------------- .../entities/Voter.java | 8 -- .../repositories/MottoRepository.java | 14 ---- .../table/TableAction.java | 38 +--------- src/main/resources/Categories2.txt | 7 +- .../resources/templates/addingCandidates.html | 4 +- 7 files changed, 35 insertions(+), 167 deletions(-) delete mode 100644 src/main/java/com/github/cato447/AbizeitungVotingSystem/entities/Motto.java delete mode 100644 src/main/java/com/github/cato447/AbizeitungVotingSystem/repositories/MottoRepository.java diff --git a/src/main/java/com/github/cato447/AbizeitungVotingSystem/controller/VotingController.java b/src/main/java/com/github/cato447/AbizeitungVotingSystem/controller/VotingController.java index c6b778f..eb13216 100644 --- a/src/main/java/com/github/cato447/AbizeitungVotingSystem/controller/VotingController.java +++ b/src/main/java/com/github/cato447/AbizeitungVotingSystem/controller/VotingController.java @@ -6,7 +6,6 @@ import com.github.cato447.AbizeitungVotingSystem.helper.RandomNumber; import com.github.cato447.AbizeitungVotingSystem.repositories.*; import com.github.cato447.AbizeitungVotingSystem.table.TableAction; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Controller; @@ -26,9 +25,8 @@ import java.util.*; @Controller public class VotingController { - private boolean votingPhase = false; - private boolean mottoPhase = false; - private boolean addingPhase = false; + private boolean votingPhase; + private boolean addingPhase; private static final Logger LOGGER = LogManager.getLogger(VotingController.class); private TableAction tableAction = new TableAction(); @@ -42,9 +40,6 @@ public class VotingController { @Autowired CategoryRepository categoryRepository; - @Autowired - MottoRepository mottoRepository; - @Autowired PossibleCandidateRepository possibleCandidateRepository; @@ -57,11 +52,10 @@ public class VotingController { @PostConstruct public void init() { - mottoPhase = false; - votingPhase = true; - addingPhase = false; + votingPhase = false; + addingPhase = true; - LOGGER.info("Program started with arguments: votingPhase="+ votingPhase + " mottoPhase=" + mottoPhase + " addingPhase=" + addingPhase); + LOGGER.info("Program started with arguments: votingPhase="+ votingPhase + " addingPhase=" + addingPhase); if (voterRepository.findAll().size() == 0) { tableAction.setUpVoters(voterRepository); @@ -70,14 +64,10 @@ public class VotingController { if (categoryRepository.findAll().size() == 0) { tableAction.setUpCategories(categoryRepository); + possibleCandidateRepository.deleteAll(); LOGGER.info("Categories successfully set up"); } - if (mottoRepository.findAll().size() == 0){ - tableAction.setUpMottos(mottoRepository); - LOGGER.info("Mottos successfully set up"); - } - if (candidateRepository.findAll().size() == 0 && votingPhase == true && possibleCandidateRepository.findAll().size() != 0) { tableAction.setUpCandidates(possibleCandidateRepository, candidateRepository); LOGGER.info("Candidates successfully set up"); @@ -122,9 +112,6 @@ public class VotingController { } else if (voter.getCandidatesubmit_status() && addingPhase) { LOGGER.warn(name + " has already submitted its candidates"); return "errors/alreadyVoted.html"; - } else if (voter.getMotto_status() && mottoPhase) { - LOGGER.warn(name + " has already chose their motto"); - return "errors/alreadyVoted.html"; } else { if (authCodesRepository.findByName(name) == null) { AuthCode authCode = tableAction.generateToken(name, RandomNumber.getRandomNumberString(), authCodesRepository); @@ -165,12 +152,7 @@ public class VotingController { String tokenStatus = tableAction.checkToken(name, code, authCodesRepository); if (tokenStatus.equals("matched")) { LOGGER.warn("matched"); - if (mottoPhase) { - List mottos = mottoRepository.findAll(); - model.addAttribute("mottos", mottos); - model.addAttribute("name", name); - return "mottoVoting.html"; - } else if (addingPhase) { + if (addingPhase) { PossibleCandidateWrapper possibleCandidates = new PossibleCandidateWrapper(); List categories = categoryRepository.findAll(); for (int i = 0; i < categories.size(); i++) { @@ -204,54 +186,59 @@ public class VotingController { @RequestMapping("/saveCandidates") public String candidateSaving(@ModelAttribute PossibleCandidateWrapper possibleCandidates, @RequestParam String name) { - if (voterRepository.findByEmail(name).getVote_status()) { + if (voterRepository.findByEmail(name).getCandidatesubmit_status()) { return "errors/alreadyVoted.html"; } else { LinkedList posCandidates = possibleCandidates.getPossibleCandidates(); + LinkedList voteForPosCandidates = new LinkedList<>(); + LinkedList addToPosCandidates = new LinkedList<>(); long index = 1; for (PossibleCandidate posCandidate : posCandidates) { if (posCandidate.getName() != "") { if (possibleCandidateRepository.findByNameAndCategory(posCandidate.getName(), categoryRepository.findById(index).get()) != null) { PossibleCandidate p = possibleCandidateRepository.findByNameAndCategory(posCandidate.getName(), categoryRepository.findById(index).get()); - p.setVotes(p.getVotes() + 1); - possibleCandidateRepository.save(p); + voteForPosCandidates.add(p); } else { + if(index > 31 && posCandidate.getName().indexOf(" ") != -1){ + posCandidate.setName(posCandidate.getName().split(" ")[1]); + } PossibleCandidate possibleCandidate = new PossibleCandidate(posCandidate.getName(), categoryRepository.findById(index).get()); - possibleCandidateRepository.save(possibleCandidate); + addToPosCandidates.add(possibleCandidate); } } index++; } + for (PossibleCandidate p: voteForPosCandidates) { + p.setVotes(p.getVotes() + 1); + possibleCandidateRepository.save(p); + } + + for (PossibleCandidate p: addToPosCandidates) { + possibleCandidateRepository.save(p); + } + tableAction.updateCandidatesubmit_status(name, voterRepository); return "voteSuccessful.html"; } } - @RequestMapping("/saveMotto") - public String mottoSaving(@RequestParam String name, @RequestParam String voteValue) { - LOGGER.info(name); - if (voterRepository.findByEmail(name).getMotto_status()) { - return "errors/alreadySubmitted.html"; - } else { - tableAction.voteForMotto(voteValue, mottoRepository); - tableAction.updateMottoStatus(name, voterRepository); - LOGGER.info(name + " has choose his motto"); - } - return "voteSuccessful.html"; - } - @RequestMapping("/processVote") public String ProcessVote(@RequestParam String name, @RequestParam String voteValues) { if (voterRepository.findByEmail(name).getVote_status()) { return "errors/alreadyVoted.html"; } else { + LinkedList voteFor = new LinkedList<>(); if(!voteValues.equals("")) { String[] partVoteValues = voteValues.split(","); for (String s : partVoteValues) { - tableAction.voteForCandidate(s, candidateRepository); + voteFor.add(s); } LOGGER.info(name + " has voted!"); } + for (String s: voteFor) { + tableAction.voteForCandidate(s, candidateRepository); + } + tableAction.updateVotingStatus(name, voterRepository); return "voteSuccessful.html"; } diff --git a/src/main/java/com/github/cato447/AbizeitungVotingSystem/entities/Motto.java b/src/main/java/com/github/cato447/AbizeitungVotingSystem/entities/Motto.java deleted file mode 100644 index e3d6334..0000000 --- a/src/main/java/com/github/cato447/AbizeitungVotingSystem/entities/Motto.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.github.cato447.AbizeitungVotingSystem.entities; - -import javax.persistence.*; - -@Entity -@Table(name = "mottos") -public class Motto implements Comparable{ - - public Motto() { - super(); - } - - public Motto(String name) { - super(); - this.name = name; - this.votes = 0; - } - - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - private String name; - - private Integer votes; - - - public Long getId() { - return id; - } - - public String getName() { - return name; - } - - public Integer getVotes() { - return votes; - } - - public void setName(String name) { - this.name = name; - } - - public void setVotes(Integer votes) { - this.votes = votes; - } - - public void voteFor() { - this.votes += 1; - } - - @Override - public int compareTo(Motto m) { - if (getVotes() == null || m.getVotes() == null) { - return 0; - } - return m.getVotes().compareTo(getVotes()); - } -} diff --git a/src/main/java/com/github/cato447/AbizeitungVotingSystem/entities/Voter.java b/src/main/java/com/github/cato447/AbizeitungVotingSystem/entities/Voter.java index 3bbe10a..97a2c51 100644 --- a/src/main/java/com/github/cato447/AbizeitungVotingSystem/entities/Voter.java +++ b/src/main/java/com/github/cato447/AbizeitungVotingSystem/entities/Voter.java @@ -14,7 +14,6 @@ public class Voter { this.email = email; this.vote_status = false; this.candidatesubmit_status = false; - this.motto_status = false; } @Id @@ -22,7 +21,6 @@ public class Voter { private Long id; private String email; private Boolean vote_status; - private Boolean motto_status; private Boolean candidatesubmit_status; public Long getId() { @@ -41,10 +39,6 @@ public class Voter { return candidatesubmit_status; } - public Boolean getMotto_status() { - return motto_status; - } - public void vote(){ vote_status = true; } @@ -52,6 +46,4 @@ public class Voter { public void submitCandidates() { candidatesubmit_status = true; } - - public void voteMotto() { motto_status = true;} } diff --git a/src/main/java/com/github/cato447/AbizeitungVotingSystem/repositories/MottoRepository.java b/src/main/java/com/github/cato447/AbizeitungVotingSystem/repositories/MottoRepository.java deleted file mode 100644 index 0d3ecb0..0000000 --- a/src/main/java/com/github/cato447/AbizeitungVotingSystem/repositories/MottoRepository.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.github.cato447.AbizeitungVotingSystem.repositories; - -import com.github.cato447.AbizeitungVotingSystem.entities.Motto; -import org.springframework.data.jpa.repository.JpaRepository; - -import java.util.Optional; - -public interface MottoRepository extends JpaRepository { - - public Motto findByName(String name); - - Optional findById(Long id); - -} diff --git a/src/main/java/com/github/cato447/AbizeitungVotingSystem/table/TableAction.java b/src/main/java/com/github/cato447/AbizeitungVotingSystem/table/TableAction.java index 25117f8..c4bc283 100644 --- a/src/main/java/com/github/cato447/AbizeitungVotingSystem/table/TableAction.java +++ b/src/main/java/com/github/cato447/AbizeitungVotingSystem/table/TableAction.java @@ -1,16 +1,9 @@ package com.github.cato447.AbizeitungVotingSystem.table; -import com.github.cato447.AbizeitungVotingSystem.controller.VotingController; import com.github.cato447.AbizeitungVotingSystem.entities.*; import com.github.cato447.AbizeitungVotingSystem.repositories.*; -import org.aspectj.weaver.loadtime.definition.LightXMLParser; import java.io.*; -import java.lang.reflect.Array; -import java.net.JarURLConnection; -import java.net.URL; -import java.net.URLConnection; -import java.nio.Buffer; import java.util.*; public class TableAction { @@ -31,12 +24,6 @@ public class TableAction { voterRepository.save(voter); } - public void updateMottoStatus(String email, VoterRepository voterRepository){ - Voter voter = voterRepository.findByEmail(email); - voter.voteMotto(); - voterRepository.save(voter); - } - public AuthCode generateToken(String name, String code, AuthCodesRepository authCodesRepository) { AuthCode authCode = new AuthCode(name, code); try{ @@ -82,13 +69,6 @@ public class TableAction { candidateRepository.save(candidate); } - public void voteForMotto(String id, MottoRepository mottoRepository){ - long mottoID = Long.valueOf(id); - Motto motto = mottoRepository.findById(mottoID).get(); - motto.voteFor(); - mottoRepository.save(motto); - } - public void setUpVoters(VoterRepository voterRepository){ try (InputStream inputStream = getClass().getResourceAsStream("/Q2_emails.txt"); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { @@ -142,7 +122,7 @@ public class TableAction { } public void setUpCategories(CategoryRepository categoryRepository){ - try (InputStream inputStream = getClass().getResourceAsStream("/Categories.txt"); + try (InputStream inputStream = getClass().getResourceAsStream("/Categories2.txt"); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { String line = ""; ArrayList categories = new ArrayList(); @@ -156,20 +136,4 @@ public class TableAction { e.printStackTrace(); } } - - public void setUpMottos(MottoRepository mottoRepository){ - try (InputStream inputStream = getClass().getResourceAsStream("/Mottos.txt"); - BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { - String line = ""; - ArrayList mottos = new ArrayList<>(); - while ((line = reader.readLine())!= null){ - String name = line; - Motto motto = new Motto(name); - mottos.add(motto); - } - mottoRepository.saveAll(mottos); - } catch (IOException e) { - e.printStackTrace(); - } - } } diff --git a/src/main/resources/Categories2.txt b/src/main/resources/Categories2.txt index 51986db..204a4a2 100644 --- a/src/main/resources/Categories2.txt +++ b/src/main/resources/Categories2.txt @@ -10,7 +10,7 @@ Ehrgeizig, ehrgeiziger, ... Miss Make-Up Meiste Punkte in Flensburg Im Bundestag -Leber als Bimsstein +Bimsstein als Leber Bester Schwiegersohn Beste Schwiegertochter Lebt im Fitnessstudio @@ -47,7 +47,4 @@ Unterrichtet auch, wenn die Schule brennt Ist immer da Lebt in der Schule Musste einen Wald für Arbeitsblätter roden lassen -Top gestylt - - - +Top gestylt \ No newline at end of file diff --git a/src/main/resources/templates/addingCandidates.html b/src/main/resources/templates/addingCandidates.html index 7d0c149..908566b 100644 --- a/src/main/resources/templates/addingCandidates.html +++ b/src/main/resources/templates/addingCandidates.html @@ -42,10 +42,10 @@
-
+

Lehrer

-
+

Schüler