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.Set;
036
037import org.hl7.fhir.r5.model.Coding;
038import org.hl7.fhir.r5.model.Base;
039import org.hl7.fhir.r5.model.Bundle;
040import org.hl7.fhir.r5.model.Bundle.BundleEntryComponent;
041import org.hl7.fhir.r5.model.Bundle.BundleLinkComponent;
042import org.hl7.fhir.r5.model.CanonicalResource;
043import org.hl7.fhir.r5.model.CodeableConcept;
044import org.hl7.fhir.r5.model.Meta;
045import org.hl7.fhir.r5.model.OperationOutcome;
046import org.hl7.fhir.r5.model.OperationOutcome.IssueSeverity;
047import org.hl7.fhir.r5.model.OperationOutcome.OperationOutcomeIssueComponent;
048import org.hl7.fhir.r5.model.Property;
049import org.hl7.fhir.r5.model.Resource;
050import org.hl7.fhir.r5.model.ResourceType;
051import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
052import org.hl7.fhir.utilities.Utilities;
053import org.hl7.fhir.utilities.xhtml.XhtmlComposer;
054
055/**
056 * Decoration utilities for various resource types
057 * @author Grahame
058 *
059 */
060public class ResourceUtilities {
061
062  public final static String FHIR_LANGUAGE = "urn:ietf:bcp:47";
063  private static JurisdictionLocales jl = new JurisdictionLocales(); 
064
065        public static boolean isAnError(OperationOutcome error) {
066                for (OperationOutcomeIssueComponent t : error.getIssue())
067                        if (t.getSeverity() == IssueSeverity.ERROR)
068                                return true;
069                        else if (t.getSeverity() == IssueSeverity.FATAL)
070                                return true;
071                return false;
072        }
073        
074        public static String getErrorDescription(OperationOutcome error) {  
075                if (error.hasText() && error.getText().hasDiv()) {
076                        return new XhtmlComposer(XhtmlComposer.XML).composePlainText(error.getText().getDiv());
077                }
078                
079                StringBuilder b = new StringBuilder();
080                for (OperationOutcomeIssueComponent t : error.getIssue()) {
081                        if (t.getSeverity() == IssueSeverity.ERROR) {
082                                b.append("Error: " +gen(t.getDetails())+"\r\n");
083                        } else if (t.getSeverity() == IssueSeverity.FATAL) {
084                                b.append("Fatal: " +gen(t.getDetails())+"\r\n");
085                        } else if (t.getSeverity() == IssueSeverity.WARNING) {
086                                b.append("Warning: " +gen(t.getDetails())+"\r\n");
087                        } else if (t.getSeverity() == IssueSeverity.INFORMATION) {
088                                b.append("Information: " +gen(t.getDetails())+"\r\n");
089                        }
090                }
091                return b.toString();
092  }
093
094
095  private static String gen(CodeableConcept details) {
096    if (details.hasText()) {
097      return details.getText();
098    }
099    for (Coding c : details.getCoding()) {
100      if (c.hasDisplay()) {
101        return c.getDisplay();
102      }
103    }
104    for (Coding c : details.getCoding()) {
105      if (c.hasCode()) {
106        return c.getCode();
107      }
108    }
109    return "(no details supplied)";   
110  }
111  
112  public static Resource getById(Bundle feed, ResourceType type, String reference) {
113    for (BundleEntryComponent item : feed.getEntry()) {
114      if (item.getResource().getId().equals(reference) && item.getResource().getResourceType() == type)
115        return item.getResource();
116    }
117    return null;
118  }
119
120  public static BundleEntryComponent getEntryById(Bundle feed, ResourceType type, String reference) {
121    for (BundleEntryComponent item : feed.getEntry()) {
122      if (item.getResource().getId().equals(reference) && item.getResource().getResourceType() == type)
123        return item;
124    }
125    return null;
126  }
127
128        public static String getLink(Bundle feed, String rel) {
129                for (BundleLinkComponent link : feed.getLink()) {
130                        if (link.getRelation().toCode().equals(rel))
131                                return link.getUrl();
132                }
133          return null;
134  }
135
136  public static Meta meta(Resource resource) {
137    if (!resource.hasMeta())
138      resource.setMeta(new Meta());
139    return resource.getMeta();
140  }
141  
142  public static Locale getLocale(CanonicalResource cr) {
143    return getLocale(cr.getLanguage(), cr.getJurisdiction());
144  }
145  
146  public static Locale getLocale(String lang, List<CodeableConcept> jurisdictions) {  
147    if (lang != null && lang.contains("-")) {
148      return new Locale(lang);        
149    }
150    for (CodeableConcept cc : jurisdictions) {
151      Locale locale = getLocale(lang, cc);
152      if (locale != null) {
153        return locale;
154      }
155    }
156    return null;
157  }
158
159
160  private static Locale getLocale(String lang, CodeableConcept cc) {
161    if (cc.hasCoding("http://unstats.un.org/unsd/methods/m49/m49.htm", "001")) {
162      return new Locale("en-US");
163    }
164    String c = cc.getCode("urn:iso:std:iso:3166");
165    if (c == null) {
166      return null;
167    }
168    String l = jl.get(c);
169    if (l == null) {
170      return null;
171    } else if (lang != null) {
172      return new Locale(lang+"-"+l.substring(l.indexOf("-")+1));
173    } else {
174      return new Locale(l);
175    }
176 }
177
178  public static String listUrls(List<? extends CanonicalResource> list) {
179    CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
180    for (CanonicalResource t : list) {
181      b.append(t.getVUrl());
182    }
183    return b.toString();
184  }
185
186  public static String listStrings(Set<String> set) {
187    List<String> list = Utilities.sorted(set);
188    CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
189    for (String s : list) {
190      b.append(s);
191    }
192    return b.toString();
193  }
194
195  public static boolean hasURL(String uri, Resource src) {
196    for (Property p : src.children()) {
197      if (hasURL(uri, p)) {
198        return true;
199      }
200    }
201    return false;
202  }
203
204  private static boolean hasURL(String uri, Property p) {
205    for (Base b : p.getValues()) {
206      if (b.isPrimitive()) {
207        return uri.equals(b.primitiveValue());
208      } else {
209        for (Property c : b.children()) {
210          if (hasURL(uri, c)) {
211            return true;
212          }
213        }
214      }
215    }
216    return false;
217  }
218}