public void editAppointment(Appointment a) { for (Appointment p : appointments) { if (a.getId() == p.getId()) { appointments.remove(p); break; } } appointments.add(a); fireTableDataChanged(); }
@Override public Object getValueAt(int rowIndex, int columnIndex) { Time now = new Time(rowIndex, 0); if (columnIndex == 0) { return now; } else { calendar.set(Calendar.DAY_OF_WEEK, columnIndex); Appointment returnValue = null; for (Appointment a : appointments) { if (a.getDate().getDate() == calendar.get(Calendar.DAY_OF_MONTH) && a.getDate().getYear() + 1900 == calendar.get(Calendar.YEAR) && a.getDate().getMonth() == calendar.get(Calendar.MONTH)) { if (a.getStart().compareTo(now) <= 0 && a.getEnd().compareTo(now) > 0) { returnValue = a; } } } calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); return returnValue; } }
public Appointment getCopy() { Appointment a = new Appointment(this.leader); a.start = this.start; a.end = this.end; if (this.date != null) { a.date = new Date(this.date.getTime()); } a.subject = this.subject; a.id = this.id; a.description = this.description; a.roomNumber = this.roomNumber; a.location = this.location; List<Participant> list = new ArrayList<Participant>(this.getParticipants().size()); for (int i = 0; i < this.getParticipants().size(); i++) { list.add( new Participant( this.getParticipants().get(i).getEmployee(), this.getParticipants().get(i).getState())); } a.participants = list; return a; }
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; }
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; }