try {
long testId = Long.parseLong(requestAttrs.getNamedItem("id").getNodeValue());
if (!isTestAvailable(testId)) {
return buildErrorPacket("Test is not available now");
}
TestVariant variant = TestsFacility.getTestVariant(testId, user.toString());
if (variant == null) {
return buildErrorPacket("Test doesn't exist");
}
Document answerDoc = XmlTools.getDocBuilder().newDocument();
Element answerRoot = answerDoc.createElement("response");
answerRoot.setAttribute("type", "test");
answerRoot.setAttribute("testId", String.valueOf(variant.getTestId()));
answerRoot.setAttribute("testName", variant.getName());
answerRoot.setAttribute("maximumMark", String.valueOf(variant.getMaximumMark()));
answerRoot.setAttribute("variantId", String.valueOf(variant.getVariantId()));
answerRoot.setAttribute("timeOut", String.valueOf(variant.getTimeOut()));
for (TaskItem item : variant.getTasks()) {
Element taskItem = answerDoc.createElement("task");
taskItem.setAttribute("cost", String.valueOf(item.getCost()));
taskItem.setAttribute("taskId", String.valueOf(item.getTaskId()));
Element taskItemQuestion = answerDoc.createElement("question");
taskItemQuestion.appendChild(answerDoc.createCDATASection(item.getQuestion()));
taskItem.appendChild(taskItemQuestion);
Element taskItemAnswerForm = answerDoc.createElement("answerForm");
taskItemAnswerForm.appendChild(answerDoc.createCDATASection(item.getAnswerForm()));
taskItem.appendChild(taskItemAnswerForm);
answerRoot.appendChild(taskItem);
}
answerDoc.appendChild(answerRoot);
inTest = variant.getTestId();
if(answers == null){
answers = new TestAnswersHandler(TestsFacility.getTestVariant(testId, user.toString()));
}
cb.onClientStartTesting(variant.getTestId());
return XmlTools.docToString(answerDoc);
} catch (DOMException | NullPointerException | NumberFormatException e) {
System.out.println("Wrong test request " + e.getMessage());
return buildErrorPacket("Wrong test request.");