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