//Course course = em.find(Course.class, selectedCourse.getId());
LOGGER.debug("Course Name: " + course.getName());
LOGGER.debug("Querying the data source to see if a GradedEvent "
+ "already exists");
GradedEventDAO gradedEventDAO =
new GradedEventDAO(emf.createEntityManager());
// CriteriaBuilder cb = em.getCriteriaBuilder();
// CriteriaQuery cq = cb.createQuery(GradedEvent.class);
// Root<GradedEvent> gradedEventRoot = cq.from(GradedEvent.class);
// cq.where(cb.and(cb.equal(gradedEventRoot.get("assignment"), txtAssignment.getText().trim()),
// cb.equal(gradedEventRoot.get("term"), txtTerm.getText().trim()),
// cb.equal(gradedEventRoot.get("course"), course),
// cb.equal(gradedEventRoot.get("version"), txtVersion.getText().trim())));
//
// TypedQuery<GradedEvent> gradedEventQuery = em.createQuery(cq);
// LOGGER.debug("Query built");
try
{
//gradedEvent = gradedEventQuery.getSingleResult();
gradedEvent = gradedEventDAO.find(course,
txtAssignment.getText().trim(), txtTerm.getText().trim(),
txtVersion.getText().trim());
} catch (IllegalArgumentException ex)
{
// Because of the check at the beginning of this method, we should
// never reach this point. But just in case...
LOGGER.warn("Invalid value provided. Ensure Course, Term, "
+ "Assignment, and Version are not blank.");
}
if (gradedEvent != null)
{
JOptionPane.showMessageDialog(this, "ERROR: This Graded "
+ "Event already exists in the system.", "ERROR",
JOptionPane.ERROR_MESSAGE);
LOGGER.error("This GradedEvent already exists in the database");
doClose(RET_CANCEL);
} else
{
LOGGER.debug("Creating a new GradedEvent");
EntityTransaction tx = em.getTransaction();
gradedEvent = new GradedEvent();
gradedEvent.setCourse(course);
gradedEvent.setAssignment(txtAssignment.getText().trim());
gradedEvent.setTerm(txtTerm.getText().trim());
gradedEvent.setVersion(txtVersion.getText().trim());
gradedEvent.setPartialCredit(chkPartialCredit.isSelected());
try
{
gradedEvent.setDueDate(txtDueDate.getText().trim());
} catch (IllegalArgumentException ex)
{
LOGGER.warn("Invalid Date Specified. No Due Date Set");
}
LOGGER.debug("Graded Event: " + gradedEvent.toString());
List<Question> objects = lstQuestions.getSelectedValuesList();
LOGGER.debug("Adding " + objects.size() + " Questions to GradedEvent");
int count = 0;
for (int i = 0; i < objects.size(); i++)
{
// TODO: RAGE-24 - Migrate to the QuestionDAO class.
// Action: Update the Question in the current Persistence Context
Question question = objects.get(i);
try
{
question = em.merge(question);
} catch (Exception ex)
{
LOGGER.error("Exception: " + ex.getLocalizedMessage());
tx.rollback();
}
gradedEvent.addQuestion(question);
count++;
}
LOGGER.debug(count + " Questions added. Saving to database");
Long newId;
try
{
newId = gradedEventDAO.create(gradedEvent);
LOGGER.info("Graded Event created with Id: " + newId);
// tx.begin();
// em.persist(gradedEvent);
// tx.commit();
// } catch (EntityExistsException ex)
// {
// LOGGER.error("Graded Event exists in the database previously");
// LOGGER.error("Exception: " + ex.getLocalizedMessage());
// em.close();
// doClose(RET_CANCEL);
// } catch (IllegalStateException ex)
// {
// LOGGER.error("EntityManager has been closed.");
// LOGGER.error("Exception: " + ex.getLocalizedMessage());
// em.close();
// doClose(RET_CANCEL);
// } catch (IllegalArgumentException ex)
// {
// LOGGER.error("Not saving a mapped Entity");
// LOGGER.error("Exception: " + ex.getLocalizedMessage());
// em.close();
// doClose(RET_CANCEL);
// } catch (TransactionRequiredException ex)
// {
// LOGGER.error("Exception: " + ex.getLocalizedMessage());
// em.close();
// doClose(RET_CANCEL);
} catch (Exception ex)
{
LOGGER.error("Exception: " + ex.getLocalizedMessage());
em.close();
doClose(RET_CANCEL);
}
LOGGER.debug("Graded Event saved to database");
txtTerm.setText("");
txtAssignment.setText("");
txtVersion.setText("");
txtDueDate.setText("");
lstQuestions.setSelectedIndex(-1);
doClose(RET_OK);
}
em.close();
courseDAO.closeConnection();
gradedEventDAO.closeConnection();
}
}