TypedQuery<Question> questionQuery = em.createQuery(cq);
EntityTransaction tx = em.getTransaction();
//See if we are editing a question or creating a new question
try
{
Question question = questionQuery.getSingleResult();
LOGGER.debug("Question" + question.getName()
+ "already exists in the database");
tx.begin();
em.remove(question);
tx.commit();
} catch (NoResultException e)
{
LOGGER.error("That question doesn't exist in the database. "
+ e.getLocalizedMessage());
} catch (IllegalStateException ex)
{
LOGGER.error("Illegal State Exception: "
+ ex.getLocalizedMessage());
} catch (IllegalArgumentException ex)
{
LOGGER.error("Illegal Argument Exception: "
+ ex.getLocalizedMessage());
} catch (TransactionRequiredException ex)
{
LOGGER.error("Transaction Required Exception: "
+ ex.getLocalizedMessage());
} catch (Exception ex)
{
LOGGER.error("Exception Caught: "
+ ex.getLocalizedMessage());
}
// This is a valid question name (not null or "ERROR in Filename:"
Question question = new Question();
question.setName(name);
// Set whether question requires a verbatim response
question.setVerbatim(chkVerbatim.isSelected());
// Set whether question requires ordered output
question.setOrderedOutput(chkOrdered.isSelected());
// Set the description
try
{
question.setDescription(txtDescription.getText(0, 50));
} catch (BadLocationException ex)
{
question.setDescription(txtDescription.getText());
} catch (IllegalArgumentException ex)
{
// Description is blank so just ignore...not required for question
}
/**
* As long as there are test cases, we can continue...otherwise stop
*/
if (modTCList.getSize() > 0)
{
try
{
// Store the test cases in the question
question.setTestCases(modTCList.cloneTestCases());
} catch (CloneNotSupportedException ex)
{
LOGGER.error("Clone not supported");
}
/**
* Run Raptor and store the returned outputs in the TestCase object
*/
ProcessBuilder launcher = new ProcessBuilder();
Map<String, String> environment = launcher.environment();
launcher.redirectErrorStream(true);
launcher.directory(refFileName.getParentFile());
int type = 0;
if (rBtnRaptor.isSelected())
{
type = 0;
} else if (rBtnProcessing.isSelected())
{
type = 1;
}
Runnable r = new TestCaseGenThread(node, launcher,
refFileName, question, type);
Thread testCaseGenThread = new Thread(r);
testCaseGenThread.run();
while (testCaseGenThread.isAlive())
{
}
/** Set the question category--this is not a nullable field
* This button will not activate if the txtCategory field is
* null or empty, so we can safely not check for it here.
*
* First check to see if category already exists in database
*/
//cq = cb.createQuery(Category.class);
//Root<Category> categoryRoot = cq.from(Category.class);
//cq.where(cb.equal(categoryRoot.get("name"),
// txtCategory.getText().trim()));
//TypedQuery<Category> categoryQuery = em.createQuery(cq);
CategoryDAO catDAO = new CategoryDAO(emf.createEntityManager());
Category queryCat;
try
{
//queryCat = categoryQuery.getSingleResult();
queryCat = catDAO.find(txtCategory.getText().trim());
LOGGER.info("Category already exists in database: "
+ queryCat.getName());
} catch (NoResultException ex)
{
queryCat = new Category(txtCategory.getText().trim());
//tx.begin();
//em.persist(queryCat);
//tx.commit();
Long newCatId = catDAO.create(queryCat);
queryCat.setId(newCatId);
LOGGER.info("Added Category: " + queryCat.getName());
}
question.setCategory(queryCat);
if (catDAO.isOpen())
{
catDAO.closeConnection();
}
// TODO: RAGE-24 - Migrate to the QuestionDAO class
cq = cb.createQuery(Question.class);
questionRoot = cq.from(Question.class);
cq.where(cb.and(cb.equal(questionRoot.get("name"),
question.getName()), cb.equal(questionRoot.get("category"),
question.getCategory())));
questionQuery = em.createQuery(cq);
Question quest;
try
{
quest = questionQuery.getSingleResult();
JOptionPane.showMessageDialog(this, "ERROR: Question " +
"already exists.", "Error",
JOptionPane.ERROR_MESSAGE);
LOGGER.warn("Question already exists: "
+ quest.getName());
} catch (NoResultException ex)
{
LOGGER.debug("Question not in database. Inserting "
+ "new question");