public void loadXml(Element root) throws Exception { String trimLeadingZeros = ApplicationProperties.getProperty("tmtbl.data.exchange.trim.externalId", "false"); if (trimLeadingZeros.equals("true")) { trimLeadingZerosFromExternalId = true; } try { String rootElementName = "lastLikeCourseDemand"; if (!root.getName().equalsIgnoreCase(rootElementName)) { throw new Exception("Given XML file is not a Course Offerings load file."); } String campus = root.attributeValue("campus"); String year = root.attributeValue("year"); String term = root.attributeValue("term"); String created = getOptionalStringAttribute(root, "created"); Session session = Session.getSessionUsingInitiativeYearTerm(campus, year, term); if (session == null) { throw new Exception("No session found for the given campus, year, and term."); } loadSubjectAreas(session.getSessionId()); loadCourseOfferings(session.getSessionId()); beginTransaction(); if (created != null) { ChangeLog.addChange( getHibSession(), getManager(), session, session, created, ChangeLog.Source.DATA_IMPORT_LASTLIKE_DEMAND, ChangeLog.Operation.UPDATE, null, null); } getHibSession() .createQuery( "delete LastLikeCourseDemand ll where ll.subjectArea.uniqueId in " + "(select s.uniqueId from SubjectArea s where s.session.uniqueId=:sessionId)") .setLong("sessionId", session.getUniqueId()) .executeUpdate(); flush(true); for (Iterator it = root.elementIterator(); it.hasNext(); ) { Element element = (Element) it.next(); String externalId = element.attributeValue("externalId"); if (trimLeadingZerosFromExternalId) { try { Integer num = new Integer(externalId); externalId = num.toString(); } catch (Exception e) { // do nothing } } Student student = fetchStudent(externalId, session.getSessionId()); if (student == null) { student = new Student(); student.setFirstName("Unknown"); student.setLastName("Student"); student.setExternalUniqueId(externalId); student.setFreeTimeCategory(new Integer(0)); student.setSchedulePreference(new Integer(0)); student.setSession(session); getHibSession().save(student); getHibSession().flush(); getHibSession().refresh(student); } loadCourses(element, student, session); flushIfNeeded(true); } flush(true); getHibSession() .createQuery( "update CourseOffering c set c.demand=" + "(select count(distinct d.student) from LastLikeCourseDemand d where " + "(c.subjectArea=d.subjectArea and c.courseNbr=d.courseNbr)) where " + "c.permId is null and c.subjectArea.uniqueId in (select sa.uniqueId from SubjectArea sa where sa.session.uniqueId=:sessionId)") .setLong("sessionId", session.getUniqueId()) .executeUpdate(); getHibSession() .createQuery( "update CourseOffering c set c.demand=" + "(select count(distinct d.student) from LastLikeCourseDemand d where " + "d.student.session=c.subjectArea.session and c.permId=d.coursePermId) where " + "c.permId is not null and c.subjectArea.uniqueId in (select sa.uniqueId from SubjectArea sa where sa.session.uniqueId=:sessionId)") .setLong("sessionId", session.getUniqueId()) .executeUpdate(); commitTransaction(); } catch (Exception e) { fatal("Exception: " + e.getMessage(), e); rollbackTransaction(); throw e; } }