* @since 1.2.4
*/
public static boolean loadQuestionData(File inputFile, EntityManager em)
{
EntityTransaction tx = em.getTransaction();
Question question = new Question();
Query query;
Document doc;
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
doc = builder.parse(inputFile);
XPathFactory xFactory = XPathFactory.newInstance();
XPath xpath = xFactory.newXPath();
// Loop through each of the questions and build the set of questions
NodeList nodes = (NodeList) xpath.evaluate("/GradedEvent/Question", doc, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++)
{
Question quest;
boolean qnew = true;
Node q = nodes.item(i);
String qname = xpath.evaluate("./Name/@name", q);
String qcategory = xpath.evaluate("./@category", q);
// Switch to the CategoryDAO class
CategoryDAO catDAO = new CategoryDAO(em);
//query = em.createQuery("SELECT c FROM Category c WHERE c.name = :cname");
//query.setParameter("cname", qcategory);
Category qcat;
try
{
//qcat = (Category) query.getSingleResult();
qcat = catDAO.find(qcategory);
} catch (NoResultException e)
{
qcat = new Category(qcategory);
Long newCatId = catDAO.create(qcat);
qcat.setId(newCatId);
}
//tx.begin();
//em.persist(qcat);
//tx.commit();
qcat = catDAO.update(qcat);
query = em.createQuery("SELECT q FROM Question q WHERE "
+ "q.name = :name AND q.category = :cat");
query.setParameter("name", qname);
query.setParameter("cat", qcat);
try
{
quest = (Question) query.getSingleResult();
qnew = false;
} catch (NoResultException e)
{
quest = new Question(qname, qcat);
qnew = true;
}
if (qnew)
{
// This is a new question, so add the other details
String qidStr = xpath.evaluate("./@id", q);
Long qid;
if (qidStr.equals(""))
{
qid = -1L;
} else
{
qid = Long.parseLong(xpath.evaluate("./@id", q));
}
String qdesc = xpath.evaluate("./Description/text()", q);
quest.setDescription(qdesc);
boolean verbatim;
if (xpath.evaluate("./Verbatim/@value", q).equals("true"))
{
verbatim = true;
} else
{
verbatim = false;
}
quest.setVerbatim(verbatim);
// Set whether the question requires ordered output
boolean orderedOutput;
if (xpath.evaluate("./OrderedOutput/@value", q).equals("true"))
{
orderedOutput = true;
} else
{
orderedOutput = false;
}
// Parse test cases for each question
NodeList testCaseNodes = (NodeList) xpath.evaluate("./TestCase", q, XPathConstants.NODESET);
for (int j = 0; j < testCaseNodes.getLength(); j++)
{
TestCase tc = new TestCase();
Node t = testCaseNodes.item(j);
String value = xpath.evaluate("./Value/@value", t);
tc.setValue(new BigDecimal(value));
NodeList tcInputs = (NodeList) xpath.evaluate("./input", t, XPathConstants.NODESET);
for (int k = 0; k < tcInputs.getLength(); k++)
{
Node inputNode = tcInputs.item(k);
String input = inputNode.getTextContent();
//System.out.println("Input: " + input);
tc.addInput(input);
}
NodeList tcOutputs = (NodeList) xpath.evaluate("./output", t, XPathConstants.NODESET);
for (int k = 0; k < tcOutputs.getLength(); k++)
{
Node outputNode = tcOutputs.item(k);
String output = outputNode.getTextContent();
//System.out.println("Expected Output: " + output);
tc.addOutput(output);
}
NodeList tcExcludes = (NodeList) xpath.evaluate("./exclude", t, XPathConstants.NODESET);
for (int k = 0; k < tcExcludes.getLength(); k++)
{
Node outputNode = tcExcludes.item(k);
String exclude = outputNode.getTextContent();
//System.out.println("Exclusion: " + exclude);
tc.addExclusion(exclude);
}
tx.begin();
em.persist(tc);
tx.commit();
quest.addTestCase(tc);
}
}
tx.begin();
em.persist(quest);
tx.commit();