Made the transition of data between possibleCandidates and Candidates easier

CHANGED VotingController:
    - changed: candidateRepositorys get now only created if empty and 'candidatesAdded == true'

CHANGED PossibleCandidate:
    - added: new getter getCategoryID

CHANGED PossibleCandidateRepository
    - removed: unnecessary public declaration of methods

CHANGED TableAction:
    - changed: 'setupCandiates' adds the top 5 (votes) of every category in possibleCandidates (Manual preselection necessary)
    - added: helper method 'getLimit
    - removed: 'addCandidate' and 'addPossibleCandidate' due to no usage

CHANGED voting.css:
    - added: font-size to 'h1', 'h2.categoryHeader' and 'button' to enhance readability

CHANGED addingCandidates.html:
    - changed: language used in html tag from 'en' to 'de'

ADDED alreadysubmittedcandidates.html:
    - added: error page if voter already submitted possibleCandidates
This commit is contained in:
2020-12-07 23:42:29 +01:00
parent 32894e5469
commit 1ee5174207
7 changed files with 56 additions and 122 deletions

View File

@@ -26,7 +26,8 @@ import java.util.*;
@Controller
public class VotingController {
private Boolean candidatesAdded = false;
private Boolean candidatesAdded = true;
private static final Logger LOGGER = LogManager.getLogger(VotingController.class);
private TableAction tableAction = new TableAction();
@@ -61,8 +62,8 @@ public class VotingController {
LOGGER.info("Categories successfully set up");
}
if (candidateRepository.findAll().size() == 0) {
tableAction.setUpCandidates(candidateRepository, categoryRepository);
if (candidateRepository.findAll().size() == 0 && candidatesAdded == true) {
tableAction.setUpCandidates(possibleCandidateRepository, candidateRepository);
LOGGER.info("Candidates successfully set up");
}
}

View File

@@ -37,7 +37,11 @@ public class PossibleCandidate{
public Category getCategory() {return category;}
public int getVotes() {
public Long getCategoryID(){
return category.getId();
}
public Integer getVotes() {
return votes;
}
@@ -52,4 +56,5 @@ public class PossibleCandidate{
public void setVotes(int votes) {
this.votes = votes;
}
}

View File

@@ -10,8 +10,7 @@ import java.util.Optional;
@Repository
public interface PossibleCandidateRepository extends JpaRepository<PossibleCandidate, Integer> {
public PossibleCandidate findByNameAndCategory(String name, Category category);
PossibleCandidate findByNameAndCategory(String name, Category category);
Optional<PossibleCandidate> findById(Long id);
}

View File

@@ -8,6 +8,7 @@ import org.aspectj.weaver.loadtime.definition.LightXMLParser;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Array;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLConnection;
@@ -19,16 +20,6 @@ public class TableAction {
}
public void addCandidate(String name, long category_id, CategoryRepository categoryRepository,CandidateRepository candidateRepository){
Candidate candidate = new Candidate(name, categoryRepository.findById(category_id).get());
candidateRepository.save(candidate);
}
public void addPossibleCandidate(String name, long category_id, CategoryRepository categoryRepository, PossibleCandidateRepository possibleCandidateRepository){
PossibleCandidate possibleCandidate = new PossibleCandidate(name, categoryRepository.findById(category_id).get());
possibleCandidateRepository.save(possibleCandidate);
}
public void updateVotingStatus(String email, VoterRepository voterRepository){
Voter voter = voterRepository.findByEmail(email);
voter.vote();
@@ -72,6 +63,10 @@ public class TableAction {
return System.currentTimeMillis() >= (time + 300*1000);
}
private int getLimit(List<PossibleCandidate> possibleCandidates){
return possibleCandidates.size() <= 5 ? possibleCandidates.size() : 5;
}
public void voteFor(String id, CandidateRepository candidateRepository){
long candidateID = Long.valueOf(id);
Candidate candidate = candidateRepository.findById(candidateID).get();
@@ -98,17 +93,31 @@ public class TableAction {
}
}
public void setUpCandidates(CandidateRepository candidateRepository, CategoryRepository categoryRepository){
ArrayList<String> names = new ArrayList<>();
Collections.addAll(names, "Greta Bentgens", "Laura König", "Aaron Glos", "Lukas Boy", "Frau Meyering"
, "Frau Adams", "Herr Petering", "Frau Milde", "Frau Meyer");
ArrayList<Candidate> candidates = new ArrayList<>();
for (String name: names) {
Candidate candidate = new Candidate(name, categoryRepository.findById(20l).get());
candidates.add(candidate);
public void setUpCandidates(PossibleCandidateRepository possibleCandidateRepository, CandidateRepository candidateRepository){
List<PossibleCandidate> possibleCandidates = possibleCandidateRepository.findAll();
Collections.sort(possibleCandidates, Comparator.comparing(PossibleCandidate::getCategoryID));
long category_id = possibleCandidates.get(0).getCategory().getId();
List<PossibleCandidate> possibleCandidatesPerCategory = new LinkedList<>();
for (int i = 0; i < possibleCandidates.size(); i++) {
PossibleCandidate p = possibleCandidates.get(i);
if (category_id == p.getCategory().getId()){
possibleCandidatesPerCategory.add(p);
} else {
category_id = p.getCategory().getId();
Collections.sort(possibleCandidatesPerCategory, Comparator.comparing(PossibleCandidate::getVotes));
Collections.reverse(possibleCandidatesPerCategory);
for (int j = 0; j < getLimit(possibleCandidatesPerCategory); j++){
Candidate candidate = new Candidate(possibleCandidatesPerCategory.get(j).getName(), possibleCandidatesPerCategory.get(j).getCategory());
candidateRepository.save(candidate);
}
i += -1;
possibleCandidatesPerCategory.clear();
}
}
for (int j = 0; j < getLimit(possibleCandidatesPerCategory); j++){
Candidate candidate = new Candidate(possibleCandidatesPerCategory.get(j).getName(), possibleCandidatesPerCategory.get(j).getCategory());
candidateRepository.save(candidate);
}
candidateRepository.saveAll(candidates);
}
public void setUpCategories(CategoryRepository categoryRepository){
@@ -129,96 +138,4 @@ public class TableAction {
e.printStackTrace();
}
}
public String logCategories(CategoryRepository categoryRepository){
List<List<String>> rows = new ArrayList<>();
List<String> headers = Arrays.asList("Id", "Name", "Candidates");
rows.add(headers);
for (Category category: categoryRepository.findAll()) {
String candidateNames = "";
for (Candidate candidate: category.getCandidateList()){
candidateNames += candidate.getName() + "; ";
}
rows.add(Arrays.asList(""+category.getId(), category.getName(), candidateNames));
}
return formatAsTable(rows);
}
public String logVoters(VoterRepository voterRepository){
List<List<String>> rows = new ArrayList<>();
List<String> headers = Arrays.asList("Id", "E-Mail", "Vote_status");
rows.add(headers);
for (Voter voter: voterRepository.findAll()) {
rows.add(Arrays.asList(""+voter.getId(), voter.getEmail(), ""+voter.getVote_status()));
}
return formatAsTable(rows);
}
public String logCandidatesRepository(CandidateRepository candidateRepository){
List<List<String>> rows = new ArrayList<>();
List<String> headers = Arrays.asList("Id", "Name", "Votes");
rows.add(headers);
for (Candidate candidate: candidateRepository.findAll()) {
rows.add(Arrays.asList(""+candidate.getId(), candidate.getName(), ""+candidate.getVotes()));
}
return formatAsTable(rows);
}
public String logCandidates(LinkedList<Candidate> candidates, CategoryRepository categoryRepository){
List<List<String>> rows = new ArrayList<>();
List<String> headers = Arrays.asList("Id", "Name", "Votes", "Category_ID");
rows.add(headers);
long i = 1;
for (Candidate candidate: candidates) {
Category category = categoryRepository.findById(i).get();
rows.add(Arrays.asList("" + i, candidate.getName(), "" + 0, "" + category.getId()));
i++;
}
return formatAsTable(rows);
}
public String logPossibleCandidates(LinkedList<PossibleCandidate> possibleCandidates, CategoryRepository categoryRepository){
List<List<String>> rows = new ArrayList<>();
List<String> headers = Arrays.asList("Id", "Name", "Votes", "Category_ID");
rows.add(headers);
long i = 1;
for (PossibleCandidate possibleCandidate: possibleCandidates) {
Category category = categoryRepository.findById(i).get();
rows.add(Arrays.asList("" + i, possibleCandidate.getName(), "" + 0, "" + category.getId()));
i++;
}
return formatAsTable(rows);
}
private String formatAsTable(List<List<String>> rows) {
int[] maxLengths = new int[rows.get(0).size()];
for (List<String> row : rows)
{
for (int i = 0; i < row.size(); i++)
{
maxLengths[i] = Math.max(maxLengths[i], row.get(i).length());
}
}
StringBuilder formatBuilder = new StringBuilder();
for (int maxLength : maxLengths)
{
formatBuilder.append("%-").append(maxLength + 2).append("s");
}
String format = formatBuilder.toString();
StringBuilder result = new StringBuilder();
result.append("\n");
for (List<String> row : rows)
{
result.append(String.format(format, row.toArray(new String[0]))).append("\n");
}
return result.toString();
}
}

View File

@@ -21,17 +21,19 @@ body {
h1 {
color: #FFF;
font-size: 75px;
}
h2.categoryHeader {
color: #FFF;
font-size: 50px;
}
button {
background: transparent;
border: none;
color: #FFF;
font-size: .875rem;
font-size: 35px;
font-weight: normal;
letter-spacing: .125rem;
text-transform: uppercase;

View File

@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org/">
<html lang="de" xmlns:th="https://www.thymeleaf.org/">
<head>
<meta charset="UTF-8">
@@ -43,7 +43,7 @@
<div id="candidateAdding">
<div class="group" th:each="category, itemStat : ${categories}">
<h2 class="categoryHeader" th:text="${category.name}"></h2>
<input th:field="*{possibleCandidates[__${itemStat.index}__].name}" />
<input th:field="*{possibleCandidates[__${itemStat.index}__].name}"/>
</div>
</div>
<button class="submitButton" id="btnNext" type="submit">Nächste Frage</button>

View File

@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="de" xmlns:th="https://www.thymeleaf.org/">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
</html>