Search in sources :

Example 6 with BaseDateTimeType

use of org.hl7.fhir.dstu3.model.BaseDateTimeType in project org.hl7.fhir.core by hapifhir.

the class FHIRPathEngine method dateAdd.

private BaseDateTimeType dateAdd(BaseDateTimeType d, Quantity q, boolean negate, ExpressionNode holder) {
    BaseDateTimeType result = (BaseDateTimeType) d.copy();
    int value = negate ? 0 - q.getValue().intValue() : q.getValue().intValue();
    switch(q.hasCode() ? q.getCode() : q.getUnit()) {
        case "years":
        case "year":
            result.add(Calendar.YEAR, value);
            break;
        case "a":
            throw new PathEngineException(String.format("Error in date arithmetic: attempt to add a definite quantity duration time unit %s", q.getCode()));
        case "months":
        case "month":
            result.add(Calendar.MONTH, value);
            break;
        case "mo":
            throw new PathEngineException(String.format("Error in date arithmetic: attempt to add a definite quantity duration time unit %s", q.getCode()), holder.getOpStart(), holder.toString());
        case "weeks":
        case "week":
        case "wk":
            result.add(Calendar.DAY_OF_MONTH, value * 7);
            break;
        case "days":
        case "day":
        case "d":
            result.add(Calendar.DAY_OF_MONTH, value);
            break;
        case "hours":
        case "hour":
        case "h":
            result.add(Calendar.HOUR, value);
            break;
        case "minutes":
        case "minute":
        case "min":
            result.add(Calendar.MINUTE, value);
            break;
        case "seconds":
        case "second":
        case "s":
            result.add(Calendar.SECOND, value);
            break;
        case "milliseconds":
        case "millisecond":
        case "ms":
            result.add(Calendar.MILLISECOND, value);
            break;
        default:
            throw new PathEngineException(String.format("Error in date arithmetic: unrecognized time unit %s", q.getCode()));
    }
    return result;
}
Also used : PathEngineException(org.hl7.fhir.exceptions.PathEngineException) BaseDateTimeType(org.hl7.fhir.r5.model.BaseDateTimeType)

Example 7 with BaseDateTimeType

use of org.hl7.fhir.dstu3.model.BaseDateTimeType in project org.hl7.fhir.core by hapifhir.

the class FHIRPathEngine method opMinus.

private List<Base> opMinus(List<Base> left, List<Base> right, ExpressionNode expr) throws PathEngineException {
    if (left.size() == 0 || right.size() == 0) {
        return new ArrayList<Base>();
    }
    if (left.size() > 1) {
        throw makeException(expr, I18nConstants.FHIRPATH_LEFT_VALUE_PLURAL, "-");
    }
    if (!left.get(0).isPrimitive() && !left.get(0).hasType("Quantity")) {
        throw makeException(expr, I18nConstants.FHIRPATH_LEFT_VALUE_WRONG_TYPE, "-", left.get(0).fhirType());
    }
    if (right.size() > 1) {
        throw makeException(expr, I18nConstants.FHIRPATH_RIGHT_VALUE_PLURAL, "-");
    }
    if (!right.get(0).isPrimitive() && !((left.get(0).isDateTime() || "0".equals(left.get(0).primitiveValue()) || left.get(0).hasType("Quantity")) && right.get(0).hasType("Quantity"))) {
        throw makeException(expr, I18nConstants.FHIRPATH_RIGHT_VALUE_WRONG_TYPE, "-", right.get(0).fhirType());
    }
    List<Base> result = new ArrayList<Base>();
    Base l = left.get(0);
    Base r = right.get(0);
    if (l.hasType("integer") && r.hasType("integer")) {
        result.add(new IntegerType(Integer.parseInt(l.primitiveValue()) - Integer.parseInt(r.primitiveValue())));
    } else if (l.hasType("decimal", "integer") && r.hasType("decimal", "integer")) {
        result.add(new DecimalType(new BigDecimal(l.primitiveValue()).subtract(new BigDecimal(r.primitiveValue()))));
    } else if (l.hasType("decimal", "integer", "Quantity") && r.hasType("Quantity")) {
        String s = l.primitiveValue();
        if ("0".equals(s)) {
            Quantity qty = (Quantity) r;
            result.add(qty.copy().setValue(qty.getValue().abs()));
        }
    } else if (l.isDateTime() && r.hasType("Quantity")) {
        result.add(dateAdd((BaseDateTimeType) l, (Quantity) r, true, expr));
    } else {
        throw makeException(expr, I18nConstants.FHIRPATH_OP_INCOMPATIBLE, "-", left.get(0).fhirType(), right.get(0).fhirType());
    }
    return result;
}
Also used : IntegerType(org.hl7.fhir.r5.model.IntegerType) ArrayList(java.util.ArrayList) DecimalType(org.hl7.fhir.r5.model.DecimalType) Quantity(org.hl7.fhir.r5.model.Quantity) Base(org.hl7.fhir.r5.model.Base) BigDecimal(java.math.BigDecimal)

Example 8 with BaseDateTimeType

use of org.hl7.fhir.dstu3.model.BaseDateTimeType in project org.hl7.fhir.core by hapifhir.

the class FHIRPathEngine method opPlus.

private List<Base> opPlus(List<Base> left, List<Base> right, ExpressionNode expr) throws PathEngineException {
    if (left.size() == 0 || right.size() == 0) {
        return new ArrayList<Base>();
    }
    if (left.size() > 1) {
        throw makeException(expr, I18nConstants.FHIRPATH_LEFT_VALUE_PLURAL, "+");
    }
    if (!left.get(0).isPrimitive()) {
        throw makeException(expr, I18nConstants.FHIRPATH_LEFT_VALUE_WRONG_TYPE, "+", left.get(0).fhirType());
    }
    if (right.size() > 1) {
        throw makeException(expr, I18nConstants.FHIRPATH_RIGHT_VALUE_PLURAL, "+");
    }
    if (!right.get(0).isPrimitive() && !((left.get(0).isDateTime() || "0".equals(left.get(0).primitiveValue()) || left.get(0).hasType("Quantity")) && right.get(0).hasType("Quantity"))) {
        throw makeException(expr, I18nConstants.FHIRPATH_RIGHT_VALUE_WRONG_TYPE, "+", right.get(0).fhirType());
    }
    List<Base> result = new ArrayList<Base>();
    Base l = left.get(0);
    Base r = right.get(0);
    if (l.hasType(FHIR_TYPES_STRING) && r.hasType(FHIR_TYPES_STRING)) {
        result.add(new StringType(l.primitiveValue() + r.primitiveValue()));
    } else if (l.hasType("integer") && r.hasType("integer")) {
        result.add(new IntegerType(Integer.parseInt(l.primitiveValue()) + Integer.parseInt(r.primitiveValue())));
    } else if (l.hasType("decimal", "integer") && r.hasType("decimal", "integer")) {
        result.add(new DecimalType(new BigDecimal(l.primitiveValue()).add(new BigDecimal(r.primitiveValue()))));
    } else if (l.isDateTime() && r.hasType("Quantity")) {
        result.add(dateAdd((BaseDateTimeType) l, (Quantity) r, false, expr));
    } else {
        throw makeException(expr, I18nConstants.FHIRPATH_OP_INCOMPATIBLE, "+", left.get(0).fhirType(), right.get(0).fhirType());
    }
    return result;
}
Also used : IntegerType(org.hl7.fhir.r4b.model.IntegerType) StringType(org.hl7.fhir.r4b.model.StringType) ArrayList(java.util.ArrayList) DecimalType(org.hl7.fhir.r4b.model.DecimalType) Base(org.hl7.fhir.r4b.model.Base) BigDecimal(java.math.BigDecimal)

Example 9 with BaseDateTimeType

use of org.hl7.fhir.dstu3.model.BaseDateTimeType in project org.hl7.fhir.core by hapifhir.

the class FHIRPathEngine method dateAdd.

private BaseDateTimeType dateAdd(BaseDateTimeType d, Quantity q, boolean negate, ExpressionNode holder) {
    BaseDateTimeType result = (BaseDateTimeType) d.copy();
    int value = negate ? 0 - q.getValue().intValue() : q.getValue().intValue();
    switch(q.hasCode() ? q.getCode() : q.getUnit()) {
        case "years":
        case "year":
            result.add(Calendar.YEAR, value);
            break;
        case "a":
            throw new PathEngineException(String.format("Error in date arithmetic: attempt to add a definite quantity duration time unit %s", q.getCode()));
        case "months":
        case "month":
            result.add(Calendar.MONTH, value);
            break;
        case "mo":
            throw new PathEngineException(String.format("Error in date arithmetic: attempt to add a definite quantity duration time unit %s", q.getCode()), holder.getOpStart(), holder.toString());
        case "weeks":
        case "week":
        case "wk":
            result.add(Calendar.DAY_OF_MONTH, value * 7);
            break;
        case "days":
        case "day":
        case "d":
            result.add(Calendar.DAY_OF_MONTH, value);
            break;
        case "hours":
        case "hour":
        case "h":
            result.add(Calendar.HOUR, value);
            break;
        case "minutes":
        case "minute":
        case "min":
            result.add(Calendar.MINUTE, value);
            break;
        case "seconds":
        case "second":
        case "s":
            result.add(Calendar.SECOND, value);
            break;
        case "milliseconds":
        case "millisecond":
        case "ms":
            result.add(Calendar.MILLISECOND, value);
            break;
        default:
            throw new PathEngineException(String.format("Error in date arithmetic: unrecognized time unit %s", q.getCode()));
    }
    return result;
}
Also used : PathEngineException(org.hl7.fhir.exceptions.PathEngineException) BaseDateTimeType(org.hl7.fhir.r4b.model.BaseDateTimeType)

Example 10 with BaseDateTimeType

use of org.hl7.fhir.dstu3.model.BaseDateTimeType in project org.hl7.fhir.core by hapifhir.

the class FHIRPathEngine method opMinus.

private List<Base> opMinus(List<Base> left, List<Base> right, ExpressionNode expr) throws PathEngineException {
    if (left.size() == 0 || right.size() == 0) {
        return new ArrayList<Base>();
    }
    if (left.size() > 1) {
        throw makeException(expr, I18nConstants.FHIRPATH_LEFT_VALUE_PLURAL, "-");
    }
    if (!left.get(0).isPrimitive() && !left.get(0).hasType("Quantity")) {
        throw makeException(expr, I18nConstants.FHIRPATH_LEFT_VALUE_WRONG_TYPE, "-", left.get(0).fhirType());
    }
    if (right.size() > 1) {
        throw makeException(expr, I18nConstants.FHIRPATH_RIGHT_VALUE_PLURAL, "-");
    }
    if (!right.get(0).isPrimitive() && !((left.get(0).isDateTime() || "0".equals(left.get(0).primitiveValue()) || left.get(0).hasType("Quantity")) && right.get(0).hasType("Quantity"))) {
        throw makeException(expr, I18nConstants.FHIRPATH_RIGHT_VALUE_WRONG_TYPE, "-", right.get(0).fhirType());
    }
    List<Base> result = new ArrayList<Base>();
    Base l = left.get(0);
    Base r = right.get(0);
    if (l.hasType("integer") && r.hasType("integer")) {
        result.add(new IntegerType(Integer.parseInt(l.primitiveValue()) - Integer.parseInt(r.primitiveValue())));
    } else if (l.hasType("decimal", "integer") && r.hasType("decimal", "integer")) {
        result.add(new DecimalType(new BigDecimal(l.primitiveValue()).subtract(new BigDecimal(r.primitiveValue()))));
    } else if (l.hasType("decimal", "integer", "Quantity") && r.hasType("Quantity")) {
        String s = l.primitiveValue();
        if ("0".equals(s)) {
            Quantity qty = (Quantity) r;
            result.add(qty.copy().setValue(qty.getValue().abs()));
        }
    } else if (l.isDateTime() && r.hasType("Quantity")) {
        result.add(dateAdd((BaseDateTimeType) l, (Quantity) r, true, expr));
    } else {
        throw makeException(expr, I18nConstants.FHIRPATH_OP_INCOMPATIBLE, "-", left.get(0).fhirType(), right.get(0).fhirType());
    }
    return result;
}
Also used : IntegerType(org.hl7.fhir.r4b.model.IntegerType) ArrayList(java.util.ArrayList) DecimalType(org.hl7.fhir.r4b.model.DecimalType) Quantity(org.hl7.fhir.r4b.model.Quantity) Base(org.hl7.fhir.r4b.model.Base) BigDecimal(java.math.BigDecimal)

Aggregations

BigDecimal (java.math.BigDecimal)6 ArrayList (java.util.ArrayList)4 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)4 Base (org.hl7.fhir.r4b.model.Base)2 DecimalType (org.hl7.fhir.r4b.model.DecimalType)2 IntegerType (org.hl7.fhir.r4b.model.IntegerType)2 Base (org.hl7.fhir.r5.model.Base)2 DecimalType (org.hl7.fhir.r5.model.DecimalType)2 IntegerType (org.hl7.fhir.r5.model.IntegerType)2 Nonnull (javax.annotation.Nonnull)1 SupportingInformationComponent (org.hl7.fhir.dstu3.model.ExplanationOfBenefit.SupportingInformationComponent)1 BaseDateTimeType (org.hl7.fhir.r4.model.BaseDateTimeType)1 DateTimeType (org.hl7.fhir.r4.model.DateTimeType)1 InstantType (org.hl7.fhir.r4.model.InstantType)1 BaseDateTimeType (org.hl7.fhir.r4b.model.BaseDateTimeType)1 Quantity (org.hl7.fhir.r4b.model.Quantity)1 StringType (org.hl7.fhir.r4b.model.StringType)1 BaseDateTimeType (org.hl7.fhir.r5.model.BaseDateTimeType)1 Quantity (org.hl7.fhir.r5.model.Quantity)1 StringType (org.hl7.fhir.r5.model.StringType)1