001package org.hl7.fhir.r5.utils;
002
003/*
004  Copyright (c) 2011+, HL7, Inc.
005  All rights reserved.
006  
007  Redistribution and use in source and binary forms, with or without modification, 
008  are permitted provided that the following conditions are met:
009    
010   * Redistributions of source code must retain the above copyright notice, this 
011     list of conditions and the following disclaimer.
012   * Redistributions in binary form must reproduce the above copyright notice, 
013     this list of conditions and the following disclaimer in the documentation 
014     and/or other materials provided with the distribution.
015   * Neither the name of HL7 nor the names of its contributors may be used to 
016     endorse or promote products derived from this software without specific 
017     prior written permission.
018  
019  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
020  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
021  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
022  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
023  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
024  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
025  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
026  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
027  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
028  POSSIBILITY OF SUCH DAMAGE.
029  
030 */
031
032
033import java.util.List;
034import java.util.Locale;
035import java.util.Map;
036
037import org.hl7.fhir.r5.model.Bundle;
038import org.hl7.fhir.r5.model.Bundle.BundleEntryComponent;
039import org.hl7.fhir.r5.model.Bundle.BundleLinkComponent;
040import org.hl7.fhir.r5.model.CanonicalResource;
041import org.hl7.fhir.r5.model.CodeableConcept;
042import org.hl7.fhir.r5.model.Coding;
043import org.hl7.fhir.r5.model.ContactPoint;
044import org.hl7.fhir.r5.model.ContactPoint.ContactPointSystem;
045import org.hl7.fhir.r5.model.DataType;
046import org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionBindingComponent;
047import org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent;
048import org.hl7.fhir.r5.model.Meta;
049import org.hl7.fhir.r5.model.OperationOutcome;
050import org.hl7.fhir.r5.model.OperationOutcome.IssueSeverity;
051import org.hl7.fhir.r5.model.OperationOutcome.OperationOutcomeIssueComponent;
052import org.hl7.fhir.r5.model.Reference;
053import org.hl7.fhir.r5.model.Resource;
054import org.hl7.fhir.r5.model.ResourceType;
055import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
056import org.hl7.fhir.utilities.Utilities;
057import org.hl7.fhir.utilities.xhtml.XhtmlComposer;
058
059/**
060 * Decoration utilities for various resource types
061 * @author Grahame
062 *
063 */
064public class ResourceUtilities {
065
066  public final static String FHIR_LANGUAGE = "urn:ietf:bcp:47";
067  private static JurisdictionLocales jl = new JurisdictionLocales(); 
068
069        public static boolean isAnError(OperationOutcome error) {
070                for (OperationOutcomeIssueComponent t : error.getIssue())
071                        if (t.getSeverity() == IssueSeverity.ERROR)
072                                return true;
073                        else if (t.getSeverity() == IssueSeverity.FATAL)
074                                return true;
075                return false;
076        }
077        
078        public static String getErrorDescription(OperationOutcome error) {  
079                if (error.hasText() && error.getText().hasDiv()) {
080                        return new XhtmlComposer(XhtmlComposer.XML).composePlainText(error.getText().getDiv());
081                }
082                
083                StringBuilder b = new StringBuilder();
084                for (OperationOutcomeIssueComponent t : error.getIssue()) {
085                        if (t.getSeverity() == IssueSeverity.ERROR) {
086                                b.append("Error:" +t.getDetails()+"\r\n");
087                        } else if (t.getSeverity() == IssueSeverity.FATAL) {
088                                b.append("Fatal:" +t.getDetails()+"\r\n");
089                        } else if (t.getSeverity() == IssueSeverity.WARNING) {
090                                b.append("Warning:" +t.getDetails()+"\r\n");
091                        } else if (t.getSeverity() == IssueSeverity.INFORMATION) {
092                                b.append("Information:" +t.getDetails()+"\r\n");
093                        }
094                }
095                return b.toString();
096  }
097
098  public static Resource getById(Bundle feed, ResourceType type, String reference) {
099    for (BundleEntryComponent item : feed.getEntry()) {
100      if (item.getResource().getId().equals(reference) && item.getResource().getResourceType() == type)
101        return item.getResource();
102    }
103    return null;
104  }
105
106  public static BundleEntryComponent getEntryById(Bundle feed, ResourceType type, String reference) {
107    for (BundleEntryComponent item : feed.getEntry()) {
108      if (item.getResource().getId().equals(reference) && item.getResource().getResourceType() == type)
109        return item;
110    }
111    return null;
112  }
113
114        public static String getLink(Bundle feed, String rel) {
115                for (BundleLinkComponent link : feed.getLink()) {
116                        if (link.getRelation().equals(rel))
117                                return link.getUrl();
118                }
119          return null;
120  }
121
122  public static Meta meta(Resource resource) {
123    if (!resource.hasMeta())
124      resource.setMeta(new Meta());
125    return resource.getMeta();
126  }
127  
128  public static Locale getLocale(CanonicalResource cr) {
129    return getLocale(cr.getLanguage(), cr.getJurisdiction());
130  }
131  
132  public static Locale getLocale(String lang, List<CodeableConcept> jurisdictions) {  
133    if (lang != null && lang.contains("-")) {
134      return new Locale(lang);        
135    }
136    for (CodeableConcept cc : jurisdictions) {
137      Locale locale = getLocale(lang, cc);
138      if (locale != null) {
139        return locale;
140      }
141    }
142    return null;
143  }
144
145
146  private static Locale getLocale(String lang, CodeableConcept cc) {
147    if (cc.hasCoding("http://unstats.un.org/unsd/methods/m49/m49.htm", "001")) {
148      return new Locale("en-US");
149    }
150    String c = cc.getCode("urn:iso:std:iso:3166");
151    if (c == null) {
152      return null;
153    }
154    String l = jl.get(c);
155    if (l == null) {
156      return null;
157    } else if (lang != null) {
158      return new Locale(lang+"-"+l.substring(l.indexOf("-")+1));
159    } else {
160      return new Locale(l);
161    }
162 }
163}