Made final changes

This commit is contained in:
2021-01-04 02:22:00 +01:00
parent 937971da63
commit 149a0dece3
7 changed files with 35 additions and 167 deletions
@@ -6,7 +6,6 @@ import com.github.cato447.AbizeitungVotingSystem.helper.RandomNumber;
import com.github.cato447.AbizeitungVotingSystem.repositories.*; import com.github.cato447.AbizeitungVotingSystem.repositories.*;
import com.github.cato447.AbizeitungVotingSystem.table.TableAction; import com.github.cato447.AbizeitungVotingSystem.table.TableAction;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
@@ -26,9 +25,8 @@ import java.util.*;
@Controller @Controller
public class VotingController { public class VotingController {
private boolean votingPhase = false; private boolean votingPhase;
private boolean mottoPhase = false; private boolean addingPhase;
private boolean addingPhase = 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();
@@ -42,9 +40,6 @@ public class VotingController {
@Autowired @Autowired
CategoryRepository categoryRepository; CategoryRepository categoryRepository;
@Autowired
MottoRepository mottoRepository;
@Autowired @Autowired
PossibleCandidateRepository possibleCandidateRepository; PossibleCandidateRepository possibleCandidateRepository;
@@ -57,11 +52,10 @@ public class VotingController {
@PostConstruct @PostConstruct
public void init() { public void init() {
mottoPhase = false; votingPhase = false;
votingPhase = true; addingPhase = true;
addingPhase = false;
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) { if (voterRepository.findAll().size() == 0) {
tableAction.setUpVoters(voterRepository); tableAction.setUpVoters(voterRepository);
@@ -70,14 +64,10 @@ public class VotingController {
if (categoryRepository.findAll().size() == 0) { if (categoryRepository.findAll().size() == 0) {
tableAction.setUpCategories(categoryRepository); tableAction.setUpCategories(categoryRepository);
possibleCandidateRepository.deleteAll();
LOGGER.info("Categories successfully set up"); 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) { 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");
@@ -122,9 +112,6 @@ public class VotingController {
} else if (voter.getCandidatesubmit_status() && addingPhase) { } else if (voter.getCandidatesubmit_status() && addingPhase) {
LOGGER.warn(name + " has already submitted its candidates"); LOGGER.warn(name + " has already submitted its candidates");
return "errors/alreadyVoted.html"; return "errors/alreadyVoted.html";
} else if (voter.getMotto_status() && mottoPhase) {
LOGGER.warn(name + " has already chose their motto");
return "errors/alreadyVoted.html";
} else { } else {
if (authCodesRepository.findByName(name) == null) { if (authCodesRepository.findByName(name) == null) {
AuthCode authCode = tableAction.generateToken(name, RandomNumber.getRandomNumberString(), authCodesRepository); AuthCode authCode = tableAction.generateToken(name, RandomNumber.getRandomNumberString(), authCodesRepository);
@@ -165,12 +152,7 @@ public class VotingController {
String tokenStatus = tableAction.checkToken(name, code, authCodesRepository); String tokenStatus = tableAction.checkToken(name, code, authCodesRepository);
if (tokenStatus.equals("matched")) { if (tokenStatus.equals("matched")) {
LOGGER.warn("matched"); LOGGER.warn("matched");
if (mottoPhase) { if (addingPhase) {
List<Motto> mottos = mottoRepository.findAll();
model.addAttribute("mottos", mottos);
model.addAttribute("name", name);
return "mottoVoting.html";
} else if (addingPhase) {
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++) {
@@ -204,54 +186,59 @@ 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).getCandidatesubmit_status()) {
return "errors/alreadyVoted.html"; return "errors/alreadyVoted.html";
} else { } else {
LinkedList<PossibleCandidate> posCandidates = possibleCandidates.getPossibleCandidates(); LinkedList<PossibleCandidate> posCandidates = possibleCandidates.getPossibleCandidates();
LinkedList<PossibleCandidate> voteForPosCandidates = new LinkedList<>();
LinkedList<PossibleCandidate> addToPosCandidates = new LinkedList<>();
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());
p.setVotes(p.getVotes() + 1); voteForPosCandidates.add(p);
possibleCandidateRepository.save(p);
} else { } else {
if(index > 31 && posCandidate.getName().indexOf(" ") != -1){
posCandidate.setName(posCandidate.getName().split(" ")[1]);
}
PossibleCandidate possibleCandidate = new PossibleCandidate(posCandidate.getName(), categoryRepository.findById(index).get()); PossibleCandidate possibleCandidate = new PossibleCandidate(posCandidate.getName(), categoryRepository.findById(index).get());
possibleCandidateRepository.save(possibleCandidate); addToPosCandidates.add(possibleCandidate);
} }
} }
index++; 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); tableAction.updateCandidatesubmit_status(name, voterRepository);
return "voteSuccessful.html"; 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") @RequestMapping("/processVote")
public String ProcessVote(@RequestParam String name, @RequestParam String voteValues) { public String ProcessVote(@RequestParam String name, @RequestParam String voteValues) {
if (voterRepository.findByEmail(name).getVote_status()) { if (voterRepository.findByEmail(name).getVote_status()) {
return "errors/alreadyVoted.html"; return "errors/alreadyVoted.html";
} else { } else {
LinkedList<String> voteFor = new LinkedList<>();
if(!voteValues.equals("")) { if(!voteValues.equals("")) {
String[] partVoteValues = voteValues.split(","); String[] partVoteValues = voteValues.split(",");
for (String s : partVoteValues) { for (String s : partVoteValues) {
tableAction.voteForCandidate(s, candidateRepository); voteFor.add(s);
} }
LOGGER.info(name + " has voted!"); LOGGER.info(name + " has voted!");
} }
for (String s: voteFor) {
tableAction.voteForCandidate(s, candidateRepository);
}
tableAction.updateVotingStatus(name, voterRepository); tableAction.updateVotingStatus(name, voterRepository);
return "voteSuccessful.html"; return "voteSuccessful.html";
} }
@@ -1,58 +0,0 @@
package com.github.cato447.AbizeitungVotingSystem.entities;
import javax.persistence.*;
@Entity
@Table(name = "mottos")
public class Motto implements Comparable<Motto>{
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());
}
}
@@ -14,7 +14,6 @@ public class Voter {
this.email = email; this.email = email;
this.vote_status = false; this.vote_status = false;
this.candidatesubmit_status = false; this.candidatesubmit_status = false;
this.motto_status = false;
} }
@Id @Id
@@ -22,7 +21,6 @@ public class Voter {
private Long id; private Long id;
private String email; private String email;
private Boolean vote_status; private Boolean vote_status;
private Boolean motto_status;
private Boolean candidatesubmit_status; private Boolean candidatesubmit_status;
public Long getId() { public Long getId() {
@@ -41,10 +39,6 @@ public class Voter {
return candidatesubmit_status; return candidatesubmit_status;
} }
public Boolean getMotto_status() {
return motto_status;
}
public void vote(){ public void vote(){
vote_status = true; vote_status = true;
} }
@@ -52,6 +46,4 @@ public class Voter {
public void submitCandidates() { public void submitCandidates() {
candidatesubmit_status = true; candidatesubmit_status = true;
} }
public void voteMotto() { motto_status = true;}
} }
@@ -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<Motto, Integer> {
public Motto findByName(String name);
Optional<Motto> findById(Long id);
}
@@ -1,16 +1,9 @@
package com.github.cato447.AbizeitungVotingSystem.table; package com.github.cato447.AbizeitungVotingSystem.table;
import com.github.cato447.AbizeitungVotingSystem.controller.VotingController;
import com.github.cato447.AbizeitungVotingSystem.entities.*; import com.github.cato447.AbizeitungVotingSystem.entities.*;
import com.github.cato447.AbizeitungVotingSystem.repositories.*; import com.github.cato447.AbizeitungVotingSystem.repositories.*;
import org.aspectj.weaver.loadtime.definition.LightXMLParser;
import java.io.*; 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.*; import java.util.*;
public class TableAction { public class TableAction {
@@ -31,12 +24,6 @@ public class TableAction {
voterRepository.save(voter); 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) { public AuthCode generateToken(String name, String code, AuthCodesRepository authCodesRepository) {
AuthCode authCode = new AuthCode(name, code); AuthCode authCode = new AuthCode(name, code);
try{ try{
@@ -82,13 +69,6 @@ public class TableAction {
candidateRepository.save(candidate); 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){ public void setUpVoters(VoterRepository voterRepository){
try (InputStream inputStream = getClass().getResourceAsStream("/Q2_emails.txt"); try (InputStream inputStream = getClass().getResourceAsStream("/Q2_emails.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
@@ -142,7 +122,7 @@ public class TableAction {
} }
public void setUpCategories(CategoryRepository categoryRepository){ 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))) { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line = ""; String line = "";
ArrayList<Category> categories = new ArrayList<Category>(); ArrayList<Category> categories = new ArrayList<Category>();
@@ -156,20 +136,4 @@ public class TableAction {
e.printStackTrace(); e.printStackTrace();
} }
} }
public void setUpMottos(MottoRepository mottoRepository){
try (InputStream inputStream = getClass().getResourceAsStream("/Mottos.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line = "";
ArrayList<Motto> 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();
}
}
} }
+2 -5
View File
@@ -10,7 +10,7 @@ Ehrgeizig, ehrgeiziger, ...
Miss Make-Up Miss Make-Up
Meiste Punkte in Flensburg Meiste Punkte in Flensburg
Im Bundestag Im Bundestag
Leber als Bimsstein Bimsstein als Leber
Bester Schwiegersohn Bester Schwiegersohn
Beste Schwiegertochter Beste Schwiegertochter
Lebt im Fitnessstudio Lebt im Fitnessstudio
@@ -47,7 +47,4 @@ Unterrichtet auch, wenn die Schule brennt
Ist immer da Ist immer da
Lebt in der Schule Lebt in der Schule
Musste einen Wald für Arbeitsblätter roden lassen Musste einen Wald für Arbeitsblätter roden lassen
Top gestylt Top gestylt
@@ -42,10 +42,10 @@
<form id="inputForm" action="#" th:action="@{/saveCandidates}" th:object="${form}" method="post"> <form id="inputForm" action="#" th:action="@{/saveCandidates}" th:object="${form}" method="post">
<div id="candidateAdding"> <div id="candidateAdding">
<div class="group" th:each="category, itemStat : ${categories}"> <div class="group" th:each="category, itemStat : ${categories}">
<div th:if="${itemStat.index > 31}"> <div th:if="${itemStat.index > 30}">
<h2 class="categoryCategory">Lehrer</h2> <h2 class="categoryCategory">Lehrer</h2>
</div> </div>
<div th:unless="${itemStat.index > 31}"> <div th:unless="${itemStat.index > 30}">
<h2 class="categoryCategory">Schüler</h2> <h2 class="categoryCategory">Schüler</h2>
</div> </div>
<h2 class="categoryHeader" th:text="${category.name}"></h2> <h2 class="categoryHeader" th:text="${category.name}"></h2>