Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.24 KB | None | 0 0
  1. package lapr.project.utils;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import java.util.Scanner;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10. import lapr.project.model.Application;
  11. import lapr.project.model.ApplicationsList;
  12. import lapr.project.model.Assignment;
  13. import lapr.project.model.AssignmentsList;
  14. import lapr.project.model.Decision;
  15. import lapr.project.model.DecisionsList;
  16. import lapr.project.model.Event;
  17. import lapr.project.model.FAE;
  18. import lapr.project.model.FAEList;
  19. import lapr.project.model.Keyword;
  20. import lapr.project.model.Stand;
  21. import lapr.project.model.StandList;
  22. import lapr.project.model.User;
  23.  
  24. /**
  25. * Class responsible for loading event information from a file
  26. *
  27. * @author Francisco
  28. */
  29. public class EventInfoImporter {
  30.  
  31. private final Event event;
  32.  
  33. private static final int IGNOREDCOLUMNS = 6; //Columns that aren't dedicated to info about the FAE
  34. private int remainingColumns;
  35. private static final int FAECOLUMNS = 4; //Number of columns of information dedicated to each FAE
  36. private static final int DECISIONCOLUMNS = 5; //Number of columns of information dedicated to each decision
  37. private int faeInfoStart = 10; //Column at which FAE's info starts
  38. private static final int DECISIONINFOSTART = 5; //Column at which Decision's info starts
  39.  
  40. /**
  41. *
  42. * @param event
  43. */
  44. public EventInfoImporter(Event event)
  45. {
  46. this.event = event;
  47. }
  48.  
  49. /**
  50. *
  51. * @param fileName
  52. */
  53. public void importFromCSVFile(String fileName)
  54. {
  55. int linesToSkip = 1;
  56. int i = 0;
  57.  
  58. FAEList faeList = new FAEList();
  59. AssignmentsList assignmentList = new AssignmentsList();
  60. DecisionsList decisionList = new DecisionsList();
  61. ApplicationsList applicationsList = new ApplicationsList();
  62.  
  63. Scanner f = null;
  64.  
  65. try
  66. {
  67. f = new Scanner(new File(fileName));
  68.  
  69. String firstLine = f.nextLine();
  70.  
  71. int totalColumns = firstLine.split(";").length;
  72.  
  73. remainingColumns = totalColumns - IGNOREDCOLUMNS;
  74.  
  75. int nFAEs = countFAES();
  76.  
  77. while (f.hasNext())
  78. {
  79. String[] line = f.nextLine().split(";");
  80.  
  81. if (i == 0) //Get FAEs from 2nd line
  82. {
  83. for (int j = 0; j < nFAEs; j++)
  84. {
  85. String name = line[faeInfoStart];
  86. String email = line[faeInfoStart + 1];
  87. String username = line[faeInfoStart + 2];
  88. String password = line[faeInfoStart + 3];
  89.  
  90. User u = new User(name, email, username, password, 0);
  91.  
  92. faeList.registerFAE(u);
  93.  
  94. faeInfoStart += DECISIONCOLUMNS + FAECOLUMNS;
  95. }
  96. }
  97.  
  98. if (i >= linesToSkip - 1) //Ignore header line (1st line)
  99. {
  100. boolean accepted = Boolean.parseBoolean(line[1]);
  101. String description = line[2];
  102. float boothArea = Float.parseFloat(line[3]);
  103. int invitesQuantity = Integer.parseInt(line[4]);
  104. List<Keyword> keywordList = parseKeywordList(line[totalColumns - 1]);
  105.  
  106. Application application = new Application(boothArea, invitesQuantity, description, keywordList);
  107. application.setFinalDecision(accepted);
  108. applicationsList.addApplication(application);
  109.  
  110. int k = DECISIONINFOSTART;
  111.  
  112. for (FAE fae : faeList.getFAElist())
  113. {
  114. Assignment assignment = new Assignment(fae,application);
  115. assignmentList.addAssignment(assignment);
  116. application.setAssigned();
  117.  
  118. String text = line[k];
  119. double faeTopicKnowledge = Double.parseDouble(line[k+1]);
  120. double eventAdequacy = Double.parseDouble(line[k+2]);
  121. double inviteAdequacy = Double.parseDouble(line[k+3]);
  122. double recommendation = Double.parseDouble(line[k+4]);
  123.  
  124. Decision d = new Decision(application, accepted, text, faeTopicKnowledge, eventAdequacy, inviteAdequacy, recommendation);
  125. decisionList.registerDecision(d);
  126. fae.addReview(d);
  127. application.setDecided();
  128.  
  129. k += DECISIONCOLUMNS + FAECOLUMNS;
  130. }
  131.  
  132. event.setFAEList(faeList);
  133. event.setAssignmentsList(assignmentList);
  134. event.setDecisionList(decisionList);
  135.  
  136. for (Application a : applicationsList.getApplicationsList())
  137. {
  138. event.getApplicationsList().addApplication(a);
  139. }
  140.  
  141. } else
  142. {
  143. f.nextLine();
  144. }
  145. i++;
  146. }
  147.  
  148. } catch (FileNotFoundException ex)
  149. {
  150. Logger.getLogger(EventInfoImporter.class.getName()).log(Level.SEVERE, null, ex);
  151. }
  152. finally
  153. {
  154. if (f != null)
  155. {
  156. f.close();
  157. }
  158. }
  159. }
  160.  
  161. /**
  162. * Transforms a string of keywords separated by commas into a keyword List
  163. *
  164. * @param keywords String of keywords
  165. * @return List of keywords
  166. */
  167. private List<Keyword> parseKeywordList(String keywords) //Should repeated keywords be tested?
  168. {
  169. List<Keyword> keywordList = new ArrayList<>();
  170.  
  171. String[] arrKeywords = keywords.split(",");
  172.  
  173. for (String keyword : arrKeywords)
  174. {
  175. Keyword kw = new Keyword(keyword);
  176. keywordList.add(kw);
  177. }
  178.  
  179. return keywordList;
  180. }
  181.  
  182. /**
  183. * Counts number of FAES by being given the header string of the table
  184. *
  185. * @param firstLine First line/header line
  186. */
  187. private int countFAES()
  188. {
  189. if ((FAECOLUMNS + DECISIONCOLUMNS) == 0)
  190. {
  191. return 0;
  192. }
  193.  
  194. return (remainingColumns / (FAECOLUMNS + DECISIONCOLUMNS));
  195. }
  196.  
  197. /**
  198. * Imports stand information from a csv file
  199. * @param fileName Name of the file containing the stand information
  200. */
  201. public void importStands(String fileName)
  202. {
  203. StandImporter si = new StandImporter();
  204.  
  205. List<Stand> sL= si.importStands(fileName);
  206.  
  207. StandList standList = new StandList();
  208.  
  209. for (Stand stand : sL)
  210. {
  211. standList.addStand(stand);
  212. }
  213.  
  214. event.setStandList(standList);
  215. }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement