Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lapr.project.utils;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import lapr.project.model.Application;
- import lapr.project.model.ApplicationsList;
- import lapr.project.model.Assignment;
- import lapr.project.model.AssignmentsList;
- import lapr.project.model.Decision;
- import lapr.project.model.DecisionsList;
- import lapr.project.model.Event;
- import lapr.project.model.FAE;
- import lapr.project.model.FAEList;
- import lapr.project.model.Keyword;
- import lapr.project.model.Stand;
- import lapr.project.model.StandList;
- import lapr.project.model.User;
- /**
- * Class responsible for loading event information from a file
- *
- * @author Francisco
- */
- public class EventInfoImporter {
- private final Event event;
- private static final int IGNOREDCOLUMNS = 6; //Columns that aren't dedicated to info about the FAE
- private int remainingColumns;
- private static final int FAECOLUMNS = 4; //Number of columns of information dedicated to each FAE
- private static final int DECISIONCOLUMNS = 5; //Number of columns of information dedicated to each decision
- private int faeInfoStart = 10; //Column at which FAE's info starts
- private static final int DECISIONINFOSTART = 5; //Column at which Decision's info starts
- /**
- *
- * @param event
- */
- public EventInfoImporter(Event event)
- {
- this.event = event;
- }
- /**
- *
- * @param fileName
- */
- public void importFromCSVFile(String fileName)
- {
- int linesToSkip = 1;
- int i = 0;
- FAEList faeList = new FAEList();
- AssignmentsList assignmentList = new AssignmentsList();
- DecisionsList decisionList = new DecisionsList();
- ApplicationsList applicationsList = new ApplicationsList();
- Scanner f = null;
- try
- {
- f = new Scanner(new File(fileName));
- String firstLine = f.nextLine();
- int totalColumns = firstLine.split(";").length;
- remainingColumns = totalColumns - IGNOREDCOLUMNS;
- int nFAEs = countFAES();
- while (f.hasNext())
- {
- String[] line = f.nextLine().split(";");
- if (i == 0) //Get FAEs from 2nd line
- {
- for (int j = 0; j < nFAEs; j++)
- {
- String name = line[faeInfoStart];
- String email = line[faeInfoStart + 1];
- String username = line[faeInfoStart + 2];
- String password = line[faeInfoStart + 3];
- User u = new User(name, email, username, password, 0);
- faeList.registerFAE(u);
- faeInfoStart += DECISIONCOLUMNS + FAECOLUMNS;
- }
- }
- if (i >= linesToSkip - 1) //Ignore header line (1st line)
- {
- boolean accepted = Boolean.parseBoolean(line[1]);
- String description = line[2];
- float boothArea = Float.parseFloat(line[3]);
- int invitesQuantity = Integer.parseInt(line[4]);
- List<Keyword> keywordList = parseKeywordList(line[totalColumns - 1]);
- Application application = new Application(boothArea, invitesQuantity, description, keywordList);
- application.setFinalDecision(accepted);
- applicationsList.addApplication(application);
- int k = DECISIONINFOSTART;
- for (FAE fae : faeList.getFAElist())
- {
- Assignment assignment = new Assignment(fae,application);
- assignmentList.addAssignment(assignment);
- application.setAssigned();
- String text = line[k];
- double faeTopicKnowledge = Double.parseDouble(line[k+1]);
- double eventAdequacy = Double.parseDouble(line[k+2]);
- double inviteAdequacy = Double.parseDouble(line[k+3]);
- double recommendation = Double.parseDouble(line[k+4]);
- Decision d = new Decision(application, accepted, text, faeTopicKnowledge, eventAdequacy, inviteAdequacy, recommendation);
- decisionList.registerDecision(d);
- fae.addReview(d);
- application.setDecided();
- k += DECISIONCOLUMNS + FAECOLUMNS;
- }
- event.setFAEList(faeList);
- event.setAssignmentsList(assignmentList);
- event.setDecisionList(decisionList);
- for (Application a : applicationsList.getApplicationsList())
- {
- event.getApplicationsList().addApplication(a);
- }
- } else
- {
- f.nextLine();
- }
- i++;
- }
- } catch (FileNotFoundException ex)
- {
- Logger.getLogger(EventInfoImporter.class.getName()).log(Level.SEVERE, null, ex);
- }
- finally
- {
- if (f != null)
- {
- f.close();
- }
- }
- }
- /**
- * Transforms a string of keywords separated by commas into a keyword List
- *
- * @param keywords String of keywords
- * @return List of keywords
- */
- private List<Keyword> parseKeywordList(String keywords) //Should repeated keywords be tested?
- {
- List<Keyword> keywordList = new ArrayList<>();
- String[] arrKeywords = keywords.split(",");
- for (String keyword : arrKeywords)
- {
- Keyword kw = new Keyword(keyword);
- keywordList.add(kw);
- }
- return keywordList;
- }
- /**
- * Counts number of FAES by being given the header string of the table
- *
- * @param firstLine First line/header line
- */
- private int countFAES()
- {
- if ((FAECOLUMNS + DECISIONCOLUMNS) == 0)
- {
- return 0;
- }
- return (remainingColumns / (FAECOLUMNS + DECISIONCOLUMNS));
- }
- /**
- * Imports stand information from a csv file
- * @param fileName Name of the file containing the stand information
- */
- public void importStands(String fileName)
- {
- StandImporter si = new StandImporter();
- List<Stand> sL= si.importStands(fileName);
- StandList standList = new StandList();
- for (Stand stand : sL)
- {
- standList.addStand(stand);
- }
- event.setStandList(standList);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement