001package org.hl7.fhir.r5.model; 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 org.hl7.fhir.exceptions.FHIRException; 035 036/** 037 * in a language with helper classes, this would be a helper class (at least, the base exgtension helpers would be) 038 * @author Grahame 039 * 040 */ 041public class ExtensionHelper { 042 043 044 /** 045 * @param name the identity of the extension of interest 046 * @return true if the named extension is on this element. Will check modifier extensions too if appropriate 047 */ 048 public static boolean hasExtension(Element element, String name) { 049 if (element != null && element instanceof BackboneElement) 050 return hasExtension((BackboneElement) element, name); 051 052 if (name == null || element == null || !element.hasExtension()) 053 return false; 054 for (Extension e : element.getExtension()) { 055 if (name.equals(e.getUrl())) 056 return true; 057 } 058 return false; 059 } 060 061 /** 062 * @param name the identity of the extension of interest 063 * @return true if the named extension is on this element. Will check modifier extensions 064 */ 065 public static boolean hasExtension(BackboneElement element, String name) { 066 if (name == null || element == null || !(element.hasExtension() || element.hasModifierExtension())) 067 return false; 068 for (Extension e : element.getModifierExtension()) { 069 if (name.equals(e.getUrl())) 070 return true; 071 } 072 for (Extension e : element.getExtension()) { 073 if (name.equals(e.getUrl())) 074 return true; 075 } 076 return false; 077 } 078 079 080 /** 081 * @param name the identity of the extension of interest 082 * @return The extension, if on this element, else null. will check modifier extensions too, if appropriate 083 */ 084 public static Extension getExtension(Element element, String name) { 085 if (element != null && element instanceof BackboneElement) 086 return getExtension((BackboneElement) element, name); 087 088 if (name == null || element == null || !element.hasExtension()) 089 return null; 090 for (Extension e : element.getExtension()) { 091 if (name.equals(e.getUrl())) 092 return e; 093 } 094 return null; 095 } 096 097 /** 098 * @param name the identity of the extension of interest 099 * @return The extension, if on this element, else null. will check modifier extensions too, if appropriate 100 */ 101 public static Extension getExtension(DomainResource resource, String name) { 102 103 if (name == null || resource == null || !resource.hasExtension()) 104 return null; 105 for (Extension e : resource.getExtension()) { 106 if (name.equals(e.getUrl())) 107 return e; 108 } 109 return null; 110 } 111 112 /** 113 * @param name the identity of the extension of interest 114 * @return The extension, if on this element, else null. will check modifier extensions too 115 */ 116 public static Extension getExtension(BackboneElement element, String name) { 117 if (name == null || element == null) 118 return null; 119 if (element.hasModifierExtension()) { 120 for (Extension e : element.getModifierExtension()) { 121 if (name.equals(e.getUrl())) 122 return e; 123 } 124 } 125 if (element.hasExtension()) { 126 for (Extension e : element.getExtension()) { 127 if (name.equals(e.getUrl())) 128 return e; 129 } 130 } 131 return null; 132 } 133 134 /** 135 * set the value of an extension on the element. if value == null, make sure it doesn't exist 136 * 137 * @param element - the element to act on. Can also be a backbone element 138 * @param modifier - whether this is a modifier. Note that this is a definitional property of the extension; don't alternate 139 * @param uri - the identifier for the extension 140 * @param value - the value of the extension. Delete if this is null 141 * @throws Exception - if the modifier logic is incorrect 142 */ 143 public static void setExtension(Element element, boolean modifier, String uri, DataType value) throws FHIRException { 144 if (value == null) { 145 // deleting the extension 146 if (element instanceof BackboneElement) 147 for (Extension e : ((BackboneElement) element).getModifierExtension()) { 148 if (uri.equals(e.getUrl())) 149 ((BackboneElement) element).getModifierExtension().remove(e); 150 } 151 for (Extension e : element.getExtension()) { 152 if (uri.equals(e.getUrl())) 153 element.getExtension().remove(e); 154 } 155 } else { 156 // it would probably be easier to delete and then create, but this would re-order the extensions 157 // not that order matters, but we'll preserve it anyway 158 boolean found = false; 159 if (element instanceof BackboneElement) 160 for (Extension e : ((BackboneElement) element).getModifierExtension()) { 161 if (uri.equals(e.getUrl())) { 162 if (!modifier) 163 throw new FHIRException("Error adding extension \""+uri+"\": found an existing modifier extension, and the extension is not marked as a modifier"); 164 e.setValue(value); 165 found = true; 166 } 167 } 168 for (Extension e : element.getExtension()) { 169 if (uri.equals(e.getUrl())) { 170 if (modifier) 171 throw new FHIRException("Error adding extension \""+uri+"\": found an existing extension, and the extension is marked as a modifier"); 172 e.setValue(value); 173 found = true; 174 } 175 } 176 if (!found) { 177 Extension ex = new Extension().setUrl(uri).setValue(value); 178 if (modifier) { 179 if (!(element instanceof BackboneElement)) 180 throw new FHIRException("Error adding extension \""+uri+"\": extension is marked as a modifier, but element is not a backbone element"); 181 ((BackboneElement) element).getModifierExtension().add(ex); 182 183 } else { 184 element.getExtension().add(ex); 185 } 186 } 187 } 188 } 189 190 public static boolean hasExtensions(Element element) { 191 if (element instanceof BackboneElement) 192 return element.hasExtension() || ((BackboneElement) element).hasModifierExtension(); 193 else 194 return element.hasExtension(); 195 } 196 197 198}