-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDBUpdate
96 lines (83 loc) · 2.82 KB
/
DBUpdate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package System;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class DBUpdate {
private File xmlFile;
private SAXBuilder builder;
public DBUpdate() {
xmlFile = null;
builder = null;
try {
builder = new SAXBuilder();
xmlFile = new File("roomDBxml.xml");
} catch (Exception e) {
e.printStackTrace();
System.out.println("Unable to find the xml file.");
}
}
public boolean update(int buildingID, String roomID, Reservation reservation) {
boolean success = false;
Document doc;
try {
doc = (Document) builder.build(xmlFile);
Element rootNode = doc.getRootElement();
System.out.println(rootNode);
Element buildings = rootNode.getChild("Buildings");
System.out.println(buildings);
List building = buildings.getChildren();
System.out.println(building);
List rooms = null;
for (int i = 0; i < building.size(); i++) {
if (buildingID == Integer.parseInt(((Element) building.get(i)).getChild("ID").getText())) {
rooms = (List) ((Element) building.get(i)).getChild("Rooms").getChildren();
}
}
if (rooms == null) {
System.out.println("could not find the rooms");
}
System.out.println(rooms);
Element room = null;
for (int i = 0; i < rooms.size(); i++) {
if (((Element) rooms.get(i)).getChild("ID").getText().equals(roomID)) {
room = ((Element) rooms.get(i));
}
}
if (room == null) {
System.out.println("room does not exist!");
}
System.out.println(room);
List reservations = room.getChild("Reservations").getChildren();
System.out.println(reservations);
Element reservationXML = null;
for (int i = 0; i < reservations.size(); i++) {
if (reservation.getReservationID() == Integer
.parseInt(((Element) reservations.get(i)).getChild("reservationID").getText())) {
reservationXML = (Element) reservations.get(i);
}
}
if (reservationXML == null) {
Element newReservationXML = new Element("Reservation");
newReservationXML.addContent(new Element("reservationID").setText(reservation.getReservationID() + ""));
newReservationXML.addContent(new Element("timeInterval").setText(reservation.getTimeInterval() + ""));
newReservationXML
.addContent(new Element("username").setText(reservation.getStudent().getUsername() + ""));
room.getChild("Reservations").addContent(newReservationXML);
success = true;
}
XMLOutputter xmlOutput = new XMLOutputter();
xmlOutput.setFormat(Format.getPrettyFormat());
xmlOutput.output(doc, new FileWriter("roomDBxml.xml"));
} catch (JDOMException | IOException e) {
e.printStackTrace();
}
return success;
}
}