{
// Search DB and set current course to one matched in DB
// Work from there instead
query = em.createQuery("select c from Course c where c.name = :cname");
query.setParameter("cname", course.getName());
Course c;
try
{
c = (Course)query.getSingleResult();
}
catch (NoResultException ex)
{
// No course in database, proceed normally since it's a new course
c = course;
}
// Cascade search for the course details: Instructors, Sections,
// and Students
//
// Since GradedEvent is listed before instructor, we need to
// pull one of the graded events and put it into the path so
// we can accurately pull the list of Instructors in the course.
// Since the Assignments will be loaded separately, we're not
// interested in them here.
String rootDir = txtRootDir.getText() + File.separator + c.getName();
String assignDir = RageLib.getSingleAssignmentDirFromPath(rootDir);
String courseDir = rootDir + File.separator + assignDir;
if (assignDir != null) // There are assignments in this directory
{
// TODO: RAGE-92
Set<Instructor> instructors = RageLib.getInstructorsFromPath(courseDir);
if (instructors != null)
{
for (Instructor ins : instructors)
{
String instructorDir = courseDir + File.separator + ins.getWebID();
// TODO: RAGE-91 fix
List<Section> sections = RageLib.getSectionsFromPath(instructorDir);
if (sections != null)
{
for (Section s : sections)
{
String sectionDir = instructorDir + File.separator + s.getName();
// TODO: RAGE-93
Set<Student> students = RageLib.getStudentsFromPath(sectionDir, node.getInt("ClassOffset", 2000));
if (students != null)
{
for (Student stu : students)
{
SectionDAO sectionDAO = new SectionDAO(emf.createEntityManager());
// TODO: Need to add query for name...but Sections are not equal by name...RAGE-94
query = em.createQuery("select s FROM Section s WHERE s.name = :name");
query.setParameter("name", s.getName());
Section res;
try
{
res = (Section)query.getSingleResult();
}
catch (NoResultException ex)
{
// Section not found in DB
res = null;
}
if (res == null) // The section is not in the database
{
tx.begin();
em.persist(s);
tx.commit();
//stu.setSection(s);
}
else
{
tx.begin();
em.persist(res);
tx.commit();
//stu.setSection(res);
}
// TODO: RAGE-34 - Migrate to StudentDAO
query = em.createQuery("select p FROM Person p WHERE p.webID = :webid");
query.setParameter("webid", stu.getWebID());
Student stuRes;
try
{
stuRes = (Student)query.getSingleResult();
}
catch (NoResultException ex)
{
// Student not found in DB
stuRes = null;
}
if (stuRes == null) // The student is not in the database
{
tx.begin();
em.persist(stu);
tx.commit();
}
sectionDAO.closeConnection();
}
tx.begin();
// TODO: RAGE-71 - Migrate to InstructorDAO
query = em.createQuery("select p FROM Person p WHERE p.webID = :webid");
query.setParameter("webid", ins.getWebID());
Instructor insRes;
try
{
insRes = (Instructor)query.getSingleResult();
}
catch (NoResultException ex)
{
// Instructor not found in DB
insRes = null;
}
if (insRes == null) // The instructor is not in the database
{
em.persist(ins);
}
else
{
ins.setId(insRes.getId());
}
query = em.createQuery("select c FROM Course c WHERE name = :name");
query.setParameter("name", c.getName());
Course crsRes;
try
{
crsRes = (Course)query.getSingleResult();
}
catch (NoResultException ex)
{
// Instructor not found in DB
crsRes = null;
}
if (crsRes == null) // The instructor is not in the database
{
em.persist(c);
}
else
{
c.setId(crsRes.getId());
}
//s.setStudents(students);
s.setInstructor(ins);
s.setCourse(c);