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