diff --git a/pom.xml b/pom.xml index 15e726a..6b34a1f 100644 --- a/pom.xml +++ b/pom.xml @@ -1,19 +1,27 @@ - + 4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.4.RELEASE - + + com.github.cato447 - AbizeitungVotingSystem - 0.0.1-SNAPSHOT + AbiVoting + 1.0 AbizeitungVotingSystem A Voting System to get voting information for our graduating book + + + ssh-repository + scpexe://81.169.149.143/program + + + 11 @@ -74,6 +82,14 @@ spring-boot-maven-plugin + + + + org.apache.maven.wagon + wagon-ssh-external + 2.10 + + diff --git a/src/main/java/com/github/cato447/AbizeitungVotingSystem/AbizeitungVotingSystemApplication.java b/src/main/java/com/github/cato447/AbizeitungVotingSystem/AbizeitungVotingSystemApplication.java index 25336d1..9d612d9 100644 --- a/src/main/java/com/github/cato447/AbizeitungVotingSystem/AbizeitungVotingSystemApplication.java +++ b/src/main/java/com/github/cato447/AbizeitungVotingSystem/AbizeitungVotingSystemApplication.java @@ -1,11 +1,11 @@ package com.github.cato447.AbizeitungVotingSystem; import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import java.util.Arrays; + @SpringBootApplication @EnableJpaRepositories public class AbizeitungVotingSystemApplication { 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 34bfa35..28987ec 100644 --- a/src/main/java/com/github/cato447/AbizeitungVotingSystem/controller/VotingController.java +++ b/src/main/java/com/github/cato447/AbizeitungVotingSystem/controller/VotingController.java @@ -1,9 +1,6 @@ package com.github.cato447.AbizeitungVotingSystem.controller; -import com.github.cato447.AbizeitungVotingSystem.entities.AuthCode; -import com.github.cato447.AbizeitungVotingSystem.entities.Category; -import com.github.cato447.AbizeitungVotingSystem.entities.PossibleCandidate; -import com.github.cato447.AbizeitungVotingSystem.entities.Voter; +import com.github.cato447.AbizeitungVotingSystem.entities.*; import com.github.cato447.AbizeitungVotingSystem.helper.PossibleCandidateWrapper; import com.github.cato447.AbizeitungVotingSystem.helper.RandomNumber; import com.github.cato447.AbizeitungVotingSystem.repositories.*; @@ -26,8 +23,9 @@ import java.util.*; @Controller public class VotingController { - - private boolean votingPhase = false; + private boolean votingPhase; + private boolean mottoPhase; + private boolean addingPhase; private static final Logger LOGGER = LogManager.getLogger(VotingController.class); private TableAction tableAction = new TableAction(); @@ -41,6 +39,9 @@ public class VotingController { @Autowired CategoryRepository categoryRepository; + @Autowired + MottoRepository mottoRepository; + @Autowired PossibleCandidateRepository possibleCandidateRepository; @@ -54,16 +55,31 @@ public class VotingController { @PostConstruct public void init() { try { - String mode = System.getProperty("votingPhase"); - if (mode.equalsIgnoreCase("true")) { + String votingPhaseConfig = System.getProperty("votingPhase"); + if (votingPhaseConfig.equalsIgnoreCase("true")) { votingPhase = true; - } else { - votingPhase = false; + } + + String mottoVotingConfig = System.getProperty("mottoVoting"); + if (mottoVotingConfig.equalsIgnoreCase("true")) { + mottoPhase = true; + } + + String addingPhaseConfig = System.getProperty("addingPhase"); + if (addingPhaseConfig.equalsIgnoreCase("true")){ + addingPhase = true; } } catch (Exception e){ - votingPhase = false; + } +// //TODO: TESTING REMOVE ON SHIPPING +// votingPhase = false; +// mottoPhase = true; +// addingPhase = false; + + LOGGER.info("Program started with arguments: votingPhase="+ votingPhase + " mottoVoting=" + mottoPhase + " addingPhase=" + addingPhase); + if (voterRepository.findAll().size() == 0) { tableAction.setUpVoters(voterRepository); LOGGER.info("Voters successfully set up"); @@ -74,11 +90,15 @@ public class VotingController { 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"); } - LOGGER.info(votingPhase); } @RequestMapping("/") @@ -86,8 +106,7 @@ public class VotingController { return "start.html"; } - public void sendSimpleMessage( - String to, String subject, String text) { + public void sendSimpleMessage(String to, String subject, String text) { SimpleMailMessage message = new SimpleMailMessage(); message.setTo(to); message.setSubject(subject); @@ -101,14 +120,18 @@ public class VotingController { try { LOGGER.warn(name); Voter voter = voterRepository.findByEmail(name.toLowerCase().strip()); - if (voter.getVote_status()) { + if (voter.getVote_status() && votingPhase) { LOGGER.warn(name + " has already voted"); return "errors/alreadyVoted.html"; - } else if (voter.getCandidatesubmit_status() && votingPhase == false) { + } else if (voter.getCandidatesubmit_status() && addingPhase) { LOGGER.warn(name + " has already submitted its candidates"); return "errors/alreadysubmittedcandidates.html"; + } else if (voter.getMotto_status() && mottoPhase){ + LOGGER.warn(name + " has already chose their motto"); + return "errors/alreadyVotedForMotto.html"; } else { if (authCodesRepository.findByName(name) == null) { + LOGGER.warn("no code"); AuthCode authCode = tableAction.generateToken(name, RandomNumber.getRandomNumberString(), authCodesRepository); sendSimpleMessage(name, "Code zur Authentifizierung", "Dein Code lautet: " + authCode.getCode()); } else if (authCodesRepository.findByName(name) != null && authCodesRepository.findByName(name).isExpired()){ @@ -132,37 +155,43 @@ public class VotingController { @RequestMapping("/vote") public String voting_adding(@RequestParam String code, @RequestParam String name, Model model) { - switch (tableAction.checkToken(name, code, authCodesRepository)) { - case "matched": - LOGGER.warn("matched"); - if (votingPhase) { - List categories = categoryRepository.findAll(); - model.addAttribute("categories", categories); - model.addAttribute("name", name); - return "voting.html"; - } else { - PossibleCandidateWrapper possibleCandidates = new PossibleCandidateWrapper(); - List categories = categoryRepository.findAll(); - for (int i = 0; i < categories.size(); i++) { - possibleCandidates.addPossibleCandidate(new PossibleCandidate()); - } - model.addAttribute("categories", categories); - model.addAttribute("form", possibleCandidates); - model.addAttribute("name", name); - return "addingCandidates.html"; + 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) { + PossibleCandidateWrapper possibleCandidates = new PossibleCandidateWrapper(); + List categories = categoryRepository.findAll(); + for (int i = 0; i < categories.size(); i++) { + possibleCandidates.addPossibleCandidate(new PossibleCandidate()); } - - case "expired": + model.addAttribute("categories", categories); + model.addAttribute("form", possibleCandidates); model.addAttribute("name", name); - model.addAttribute("codeExpired", true); - model.addAttribute("codeFalse", false); - return "authenticate.html"; - - case "wrong": + return "addingCandidates.html"; + } else if (votingPhase) { + List categories = categoryRepository.findAll(); + model.addAttribute("categories", categories); model.addAttribute("name", name); - model.addAttribute("codeExpired", false); - model.addAttribute("codeFalse", true); - return "authenticate.html"; + return "voting.html"; + } + } else if (tokenStatus.equals("expired")){ + LOGGER.warn("expired"); + model.addAttribute("name", name); + model.addAttribute("codeExpired", true); + model.addAttribute("codeFalse", false); + return "authenticate.html"; + } else if (tokenStatus.equals("wrong")){ + LOGGER.warn("wrong"); + model.addAttribute("name", name); + model.addAttribute("codeExpired", false); + model.addAttribute("codeFalse", true); + return "authenticate.html"; } return "fatalError"; } @@ -192,6 +221,19 @@ public class VotingController { } } + @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).getCandidatesubmit_status()) { @@ -199,7 +241,7 @@ public class VotingController { } else { String[] partVoteValues = voteValues.split(","); for (String s : partVoteValues) { - tableAction.voteFor(s, candidateRepository); + tableAction.voteForCandidate(s, candidateRepository); } tableAction.updateVotingStatus(name, voterRepository); LOGGER.info(name + " has voted!"); diff --git a/src/main/java/com/github/cato447/AbizeitungVotingSystem/entities/Motto.java b/src/main/java/com/github/cato447/AbizeitungVotingSystem/entities/Motto.java new file mode 100644 index 0000000..e3d6334 --- /dev/null +++ b/src/main/java/com/github/cato447/AbizeitungVotingSystem/entities/Motto.java @@ -0,0 +1,58 @@ +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 97a2c51..3bbe10a 100644 --- a/src/main/java/com/github/cato447/AbizeitungVotingSystem/entities/Voter.java +++ b/src/main/java/com/github/cato447/AbizeitungVotingSystem/entities/Voter.java @@ -14,6 +14,7 @@ public class Voter { this.email = email; this.vote_status = false; this.candidatesubmit_status = false; + this.motto_status = false; } @Id @@ -21,6 +22,7 @@ public class Voter { private Long id; private String email; private Boolean vote_status; + private Boolean motto_status; private Boolean candidatesubmit_status; public Long getId() { @@ -39,6 +41,10 @@ public class Voter { return candidatesubmit_status; } + public Boolean getMotto_status() { + return motto_status; + } + public void vote(){ vote_status = true; } @@ -46,4 +52,6 @@ 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 new file mode 100644 index 0000000..0d3ecb0 --- /dev/null +++ b/src/main/java/com/github/cato447/AbizeitungVotingSystem/repositories/MottoRepository.java @@ -0,0 +1,14 @@ +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 df9aceb..c2ce93a 100644 --- a/src/main/java/com/github/cato447/AbizeitungVotingSystem/table/TableAction.java +++ b/src/main/java/com/github/cato447/AbizeitungVotingSystem/table/TableAction.java @@ -10,6 +10,7 @@ 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 { @@ -30,6 +31,12 @@ 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{ @@ -45,16 +52,19 @@ public class TableAction { } public String checkToken(String name, String code, AuthCodesRepository authCodesRepository){ - AuthCode authCode = authCodesRepository.findByName(name); - if (authCode.getCode().equals(code) && !fiveMinutesPassed(authCode.getTime())){ - authCodesRepository.delete(authCode); - return "matched"; - } else if(fiveMinutesPassed(authCode.getTime())) { - authCodesRepository.delete(authCode); - return "expired"; - } else { + try { + AuthCode authCode = authCodesRepository.findByName(name); + if (authCode.getCode().equals(code) && !authCode.isExpired()) { + authCodesRepository.delete(authCode); + return "matched"; + } else if (authCode.isExpired()) { + authCodesRepository.delete(authCode); + return "expired"; + } + } catch(Exception e){ return "wrong"; } + return "wrong"; } private boolean fiveMinutesPassed(Long time){ @@ -65,13 +75,20 @@ public class TableAction { return possibleCandidates.size() <= 5 ? possibleCandidates.size() : 5; } - public void voteFor(String id, CandidateRepository candidateRepository){ + public void voteForCandidate(String id, CandidateRepository candidateRepository){ long candidateID = Long.valueOf(id); Candidate candidate = candidateRepository.findById(candidateID).get(); candidate.votedFor(); 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))) { @@ -130,4 +147,20 @@ 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/Mottos.txt b/src/main/resources/Mottos.txt new file mode 100644 index 0000000..645759b --- /dev/null +++ b/src/main/resources/Mottos.txt @@ -0,0 +1,11 @@ +CannABIs - wir haben es durchgezogen! +ABI 2,010 ‰ - Meist dichter als Denker +KABItän Blaubär - Immer blau und trotzdem schlau +HABI Potter - Wir verlassen die Kammer des Schreckens +A BItch - Für einen Punkt tue ich alles! +ABIthur - Wenn das Elite von die Gymnahsium gehen tut +LABIrinth - Planlos zum Ziel +ABIcrombie & Fitch - Models gehen, Elche bleiben (Abercrombie & Fitch) +SemipermeABIlität - Nur die Besten kommen durch +KohlrABI - Wir machen uns vom Acker +ABIgasmus - Aber das war nur das Vorspiel \ No newline at end of file diff --git a/src/main/resources/static/styles/mottoVoting.css b/src/main/resources/static/styles/mottoVoting.css new file mode 100644 index 0000000..43f3c9a --- /dev/null +++ b/src/main/resources/static/styles/mottoVoting.css @@ -0,0 +1,66 @@ +body { + background-color: rgb(44, 49, 54); + font-family: Arial, Helvetica, sans-serif; +} + +.center-screen { + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + text-align: center; +} + +.centered { + background: transparent; + margin: 0 auto; + padding: 20px; + width: 100%; + overflow: auto; +} + +h1 { + color: #FFF; + font-size: 75px; +} + +h2.mottoHeader { + color: #FFF; + font-size: 50px; +} + +button { + background: transparent; + border: none; + color: #FFF; + font-size: 35px; + font-weight: normal; + letter-spacing: .125rem; + text-transform: uppercase; + text-align: center; + transition: opacity .25s .5s; + margin: 0.5%; +} + +.submitButton { + margin-top: 5%; + padding: .25em 0; + border: 0; + outline: 0; + background: #bb1515; + color: rgba(255, 255, 255, 0.85); + font-size: 2rem; + width: 500px; + letter-spacing: .0625rem; + border-radius: 12px; + box-shadow: 0 3px 5px 1px rgba(0, 0, 0, 0.25); + text-shadow: 0 -2px 0 rgba(0, 0, 0, 0.25), 0 1px 0 rgba(255, 255, 255, 0.2); +} + +html, +body { + width: 100%; + height: 100%; + background-image: linear-gradient(to bottom right, #111E25 0%, #111 100%); + font-family: 'Lato', sans-serif; +} \ No newline at end of file diff --git a/src/main/resources/templates/errors/alreadySubmitted.html b/src/main/resources/templates/errors/alreadyVotedForMotto.html similarity index 100% rename from src/main/resources/templates/errors/alreadySubmitted.html rename to src/main/resources/templates/errors/alreadyVotedForMotto.html diff --git a/src/main/resources/templates/alreadysubmittedcandidates.html b/src/main/resources/templates/errors/alreadysubmittedcandidates.html similarity index 100% rename from src/main/resources/templates/alreadysubmittedcandidates.html rename to src/main/resources/templates/errors/alreadysubmittedcandidates.html diff --git a/src/main/resources/templates/mottoVoting.html b/src/main/resources/templates/mottoVoting.html new file mode 100644 index 0000000..0316aa9 --- /dev/null +++ b/src/main/resources/templates/mottoVoting.html @@ -0,0 +1,65 @@ + + + + + + Title + + + + + + + +
+

Wähle das Abimotto:

+
+
+ +
+
+ +
+ + + +
+
+ + + + \ No newline at end of file