Added dynamic resource parsing and fixed breaking bug

This commit is contained in:
2020-12-08 23:03:14 +01:00
parent 525ba69689
commit 9bf9f8acdc
2 changed files with 14 additions and 22 deletions

View File

@@ -62,7 +62,7 @@ public class VotingController {
LOGGER.info("Categories successfully set up"); LOGGER.info("Categories successfully set up");
} }
if (candidateRepository.findAll().size() == 0 && candidatesAdded == true) { if (candidateRepository.findAll().size() == 0 && candidatesAdded == 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");
} }

View File

@@ -5,9 +5,7 @@ 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 org.aspectj.weaver.loadtime.definition.LightXMLParser;
import java.io.File; import java.io.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.net.JarURLConnection; import java.net.JarURLConnection;
import java.net.URL; import java.net.URL;
@@ -75,20 +73,17 @@ public class TableAction {
} }
public void setUpVoters(VoterRepository voterRepository){ public void setUpVoters(VoterRepository voterRepository){
try { try (InputStream inputStream = getClass().getResourceAsStream("/Q2_emails.txt");
String path = "src/main/resources/Q2_emails.txt"; BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
File emailFile = new File(path); String line = "";
Scanner myReader = new Scanner(emailFile);
ArrayList<Voter> voters = new ArrayList<Voter>(); ArrayList<Voter> voters = new ArrayList<Voter>();
while (myReader.hasNextLine()) { while ((line = reader.readLine())!= null){
String email = myReader.nextLine(); String email = line;
Voter voter = new Voter(email); Voter voter = new Voter(email);
voters.add(voter); voters.add(voter);
} }
voterRepository.saveAll(voters); voterRepository.saveAll(voters);
myReader.close(); } catch (IOException e) {
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace(); e.printStackTrace();
} }
} }
@@ -121,20 +116,17 @@ public class TableAction {
} }
public void setUpCategories(CategoryRepository categoryRepository){ public void setUpCategories(CategoryRepository categoryRepository){
try { try (InputStream inputStream = getClass().getResourceAsStream("/Categories.txt");
String path = "src/main/resources/Categories.txt"; BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
File categoryFile = new File(path); String line = "";
Scanner myReader = new Scanner(categoryFile);
ArrayList<Category> categories = new ArrayList<Category>(); ArrayList<Category> categories = new ArrayList<Category>();
while (myReader.hasNextLine()) { while ((line = reader.readLine())!= null){
String name = myReader.nextLine(); String name = line;
Category category = new Category(name); Category category = new Category(name);
categories.add(category); categories.add(category);
} }
categoryRepository.saveAll(categories); categoryRepository.saveAll(categories);
myReader.close(); } catch (IOException e) {
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace(); e.printStackTrace();
} }
} }