001package org.hl7.fhir.r5.model;
002
003import java.util.ArrayList;
004import java.util.List;
005
006/*
007  Copyright (c) 2011+, HL7, Inc.
008  All rights reserved.
009  
010  Redistribution and use in source and binary forms, with or without modification, 
011  are permitted provided that the following conditions are met:
012    
013   * Redistributions of source code must retain the above copyright notice, this 
014     list of conditions and the following disclaimer.
015   * Redistributions in binary form must reproduce the above copyright notice, 
016     this list of conditions and the following disclaimer in the documentation 
017     and/or other materials provided with the distribution.
018   * Neither the name of HL7 nor the names of its contributors may be used to 
019     endorse or promote products derived from this software without specific 
020     prior written permission.
021  
022  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
023  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
024  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
025  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
026  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
027  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
028  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
029  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
030  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
031  POSSIBILITY OF SUCH DAMAGE.
032  
033 */
034
035
036
037import org.hl7.fhir.exceptions.FHIRException;
038
039/**
040 * in a language with helper classes, this would be a helper class (at least, the base exgtension helpers would be)
041 * @author Grahame
042 *
043 */
044public class ExtensionHelper {
045
046  
047  /**
048   * @param name the identity of the extension of interest
049   * @return true if the named extension is on this element. Will check modifier extensions too if appropriate
050   */
051  public static boolean hasExtension(Element element, String name) {
052        if (element != null && element instanceof BackboneElement) 
053                return hasExtension((BackboneElement) element, name);
054        
055    if (name == null || element == null || !element.hasExtension())
056      return false;
057    for (Extension e : element.getExtension()) {
058      if (name.equals(e.getUrl()))
059        return true;
060    }
061    return false;
062  }
063  
064  /**
065   * @param name the identity of the extension of interest
066   * @return true if the named extension is on this element. Will check modifier extensions
067   */
068  public static boolean hasExtension(BackboneElement element, String name) {
069    if (name == null || element == null || !(element.hasExtension() || element.hasModifierExtension()))
070      return false;
071    for (Extension e : element.getModifierExtension()) {
072      if (name.equals(e.getUrl()))
073        return true;
074    }
075    for (Extension e : element.getExtension()) {
076      if (name.equals(e.getUrl()))
077        return true;
078    }
079    return false;
080  }
081  
082  
083  /**
084   * @param name the identity of the extension of interest
085   * @return The extension, if on this element, else null. will check modifier extensions too, if appropriate
086   */
087  public static Extension getExtension(Element element, String name) {
088        if (element != null && element instanceof BackboneElement) 
089                return getExtension((BackboneElement) element, name);
090        
091    if (name == null || element == null || !element.hasExtension())
092      return null;
093    for (Extension e : element.getExtension()) {
094      if (name.equals(e.getUrl()))
095        return e;
096    }
097    return null;
098  }
099  
100  /**
101   * @param name the identity of the extension of interest
102   * @return The extension, if on this element, else null. will check modifier extensions too, if appropriate
103   */
104  public static List<Extension> getExtensionList(Element element, String name) {
105    List<Extension> res = new ArrayList<>();
106    
107    if (element != null && element instanceof BackboneElement) { 
108      res.addAll(getExtensionList((BackboneElement) element, name));
109    } else if (name != null && element != null) {
110      for (Extension e : element.getExtension()) {
111        if (name.equals(e.getUrl())) {
112          res.add(e);
113        }
114      }
115    }
116    return res;
117  }
118  
119  /**
120   * @param name the identity of the extension of interest
121   * @return The extension, if on this element, else null. will check modifier extensions too, if appropriate
122   */
123  public static Extension getExtension(DomainResource resource, String name) {
124    
125    if (name == null || resource == null || !resource.hasExtension())
126      return null;
127    for (Extension e : resource.getExtension()) {
128      if (name.equals(e.getUrl()))
129        return e;
130    }
131    return null;
132  }
133  
134  /**
135   * @param name the identity of the extension of interest
136   * @return The extension, if on this element, else null. will check modifier extensions too
137   */
138  public static Extension getExtension(BackboneElement element, String name) {
139    if (name == null || element == null)
140      return null;
141    if (element.hasModifierExtension()) {
142      for (Extension e : element.getModifierExtension()) {
143        if (name.equals(e.getUrl()))
144          return e;
145      }
146    }
147    if (element.hasExtension()) {
148      for (Extension e : element.getExtension()) {
149        if (name.equals(e.getUrl()))
150          return e;
151      }
152    }
153    return null;
154  }
155
156  /**
157   * @param name the identity of the extension of interest
158   * @return The extension, if on this element, else null. will check modifier extensions too
159   */
160  public static List<Extension> getExtensionList(BackboneElement element, String name) {
161    List<Extension> res = new ArrayList<>();
162
163    if (name != null && element != null) {
164      if (element.hasModifierExtension()) {
165        for (Extension e : element.getModifierExtension()) {
166          if (name.equals(e.getUrl()))
167            res.add(e);
168        }
169      }
170      if (element.hasExtension()) {
171        for (Extension e : element.getExtension()) {
172          if (name.equals(e.getUrl()))
173            res.add(e);
174        }
175      }
176    }
177    return res;
178  }
179
180  
181  /**
182   * set the value of an extension on the element. if value == null, make sure it doesn't exist
183   * 
184   * @param element - the element to act on. Can also be a backbone element 
185   * @param modifier - whether this is a modifier. Note that this is a definitional property of the extension; don't alternate
186   * @param uri - the identifier for the extension
187   * @param value - the value of the extension. Delete if this is null
188   * @throws Exception - if the modifier logic is incorrect
189   */
190  public static void setExtension(Element element, boolean modifier, String uri, DataType value) throws FHIRException {
191        if (value == null) {
192        // deleting the extension
193                if (element instanceof BackboneElement)
194                        for (Extension e : ((BackboneElement) element).getModifierExtension()) {
195                                if (uri.equals(e.getUrl()))
196                                        ((BackboneElement) element).getModifierExtension().remove(e);
197                        }
198                for (Extension e : element.getExtension()) {
199                        if (uri.equals(e.getUrl()))
200                                element.getExtension().remove(e);
201                }
202        } else {
203                // it would probably be easier to delete and then create, but this would re-order the extensions
204                // not that order matters, but we'll preserve it anyway
205                boolean found = false;
206                if (element instanceof BackboneElement)
207                        for (Extension e : ((BackboneElement) element).getModifierExtension()) {
208                                if (uri.equals(e.getUrl())) {
209                                        if (!modifier)
210                                                throw new FHIRException("Error adding extension \""+uri+"\": found an existing modifier extension, and the extension is not marked as a modifier");
211                                        e.setValue(value);
212                                        found = true;
213                                }
214                        }
215                for (Extension e : element.getExtension()) {
216                        if (uri.equals(e.getUrl())) {
217                                        if (modifier)
218                                                throw new FHIRException("Error adding extension \""+uri+"\": found an existing extension, and the extension is marked as a modifier");
219                                        e.setValue(value);
220                                        found = true;
221                        }
222                }
223                if (!found) {
224                        Extension ex = new Extension().setUrl(uri).setValue(value);
225                        if (modifier) {
226                        if (!(element instanceof BackboneElement))
227                                                throw new FHIRException("Error adding extension \""+uri+"\": extension is marked as a modifier, but element is not a backbone element");
228                                ((BackboneElement) element).getModifierExtension().add(ex);
229                                
230                        } else {
231                                element.getExtension().add(ex);
232                        }
233                }
234        }
235  }
236
237  public static boolean hasExtensions(Element element) {
238        if (element instanceof BackboneElement)
239                return element.hasExtension() || ((BackboneElement) element).hasModifierExtension();
240        else
241                return element.hasExtension();
242  }
243
244
245}