org.carewebframework.cal.api.documents.Document.java Source code

Java tutorial

Introduction

Here is the source code for org.carewebframework.cal.api.documents.Document.java

Source

/**
 * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
 * If a copy of the MPL was not distributed with this file, You can obtain one at
 * http://mozilla.org/MPL/2.0/.
 *
 * This Source Code Form is also subject to the terms of the Health-Related Additional
 * Disclaimer of Warranty and Limitation of Liability available at
 * http://www.carewebframework.org/licensing/disclaimer.
 */
package org.carewebframework.cal.api.documents;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import ca.uhn.fhir.model.dstu.composite.CodeableConceptDt;
import ca.uhn.fhir.model.dstu.composite.CodingDt;
import ca.uhn.fhir.model.dstu.composite.ResourceReferenceDt;
import ca.uhn.fhir.model.dstu.resource.Binary;
import ca.uhn.fhir.model.dstu.resource.DocumentReference;
import ca.uhn.fhir.model.dstu.resource.DocumentReference.Context;

import org.carewebframework.cal.api.ClientUtil;
import org.carewebframework.fhir.common.FhirUtil;

import org.springframework.util.StringUtils;

/**
 * Model object wrapping a document reference and its contents (lazily loaded).
 */
public class Document implements Comparable<Document> {

    private final DocumentReference reference;

    private Set<String> types;

    private Binary contents;

    public Document(DocumentReference reference) {
        this.reference = reference;
    }

    public DocumentReference getReference() {
        return reference;
    }

    public String getTitle() {
        String title = reference.getDescription().getValue();

        if (title == null) {
            CodingDt coding = reference.getType().getCodingFirstRep();
            title = coding == null ? null : coding.getDisplay().getValue();
        }

        return title == null ? "" : title;
    }

    public Date getDateTime() {
        return reference.getCreated().getValue();
    }

    public String getLocationName() {
        Context ctx = reference.getContext();
        CodeableConceptDt facilityType = ctx == null ? null : ctx.getFacilityType();
        CodingDt coding = facilityType == null ? null : FhirUtil.getFirst(facilityType.getCoding());
        return coding == null ? "" : coding.getDisplay().toString();
    }

    public String getAuthorName() {
        ResourceReferenceDt resource = FhirUtil.getFirst(reference.getAuthor());
        return resource == null ? "" : resource.getDisplay().toString();
    }

    public Collection<String> getTypes() {
        if (types == null) {
            types = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
            CodeableConceptDt dt = reference.getType();
            List<CodingDt> codings = dt == null ? null : dt.getCoding();

            if (codings != null) {
                for (CodingDt coding : codings) {
                    String type = coding.getDisplay().toString();

                    if (!StringUtils.isEmpty(type)) {
                        types.add(type);
                    }
                }

            }
        }

        return types;
    }

    public boolean hasType(String type) {
        return getTypes().contains(type);
    }

    public Binary getContents() {
        if (contents == null) {
            contents = ClientUtil.getFhirClient().read(Binary.class, reference.getLocation());
        }

        return contents;
    }

    @Override
    public int compareTo(Document document) {
        return getTitle().compareToIgnoreCase(document.getTitle());
    }
}