LOGGER.debug("(create) Looping through " + course.getInstructors().size()
+ " instructors");
for (Instructor instructor : course.getInstructors())
{
Instructor dbInstructor = null;
if (instructor.getId() != null)
{
dbInstructor = entityManager.find(Instructor.class,
instructor.getId());
} else
{
CriteriaQuery<Instructor> cq = cb.createQuery(Instructor.class);
Root<Instructor> instructorRoot = cq.from(Instructor.class);
cq.where(cb.and(cb.equal(instructorRoot.get("firstName"),
instructor.getFirstName()),
cb.equal(instructorRoot.get("lastName"),
instructor.getLastName()),
cb.equal(instructorRoot.get("domainAccount"),
instructor.getDomainAccount())));
TypedQuery<Instructor> instructorQuery =
entityManager.createQuery(cq);
try
{
dbInstructor = instructorQuery.getSingleResult();
} catch (NoResultException ex)
{
dbInstructor = null;
}
}
if (dbInstructor != null)
{
LOGGER.debug("(create) Instructor " + dbInstructor.getLastName()
+ ", " + dbInstructor.getFirstName()
+ " exists in DB. Adding to course");
LOGGER.debug("(create) Instructor ID: " + dbInstructor.getId());
} else
{
LOGGER.debug("(create) Instructor " + instructor.getLastName()
+ ", " + instructor.getFirstName()
+ " doesn't exist in DB. Adding to course");
dbInstructor = new Instructor(instructor.getFirstName(),
instructor.getLastName(), instructor.getWebID());
dbInstructor.setDomainAccount(instructor.getDomainAccount());
if (instructor.getId() != null)
{
dbInstructor.setId(instructor.getId());
}
try
{
tx.begin();
this.entityManager.persist(dbInstructor);
tx.commit();
} catch (Exception ex)
{
LOGGER.error("(create) Exception Caught: "
+ ex.getLocalizedMessage());
throw new IllegalStateException(ex.getLocalizedMessage());
}
LOGGER.debug("(create) Instructor ID: " + dbInstructor.getId());
}
dbCourse.addInstructor(dbInstructor);
tx.begin();
dbCourse = entityManager.merge(dbCourse);
tx.commit();
LOGGER.debug("(create) Added Instructor to Course: "
+ dbCourse.getId());
} // Loop through Course Instructors
LOGGER.debug("(create) Looping through " + course.getSections().size()
+ " sections");
for (Section section : course.getSections())
{
LOGGER.debug("(create) Section: " + section);
Instructor dbInstructor = null;
CriteriaQuery<Instructor> cq = cb.createQuery(Instructor.class);
Root<Instructor> instructorRoot = cq.from(Instructor.class);
cq.where(cb.and(cb.equal(instructorRoot.get("firstName"),
section.getInstructor().getFirstName()),
cb.equal(instructorRoot.get("lastName"),
section.getInstructor().getLastName()),
cb.equal(instructorRoot.get("domainAccount"),
section.getInstructor().getDomainAccount())));
TypedQuery<Instructor> instructorQuery =
entityManager.createQuery(cq);
try
{
dbInstructor = instructorQuery.getSingleResult();
LOGGER.debug("(create) Instructor for Section " + section
+ " found: " + dbInstructor.getLastName() + ", "
+ dbInstructor.getFirstName());
LOGGER.debug("(create) Instructor ID: " + dbInstructor.getId());
} catch (NoResultException ex)
{
dbInstructor = null;
LOGGER.error("(create) Section " + section
+ " Instructor not found in DB");