use of org.hl7.fhir.r4b.model.QuestionnaireResponse.QuestionnaireResponseItemComponent in project org.hl7.fhir.core by hapifhir.
the class QuestionnaireResponseRenderer method renderTree.
public boolean renderTree(XhtmlNode x, QuestionnaireResponse q) throws UnsupportedEncodingException, IOException {
HierarchicalTableGenerator gen = new HierarchicalTableGenerator(context.getDestDir(), context.isInlineGraphics(), true);
TableModel model = gen.new TableModel("qtree=" + q.getId(), true);
model.setAlternating(true);
model.setDocoImg(context.getSpecificationLink() + "help16.png");
model.setDocoRef(context.getSpecificationLink() + "formats.html#table");
model.getTitles().add(gen.new Title(null, model.getDocoRef(), translate("sd.head", "LinkId"), translate("sd.hint", "The linkId for the item"), null, 0));
model.getTitles().add(gen.new Title(null, model.getDocoRef(), translate("sd.head", "Text"), translate("sd.hint", "Text for the item"), null, 0));
model.getTitles().add(gen.new Title(null, model.getDocoRef(), translate("sd.head", "Definition"), translate("sd.hint", "Minimum and Maximum # of times the the itemcan appear in the instance"), null, 0));
model.getTitles().add(gen.new Title(null, model.getDocoRef(), translate("sd.head", "Answer"), translate("sd.hint", "The type of the item"), null, 0));
boolean hasExt = false;
// first we add a root for the questionaire itself
Row row = addTreeRoot(gen, model.getRows(), q);
for (QuestionnaireResponseItemComponent i : q.getItem()) {
hasExt = renderTreeItem(gen, row.getSubRows(), q, i) || hasExt;
}
XhtmlNode xn = gen.generate(model, context.getLocalPrefix(), 1, null);
x.getChildNodes().add(xn);
return hasExt;
}
use of org.hl7.fhir.r4b.model.QuestionnaireResponse.QuestionnaireResponseItemComponent in project CRD by HL7-DaVinci.
the class QuestionnaireController method getNextQuestionOperation.
/**
* Returns the next question based on the request.
* @param body
* @param request
* @return
*/
private ResponseEntity<String> getNextQuestionOperation(String body, HttpServletRequest request) {
logger.info("POST /Questionnaire/$next-question fhir+");
FhirContext ctx = new FhirComponents().getFhirContext();
IParser parser = ctx.newJsonParser();
// Parses the body.
IDomainResource domainResource = (IDomainResource) parser.parseResource(QuestionnaireResponse.class, body);
if (!domainResource.fhirType().equalsIgnoreCase("QuestionnaireResponse")) {
logger.warning("unsupported resource type: ");
HttpStatus status = HttpStatus.BAD_REQUEST;
MediaType contentType = MediaType.TEXT_PLAIN;
return ResponseEntity.status(status).contentType(contentType).body("Bad Request");
} else {
logger.info(" ---- Resource received " + domainResource.toString());
QuestionnaireResponse inputQuestionnaireResponse = (QuestionnaireResponse) domainResource;
String fragmentId = inputQuestionnaireResponse.getQuestionnaire();
List<Resource> containedResource = inputQuestionnaireResponse.getContained();
Questionnaire inputQuestionnaireFromRequest = null;
for (int i = 0; i < containedResource.size(); i++) {
Resource item = containedResource.get(i);
if (item.getResourceType().equals(ResourceType.Questionnaire)) {
Questionnaire checkInputQuestionnaire = (Questionnaire) item;
if (checkInputQuestionnaire.getId().equals(fragmentId)) {
inputQuestionnaireFromRequest = checkInputQuestionnaire;
break;
}
}
}
logger.info("--- Received questionnaire response: " + ctx.newJsonParser().encodeResourceToString(inputQuestionnaireResponse));
// Check that there are no duplicates in the recieved set of questions.
if ((new HashSet(((Questionnaire) inputQuestionnaireResponse.getContained().get(0)).getItem().stream().map(item -> item.getLinkId()).collect(Collectors.toList()))).size() != ((Questionnaire) inputQuestionnaireResponse.getContained().get(0)).getItem().size()) {
throw new RuntimeException("Received a set of questions with duplicates.");
}
String questionnaireId = ((Reference) inputQuestionnaireResponse.getExtensionByUrl("http://hl7.org/fhir/StructureDefinition/contained-id").getValue()).getReference();
System.out.println("Input Questionnaire: " + questionnaireId);
if (inputQuestionnaireFromRequest != null) {
if (!questionnaireTrees.containsKey(questionnaireId)) {
// If there is not already a tree that matches the requested questionnaire id, build it.
// Import the requested CDS-Library Questionnaire.
Questionnaire cdsQuestionnaire = importCdsAdaptiveQuestionnaire(request, parser, fileStore, questionnaireId);
// Build the tree.
AdaptiveQuestionnaireTree newTree = new AdaptiveQuestionnaireTree(cdsQuestionnaire);
questionnaireTrees.put(questionnaireId, newTree);
logger.info("--- Built Questionnaire Tree for " + questionnaireId);
}
// Pull the tree for the requested questionnaire id.
AdaptiveQuestionnaireTree currentTree = questionnaireTrees.get(questionnaireId);
// Get the request's set of answer responses.
List<QuestionnaireResponseItemComponent> allResponses = inputQuestionnaireResponse.getItem();
// Pull the resulting next question that the recieved responses and answers point to from the tree without including its children.
List<QuestionnaireItemComponent> nextQuestionSetResults = currentTree.getNextQuestionsForAnswers(allResponses, inputQuestionnaireResponse);
// Add the next set of questions to the response.
QuestionnaireController.addQuestionSetToQuestionnaireResponse(inputQuestionnaireResponse, nextQuestionSetResults);
// Check that there no duplicates in the set of questions.
if ((new HashSet(((Questionnaire) inputQuestionnaireResponse.getContained().get(0)).getItem().stream().map(item -> item.getLinkId()).collect(Collectors.toList()))).size() != ((Questionnaire) inputQuestionnaireResponse.getContained().get(0)).getItem().size()) {
throw new RuntimeException("Attempted to send a set of questions with duplicates. Question IDs are: " + (((Questionnaire) inputQuestionnaireResponse.getContained().get(0)).getItem().stream().map(item -> item.getLinkId()).collect(Collectors.toList())));
}
logger.info("--- Added next question set for questionnaire \'" + questionnaireId + "\' for responses \'" + allResponses + "\'.");
// Build and send the response.
String formattedResourceString = ctx.newJsonParser().encodeResourceToString(inputQuestionnaireResponse);
logger.info("--- Sending questionnaire response: " + formattedResourceString);
return ResponseEntity.status(HttpStatus.ACCEPTED).contentType(MediaType.APPLICATION_JSON).header(HttpHeaders.CONTENT_TYPE, "application/fhir+json" + "; charset=utf-8").body(formattedResourceString);
} else {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).contentType(MediaType.APPLICATION_JSON).header(HttpHeaders.CONTENT_TYPE, "application/fhir+json" + "; charset=utf-8").body("Input questionnaire from the request does not exist.");
}
}
}
Aggregations