Have to go to bed now need to write

This commit is contained in:
2020-12-10 23:50:44 +01:00
parent 7a64b195ed
commit 4dd3468bef
13 changed files with 4145 additions and 154 deletions

File diff suppressed because it is too large Load Diff

26
pom.xml
View File

@@ -1,19 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<groupId>com.github.cato447</groupId>
<artifactId>AbizeitungVotingSystem</artifactId>
<version>0.0.1-SNAPSHOT</version>
<artifactId>AbiVoting</artifactId>
<version>1.0</version>
<name>AbizeitungVotingSystem</name>
<description>A Voting System to get voting information for our graduating book</description>
<distributionManagement>
<repository>
<id>ssh-repository</id>
<url>scpexe://81.169.149.143/program</url>
</repository>
</distributionManagement>
<properties>
<java.version>11</java.version>
</properties>
@@ -74,6 +82,14 @@
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<extensions>
<!-- Enabling the use of SSH -->
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ssh-external</artifactId>
<version>2.10</version>
</extension>
</extensions>
</build>
</project>

View File

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

View File

@@ -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,15 +155,16 @@ 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":
String tokenStatus = tableAction.checkToken(name, code, authCodesRepository);
if (tokenStatus.equals("matched")){
LOGGER.warn("matched");
if (votingPhase) {
List<Category> categories = categoryRepository.findAll();
model.addAttribute("categories", categories);
if (mottoPhase){
List<Motto> mottos = mottoRepository.findAll();
model.addAttribute("mottos", mottos);
model.addAttribute("name", name);
return "voting.html";
} else {
return "mottoVoting.html";
} else if (addingPhase) {
PossibleCandidateWrapper possibleCandidates = new PossibleCandidateWrapper();
List<Category> categories = categoryRepository.findAll();
for (int i = 0; i < categories.size(); i++) {
@@ -150,15 +174,20 @@ public class VotingController {
model.addAttribute("form", possibleCandidates);
model.addAttribute("name", name);
return "addingCandidates.html";
} else if (votingPhase) {
List<Category> categories = categoryRepository.findAll();
model.addAttribute("categories", categories);
model.addAttribute("name", name);
return "voting.html";
}
case "expired":
} else if (tokenStatus.equals("expired")){
LOGGER.warn("expired");
model.addAttribute("name", name);
model.addAttribute("codeExpired", true);
model.addAttribute("codeFalse", false);
return "authenticate.html";
case "wrong":
} else if (tokenStatus.equals("wrong")){
LOGGER.warn("wrong");
model.addAttribute("name", name);
model.addAttribute("codeExpired", false);
model.addAttribute("codeFalse", true);
@@ -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!");

View File

@@ -0,0 +1,58 @@
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,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;}
}

View File

@@ -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<Motto, Integer> {
public Motto findByName(String name);
Optional<Motto> findById(Long id);
}

View File

@@ -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){
try {
AuthCode authCode = authCodesRepository.findByName(name);
if (authCode.getCode().equals(code) && !fiveMinutesPassed(authCode.getTime())){
if (authCode.getCode().equals(code) && !authCode.isExpired()) {
authCodesRepository.delete(authCode);
return "matched";
} else if(fiveMinutesPassed(authCode.getTime())) {
} else if (authCode.isExpired()) {
authCodesRepository.delete(authCode);
return "expired";
} else {
}
} 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<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

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

View File

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

View File

@@ -0,0 +1,65 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org/">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link th:href="@{/styles/mottoVoting.css}" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body class="center-screen">
<div class="centered">
<h1>Wähle das Abimotto:</h1>
<div id="votingButtons">
<div th:each="motto : ${mottos}" class="voteDiv">
<button class="inputButton" th:id="|motto${motto.id}|" th:text="${motto.name}" th:onclick="|setColor(motto${motto.id})|"></button>
</div>
</div>
<form action="#" th:action="@{/saveMotto}" method="post" id="mottoForm">
<input id="voteValue" type="hidden" name="voteValue" value="" />
<input id="voterName" type="hidden" name="name" th:value="${name}" />
<button class="submitButton" id="btnNext" onclick=getVotes()>Auswahl bestätigen</button>
</form>
</div>
<script>
function setColor(button) {
console.log(button);
groupButtons = document.querySelectorAll('[id^=motto]');
groupButtons.forEach(button => {
button.style.background = 'transparent';
button.style.fontWeight = 'normal';
});
button.style.background = "#bb1515";
button.style.fontWeight = 'bold';
}
function rgbToHex(rgb) {
rgb = rgb.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i);
var color = (rgb && rgb.length === 4) ? "#" +
("0" + parseInt(rgb[1], 10).toString(16)).slice(-2) +
("0" + parseInt(rgb[2], 10).toString(16)).slice(-2) +
("0" + parseInt(rgb[3], 10).toString(16)).slice(-2) : '';
return color;
}
function getVotes() {
groupButtons = document.querySelectorAll(".inputButton");
var voteID;
groupButtons.forEach(button => {
if (button.style.fontWeight == 'bold') {
voteID = 5;
}
});
input = document.getElementById("voteValue");
input.value = voteID;
document.getElementById("mottoForm").submit();
}
</script>
</body>
</html>