001package org.hl7.fhir.r4.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; 034 035import org.hl7.fhir.r4.model.Bundle; 036import org.hl7.fhir.r4.model.Bundle.BundleEntryComponent; 037import org.hl7.fhir.r4.model.Bundle.BundleLinkComponent; 038import org.hl7.fhir.r4.model.CodeableConcept; 039import org.hl7.fhir.r4.model.Coding; 040import org.hl7.fhir.r4.model.ContactPoint; 041import org.hl7.fhir.r4.model.ContactPoint.ContactPointSystem; 042import org.hl7.fhir.r4.model.ElementDefinition.ElementDefinitionBindingComponent; 043import org.hl7.fhir.r4.model.ElementDefinition.TypeRefComponent; 044import org.hl7.fhir.r4.model.Meta; 045import org.hl7.fhir.r4.model.OperationOutcome; 046import org.hl7.fhir.r4.model.OperationOutcome.IssueSeverity; 047import org.hl7.fhir.r4.model.OperationOutcome.OperationOutcomeIssueComponent; 048import org.hl7.fhir.r4.model.Reference; 049import org.hl7.fhir.r4.model.Resource; 050import org.hl7.fhir.r4.model.ResourceType; 051import org.hl7.fhir.r4.model.Type; 052import org.hl7.fhir.utilities.CommaSeparatedStringBuilder; 053import org.hl7.fhir.utilities.Utilities; 054import org.hl7.fhir.utilities.xhtml.XhtmlComposer; 055 056/** 057 * Decoration utilities for various resource types 058 * @author Grahame 059 * 060 */ 061public class ResourceUtilities { 062 063 public final static String FHIR_LANGUAGE = "urn:ietf:bcp:47"; 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 StringBuilder b = new StringBuilder(); 079 for (OperationOutcomeIssueComponent t : error.getIssue()) 080 if (t.getSeverity() == IssueSeverity.ERROR) 081 b.append("Error:" + gen(t.getDetails())+"\r\n"); 082 else if (t.getSeverity() == IssueSeverity.FATAL) 083 b.append("Fatal:"+gen(t.getDetails())+"\r\n"); 084 else if (t.getSeverity() == IssueSeverity.WARNING) 085 b.append("Warning:" +gen(t.getDetails())+"\r\n"); 086 else if (t.getSeverity() == IssueSeverity.INFORMATION) 087 b.append("Information:" +gen(t.getDetails())+"\r\n"); 088 return b.toString(); 089 } 090 091 092 private static String gen(CodeableConcept details) { 093 if (details.hasText()) { 094 return details.getText(); 095 } 096 for (Coding c : details.getCoding()) { 097 if (c.hasDisplay()) { 098 return c.getDisplay(); 099 } 100 } 101 for (Coding c : details.getCoding()) { 102 if (c.hasCode()) { 103 return c.getCode(); 104 } 105 } 106 return "(no details supplied)"; 107 } 108 109 public static Resource getById(Bundle feed, ResourceType type, String reference) { 110 for (BundleEntryComponent item : feed.getEntry()) { 111 if (item.getResource().getId().equals(reference) && item.getResource().getResourceType() == type) 112 return item.getResource(); 113 } 114 return null; 115 } 116 117 public static BundleEntryComponent getEntryById(Bundle feed, ResourceType type, String reference) { 118 for (BundleEntryComponent item : feed.getEntry()) { 119 if (item.getResource().getId().equals(reference) && item.getResource().getResourceType() == type) 120 return item; 121 } 122 return null; 123 } 124 125 public static String getLink(Bundle feed, String rel) { 126 for (BundleLinkComponent link : feed.getLink()) { 127 if (link.getRelation().equals(rel)) 128 return link.getUrl(); 129 } 130 return null; 131 } 132 133 public static Meta meta(Resource resource) { 134 if (!resource.hasMeta()) 135 resource.setMeta(new Meta()); 136 return resource.getMeta(); 137 } 138 139// public static String representDataElementCollection(IWorkerContext context, Bundle bundle, boolean profileLink, String linkBase) { 140// StringBuilder b = new StringBuilder(); 141// DataElement common = showDECHeader(b, bundle); 142// b.append("<table class=\"grid\">\r\n"); 143// List<String> cols = chooseColumns(bundle, common, b, profileLink); 144// for (BundleEntryComponent e : bundle.getEntry()) { 145// DataElement de = (DataElement) e.getResource(); 146// renderDE(de, cols, b, profileLink, linkBase); 147// } 148// b.append("</table>\r\n"); 149// return b.toString(); 150// } 151// 152// 153// private static void renderDE(DataElement de, List<String> cols, StringBuilder b, boolean profileLink, String linkBase) { 154// b.append("<tr>"); 155// for (String col : cols) { 156// String v; 157// ElementDefinition dee = de.getElement().get(0); 158// if (col.equals("DataElement.name")) { 159// v = de.hasName() ? Utilities.escapeXml(de.getName()) : ""; 160// } else if (col.equals("DataElement.status")) { 161// v = de.hasStatusElement() ? de.getStatusElement().asStringValue() : ""; 162// } else if (col.equals("DataElement.code")) { 163// v = renderCoding(dee.getCode()); 164// } else if (col.equals("DataElement.type")) { 165// v = dee.hasType() ? Utilities.escapeXml(dee.getType().get(0).getCode()) : ""; 166// } else if (col.equals("DataElement.units")) { 167// v = renderDEUnits(ToolingExtensions.getAllowedUnits(dee)); 168// } else if (col.equals("DataElement.binding")) { 169// v = renderBinding(dee.getBinding()); 170// } else if (col.equals("DataElement.minValue")) { 171// v = ToolingExtensions.hasExtension(de, "http://hl7.org/fhir/StructureDefinition/minValue") ? Utilities.escapeXml(ToolingExtensions.readPrimitiveExtension(de, "http://hl7.org/fhir/StructureDefinition/minValue").asStringValue()) : ""; 172// } else if (col.equals("DataElement.maxValue")) { 173// v = ToolingExtensions.hasExtension(de, "http://hl7.org/fhir/StructureDefinition/maxValue") ? Utilities.escapeXml(ToolingExtensions.readPrimitiveExtension(de, "http://hl7.org/fhir/StructureDefinition/maxValue").asStringValue()) : ""; 174// } else if (col.equals("DataElement.maxLength")) { 175// v = ToolingExtensions.hasExtension(de, "http://hl7.org/fhir/StructureDefinition/maxLength") ? Utilities.escapeXml(ToolingExtensions.readPrimitiveExtension(de, "http://hl7.org/fhir/StructureDefinition/maxLength").asStringValue()) : ""; 176// } else if (col.equals("DataElement.mask")) { 177// v = ToolingExtensions.hasExtension(de, "http://hl7.org/fhir/StructureDefinition/mask") ? Utilities.escapeXml(ToolingExtensions.readPrimitiveExtension(de, "http://hl7.org/fhir/StructureDefinition/mask").asStringValue()) : ""; 178// } else 179// throw new Error("Unknown column name: "+col); 180// 181// b.append("<td>"+v+"</td>"); 182// } 183// if (profileLink) { 184// b.append("<td><a href=\""+linkBase+"-"+de.getId()+".html\">Profile</a>, <a href=\"http://www.opencem.org/#/20140917/Intermountain/"+de.getId()+"\">CEM</a>"); 185// if (ToolingExtensions.hasExtension(de, ToolingExtensions.EXT_CIMI_REFERENCE)) 186// b.append(", <a href=\""+ToolingExtensions.readStringExtension(de, ToolingExtensions.EXT_CIMI_REFERENCE)+"\">CIMI</a>"); 187// b.append("</td>"); 188// } 189// b.append("</tr>\r\n"); 190// } 191 192 193 194 private static String renderBinding(ElementDefinitionBindingComponent binding) { 195 // TODO Auto-generated method stub 196 return null; 197 } 198 199 private static String renderDEUnits(Type units) { 200 if (units == null || units.isEmpty()) 201 return ""; 202 if (units instanceof CodeableConcept) 203 return renderCodeable((CodeableConcept) units); 204 else 205 return "<a href=\""+Utilities.escapeXml(((Reference) units).getReference())+"\">"+Utilities.escapeXml(((Reference) units).getReference())+"</a>"; 206 207 } 208 209 private static String renderCodeable(CodeableConcept units) { 210 if (units == null || units.isEmpty()) 211 return ""; 212 String v = renderCoding(units.getCoding()); 213 if (units.hasText()) 214 v = v + " " +Utilities.escapeXml(units.getText()); 215 return v; 216 } 217 218 private static String renderCoding(List<Coding> codes) { 219 CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder(); 220 for (Coding c : codes) 221 b.append(renderCoding(c)); 222 return b.toString(); 223 } 224 225 private static String renderCoding(Coding code) { 226 if (code == null || code.isEmpty()) 227 return ""; 228 else 229 return "<span title=\""+Utilities.escapeXml(code.getSystem())+"\">"+Utilities.escapeXml(code.getCode())+"</span>"; 230 } 231 232// private static List<String> chooseColumns(Bundle bundle, DataElement common, StringBuilder b, boolean profileLink) { 233// b.append("<tr>"); 234// List<String> results = new ArrayList<String>(); 235// results.add("DataElement.name"); 236// b.append("<td width=\"250\"><b>Name</b></td>"); 237// if (!common.hasStatus()) { 238// results.add("DataElement.status"); 239// b.append("<td><b>Status</b></td>"); 240// } 241// if (hasCode(bundle)) { 242// results.add("DataElement.code"); 243// b.append("<td><b>Code</b></td>"); 244// } 245// if (!common.getElement().get(0).hasType() && hasType(bundle)) { 246// results.add("DataElement.type"); 247// b.append("<td><b>Type</b></td>"); 248// } 249// if (hasUnits(bundle)) { 250// results.add("DataElement.units"); 251// b.append("<td><b>Units</b></td>"); 252// } 253// if (hasBinding(bundle)) { 254// results.add("DataElement.binding"); 255// b.append("<td><b>Binding</b></td>"); 256// } 257// if (hasExtension(bundle, "http://hl7.org/fhir/StructureDefinition/minValue")) { 258// results.add("DataElement.minValue"); 259// b.append("<td><b>Min Value</b></td>"); 260// } 261// if (hasExtension(bundle, "http://hl7.org/fhir/StructureDefinition/maxValue")) { 262// results.add("DataElement.maxValue"); 263// b.append("<td><b>Max Value</b></td>"); 264// } 265// if (hasExtension(bundle, "http://hl7.org/fhir/StructureDefinition/maxLength")) { 266// results.add("DataElement.maxLength"); 267// b.append("<td><b>Max Length</b></td>"); 268// } 269// if (hasExtension(bundle, "http://hl7.org/fhir/StructureDefinition/mask")) { 270// results.add("DataElement.mask"); 271// b.append("<td><b>Mask</b></td>"); 272// } 273// if (profileLink) 274// b.append("<td><b>Links</b></td>"); 275// b.append("</tr>\r\n"); 276// return results; 277// } 278// 279// private static boolean hasExtension(Bundle bundle, String url) { 280// for (BundleEntryComponent e : bundle.getEntry()) { 281// DataElement de = (DataElement) e.getResource(); 282// if (ToolingExtensions.hasExtension(de, url)) 283// return true; 284// } 285// return false; 286// } 287// 288// private static boolean hasBinding(Bundle bundle) { 289// for (BundleEntryComponent e : bundle.getEntry()) { 290// DataElement de = (DataElement) e.getResource(); 291// if (de.getElement().get(0).hasBinding()) 292// return true; 293// } 294// return false; 295// } 296// 297// private static boolean hasCode(Bundle bundle) { 298// for (BundleEntryComponent e : bundle.getEntry()) { 299// DataElement de = (DataElement) e.getResource(); 300// if (de.getElement().get(0).hasCode()) 301// return true; 302// } 303// return false; 304// } 305// 306// private static boolean hasType(Bundle bundle) { 307// for (BundleEntryComponent e : bundle.getEntry()) { 308// DataElement de = (DataElement) e.getResource(); 309// if (de.getElement().get(0).hasType()) 310// return true; 311// } 312// return false; 313// } 314// 315// private static boolean hasUnits(Bundle bundle) { 316// for (BundleEntryComponent e : bundle.getEntry()) { 317// DataElement de = (DataElement) e.getResource(); 318// if (ToolingExtensions.getAllowedUnits(de.getElement().get(0)) != null) 319// return true; 320// } 321// return false; 322// } 323// 324// private static DataElement showDECHeader(StringBuilder b, Bundle bundle) { 325// DataElement meta = new DataElement(); 326// DataElement prototype = (DataElement) bundle.getEntry().get(0).getResource(); 327// meta.setPublisher(prototype.getPublisher()); 328// meta.getContact().addAll(prototype.getContact()); 329// meta.setStatus(prototype.getStatus()); 330// meta.setDate(prototype.getDate()); 331// meta.addElement().getType().addAll(prototype.getElement().get(0).getType()); 332// 333// for (BundleEntryComponent e : bundle.getEntry()) { 334// DataElement de = (DataElement) e.getResource(); 335// if (!Base.compareDeep(de.getPublisherElement(), meta.getPublisherElement(), false)) 336// meta.setPublisherElement(null); 337// if (!Base.compareDeep(de.getContact(), meta.getContact(), false)) 338// meta.getContact().clear(); 339// if (!Base.compareDeep(de.getStatusElement(), meta.getStatusElement(), false)) 340// meta.setStatusElement(null); 341// if (!Base.compareDeep(de.getDateElement(), meta.getDateElement(), false)) 342// meta.setDateElement(null); 343// if (!Base.compareDeep(de.getElement().get(0).getType(), meta.getElement().get(0).getType(), false)) 344// meta.getElement().get(0).getType().clear(); 345// } 346// if (meta.hasPublisher() || meta.hasContact() || meta.hasStatus() || meta.hasDate() /* || meta.hasType() */) { 347// b.append("<table class=\"grid\">\r\n"); 348// if (meta.hasPublisher()) 349// b.append("<tr><td>Publisher:</td><td>"+meta.getPublisher()+"</td></tr>\r\n"); 350// if (meta.hasContact()) { 351// b.append("<tr><td>Contacts:</td><td>"); 352// boolean firsti = true; 353// for (ContactDetail c : meta.getContact()) { 354// if (firsti) 355// firsti = false; 356// else 357// b.append("<br/>"); 358// if (c.hasName()) 359// b.append(Utilities.escapeXml(c.getName())+": "); 360// boolean first = true; 361// for (ContactPoint cp : c.getTelecom()) { 362// if (first) 363// first = false; 364// else 365// b.append(", "); 366// renderContactPoint(b, cp); 367// } 368// } 369// b.append("</td></tr>\r\n"); 370// } 371// if (meta.hasStatus()) 372// b.append("<tr><td>Status:</td><td>"+meta.getStatus().toString()+"</td></tr>\r\n"); 373// if (meta.hasDate()) 374// b.append("<tr><td>Date:</td><td>"+meta.getDateElement().asStringValue()+"</td></tr>\r\n"); 375// if (meta.getElement().get(0).hasType()) 376// b.append("<tr><td>Type:</td><td>"+renderType(meta.getElement().get(0).getType())+"</td></tr>\r\n"); 377// b.append("</table>\r\n"); 378// } 379// return meta; 380// } 381 382 private static String renderType(List<TypeRefComponent> type) { 383 if (type == null || type.isEmpty()) 384 return ""; 385 CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder(); 386 for (TypeRefComponent c : type) 387 b.append(renderType(c)); 388 return b.toString(); 389 } 390 391 private static String renderType(TypeRefComponent type) { 392 if (type == null || type.isEmpty()) 393 return ""; 394 return type.getWorkingCode(); 395 } 396 397 public static void renderContactPoint(StringBuilder b, ContactPoint cp) { 398 if (cp != null && !cp.isEmpty()) { 399 if (cp.getSystem() == ContactPointSystem.EMAIL) 400 b.append("<a href=\"mailto:"+cp.getValue()+"\">"+cp.getValue()+"</a>"); 401 else if (cp.getSystem() == ContactPointSystem.FAX) 402 b.append("Fax: "+cp.getValue()); 403 else if (cp.getSystem() == ContactPointSystem.OTHER) 404 b.append("<a href=\""+cp.getValue()+"\">"+cp.getValue()+"</a>"); 405 else 406 b.append(cp.getValue()); 407 } 408 } 409}