public void loadXml(Element root) throws Exception { if (!root.getName().equalsIgnoreCase("roomSharing")) { throw new Exception("Given XML file is not a Room Sharing load file."); } try { beginTransaction(); String campus = root.attributeValue("campus"); String year = root.attributeValue("year"); String term = root.attributeValue("term"); iTimeFormat = root.attributeValue("timeFormat"); if (iTimeFormat == null) iTimeFormat = "HHmm"; Session session = Session.getSessionUsingInitiativeYearTerm(campus, year, term); if (session == null) { throw new Exception("No session found for the given campus, year, and term."); } if (!"true".equals(root.attributeValue("force")) && Solution.hasTimetable(session.getUniqueId())) { info( "Note: set the attribute force='true' of the root element to override the following import eligibility check."); throw new Exception( "Room sharing import is disabled: " + session.getLabel() + " already has a committed timetable."); } info("Loading rooms..."); Set<String> avoidRoomId = new HashSet<String>(); Set<String> avoidRoomName = new HashSet<String>(); Map<String, Location> id2location = new HashMap<String, Location>(); Map<String, Location> name2location = new HashMap<String, Location>(); for (Location location : (List<Location>) getHibSession() .createQuery("from Location where session.uniqueId = :sessionId") .setLong("sessionId", session.getUniqueId()) .list()) { if (location.getExternalUniqueId() != null && !avoidRoomId.contains(avoidRoomId)) { Location old = id2location.put(location.getExternalUniqueId(), location); if (old != null) { warn( "There are two or more rooms with the same external id " + location.getExternalUniqueId() + ": " + location.getLabel() + " and " + old.getLabel() + "."); avoidRoomId.add(location.getExternalUniqueId()); } } if (!avoidRoomName.contains(location.getLabel())) { Location old = name2location.put(location.getLabel(), location); if (old != null) { warn("There are two or more rooms with the same name " + location.getLabel() + "."); avoidRoomName.add(location.getLabel()); } } } info("Loading departments..."); Map<String, Department> id2department = new HashMap<String, Department>(); Map<String, Department> code2department = new HashMap<String, Department>(); for (Department dept : (List<Department>) getHibSession() .createQuery("from Department where session.uniqueId = :sessionId") .setLong("sessionId", session.getUniqueId()) .list()) { if (dept.getExternalUniqueId() != null) { Department old = id2department.put(dept.getExternalUniqueId(), dept); if (old != null) { warn( "There are two departments with the same external id " + dept.getExternalUniqueId() + ": " + dept.getLabel() + " and " + old.getLabel() + "."); } } Department old = code2department.put(dept.getDeptCode(), dept); if (old != null) { warn( "There are two rooms with the same code " + dept.getDeptCode() + ": " + dept.getName() + " and " + old.getName() + "."); } } info("Importing room sharing..."); int nrChanged = 0; for (Iterator i = root.elementIterator("location"); i.hasNext(); ) { Element locEl = (Element) i.next(); Location location = null; String locId = locEl.attributeValue("id"); if (locId != null && !avoidRoomId.contains(locId)) { location = id2location.get(locId); if (location == null) warn("Location of id " + locId + " does not exist."); } if (location == null) { String building = locEl.attributeValue("building"); String roomNbr = locEl.attributeValue("roomNbr"); if (building != null && roomNbr != null && !avoidRoomName.contains(building + " " + roomNbr)) { location = name2location.get(building + " " + roomNbr); if (location == null) warn( "Location of building " + building + " and room number " + roomNbr + " does not exist."); } } if (location == null) { String name = locEl.attributeValue("name"); if (name != null && !avoidRoomName.contains(name)) { location = name2location.get(name); if (location == null) warn("Location of name " + name + " does not exist."); } } if (location == null) continue; Set<RoomDept> existing = new HashSet<RoomDept>(location.getRoomDepts()); Set<Department> departments = new HashSet<Department>(); boolean changed = false; String note = locEl.attributeValue("note"); if (note == null && location.getShareNote() != null) { location.setShareNote(null); info(location.getLabel() + ": share note removed."); changed = true; } else if (note != null && !note.equals(location.getShareNote())) { location.setShareNote(note); info(location.getLabel() + ": share note changed to '" + note + "'."); changed = true; } department: for (Iterator j = locEl.elementIterator("department"); j.hasNext(); ) { Element deptEl = (Element) j.next(); Department dept = null; String deptId = deptEl.attributeValue("id"); if (deptId != null) { dept = id2department.get(deptId); if (dept == null) warn(location.getLabel() + ": Department of id " + deptId + " does not exist."); } String deptCode = deptEl.attributeValue("code"); if (deptCode != null) { dept = code2department.get(deptCode); if (dept == null) warn(location.getLabel() + ": Department of code " + deptCode + " does not exist."); } if (dept == null) continue; Boolean control = "true".equals(deptEl.attributeValue("control")); for (Iterator<RoomDept> k = existing.iterator(); k.hasNext(); ) { RoomDept rd = k.next(); if (rd.getDepartment().equals(dept)) { if (!control.equals(rd.getControl())) { rd.setControl(control); getHibSession().update(rd); info( location.getLabel() + ": " + (control ? " control moved to " + dept.getLabel() : " control removed from " + dept.getLabel())); changed = true; } k.remove(); departments.add(dept); continue department; } } RoomDept rd = new RoomDept(); rd.setControl(control); rd.setDepartment(dept); rd.setRoom(location); location.getRoomDepts().add(rd); dept.getRoomDepts().add(rd); getHibSession().save(rd); departments.add(dept); info( location.getLabel() + ": added " + (control ? "controlling " : "") + " department" + dept.getLabel()); changed = true; } for (RoomDept rd : existing) { info( location.getLabel() + ": removed " + (rd.isControl() ? "controlling " : "") + " department" + rd.getDepartment().getLabel()); location.getRoomDepts().remove(rd); rd.getDepartment().getRoomDepts().remove(rd); getHibSession().delete(rd); changed = true; } RoomSharingModel model = location.getRoomSharingModel(); String oldModel = model.toString(); model.setPreferences(null); Element sharingEl = locEl.element("sharing"); if (sharingEl != null) { for (Iterator j = sharingEl.elementIterator(); j.hasNext(); ) { Element el = (Element) j.next(); TimeObject time = new TimeObject( el.attributeValue("start"), el.attributeValue("end"), el.attributeValue("days")); String pref = null; if ("unavailable".equals(el.getName())) { pref = RoomSharingModel.sNotAvailablePref.toString(); } else if ("assigned".equals(el.getName())) { Department dept = null; String deptId = el.attributeValue("id"); if (deptId != null) { dept = id2department.get(deptId); if (dept == null) warn(location.getLabel() + ": Department of id " + deptId + " does not exist."); } String deptCode = el.attributeValue("code"); if (deptCode != null) { dept = code2department.get(deptCode); if (dept == null) warn( location.getLabel() + ": Department of code " + deptCode + " does not exist."); } if (dept == null) continue; if (!departments.contains(dept)) { warn( location.getLabel() + ": Department " + dept.getLabel() + " is not among the room sharing departments."); continue; } pref = dept.getUniqueId().toString(); } if (pref == null) continue; if (time.hasDays()) { for (int d : time.getDays()) for (int t = time.getStartPeriod(); t < time.getEndPeriod(); t++) model.setPreference(d, t, pref); } else { for (int d = 0; d < model.getNrDays(); d++) for (int t = time.getStartPeriod(); t < time.getEndPeriod(); t++) model.setPreference(d, t, pref); } } } String newModel = model.toString(); if (!oldModel.equals(newModel)) { info( location.getLabel() + ": room sharing changed to " + (newModel.isEmpty() ? "free for all" : newModel)); changed = true; } location.setRoomSharingModel(model); getHibSession().update(location); if (changed) nrChanged++; } if (nrChanged == 0) { info("No change detected."); } else { info(nrChanged + " locations have changed."); } info("All done."); commitTransaction(); } catch (Exception e) { fatal("Exception: " + e.getMessage(), e); rollbackTransaction(); throw e; } }