Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package lapr.project.controller;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.List;
- import javafx.scene.control.Alert;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.parsers.ParserConfigurationException;
- import lapr.project.model.Application;
- import lapr.project.model.ApplicationRegister;
- import lapr.project.model.ApplicationSubmissionPeriod;
- import lapr.project.model.EmployeeRegister;
- import lapr.project.model.Event;
- import lapr.project.model.EventsRegister;
- import lapr.project.model.RelativeDistance;
- import lapr.project.model.Stand;
- import lapr.project.model.StandsRegister;
- import lapr.project.model.User;
- import lapr.project.model.UserRegister;
- import lapr.project.model.Workshop;
- import lapr.project.model.WorkshopList;
- import lapr.project.ui.MainApp;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- /**
- *
- * @author jose
- */
- public class ImportDataFromFileController {
- /**
- *
- * @param fileName
- */
- public static void importRegister(String fileName) {
- File xmlFile;
- try {
- if (fileName.equals("")) {
- xmlFile = new File("backup.xml");
- } else {
- xmlFile = new File(fileName);
- }
- try {
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder = factory.newDocumentBuilder();
- Document doc = builder.parse(xmlFile);
- NodeList inicialNode = doc.getElementsByTagName("ExpoCenter");
- importAllElements(inicialNode);
- } catch (ParserConfigurationException e) {
- Alert alert = new Alert(Alert.AlertType.ERROR, "Import Fail");
- alert.show();
- }
- } catch (Exception e) {
- Alert alert = new Alert(Alert.AlertType.ERROR, "File not found");
- alert.show();
- }
- }
- /**
- *
- * @param inicialNode
- */
- public static void importAllElements(NodeList inicialNode) {
- UserRegister userRegister = null;
- EmployeeRegister emplyoeeRegister = null;
- EventsRegister eventRegister = null;
- for (int i = 0; i < inicialNode.getLength(); i++) {
- Node node = inicialNode.item(i);
- if (node.getNodeType() == Node.ELEMENT_NODE) {
- Element e = (Element) node;
- NodeList nodeUser = e.getElementsByTagName("UserRegister");
- userRegister = getUserRegister(nodeUser);
- NodeList nodeEvent = e.getElementsByTagName("EventRegister");
- eventRegister = getEventRegister(nodeEvent);
- NodeList nodeEmployee = e.getElementsByTagName("EmployeeRegister");
- emplyoeeRegister = getEmployeeRegister(nodeEmployee);
- }
- }
- MainApp.getExpo().setUsersRegister(userRegister);
- MainApp.getExpo().setEmployeeRegister(emplyoeeRegister);
- MainApp.getExpo().setEventsRegister(eventRegister);
- }
- /**
- *
- * @param nodeList
- * @return
- */
- public static EventsRegister getEventRegister(NodeList nodeList) {
- List<Event> listEvent = new ArrayList<>();
- Event event = null;
- for (int i = 0; i < nodeList.getLength(); i++) {
- Node nodeEvent = nodeList.item(i);
- if (nodeEvent.getNodeType() == Node.ELEMENT_NODE) {
- Element ee = (Element) nodeEvent;
- event = getEvent(ee);
- Event c = new Event(event);
- listEvent.add(c);
- }
- }
- EventsRegister eventRegister = new EventsRegister(listEvent);
- return eventRegister;
- }
- /**
- *
- * @param element
- * @return
- */
- public static Event getEvent(Element element) {
- Event event = new Event(element.getElementsByTagName("Title").item(0).getTextContent(), element.getElementsByTagName("Descritpion").item(0).getTextContent(),
- element.getElementsByTagName("Date").item(0).getTextContent(), element.getElementsByTagName("Location").item(0).getTextContent(),
- getApplicationSubmissionPeriod(element.getElementsByTagName("ApplicationSubmissionPeriod")));
- event.setApplicationRegister(getApplicationRegister(element.getElementsByTagName("ApplicationRegister")));
- event.setStandsRegister(getStandRegister(element.getElementsByTagName("StandRegister")));
- event.setUserRegister(getUserRegister(element.getElementsByTagName("UserRegister")));
- event.setOrganizersList(getListUser(element.getElementsByTagName("organizersList")));
- event.setStaffList(getListUser(element.getElementsByTagName("staffList")));
- event.setEventManager(getEventManager(element.getElementsByTagName("EventManager").item(0)));
- return event;
- }
- public static User getEventManager(Node node) {
- Element user = (Element) node;
- return getUser(user);
- }
- /**
- *
- * @param nodeList
- * @return
- */
- public static StandsRegister getStandRegister(NodeList nodeList) {
- List<Stand> list = new ArrayList<>();
- Stand stand = null;
- for (int i = 0; i < nodeList.getLength(); i++) {
- Node node = nodeList.item(i);
- if (node.getNodeType() == Node.ELEMENT_NODE) {
- Element e = (Element) node;
- stand = getStand(e);
- Stand c = new Stand(stand);
- list.add(c);
- }
- }
- StandsRegister standRegister = new StandsRegister(list);
- return standRegister;
- }
- /**
- *
- * @param nodeList
- * @return
- */
- public static ApplicationRegister getApplicationRegister(NodeList nodeList) {
- List<Application> reviewList = getApplicationList(nodeList, "ReviewList");
- List<Application> acceptedList = getApplicationList(nodeList, "AcceptedList");
- List<Application> declinedList = getApplicationList(nodeList, "DeclinedList");
- List<Application> pendingList = getApplicationList(nodeList, "PendingList");
- ApplicationRegister applicationRegister = new ApplicationRegister(reviewList, acceptedList, declinedList, pendingList);
- return applicationRegister;
- }
- /**
- *
- * @param nodeList
- * @param opList
- * @return
- */
- public static List<Application> getApplicationList(NodeList nodeList, String opList) {
- List<Application> list = new ArrayList<>();
- Application application = null;
- for (int i = 0; i < nodeList.getLength(); i++) {
- Node node = nodeList.item(i);
- if (node.getNodeType() == Node.ELEMENT_NODE) {
- Element element = (Element) node;
- application = getApplication(element);
- Application c = new Application(application);
- list.add(c);
- }
- }
- return list;
- }
- /**
- *
- * @param element
- * @return
- */
- public static Application getApplication(Element element) {
- Application application = new Application(element.getElementsByTagName("TradeName").item(0).getTextContent(), Integer.parseInt(element.getElementsByTagName("VatNumber").item(0).getTextContent()),
- Integer.parseInt(element.getElementsByTagName("PhoneNumber").item(0).getTextContent()), Double.parseDouble(element.getElementsByTagName("StandArea").item(0).getTextContent()),
- getListString(element.getElementsByTagName("ProductsDisplayed")), Integer.parseInt(element.getElementsByTagName("NumberInvites").item(0).getTextContent()),
- getListString(element.getElementsByTagName("ApplicationTopics")), getWorkshopList(element.getElementsByTagName("WorkshopsList")), getStand(element.getElementsByTagName("Stand").item(0)));
- application.setRates(rates);
- return application;
- }
- /**
- *
- * @param node
- * @return
- */
- public static Stand getStand(Node node) {
- Stand stand = null;
- if (node.getNodeType() == Node.ELEMENT_NODE) {
- Element e = (Element) node;
- stand = new Stand(Double.parseDouble(e.getElementsByTagName("Area").item(0).getTextContent()), e.getElementsByTagName("Description").item(0).getTextContent(),
- getRelativeDistance(e.getElementsByTagName("RelativeDistance")));
- }
- return stand;
- }
- public static List<RelativeDistance> getRelativeDistance(NodeList nodeList) {
- List<RelativeDistance> list = new ArrayList<>();
- RelativeDistance relativeDistance = null;
- for (int i = 0; i < nodeList.getLength(); i++) {
- Node node = nodeList.item(i);
- if (node.getNodeType() == Node.ELEMENT_NODE) {
- Element e = (Element) node;
- relativeDistance = new RelativeDistance(getStand(e.getElementsByTagName("Stand").item(i)), Double.parseDouble(e.getElementsByTagName("Distance").item(0).getTextContent()));
- RelativeDistance c = new RelativeDistance(relativeDistance);
- list.add(c);
- }
- }
- return list;
- }
- /**
- *
- * @param nodeList
- * @return
- */
- public static WorkshopList getWorkshopList(NodeList nodeList) {
- WorkshopList workshop = null;
- for (int i = 0; i < nodeList.getLength(); i++) {
- Node node = nodeList.item(i);
- if (node.getNodeType() == Node.ELEMENT_NODE) {
- Element e = (Element) node;
- List<Workshop> waitingList = getWorkshopL(e.getElementsByTagName("WaitingList"));
- List<Workshop> acceptedList = getWorkshopL(e.getElementsByTagName("AcceptedList"));
- List<Workshop> declinedList = getWorkshopL(e.getElementsByTagName("DeclinedList"));
- workshop = new WorkshopList(waitingList, acceptedList, declinedList);
- }
- }
- return workshop;
- }
- public static List<Workshop> getWorkshopL(NodeList nodeList) {
- List<Workshop> list = null;
- Workshop workshop;
- for (int i = 0; i < nodeList.getLength(); i++) {
- Node node = nodeList.item(i);
- if (node.getNodeType() == Node.ELEMENT_NODE) {
- Element e = (Element) node;
- workshop = new Workshop(e.getElementsByTagName("Description").item(0).getTextContent(), e.getElementsByTagName("Duration").item(0).getTextContent(), getListString(e.getElementsByTagName("Equipment")));
- Workshop c = new Workshop(workshop);
- list.add(c);
- }
- }
- return list;
- }
- /**
- *
- * @param nodeList
- * @return
- */
- public static List<String> getListString(NodeList nodeList) {
- List<String> list = new ArrayList<>();
- for (int i = 0; i < nodeList.getLength(); i++) {
- Node node = nodeList.item(i);
- if (node.getNodeType() == Node.ELEMENT_NODE) {
- Element e = (Element) node;
- list.add(e.getTextContent());
- }
- }
- return list;
- }
- /**
- *
- * @param nodeList
- * @return
- */
- public static ApplicationSubmissionPeriod getApplicationSubmissionPeriod(NodeList nodeList) {
- Node begin = nodeList.item(0);
- Node end = nodeList.item(1);
- ApplicationSubmissionPeriod applicationSubmissionPeriod = new ApplicationSubmissionPeriod(begin.getTextContent(), end.getTextContent());
- return applicationSubmissionPeriod;
- }
- /**
- *
- * @param nodeList
- * @return
- */
- public static EmployeeRegister getEmployeeRegister(NodeList nodeList) {
- List<User> listUser = getListUser(nodeList);
- EmployeeRegister employeeRegister = new EmployeeRegister(listUser);
- return employeeRegister;
- }
- /**
- *
- * @param nodeList
- * @return
- */
- public static UserRegister getUserRegister(NodeList nodeList) {
- List<User> listUser = getListUser(nodeList);
- UserRegister userRegister = new UserRegister(listUser);
- return userRegister;
- }
- /**
- *
- * @param nodeList
- * @return
- */
- public static List<User> getListUser(NodeList nodeList) {
- User u;
- List<User> listUser = new ArrayList<>();
- for (int i = 0; i < nodeList.getLength(); i++) {
- Node nodeUser = nodeList.item(i);
- if (nodeUser.getNodeType() == Node.ELEMENT_NODE) {
- Element us = (Element) nodeUser;
- NodeList nodeUserList = us.getElementsByTagName("User");
- for (int j = 0; j < nodeUserList.getLength(); j++) {
- Node nodeGetUser = nodeUserList.item(j);
- Element user = (Element) nodeGetUser;
- u = getUser(user);
- User c = new User(u);
- listUser.add(c);
- }
- }
- }
- return listUser;
- }
- /**
- *
- * @param user
- * @return
- */
- public static User getUser(Element user) {
- User u = new User(user.getElementsByTagName("Name").item(0).getTextContent(), user.getElementsByTagName("Email").item(0).getTextContent(),
- user.getElementsByTagName("Username").item(0).getTextContent(), User.encripPassword(user.getElementsByTagName("Password").item(0).getTextContent()));
- return u;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement