Пример #1
0
 public Participant getParticipant(String username) {
   for (Participant p : participants) {
     if (p.getEmployee().getUsername().equals(username)) {
       return p;
     }
   }
   return null;
 }
Пример #2
0
  public static ArrayList<Appointment> xmlToAppoinmentList(String xml)
      throws ParserConfigurationException, SAXException, IOException, TimeException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    InputSource is = new InputSource(new StringReader(xml));
    Document doc = db.parse(is);
    doc.getDocumentElement().normalize();

    String loc = "", roomnr = "13";

    ArrayList<Appointment> appointmentList = new ArrayList<Appointment>();
    NodeList nodeLst = doc.getElementsByTagName("appointment");

    for (int i = 0; i < nodeLst.getLength(); i++) {
      Appointment appointment = new Appointment();
      List<Participant> participants = new ArrayList<Participant>();
      Node nNode = nodeLst.item(i);
      if (nNode.getNodeType() == Node.ELEMENT_NODE) {
        Element element = (Element) nNode;
        int month = Integer.parseInt(getTagValues("month", element));
        int year = Integer.parseInt(getTagValues("year", element));
        int dayInMonth = Integer.parseInt(getTagValues("dayInMonth", element));
        appointment.setDate(new Date(year, month, dayInMonth));
        String starttime = getTagValues("starttime", element);
        appointment.setStart(Time.parseTime(starttime));
        String endtime = getTagValues("endtime", element);
        appointment.setEnd(Time.parseTime(endtime));
        String subject = getTagValues("subject", element);
        appointment.setSubject(subject);
        int id = Integer.parseInt(getTagValues("id", element));
        appointment.setId(id);

        // Element has no attributes, which is why element.hasAttribute("..."); never works
        if (getTagValues("location", element) != null) {
          loc = getTagValues("location", element);
          appointment.setLocation(loc);
        }

        if (getTagValues("roomnr", element) != null) {
          roomnr = getTagValues("roomnr", element);
          appointment.setRoomNumber((Integer.parseInt(roomnr)));
        }

        String description = getTagValues("description", element);
        appointment.setDescription(description);
        String lusername = getTagValues("lusername", element);
        String lname = getTagValues("lname", element);
        Employee l = new Employee(lname, lusername);
        appointment.setLeader(l);
        participants.add(new Participant(l, State.ACCEPTED));

        //
        NodeList pList = doc.getElementsByTagName("participant" + i);
        System.out.println("pList lengde: " + pList.getLength());
        for (int j = 0; j < pList.getLength(); j++) {
          Node pNode = pList.item(j);
          if (pNode.getNodeType() == Node.ELEMENT_NODE) {
            Element pElement = (Element) pNode;
            String username = getTagValues("username", pElement);
            String name = getTagValues("name", pElement);
            String state = getTagValues("state", pElement);
            if (username.equals(lusername)) continue;
            participants.add(new Participant(new Employee(name, username), stringToState(state)));
          }
        }

        // Set leader as accepted
        for (Participant part : participants) {
          if (part.getEmployee().getUsername().equals(lusername)) part.setState(State.ACCEPTED);
        }
        appointment.setParticipants(participants);
        appointmentList.add(appointment);
      }
    }
    return appointmentList;
  }
Пример #3
0
  public static String appointmentListToXML(ArrayList<Appointment> appointmentList) {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder;
    try {
      docBuilder = docFactory.newDocumentBuilder();
      Document doc = docBuilder.newDocument();
      Element rootElement = doc.createElement("appointments");
      doc.appendChild(rootElement);

      int j = 0;
      for (Appointment a : appointmentList) {
        Element appointment = doc.createElement("appointment");
        rootElement.appendChild(appointment);

        Element date = doc.createElement("date");
        appointment.appendChild(date);

        Element month = doc.createElement("month");
        date.appendChild(month);
        month.appendChild(doc.createTextNode(a.getDate().getMonth() + ""));

        Element year = doc.createElement("year");
        date.appendChild(year);
        year.appendChild(doc.createTextNode(a.getDate().getYear() + ""));

        Element dayInMonth = doc.createElement("dayInMonth");
        date.appendChild(dayInMonth);
        dayInMonth.appendChild(doc.createTextNode(a.getDate().getDate() + ""));

        Element startTime = doc.createElement("starttime");
        appointment.appendChild(startTime);
        startTime.appendChild(doc.createTextNode(a.getStart().toString()));

        Element endTime = doc.createElement("endtime");
        appointment.appendChild(endTime);
        endTime.appendChild(doc.createTextNode(a.getEnd().toString()));

        Element subject = doc.createElement("subject");
        appointment.appendChild(subject);
        subject.appendChild(doc.createTextNode(a.getSubject()));

        Element id = doc.createElement("id");
        appointment.appendChild(id);
        id.appendChild(doc.createTextNode(a.getId() + ""));
        if (a.getLocation() != null) {
          Element location = doc.createElement("location");
          appointment.appendChild(location);
          location.appendChild(doc.createTextNode(a.getLocation()));
        }
        if (a.getRoomNumber() != 0) {
          Element roomnr = doc.createElement("roomnr");
          appointment.appendChild(roomnr);
          roomnr.appendChild(doc.createTextNode(a.getRoomNumber() + ""));
        }

        for (int i = 0; i < a.getParticipants().size(); i++) {
          Participant p = a.getParticipants().get(i);
          Element participant = doc.createElement("participant" + j);
          appointment.appendChild(participant);
          Element username = doc.createElement("username");
          participant.appendChild(username);
          username.appendChild(doc.createTextNode(p.getEmployee().getUsername()));
          Element name = doc.createElement("name");
          participant.appendChild(name);
          name.appendChild(doc.createTextNode(p.getEmployee().getName()));
          Element state = doc.createElement("state");
          participant.appendChild(state);
          state.appendChild(doc.createTextNode(a.stateToString(p.getState())));
        }

        Element leader = doc.createElement("leader");
        appointment.appendChild(leader);
        Element username = doc.createElement("lusername");
        leader.appendChild(username);
        username.appendChild(doc.createTextNode(a.getLeader().getUsername()));
        Element name = doc.createElement("lname");
        leader.appendChild(name);
        name.appendChild(doc.createTextNode(a.getLeader().getName()));

        Element description = doc.createElement("description");
        appointment.appendChild(description);
        description.appendChild(doc.createTextNode(a.getDescription()));
        j++;
      }

      DOMSource source = new DOMSource(doc);
      TransformerFactory transformerFactory = TransformerFactory.newInstance();
      Transformer optimusPrime = transformerFactory.newTransformer();
      StringWriter stringWriter = new StringWriter();
      Result result = new StreamResult(stringWriter);
      optimusPrime.transform(source, result);

      return stringWriter.getBuffer().toString();
    } catch (ParserConfigurationException e) {
      e.printStackTrace();
    } catch (TransformerConfigurationException e) {
      e.printStackTrace();
    } catch (TransformerException e) {
      e.printStackTrace();
    }
    return null;
  }
Пример #4
0
  // Lager en xml fil med data fra appointment
  public String toXML() throws ParserConfigurationException, TransformerException {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

    Document doc = docBuilder.newDocument();
    Element rootElement = doc.createElement("appointment");
    doc.appendChild(rootElement);

    Element date = doc.createElement("date");
    rootElement.appendChild(date);

    Element month = doc.createElement("month");
    date.appendChild(month);
    month.appendChild(doc.createTextNode(getDate().getMonth() + ""));

    Element year = doc.createElement("year");
    date.appendChild(year);
    year.appendChild(doc.createTextNode(getDate().getYear() + ""));

    Element dayInMonth = doc.createElement("dayInMonth");
    date.appendChild(dayInMonth);
    dayInMonth.appendChild(doc.createTextNode(getDate().getDate() + ""));

    Element startTime = doc.createElement("starttime");
    rootElement.appendChild(startTime);
    startTime.appendChild(doc.createTextNode(getStart().toString()));

    Element endTime = doc.createElement("endtime");
    rootElement.appendChild(endTime);
    endTime.appendChild(doc.createTextNode(getEnd().toString()));

    Element subject = doc.createElement("subject");
    rootElement.appendChild(subject);
    subject.appendChild(doc.createTextNode(getSubject()));

    Element id = doc.createElement("id");
    rootElement.appendChild(id);
    id.appendChild(doc.createTextNode(getId() + ""));

    if (getLocation() != null) {
      Element location = doc.createElement("location");
      rootElement.appendChild(location);
      location.appendChild(doc.createTextNode(getLocation()));
    }

    if (getRoomNumber() != 0) {
      Element roomnr = doc.createElement("roomnr");
      rootElement.appendChild(roomnr);
      roomnr.appendChild(doc.createTextNode(getRoomNumber() + ""));
    }

    Element participants = doc.createElement("participants");
    rootElement.appendChild(participants);
    for (Participant p : this.participants) {
      Element participant = doc.createElement("participant");
      participants.appendChild(participant);
      Element username = doc.createElement("username");
      participant.appendChild(username);
      username.appendChild(doc.createTextNode(p.getEmployee().getUsername()));
      Element name = doc.createElement("name");
      participant.appendChild(name);
      name.appendChild(doc.createTextNode(p.getEmployee().getName()));
      Element state = doc.createElement("state");
      participant.appendChild(state);
      state.appendChild(doc.createTextNode(stateToString(p.getState())));
    }

    Element leader = doc.createElement("leader");
    rootElement.appendChild(leader);
    Element username = doc.createElement("lusername");
    leader.appendChild(username);
    username.appendChild(doc.createTextNode(getLeader().getUsername()));
    Element name = doc.createElement("lname");
    leader.appendChild(name);
    name.appendChild(doc.createTextNode(getLeader().getName()));

    Element description = doc.createElement("description");
    rootElement.appendChild(description);
    description.appendChild(doc.createTextNode(getDescription()));

    TransformerFactory transformerFactory2 = TransformerFactory.newInstance();
    Transformer transformer2 = transformerFactory2.newTransformer();
    // write the content into xml file for testing
    DOMSource source = new DOMSource(doc);
    //		StreamResult toFile = new StreamResult(new File("file.xml"));
    //		transformer2.transform(source, toFile);

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    StringWriter stringWriter = new StringWriter();
    Result result = new StreamResult(stringWriter);
    transformer.transform(source, result);
    return stringWriter.getBuffer().toString();
  }