throw new IllegalStateException("Instructors not added to DB "
+ "properly");
}
LOGGER.debug("(create) Determining if Section is already in DB");
Section dbSection = null;
CriteriaQuery<Section> cqSection = cb.createQuery(Section.class);
Root<Section> sectionRoot = cqSection.from(Section.class);
cqSection.where(cb.and(cb.equal(sectionRoot.get("course"), dbCourse)),
cb.equal(sectionRoot.get("name"), section.getName()));
TypedQuery<Section> sectionQuery =
entityManager.createQuery(cqSection);
try
{
dbSection = sectionQuery.getSingleResult();
LOGGER.debug("(create) Section found. ID: " + dbSection.getId());
} catch (NoResultException ex)
{
LOGGER.debug("(create) Section " + section.getName()
+ " doesn't exist in the database. Adding.");
dbSection = new Section(dbCourse, section.getName(), dbInstructor);
if (section.getId() != null)
{
dbSection.setId(section.getId());
}
tx.begin();
entityManager.persist(dbSection);
dbCourse = entityManager.merge(dbCourse);
dbInstructor = entityManager.merge(dbInstructor);
tx.commit();
LOGGER.debug("(create) Section added. ID: " + dbSection.getId());
}
/**
* All sections for this course were removed previously, so we don't
* need to query the database for potential merged sections. We
* do need to look for potential existing students and update the
* students accordingly.
*/
LOGGER.debug("(create) Looping through " + section.getStudents().size()
+ " students");
for (Student student : section.getStudents())
{
LOGGER.debug("(create) Querying for Student: " + student.getWebID()
+ ", " + student.getFirstName() + ", "
+ student.getLastName() + " (" + student.getClassYear()
+ ")");
Student dbStudent = null;
CriteriaQuery<Student> cqStudent = cb.createQuery(Student.class);
Root<Student> studentRoot = cqStudent.from(Student.class);
cqStudent.where(cb.and(
cb.equal(studentRoot.get("webID"), student.getWebID()),
cb.equal(studentRoot.get("firstName"),
student.getFirstName()),
cb.equal(studentRoot.get("lastName"),
student.getLastName()),
cb.equal(studentRoot.get("classYear"),
student.getClassYear())));
TypedQuery<Student> studentQuery =
entityManager.createQuery(cqStudent);
try
{
dbStudent = studentQuery.getSingleResult();
LOGGER.debug("(create) Student " + dbStudent.getLastName()
+ ", " + dbStudent.getFirstName() + " ("
+ dbStudent.getClassYear() + ") does exist in DB. "
+ "Adding to Section " + dbSection);
LOGGER.debug("(create) Student ID: " + dbStudent.getId());
} // Student in Database
catch (NoResultException ex)
{
LOGGER.debug("(create) Student " + student.getLastName()
+ ", " + student.getFirstName() + " ("
+ student.getClassYear() + ") doesn't exist in DB. "
+ "Adding to Section " + dbSection);
dbStudent = new Student(student.getFirstName(),
student.getLastName(), student.getWebID(),
student.getClassYear());
if (student.getId() != null)
{
dbStudent.setId(student.getId());
}
tx.begin();
entityManager.persist(dbStudent);
tx.commit();
LOGGER.debug("(create) Student ID: " + dbStudent.getId());
} // Student not in Database
dbSection.addStudent(dbStudent);
tx.begin();
dbSection = entityManager.merge(dbSection);
tx.commit();
} // Loop through Students
dbCourse.addSection(dbSection);
tx.begin();
dbCourse = entityManager.merge(dbCourse);
//entityManager.persist(dbSection);
//dbSection = entityManager.merge(dbSection);
tx.commit();
LOGGER.debug("(create) Course ID (Added Section " + dbSection
+ "): " + dbCourse.getId() + "; Section Course ID: "
+ dbSection.getCourse().getId());
} // Loop through Course Sections
LOGGER.debug("- Instructors: " + dbCourse.getInstructors().size());
LOGGER.debug("- Sections: " + dbCourse.getSections().size());