Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.79 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package lapr.project.controller;
  7.  
  8. import java.io.File;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import javafx.scene.control.Alert;
  12. import javax.xml.parsers.DocumentBuilder;
  13. import javax.xml.parsers.DocumentBuilderFactory;
  14. import javax.xml.parsers.ParserConfigurationException;
  15. import lapr.project.model.Application;
  16. import lapr.project.model.ApplicationRegister;
  17. import lapr.project.model.ApplicationSubmissionPeriod;
  18. import lapr.project.model.EmployeeRegister;
  19. import lapr.project.model.Event;
  20. import lapr.project.model.EventsRegister;
  21. import lapr.project.model.RelativeDistance;
  22. import lapr.project.model.Stand;
  23. import lapr.project.model.StandsRegister;
  24. import lapr.project.model.User;
  25. import lapr.project.model.UserRegister;
  26. import lapr.project.model.Workshop;
  27. import lapr.project.model.WorkshopList;
  28. import lapr.project.ui.MainApp;
  29. import org.w3c.dom.Document;
  30. import org.w3c.dom.Element;
  31. import org.w3c.dom.Node;
  32. import org.w3c.dom.NodeList;
  33.  
  34. /**
  35. *
  36. * @author jose
  37. */
  38. public class ImportDataFromFileController {
  39.  
  40. /**
  41. *
  42. * @param fileName
  43. */
  44. public static void importRegister(String fileName) {
  45. File xmlFile;
  46. try {
  47. if (fileName.equals("")) {
  48. xmlFile = new File("backup.xml");
  49. } else {
  50. xmlFile = new File(fileName);
  51. }
  52. try {
  53. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  54. DocumentBuilder builder = factory.newDocumentBuilder();
  55. Document doc = builder.parse(xmlFile);
  56. NodeList inicialNode = doc.getElementsByTagName("ExpoCenter");
  57. importAllElements(inicialNode);
  58. } catch (ParserConfigurationException e) {
  59. Alert alert = new Alert(Alert.AlertType.ERROR, "Import Fail");
  60. alert.show();
  61. }
  62. } catch (Exception e) {
  63. Alert alert = new Alert(Alert.AlertType.ERROR, "File not found");
  64. alert.show();
  65. }
  66.  
  67. }
  68.  
  69. /**
  70. *
  71. * @param inicialNode
  72. */
  73. public static void importAllElements(NodeList inicialNode) {
  74. UserRegister userRegister = null;
  75. EmployeeRegister emplyoeeRegister = null;
  76. EventsRegister eventRegister = null;
  77. for (int i = 0; i < inicialNode.getLength(); i++) {
  78. Node node = inicialNode.item(i);
  79. if (node.getNodeType() == Node.ELEMENT_NODE) {
  80. Element e = (Element) node;
  81. NodeList nodeUser = e.getElementsByTagName("UserRegister");
  82. userRegister = getUserRegister(nodeUser);
  83.  
  84. NodeList nodeEvent = e.getElementsByTagName("EventRegister");
  85. eventRegister = getEventRegister(nodeEvent);
  86.  
  87. NodeList nodeEmployee = e.getElementsByTagName("EmployeeRegister");
  88. emplyoeeRegister = getEmployeeRegister(nodeEmployee);
  89. }
  90. }
  91. MainApp.getExpo().setUsersRegister(userRegister);
  92. MainApp.getExpo().setEmployeeRegister(emplyoeeRegister);
  93. MainApp.getExpo().setEventsRegister(eventRegister);
  94. }
  95.  
  96. /**
  97. *
  98. * @param nodeList
  99. * @return
  100. */
  101. public static EventsRegister getEventRegister(NodeList nodeList) {
  102. List<Event> listEvent = new ArrayList<>();
  103. Event event = null;
  104. for (int i = 0; i < nodeList.getLength(); i++) {
  105. Node nodeEvent = nodeList.item(i);
  106. if (nodeEvent.getNodeType() == Node.ELEMENT_NODE) {
  107. Element ee = (Element) nodeEvent;
  108. event = getEvent(ee);
  109. Event c = new Event(event);
  110. listEvent.add(c);
  111. }
  112. }
  113. EventsRegister eventRegister = new EventsRegister(listEvent);
  114. return eventRegister;
  115. }
  116.  
  117. /**
  118. *
  119. * @param element
  120. * @return
  121. */
  122. public static Event getEvent(Element element) {
  123. Event event = new Event(element.getElementsByTagName("Title").item(0).getTextContent(), element.getElementsByTagName("Descritpion").item(0).getTextContent(),
  124. element.getElementsByTagName("Date").item(0).getTextContent(), element.getElementsByTagName("Location").item(0).getTextContent(),
  125. getApplicationSubmissionPeriod(element.getElementsByTagName("ApplicationSubmissionPeriod")));
  126. event.setApplicationRegister(getApplicationRegister(element.getElementsByTagName("ApplicationRegister")));
  127. event.setStandsRegister(getStandRegister(element.getElementsByTagName("StandRegister")));
  128. event.setUserRegister(getUserRegister(element.getElementsByTagName("UserRegister")));
  129. event.setOrganizersList(getListUser(element.getElementsByTagName("organizersList")));
  130. event.setStaffList(getListUser(element.getElementsByTagName("staffList")));
  131. event.setEventManager(getEventManager(element.getElementsByTagName("EventManager").item(0)));
  132. return event;
  133. }
  134.  
  135. public static User getEventManager(Node node) {
  136. Element user = (Element) node;
  137. return getUser(user);
  138. }
  139.  
  140. /**
  141. *
  142. * @param nodeList
  143. * @return
  144. */
  145. public static StandsRegister getStandRegister(NodeList nodeList) {
  146. List<Stand> list = new ArrayList<>();
  147. Stand stand = null;
  148. for (int i = 0; i < nodeList.getLength(); i++) {
  149. Node node = nodeList.item(i);
  150. if (node.getNodeType() == Node.ELEMENT_NODE) {
  151. Element e = (Element) node;
  152. stand = getStand(e);
  153. Stand c = new Stand(stand);
  154. list.add(c);
  155. }
  156. }
  157. StandsRegister standRegister = new StandsRegister(list);
  158. return standRegister;
  159. }
  160.  
  161. /**
  162. *
  163. * @param nodeList
  164. * @return
  165. */
  166. public static ApplicationRegister getApplicationRegister(NodeList nodeList) {
  167. List<Application> reviewList = getApplicationList(nodeList, "ReviewList");
  168. List<Application> acceptedList = getApplicationList(nodeList, "AcceptedList");
  169. List<Application> declinedList = getApplicationList(nodeList, "DeclinedList");
  170. List<Application> pendingList = getApplicationList(nodeList, "PendingList");
  171. ApplicationRegister applicationRegister = new ApplicationRegister(reviewList, acceptedList, declinedList, pendingList);
  172. return applicationRegister;
  173. }
  174.  
  175. /**
  176. *
  177. * @param nodeList
  178. * @param opList
  179. * @return
  180. */
  181. public static List<Application> getApplicationList(NodeList nodeList, String opList) {
  182. List<Application> list = new ArrayList<>();
  183. Application application = null;
  184. for (int i = 0; i < nodeList.getLength(); i++) {
  185. Node node = nodeList.item(i);
  186. if (node.getNodeType() == Node.ELEMENT_NODE) {
  187. Element element = (Element) node;
  188. application = getApplication(element);
  189. Application c = new Application(application);
  190. list.add(c);
  191. }
  192. }
  193. return list;
  194. }
  195.  
  196. /**
  197. *
  198. * @param element
  199. * @return
  200. */
  201. public static Application getApplication(Element element) {
  202. Application application = new Application(element.getElementsByTagName("TradeName").item(0).getTextContent(), Integer.parseInt(element.getElementsByTagName("VatNumber").item(0).getTextContent()),
  203. Integer.parseInt(element.getElementsByTagName("PhoneNumber").item(0).getTextContent()), Double.parseDouble(element.getElementsByTagName("StandArea").item(0).getTextContent()),
  204. getListString(element.getElementsByTagName("ProductsDisplayed")), Integer.parseInt(element.getElementsByTagName("NumberInvites").item(0).getTextContent()),
  205. getListString(element.getElementsByTagName("ApplicationTopics")), getWorkshopList(element.getElementsByTagName("WorkshopsList")), getStand(element.getElementsByTagName("Stand").item(0)));
  206.  
  207. application.setRates(rates);
  208.  
  209. return application;
  210. }
  211.  
  212. /**
  213. *
  214. * @param node
  215. * @return
  216. */
  217. public static Stand getStand(Node node) {
  218. Stand stand = null;
  219. if (node.getNodeType() == Node.ELEMENT_NODE) {
  220. Element e = (Element) node;
  221. stand = new Stand(Double.parseDouble(e.getElementsByTagName("Area").item(0).getTextContent()), e.getElementsByTagName("Description").item(0).getTextContent(),
  222. getRelativeDistance(e.getElementsByTagName("RelativeDistance")));
  223. }
  224. return stand;
  225. }
  226.  
  227. public static List<RelativeDistance> getRelativeDistance(NodeList nodeList) {
  228. List<RelativeDistance> list = new ArrayList<>();
  229. RelativeDistance relativeDistance = null;
  230. for (int i = 0; i < nodeList.getLength(); i++) {
  231. Node node = nodeList.item(i);
  232. if (node.getNodeType() == Node.ELEMENT_NODE) {
  233. Element e = (Element) node;
  234. relativeDistance = new RelativeDistance(getStand(e.getElementsByTagName("Stand").item(i)), Double.parseDouble(e.getElementsByTagName("Distance").item(0).getTextContent()));
  235. RelativeDistance c = new RelativeDistance(relativeDistance);
  236. list.add(c);
  237. }
  238. }
  239. return list;
  240. }
  241.  
  242. /**
  243. *
  244. * @param nodeList
  245. * @return
  246. */
  247. public static WorkshopList getWorkshopList(NodeList nodeList) {
  248. WorkshopList workshop = null;
  249. for (int i = 0; i < nodeList.getLength(); i++) {
  250. Node node = nodeList.item(i);
  251. if (node.getNodeType() == Node.ELEMENT_NODE) {
  252. Element e = (Element) node;
  253. List<Workshop> waitingList = getWorkshopL(e.getElementsByTagName("WaitingList"));
  254. List<Workshop> acceptedList = getWorkshopL(e.getElementsByTagName("AcceptedList"));
  255. List<Workshop> declinedList = getWorkshopL(e.getElementsByTagName("DeclinedList"));
  256. workshop = new WorkshopList(waitingList, acceptedList, declinedList);
  257. }
  258. }
  259. return workshop;
  260. }
  261.  
  262. public static List<Workshop> getWorkshopL(NodeList nodeList) {
  263. List<Workshop> list = null;
  264. Workshop workshop;
  265. for (int i = 0; i < nodeList.getLength(); i++) {
  266. Node node = nodeList.item(i);
  267. if (node.getNodeType() == Node.ELEMENT_NODE) {
  268. Element e = (Element) node;
  269. workshop = new Workshop(e.getElementsByTagName("Description").item(0).getTextContent(), e.getElementsByTagName("Duration").item(0).getTextContent(), getListString(e.getElementsByTagName("Equipment")));
  270. Workshop c = new Workshop(workshop);
  271. list.add(c);
  272. }
  273. }
  274. return list;
  275. }
  276.  
  277. /**
  278. *
  279. * @param nodeList
  280. * @return
  281. */
  282. public static List<String> getListString(NodeList nodeList) {
  283. List<String> list = new ArrayList<>();
  284. for (int i = 0; i < nodeList.getLength(); i++) {
  285. Node node = nodeList.item(i);
  286. if (node.getNodeType() == Node.ELEMENT_NODE) {
  287. Element e = (Element) node;
  288. list.add(e.getTextContent());
  289. }
  290. }
  291. return list;
  292. }
  293.  
  294. /**
  295. *
  296. * @param nodeList
  297. * @return
  298. */
  299. public static ApplicationSubmissionPeriod getApplicationSubmissionPeriod(NodeList nodeList) {
  300. Node begin = nodeList.item(0);
  301. Node end = nodeList.item(1);
  302. ApplicationSubmissionPeriod applicationSubmissionPeriod = new ApplicationSubmissionPeriod(begin.getTextContent(), end.getTextContent());
  303. return applicationSubmissionPeriod;
  304. }
  305.  
  306. /**
  307. *
  308. * @param nodeList
  309. * @return
  310. */
  311. public static EmployeeRegister getEmployeeRegister(NodeList nodeList) {
  312. List<User> listUser = getListUser(nodeList);
  313. EmployeeRegister employeeRegister = new EmployeeRegister(listUser);
  314. return employeeRegister;
  315. }
  316.  
  317. /**
  318. *
  319. * @param nodeList
  320. * @return
  321. */
  322. public static UserRegister getUserRegister(NodeList nodeList) {
  323. List<User> listUser = getListUser(nodeList);
  324. UserRegister userRegister = new UserRegister(listUser);
  325. return userRegister;
  326. }
  327.  
  328. /**
  329. *
  330. * @param nodeList
  331. * @return
  332. */
  333. public static List<User> getListUser(NodeList nodeList) {
  334. User u;
  335. List<User> listUser = new ArrayList<>();
  336. for (int i = 0; i < nodeList.getLength(); i++) {
  337. Node nodeUser = nodeList.item(i);
  338. if (nodeUser.getNodeType() == Node.ELEMENT_NODE) {
  339. Element us = (Element) nodeUser;
  340. NodeList nodeUserList = us.getElementsByTagName("User");
  341. for (int j = 0; j < nodeUserList.getLength(); j++) {
  342. Node nodeGetUser = nodeUserList.item(j);
  343. Element user = (Element) nodeGetUser;
  344. u = getUser(user);
  345. User c = new User(u);
  346. listUser.add(c);
  347. }
  348. }
  349. }
  350. return listUser;
  351. }
  352.  
  353. /**
  354. *
  355. * @param user
  356. * @return
  357. */
  358. public static User getUser(Element user) {
  359. User u = new User(user.getElementsByTagName("Name").item(0).getTextContent(), user.getElementsByTagName("Email").item(0).getTextContent(),
  360. user.getElementsByTagName("Username").item(0).getTextContent(), User.encripPassword(user.getElementsByTagName("Password").item(0).getTextContent()));
  361. return u;
  362. }
  363.  
  364. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement