001package ca.uhn.fhir.cql.r4.builder;
002
003/*-
004 * #%L
005 * HAPI FHIR JPA Server - Clinical Quality Language
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.cql.common.builder.BaseBuilder;
024import org.hl7.fhir.exceptions.FHIRException;
025import org.hl7.fhir.r4.model.MeasureReport;
026import org.hl7.fhir.r4.model.Period;
027import org.hl7.fhir.r4.model.Reference;
028import org.opencds.cqf.cql.engine.runtime.DateTime;
029import org.opencds.cqf.cql.engine.runtime.Interval;
030
031import java.util.Date;
032
033public class MeasureReportBuilder extends BaseBuilder<MeasureReport> {
034        private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ca.uhn.fhir.cql.dstu3.builder.MeasureReportBuilder.class);
035
036        public MeasureReportBuilder() {
037        super(new MeasureReport());
038    }
039
040    public MeasureReportBuilder buildStatus(String status) {
041        try {
042            this.complexProperty.setStatus(MeasureReport.MeasureReportStatus.fromCode(status));
043        } catch (FHIRException e) {
044                          ourLog.warn("Exception caught while attempting to set Status to '" + status + "', assuming status COMPLETE!"
045                                  + System.lineSeparator() + e.getMessage());
046                          this.complexProperty.setStatus(MeasureReport.MeasureReportStatus.COMPLETE);
047        }
048        return this;
049    }
050
051    public MeasureReportBuilder buildType(MeasureReport.MeasureReportType type) {
052        this.complexProperty.setType(type);
053        return this;
054    }
055
056    public MeasureReportBuilder buildType(String type) {
057        this.complexProperty.setType(MeasureReport.MeasureReportType.fromCode(type));
058        return this;
059    }
060
061    public MeasureReportBuilder buildMeasureReference(String measureRef) {
062        this.complexProperty.setMeasure(measureRef);
063        return this;
064    }
065
066    public MeasureReportBuilder buildPatientReference(String patientRef) {
067        this.complexProperty.setSubject(new Reference(patientRef));
068        return this;
069    }
070
071    public MeasureReportBuilder buildPeriod(Interval period) {
072        Object start = period.getStart();
073        if (start instanceof DateTime) {
074            this.complexProperty
075                    .setPeriod(new Period().setStart(Date.from(((DateTime) start).getDateTime().toInstant()))
076                            .setEnd(Date.from(((DateTime) period.getEnd()).getDateTime().toInstant())));
077        } else if (start instanceof Date) {
078            this.complexProperty
079                    .setPeriod(new Period().setStart((Date) period.getStart()).setEnd((Date) period.getEnd()));
080        }
081
082        return this;
083    }
084}