/** * Extracts the QuestionFormAnswers object from the given answer XML. * * @param answerXML the answer XML string * @return a QuestionFormAnswers object that contains the answers */ public static QuestionFormAnswers parseAnswers(String answerXML) { try { JAXBContext jc = JAXBContext.newInstance(RequesterService.DATASCHEMA_PACKAGE_PREFIX, ObjectFactory.class.getClassLoader()); Unmarshaller u = jc.createUnmarshaller(); QuestionFormAnswers qfa = (QuestionFormAnswers) u.unmarshal(new InputSource(new StringReader(answerXML))); return qfa; } catch (Exception e) { e.printStackTrace(); return null; } }
@SuppressWarnings("unchecked") public void testParseAnswers() throws ServiceException { QuestionFormAnswers qfa = RequesterService.parseAnswers(DEFAULT_ANSWER_XML); List<QuestionFormAnswersType.AnswerType> answers = (List<QuestionFormAnswersType.AnswerType>) qfa.getAnswer(); for (int i=0; i< answers.size(); i++) { QuestionFormAnswersType.AnswerType answer = answers.get(i); assertNotNull(null, RequesterService.getAnswerValue(null, answer)); // check order String result = RequesterService.getAnswerValue("TEST_ASSIGNMENT_ID", answer, true); if (i == 0) { assertTrue(result.equals("freeText\tfreeText_answer")); } else if (i == 1) { assertTrue(result.equals("otherSelection\totherSelection_answer")); } else if (i == 2) { assertTrue(result.startsWith("url\t")); } else { assertTrue(result.equals("selectionIdentifier\tselection_answer|selection_answer2")); } } }
@SuppressWarnings("unchecked") private String getAnswers(Assignment assignment) { String result = EMPTY; AssignmentStatus status = assignment.getAssignmentStatus(); if (status == null) { return NO_ANSWER; } String answerXML = assignment.getAnswer(); QuestionFormAnswers qfa = RequesterService.parseAnswers(answerXML); List<QuestionFormAnswersType.AnswerType> answers = (List<QuestionFormAnswersType.AnswerType>) qfa.getAnswer(); for (QuestionFormAnswersType.AnswerType answer : answers) { String assignmentId = assignment.getAssignmentId(); String answerValue = RequesterService.getAnswerValue(assignmentId, answer, true); if (answerValue != null) { result += answerValue + DELIMITER; } } return result; }
public static void main(String[] args) { QuestionFormAnswers qfa = RequesterService.parseAnswers(DEFAULT_ANSWER_XML); List<QuestionFormAnswersType.AnswerType> answers = (List<QuestionFormAnswersType.AnswerType>) qfa.getAnswer(); TestRunner.run(TestRequesterService.class); }
@SuppressWarnings("unchecked") /** * Prints the submitted results of a HIT when provided with a HIT ID. * @param hitId The HIT ID of the HIT to be retrieved. */ public void reviewAnswers(String hitId) { Assignment[] assignments = service.getAllAssignmentsForHIT(hitId); System.out.println("--[Reviewing HITs]----------"); System.out.println(" HIT Id: " + hitId); for (Assignment assignment : assignments) { //Only assignments that have been submitted will contain answer data if (assignment.getAssignmentStatus() == AssignmentStatus.Submitted) { //By default, answers are specified in XML String answerXML = assignment.getAnswer(); //Calling a convenience method that will parse the answer XML and extract out the question/answer pairs. QuestionFormAnswers qfa = RequesterService.parseAnswers(answerXML); List<QuestionFormAnswersType.AnswerType> answers = (List<QuestionFormAnswersType.AnswerType>) qfa.getAnswer(); for (QuestionFormAnswersType.AnswerType answer : answers) { String assignmentId = assignment.getAssignmentId(); String answerValue = RequesterService.getAnswerValue(assignmentId, answer); if (answerValue != null) { System.out.println("Got an answer \"" + answerValue + "\" from worker " + assignment.getWorkerId() + "."); } } //Approving the assignment. service.approveAssignment(assignment.getAssignmentId(), "Well Done!"); System.out.println("Approved."); } } System.out.println("--[End Reviewing HITs]----------"); }
@SuppressWarnings("unchecked") /** * Prints the submitted results of a HIT when provided with a HIT ID. * @param hitId The HIT ID of the HIT to be retrieved. */ public void reviewAnswers(ReviewHIT hit) { Assignment[] assignments = service.getAllAssignmentsForHIT(hit.id); System.out.println("--[Reviewing HITs]----------"); System.out.println("author:review=" + hit.author + ":" + hit.review); System.out.println(" HIT Id: " + hit.id); for (Assignment assignment : assignments) { String path = Utils.getTranslatedFixedFilename(filenames.get(hit.author), hit.review); System.out.println(path.substring(path.lastIndexOf('/')+1)); //Only assignments that have been submitted will contain answer data if (assignment.getAssignmentStatus() == AssignmentStatus.Submitted) { //By default, answers are specified in XML String answerXML = assignment.getAnswer(); //Calling a convenience method that will parse the answer XML and extract out the question/answer pairs. QuestionFormAnswers qfa = RequesterService.parseAnswers(answerXML); List<QuestionFormAnswersType.AnswerType> answers = (List<QuestionFormAnswersType.AnswerType>) qfa.getAnswer(); for (QuestionFormAnswersType.AnswerType answer : answers) { String assignmentId = assignment.getAssignmentId(); String answerValue = RequesterService.getAnswerValue(assignmentId, answer); if (answerValue != null) { if (answerValue.equals("emptyanswer") || answerValue.equals("fair") || answerValue.equals("nice") || answerValue.equals("poor")) { // service.rejectAssignment(assignmentId, ""); System.out.println("BAD ANSWER"); } else { // service.rejectAssignment(assignmentId, ""); System.out.println("Got an answer \"" + answerValue + "\" from worker " + assignment.getWorkerId() + "."); size += 1; } } // ReviewWriter.writeTranslatedFixedReview(answerValue, hit.author, hit.review); /** Reject bad workers here. */ // if (false) { // System.out.println("BAD WORKER"); // service.rejectAssignment(assignmentId, "Do not follow the submission instructions: Submit a result(explained below) followed by the explanation of differences."); // } } // Approving the assignment. // service.approveAssignment(assignment.getAssignmentId(), "Well Done!"); // System.out.println("Approved."); // service.disposeHIT(hit.id); // service.disableHIT(hit.id); // } } System.out.println("--[End Reviewing HITs]----------"); }