Search in sources :

Example 1 with QuestionnaireItemType

use of org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemType in project org.hl7.fhir.core by hapifhir.

the class QuestionnaireBuilder method convertType.

@SuppressWarnings("unchecked")
private Type convertType(Base value, QuestionnaireItemType af, ValueSet vs, String path) throws FHIRException {
    switch(af) {
        // simple cases
        case BOOLEAN:
            if (value instanceof BooleanType)
                return (Type) value;
        case DECIMAL:
            if (value instanceof DecimalType)
                return (Type) value;
        case INTEGER:
            if (value instanceof IntegerType)
                return (Type) value;
        case DATE:
            if (value instanceof DateType)
                return (Type) value;
        case DATETIME:
            if (value instanceof DateTimeType)
                return (Type) value;
        case INSTANT:
            if (value instanceof InstantType)
                return (Type) value;
        case TIME:
            if (value instanceof TimeType)
                return (Type) value;
        case STRING:
            if (value instanceof StringType)
                return (Type) value;
            else if (value instanceof UriType)
                return new StringType(((UriType) value).asStringValue());
        case TEXT:
            if (value instanceof StringType)
                return (Type) value;
        case QUANTITY:
            if (value instanceof Quantity)
                return (Type) value;
        // ? QuestionnaireItemTypeAttachment: ...?
        case CHOICE:
        case OPENCHOICE:
            if (value instanceof Coding)
                return (Type) value;
            else if (value instanceof Enumeration) {
                Coding cc = new Coding();
                cc.setCode(((Enumeration<Enum<?>>) value).asStringValue());
                cc.setSystem(getSystemForCode(vs, cc.getCode(), path));
                return cc;
            } else if (value instanceof StringType) {
                Coding cc = new Coding();
                cc.setCode(((StringType) value).asStringValue());
                cc.setSystem(getSystemForCode(vs, cc.getCode(), path));
                return cc;
            }
        case REFERENCE:
            if (value instanceof Reference)
                return (Type) value;
            else if (value instanceof StringType) {
                Reference r = new Reference();
                r.setReference(((StringType) value).asStringValue());
            }
    }
    throw new FHIRException("Unable to convert from '" + value.getClass().toString() + "' for Answer Format " + af.toCode() + ", path = " + path);
}
Also used : Enumeration(org.hl7.fhir.dstu2016may.model.Enumeration) StringType(org.hl7.fhir.dstu2016may.model.StringType) Reference(org.hl7.fhir.dstu2016may.model.Reference) BooleanType(org.hl7.fhir.dstu2016may.model.BooleanType) Quantity(org.hl7.fhir.dstu2016may.model.Quantity) FHIRException(org.hl7.fhir.exceptions.FHIRException) DateTimeType(org.hl7.fhir.dstu2016may.model.DateTimeType) TimeType(org.hl7.fhir.dstu2016may.model.TimeType) UriType(org.hl7.fhir.dstu2016may.model.UriType) IntegerType(org.hl7.fhir.dstu2016may.model.IntegerType) DateTimeType(org.hl7.fhir.dstu2016may.model.DateTimeType) Coding(org.hl7.fhir.dstu2016may.model.Coding) DecimalType(org.hl7.fhir.dstu2016may.model.DecimalType) InstantType(org.hl7.fhir.dstu2016may.model.InstantType) DateType(org.hl7.fhir.dstu2016may.model.DateType)

Example 2 with QuestionnaireItemType

use of org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemType in project org.hl7.fhir.core by hapifhir.

the class QuestionnaireBuilder method convertType.

@SuppressWarnings("unchecked")
private Type convertType(Base value, QuestionnaireItemType af, ValueSet vs, String path) throws FHIRException {
    switch(af) {
        // simple cases
        case BOOLEAN:
            if (value instanceof BooleanType)
                return (Type) value;
        case DECIMAL:
            if (value instanceof DecimalType)
                return (Type) value;
        case INTEGER:
            if (value instanceof IntegerType)
                return (Type) value;
        case DATE:
            if (value instanceof DateType)
                return (Type) value;
        case DATETIME:
            if (value instanceof DateTimeType)
                return (Type) value;
        case TIME:
            if (value instanceof TimeType)
                return (Type) value;
        case STRING:
            if (value instanceof StringType)
                return (Type) value;
            else if (value instanceof UriType)
                return new StringType(((UriType) value).asStringValue());
        case TEXT:
            if (value instanceof StringType)
                return (Type) value;
        case QUANTITY:
            if (value instanceof Quantity)
                return (Type) value;
        // ? QuestionnaireItemTypeAttachment: ...?
        case CHOICE:
        case OPENCHOICE:
            if (value instanceof Coding)
                return (Type) value;
            else if (value instanceof Enumeration) {
                Coding cc = new Coding();
                cc.setCode(((Enumeration<Enum<?>>) value).asStringValue());
                cc.setSystem(getSystemForCode(vs, cc.getCode(), path));
                return cc;
            } else if (value instanceof StringType) {
                Coding cc = new Coding();
                cc.setCode(((StringType) value).asStringValue());
                cc.setSystem(getSystemForCode(vs, cc.getCode(), path));
                return cc;
            }
        case REFERENCE:
            if (value instanceof Reference)
                return (Type) value;
            else if (value instanceof StringType) {
                Reference r = new Reference();
                r.setReference(((StringType) value).asStringValue());
            }
    }
    throw new FHIRException("Unable to convert from '" + value.getClass().toString() + "' for Answer Format " + af.toCode() + ", path = " + path);
}
Also used : Enumeration(org.hl7.fhir.dstu3.model.Enumeration) StringType(org.hl7.fhir.dstu3.model.StringType) Reference(org.hl7.fhir.dstu3.model.Reference) BooleanType(org.hl7.fhir.dstu3.model.BooleanType) Quantity(org.hl7.fhir.dstu3.model.Quantity) FHIRException(org.hl7.fhir.exceptions.FHIRException) TimeType(org.hl7.fhir.dstu3.model.TimeType) DateTimeType(org.hl7.fhir.dstu3.model.DateTimeType) UriType(org.hl7.fhir.dstu3.model.UriType) IntegerType(org.hl7.fhir.dstu3.model.IntegerType) DateTimeType(org.hl7.fhir.dstu3.model.DateTimeType) Coding(org.hl7.fhir.dstu3.model.Coding) DecimalType(org.hl7.fhir.dstu3.model.DecimalType) DateType(org.hl7.fhir.dstu3.model.DateType)

Example 3 with QuestionnaireItemType

use of org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemType in project org.hl7.fhir.core by hapifhir.

the class QuestionnaireBuilder method addQuestion.

private QuestionnaireItemComponent addQuestion(QuestionnaireItemComponent group, QuestionnaireItemType af, String path, String id, String name, List<QuestionnaireResponse.QuestionnaireResponseItemComponent> answerGroups, ValueSet vs) throws FHIRException {
    QuestionnaireItemComponent result = group.addItem();
    if (vs != null) {
        result.setOptions(new Reference());
        if (vs.getExpansion() == null) {
            result.getOptions().setReference(vs.getUrl());
            ToolingExtensions.addControl(result, "lookup");
        } else {
            if (Utilities.noString(vs.getId())) {
                vs.setId(nextId("vs"));
                questionnaire.getContained().add(vs);
                vsCache.put(vs.getUrl(), vs.getId());
                vs.setText(null);
                vs.setCompose(null);
                vs.getContact().clear();
                vs.setPublisherElement(null);
                vs.setCopyrightElement(null);
            }
            result.getOptions().setReference("#" + vs.getId());
        }
    }
    result.setLinkId(path + '.' + id);
    result.setText(name);
    result.setType(af);
    result.setRequired(false);
    result.setRepeats(false);
    if (id.endsWith("/1"))
        id = id.substring(0, id.length() - 2);
    if (answerGroups != null) {
        for (QuestionnaireResponse.QuestionnaireResponseItemComponent ag : answerGroups) {
            List<Base> children = new ArrayList<Base>();
            QuestionnaireResponse.QuestionnaireResponseItemComponent aq = null;
            Element obj = (Element) ag.getUserData("object");
            if (isPrimitive((TypeRefComponent) obj))
                children.add(obj);
            else if (obj instanceof Enumeration) {
                String value = ((Enumeration) obj).toString();
                children.add(new StringType(value));
            } else
                children = obj.listChildrenByName(id);
            for (Base child : children) {
                if (child != null) {
                    if (aq == null) {
                        aq = ag.addItem();
                        aq.setLinkId(result.getLinkId());
                        aq.setText(result.getText());
                    }
                    aq.addAnswer().setValue(convertType(child, af, vs, result.getLinkId()));
                }
            }
        }
    }
    return result;
}
Also used : QuestionnaireItemComponent(org.hl7.fhir.dstu3.model.Questionnaire.QuestionnaireItemComponent) Enumeration(org.hl7.fhir.dstu3.model.Enumeration) StringType(org.hl7.fhir.dstu3.model.StringType) Reference(org.hl7.fhir.dstu3.model.Reference) Element(org.hl7.fhir.dstu3.model.Element) ArrayList(java.util.ArrayList) QuestionnaireResponse(org.hl7.fhir.dstu3.model.QuestionnaireResponse) Base(org.hl7.fhir.dstu3.model.Base)

Example 4 with QuestionnaireItemType

use of org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemType in project org.hl7.fhir.core by hapifhir.

the class QuestionnaireBuilder method addQuestion.

private QuestionnaireItemComponent addQuestion(QuestionnaireItemComponent group, QuestionnaireItemType af, String path, String id, String name, List<QuestionnaireResponse.QuestionnaireResponseItemComponent> answerGroups, ValueSet vs) throws FHIRException {
    QuestionnaireItemComponent result = group.addItem();
    if (vs != null) {
        if (vs.getExpansion() == null) {
            result.setAnswerValueSet(vs.getUrl());
            ToolingExtensions.addControl(result, "lookup");
        } else {
            if (Utilities.noString(vs.getId())) {
                vs.setId(nextId("vs"));
                questionnaire.getContained().add(vs);
                vsCache.put(vs.getUrl(), vs.getId());
                vs.setText(null);
                vs.setCompose(null);
                vs.getContact().clear();
                vs.setPublisherElement(null);
                vs.setCopyrightElement(null);
            }
            result.setAnswerValueSet("#" + vs.getId());
        }
    }
    result.setLinkId(path + '.' + id);
    result.setText(name);
    result.setType(af);
    result.setRequired(false);
    result.setRepeats(false);
    if (id.endsWith("/1"))
        id = id.substring(0, id.length() - 2);
    if (answerGroups != null) {
        for (QuestionnaireResponse.QuestionnaireResponseItemComponent ag : answerGroups) {
            List<Base> children = new ArrayList<Base>();
            QuestionnaireResponse.QuestionnaireResponseItemComponent aq = null;
            Element obj = (Element) ag.getUserData("object");
            if (isPrimitive((TypeRefComponent) obj))
                children.add(obj);
            else if (obj instanceof Enumeration) {
                String value = ((Enumeration) obj).toString();
                children.add(new StringType(value));
            } else
                children = obj.listChildrenByName(id);
            for (Base child : children) {
                if (child != null) {
                    if (aq == null) {
                        aq = ag.addItem();
                        aq.setLinkId(result.getLinkId());
                        aq.setText(result.getText());
                    }
                    aq.addAnswer().setValue(convertType(child, af, vs, result.getLinkId()));
                }
            }
        }
    }
    return result;
}
Also used : QuestionnaireItemComponent(org.hl7.fhir.r4b.model.Questionnaire.QuestionnaireItemComponent) Enumeration(org.hl7.fhir.r4b.model.Enumeration) StringType(org.hl7.fhir.r4b.model.StringType) Element(org.hl7.fhir.r4b.model.Element) ArrayList(java.util.ArrayList) QuestionnaireResponse(org.hl7.fhir.r4b.model.QuestionnaireResponse) Base(org.hl7.fhir.r4b.model.Base)

Example 5 with QuestionnaireItemType

use of org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemType in project org.hl7.fhir.core by hapifhir.

the class QuestionnaireBuilder method convertType.

@SuppressWarnings("unchecked")
private DataType convertType(Base value, QuestionnaireItemType af, ValueSet vs, String path) throws FHIRException {
    switch(af) {
        // simple cases
        case BOOLEAN:
            if (value instanceof BooleanType)
                return (DataType) value;
            break;
        case DECIMAL:
            if (value instanceof DecimalType)
                return (DataType) value;
            break;
        case INTEGER:
            if (value instanceof IntegerType)
                return (DataType) value;
            break;
        case DATE:
            if (value instanceof DateType)
                return (DataType) value;
            break;
        case DATETIME:
            if (value instanceof DateTimeType)
                return (DataType) value;
            break;
        case TIME:
            if (value instanceof TimeType)
                return (DataType) value;
            break;
        case STRING:
            if (value instanceof StringType)
                return (DataType) value;
            else if (value instanceof UriType)
                return new StringType(((UriType) value).asStringValue());
            break;
        case TEXT:
            if (value instanceof StringType)
                return (DataType) value;
            break;
        case QUANTITY:
            if (value instanceof Quantity)
                return (DataType) value;
            break;
        // ? QuestionnaireItemTypeAttachment: ...?
        case CODING:
            if (value instanceof Coding)
                return (DataType) value;
            else if (value instanceof Enumeration) {
                Coding cc = new Coding();
                cc.setCode(((Enumeration<Enum<?>>) value).asStringValue());
                cc.setSystem(getSystemForCode(vs, cc.getCode(), path));
                return cc;
            } else if (value instanceof StringType) {
                Coding cc = new Coding();
                cc.setCode(((StringType) value).asStringValue());
                cc.setSystem(getSystemForCode(vs, cc.getCode(), path));
                return cc;
            }
            break;
        case REFERENCE:
            if (value instanceof Reference)
                return (DataType) value;
            else if (value instanceof StringType) {
                Reference r = new Reference();
                r.setReference(((StringType) value).asStringValue());
            }
            break;
        default:
            break;
    }
    throw new FHIRException("Unable to convert from '" + value.getClass().toString() + "' for Answer Format " + af.toCode() + ", path = " + path);
}
Also used : Enumeration(org.hl7.fhir.r5.model.Enumeration) StringType(org.hl7.fhir.r5.model.StringType) Reference(org.hl7.fhir.r5.model.Reference) BooleanType(org.hl7.fhir.r5.model.BooleanType) Quantity(org.hl7.fhir.r5.model.Quantity) FHIRException(org.hl7.fhir.exceptions.FHIRException) TimeType(org.hl7.fhir.r5.model.TimeType) DateTimeType(org.hl7.fhir.r5.model.DateTimeType) UriType(org.hl7.fhir.r5.model.UriType) IntegerType(org.hl7.fhir.r5.model.IntegerType) DateTimeType(org.hl7.fhir.r5.model.DateTimeType) Coding(org.hl7.fhir.r5.model.Coding) DecimalType(org.hl7.fhir.r5.model.DecimalType) DateType(org.hl7.fhir.r5.model.DateType)

Aggregations

ArrayList (java.util.ArrayList)5 FHIRException (org.hl7.fhir.exceptions.FHIRException)5 Enumeration (org.hl7.fhir.dstu2016may.model.Enumeration)2 Reference (org.hl7.fhir.dstu2016may.model.Reference)2 StringType (org.hl7.fhir.dstu2016may.model.StringType)2 Enumeration (org.hl7.fhir.dstu3.model.Enumeration)2 Reference (org.hl7.fhir.dstu3.model.Reference)2 StringType (org.hl7.fhir.dstu3.model.StringType)2 Enumeration (org.hl7.fhir.r4.model.Enumeration)2 StringType (org.hl7.fhir.r4.model.StringType)2 Enumeration (org.hl7.fhir.r4b.model.Enumeration)2 StringType (org.hl7.fhir.r4b.model.StringType)2 Base (org.hl7.fhir.dstu2016may.model.Base)1 BooleanType (org.hl7.fhir.dstu2016may.model.BooleanType)1 Coding (org.hl7.fhir.dstu2016may.model.Coding)1 DateTimeType (org.hl7.fhir.dstu2016may.model.DateTimeType)1 DateType (org.hl7.fhir.dstu2016may.model.DateType)1 DecimalType (org.hl7.fhir.dstu2016may.model.DecimalType)1 Element (org.hl7.fhir.dstu2016may.model.Element)1 InstantType (org.hl7.fhir.dstu2016may.model.InstantType)1