Made final changes

This commit is contained in:
2021-01-04 02:22:00 +01:00
parent dfaf2c55e2
commit e9e9cfca97
9 changed files with 449 additions and 168 deletions

View File

@@ -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<Motto> mottos = mottoRepository.findAll();
model.addAttribute("mottos", mottos);
model.addAttribute("name", name);
return "mottoVoting.html";
} else if (addingPhase) {
if (addingPhase) {
PossibleCandidateWrapper possibleCandidates = new PossibleCandidateWrapper();
List<Category> 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<PossibleCandidate> posCandidates = possibleCandidates.getPossibleCandidates();
LinkedList<PossibleCandidate> voteForPosCandidates = new LinkedList<>();
LinkedList<PossibleCandidate> 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<String> 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";
}

View File

@@ -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());
}
}

View File

@@ -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;}
}

View File

@@ -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);
}

View File

@@ -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<Category> categories = new ArrayList<Category>();
@@ -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<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();
}
}
}

View File

@@ -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

View File

@@ -158,4 +158,6 @@ niklas.klaffki@adolfinum.de
alessio.padovano@adolfinum.de
jonas.breder@adolfinum.de
ben.maas@adolfinum.de
mina.ercan@adolfinum.de
mina.ercan@adolfinum.de
maya.springstein@adolfinum.de
can.patir@adolfinum.de

View File

@@ -42,10 +42,10 @@
<form id="inputForm" action="#" th:action="@{/saveCandidates}" th:object="${form}" method="post">
<div id="candidateAdding">
<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>
</div>
<div th:unless="${itemStat.index > 31}">
<div th:unless="${itemStat.index > 30}">
<h2 class="categoryCategory">Schüler</h2>
</div>
<h2 class="categoryHeader" th:text="${category.name}"></h2>