{
LOGGER.debug("Question not in database. Inserting "
+ "new question");
// Need to persist each of the test cases before the question
TestCaseDAO testCaseDAO = new TestCaseDAO(emf.createEntityManager());
for (TestCase tc : question.getTestCases())
{
if (tc.getId() != null)
{
try
{
TestCase dbTestCase = testCaseDAO.find(tc.getId());
// TestCase exists in the database, update
tc = testCaseDAO.update(tc);
} catch (IllegalArgumentException exTestCase)
{
// TestCase not in data source, just save
Long newId = testCaseDAO.create(tc);
}
} else
{
// New TestCase with no Id
Long newId = testCaseDAO.create(tc);
tc.setId(newId);
}
// tx.begin();
// tc.setId(null); // Reset the TestCase Id for persisting
// LOGGER.debug("Inputs / Outputs: " + tc.getInputs().size()
// + " / " + tc.getOutputs().size());
// em.persist(tc);
// tx.commit();
}
testCaseDAO.closeConnection();
// Can persist the question now that all components are saved
// TODO: RAGE-24 - Migrate to the QuestionDAO class
tx.begin();
em.persist(question);
tx.commit();