// Minimum requirement: Course and Assignment MUST be selected to continue
if ((cboCourse.getSelectedIndex() >= 0) && (cboAssignment.getSelectedIndex() >= 0))
{
GradedEventDAO gradedEventDAO = new GradedEventDAO(emf.createEntityManager());
CourseDAO courseDAO = new CourseDAO(emf.createEntityManager());
String[] name;
Course selectedCourse = (Course) cboCourse.getSelectedItem();
Course course = courseDAO.find(selectedCourse.getId());
GradedEvent selectedAssignment = (GradedEvent) cboAssignment.getSelectedItem();
LOGGER.debug("Querying database for the selected GradedEvent");
int numVersions = gradedEventDAO.getNumVersionsOfGradedEvent(selectedAssignment);
GradedEvent gevent = null;
List<GradedEvent> gevents = null;
// If more than 1 result comes up for the GradedEvent query (not
// including the version). We can't set gevent until we know which
// section is to be graded
if (numVersions == 1)
{
gevent = gradedEventDAO.find(selectedAssignment.getId());
} else
{ // Set it to the first GradedEvent so we can get the name
gevents = gradedEventDAO.getAllVersionsOfGradedEvent(selectedAssignment);
gevent = gevents.get(0);
multVersions = true;
}
String path = this.buildBasePath();
Instructor selectedInstructor = null;
Section selectedSection = null;
Student selectedStudent = null;
if (cboInstructor.getSelectedIndex() != -1)
{
selectedInstructor = (Instructor) cboInstructor.getSelectedItem();
}
if (cboSection.getSelectedIndex() != -1)
{
selectedSection = (Section) cboSection.getSelectedItem();
}
if (cboStudent.getSelectedIndex() != -1)
{
selectedStudent = (Student) cboStudent.getSelectedItem();
}
// // Derive the grading path from the selected comboboxes
// String path = node.get("RootDir", "");
// path = path.concat(File.separator);
// path = path.concat(selectedCourse.getName());
// path = path.concat(File.separator);
// path = path.concat(selectedAssignment.getAssignment());
/**
* Get the selected Instructor and Section objects. If either is
* not selected, getSelectedIndex will return -1 and the object will
* stay null.
*/
// Instructor selectedInstructor = null;
// Section selectedSection = null;
// if (cboInstructor.getSelectedIndex() != -1)
// {
// selectedInstructor = (Instructor) cboInstructor.getSelectedItem();
// }
// if (cboSection.getSelectedIndex() != -1)
// {
// selectedSection = (Section) cboSection.getSelectedItem();
// }
//
// // Add the Instructor if one is selected
// if (cboInstructor.getSelectedIndex() > 0)
// {
// path = path.concat(File.separator);
//
// //InstructorDAO instructorDAO = new InstructorDAO(emf.createEntityManager());
// //instructor = instructorDAO.find(selectedInstructor.getId());
// path = path.concat(selectedInstructor.getWebID());
//
// // Add the section if one is selected
// if (cboSection.getSelectedIndex() > 0)
// {
// //SectionDAO sectionDAO = new SectionDAO(emf.createEntityManager());
// //section = sectionDAO.find(selectedSection.getId());
//
// path = path.concat(File.separator);
// path = path.concat(selectedSection.getName());
//
// // Add the Student if one is selected
// if (cboStudent.getSelectedIndex() > 0)
// {
// path = path.concat(File.separator);
// name = cboStudent.getSelectedItem().toString().split(",");
// query = em.createQuery("SELECT p FROM Person p WHERE "
// + "p.firstName = :fname AND p.lastName = :lname");
// query.setParameter("fname", name[1].trim());
// query.setParameter("lname", name[0].trim());
// student = (Student) query.getSingleResult();
// path = path.concat(student.getWebID());
// }
// }
// }
// Build the thread environment regardless of whether doing student or
ProcessBuilder launcher = new ProcessBuilder();
Map<String, String> environment = launcher.environment();
launcher.redirectErrorStream(true);
File outputFile = null;
SectionReport sectionReport = null;
Date dateStamp = new Date();
String dateStampString = String.format("%tY%tm%td%tH%tM", dateStamp);
/**
* Grade All Instructors. Section and Student are not selected
*/
if ((cboAssignment.getSelectedIndex() >= 0)
&& (cboInstructor.getSelectedIndex() == 0)
&& (cboSection.getSelectedIndex() < 0)
&& (cboStudent.getSelectedIndex() < 0))
{
LOGGER.debug("Grading All Instructors. Section and Student are "
+ "not selected");
for (Instructor insIter : course.getInstructors())
{
String tempPath = new String();
tempPath = path.concat(File.separator);
tempPath = tempPath.concat(insIter.getWebID());
for (Section sectionIter : insIter.getSections())
{
sectionReport = new SectionReport(course.getName(),
insIter, sectionIter.getName(),
selectedAssignment.getAssignment());
try
{
outputFile = new File(tempPath + File.separator
+ sectionIter.getName() + File.separator
+ sectionIter.getName() + dateStampString + ".html");
LOGGER.debug("Output File: " + outputFile.getCanonicalPath());
outputFile.createNewFile();
} catch (IOException ex)
{
LOGGER.error("ERROR: IOException caught.");
LOGGER.error(ex.getLocalizedMessage());
}
/**
* If a section is selected and there are multiple versions,
* pull the right one for the gevent variable...can we
* search the list and return the right one?
*/
if (multVersions)
{
/**
* If there are multiple versions of the GradedEvent,
* pull only the one for this section
*/
gevent = RageLib.getGradedEventFromSectionName(gevents,
sectionIter.getName());
}
// No else needed since if there are no multiple versions,
// gevent is already set above
for (Student studentIter : sectionIter.getStudents())
{
StudentReport studentReport = new StudentReport(studentIter,
sectionIter);
String newPath = new String();
newPath = tempPath.concat(File.separator);
newPath = newPath.concat(sectionIter.getName());
newPath = newPath.concat(File.separator);
newPath = newPath.concat(studentIter.getWebID());
LOGGER.debug("Path to grade: " + newPath);
statusMessageLabel.setText("Grading: "
+ studentIter.getLastName()
+ ", " + studentIter.getFirstName());
LOGGER.info("Grading: " + studentIter.getLastName()
+ ", " + studentIter.getFirstName());
try
{
File directory = new File(newPath);
LOGGER.debug("Grading path: " + directory.getCanonicalPath());
launcher.directory(directory);
Runnable r = new GraderThread(node, launcher, directory,
gevent, studentReport, type);
grader_thread = new Thread(r);
grader_thread.start();
while (grader_thread.isAlive())
{
}
// After grader is complete, add StudentReport to
// SectionReport
sectionReport.addStudentReport(studentReport);
} catch (IOException ex)
{
LOGGER.error("IO Exception: " + ex.getLocalizedMessage());
}
}
sectionReport.sortStudentReports();
RageLib.printSectionReport(outputFile, sectionReport);
}
}
} /**
* Grade a single instructor, but All Sections is selected
*/
else if ((cboInstructor.getSelectedIndex() > 0)
&& (cboSection.getSelectedIndex() == 0)
&& (cboStudent.getSelectedIndex() < 0))
{
LOGGER.debug("Grading all sections for Instructor "
+ selectedInstructor.getLastName() + ", "
+ selectedInstructor.getFirstName());
// Pull the Instructor from the Data Source to be able to grab Sections
InstructorDAO instructorDAO = new InstructorDAO(emf.createEntityManager());
Instructor instructor = instructorDAO.find(selectedInstructor.getId());
for (Section sectionIter : instructor.getSections())
{
sectionReport = new SectionReport(course.getName(),
instructor, sectionIter.getName(),
selectedAssignment.getAssignment());
try
{
outputFile = new File(path + File.separator
+ sectionIter.getName() + File.separator
+ sectionIter.getName() + dateStampString + ".html");
LOGGER.debug("Output File: " + outputFile.getCanonicalPath());
outputFile.createNewFile();
} catch (IOException ex)
{
LOGGER.error("IOException caught: "
+ ex.getLocalizedMessage());
}
/**
* If a section is selected and there are multiple versions,
* pull the right one for the gevent variable...can we
* search the list and return the right one?
*/
if (multVersions)
{
/**
* If there are multiple versions of the GradedEvent,
* pull only the one for this section
*/
gevent = RageLib.getGradedEventFromSectionName(gevents,
sectionIter.getName());
}
// No else needed since if there are no multiple versions,
// gevent is already set above
for (Student studentIter : sectionIter.getStudents())
{
// StudentReport studentReport = new StudentReport(studentIter);
StudentReport studentReport = new StudentReport(studentIter,
sectionIter);
String newPath = new String();
newPath = path.concat(File.separator);
newPath = newPath.concat(sectionIter.getName());
newPath = newPath.concat(File.separator);
newPath = newPath.concat(studentIter.getWebID());
LOGGER.debug("Path to grade: " + newPath);
statusMessageLabel.setText("Grading: " + studentIter.getLastName()
+ ", " + studentIter.getFirstName());
LOGGER.info("Grading: " + studentIter.getLastName()
+ ", " + studentIter.getFirstName());
File directory = new File(newPath);
launcher.directory(directory);
Runnable r = new GraderThread(node, launcher, directory,
gevent, studentReport, type);
grader_thread = new Thread(r);
grader_thread.start();
while (grader_thread.isAlive())
{
}
sectionReport.addStudentReport(studentReport);
}
sectionReport.sortStudentReports();
RageLib.printSectionReport(outputFile, sectionReport);
}
// Close the connection to the Data Source
instructorDAO.closeConnection();
} /**
* Grade all students in a single section
*/
else if ((cboStudent.getSelectedIndex() == 0)
&& (cboSection.getSelectedIndex() > 0))
{
LOGGER.debug("Grading all students in section "
+ selectedSection.getName());
// Pull the Section from the Data Source to get the full list of
// Students
SectionDAO sectionDAO = new SectionDAO(emf.createEntityManager());
Section section = sectionDAO.find(selectedSection.getId());
sectionReport = new SectionReport(course.getName(), selectedInstructor,
section.getName(), selectedAssignment.getAssignment());
try
{
outputFile = new File(path + File.separator
+ selectedSection.getName() + dateStampString + ".html");
LOGGER.debug("Output File: " + outputFile.getCanonicalPath());
outputFile.createNewFile();
/**
* If a section is selected and there are multiple versions,
* pull the right one for the gevent variable...can we
* search the list and return the right one?
*/
if (multVersions)
{
/**
* If there are multiple versions of the GradedEvent,
* pull only the one for this section
*/
gevent = RageLib.getGradedEventFromSectionName(gevents,
selectedSection.getName());
}
// No else needed since if there are no multiple versions,
// gevent is already set above
} catch (FileNotFoundException ex)
{
// Do nothing...the file should be created if it's not there
} catch (IOException ex)
{
LOGGER.error("ERROR: IOException caught.");
LOGGER.error(ex.getLocalizedMessage());
}
for (Student studentIter : section.getStudents())
{
StudentReport studentReport = new StudentReport(studentIter, section);
// StudentReport studentReport = new StudentReport(studentIter);
String newPath = new String();
newPath = path.concat(File.separator);
newPath = newPath.concat(studentIter.getWebID());
LOGGER.debug("Path to grade: " + newPath);
statusMessageLabel.setText("Grading: " + studentIter.getLastName()
+ ", " + studentIter.getFirstName());
LOGGER.info("Grading: " + studentIter.getLastName() + ", "
+ studentIter.getFirstName());
File directory = new File(newPath);
launcher.directory(directory);
Runnable r = new GraderThread(node, launcher, directory, gevent,
studentReport, type);
grader_thread = new Thread(r);
grader_thread.start();
while (grader_thread.isAlive())
{
}
sectionReport.addStudentReport(studentReport);
}
sectionReport.sortStudentReports();
RageLib.printSectionReport(outputFile, sectionReport);
// Close the connection to the Data Source
sectionDAO.closeConnection();
} /**
* Just grade a single student
*/
else
{
LOGGER.debug("Grading a single Student:" + selectedStudent.getWebID());
LOGGER.debug("Path to grade: " + path);
File directory = null;
try
{
directory = new File(path);
LOGGER.debug("Creating Output File");
outputFile = new File(directory.getParent()
+ File.separator + selectedStudent.getWebID()
+ dateStampString + ".html");
LOGGER.debug("Output File: " + outputFile.getCanonicalPath());
outputFile.createNewFile();
/**
* If a section is selected and there are multiple versions,
* pull the right one for the gevent variable...can we
* search the list and return the right one?
*/
if (multVersions)
{
/**
* If there are multiple versions of the GradedEvent,
* pull only the one for this section
*/
gevent = RageLib.getGradedEventFromSectionName(gevents,
selectedSection.getName());
}
// No else needed since if there are no multiple versions,
// gevent is already set above
} catch (FileNotFoundException ex)
{
// Do nothing...the file should be created if it's not there
} catch (IOException ex)
{
LOGGER.error("IOException caught.");
LOGGER.error(ex.getLocalizedMessage());
} catch (NullPointerException ex)
{
LOGGER.error("Null Pointer Exception: " + ex.getLocalizedMessage());
} catch (Exception ex)
{
LOGGER.error("Exception caught: " + ex.getLocalizedMessage());
}
LOGGER.info("Grading: " + selectedStudent.getLastName() + ", "
+ selectedStudent.getFirstName());
try
{
LOGGER.debug("Grading Directory: " + directory.getCanonicalPath());
launcher.directory(directory);
StudentReport studentReport = new StudentReport(selectedStudent,
selectedSection);
Runnable r = new GraderThread(node, launcher, directory, gevent,
studentReport, type);
grader_thread = new Thread(r);
grader_thread.start();
statusMessageLabel.setText("");
while (grader_thread.isAlive())
{
}
sectionReport = new SectionReport(course.getName(), selectedInstructor,
selectedSection.getName(), gevent.getAssignment());
sectionReport.addStudentReport(studentReport);
} catch (IOException ex)
{
LOGGER.error("IO Exception: " + ex.getLocalizedMessage());
}
RageLib.printSectionReport(outputFile, sectionReport);
}
statusMessageLabel.setText("Grading complete");
JOptionPane.showMessageDialog(null, "Grading complete.",
"Information", JOptionPane.INFORMATION_MESSAGE);
LOGGER.info("Grading Complete");
// Release the EntityManager objects.
gradedEventDAO.closeConnection();
courseDAO.closeConnection();
}
btnGrade.setEnabled(true);
gradeAssignmentsMenuItem.setEnabled(true);
btnCancel.setEnabled(false);