001package org.hl7.fhir.dstu3.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 || !element.hasExtension())
118      return null;
119    for (Extension e : element.getModifierExtension()) {
120      if (name.equals(e.getUrl()))
121        return e;
122    }
123    for (Extension e : element.getExtension()) {
124      if (name.equals(e.getUrl()))
125        return e;
126    }
127    return null;
128  }
129
130  /**
131   * set the value of an extension on the element. if value == null, make sure it doesn't exist
132   * 
133   * @param element - the element to act on. Can also be a backbone element 
134   * @param modifier - whether this is a modifier. Note that this is a definitional property of the extension; don't alternate
135   * @param uri - the identifier for the extension
136   * @param value - the value of the extension. Delete if this is null
137   * @throws Exception - if the modifier logic is incorrect
138   */
139  public static void setExtension(Element element, boolean modifier, String uri, Type value) throws FHIRException {
140        if (value == null) {
141        // deleting the extension
142                if (element instanceof BackboneElement)
143                        for (Extension e : ((BackboneElement) element).getModifierExtension()) {
144                                if (uri.equals(e.getUrl()))
145                                        ((BackboneElement) element).getModifierExtension().remove(e);
146                        }
147                for (Extension e : element.getExtension()) {
148                        if (uri.equals(e.getUrl()))
149                                element.getExtension().remove(e);
150                }
151        } else {
152                // it would probably be easier to delete and then create, but this would re-order the extensions
153                // not that order matters, but we'll preserve it anyway
154                boolean found = false;
155                if (element instanceof BackboneElement)
156                        for (Extension e : ((BackboneElement) element).getModifierExtension()) {
157                                if (uri.equals(e.getUrl())) {
158                                        if (!modifier)
159                                                throw new FHIRException("Error adding extension \""+uri+"\": found an existing modifier extension, and the extension is not marked as a modifier");
160                                        e.setValue(value);
161                                        found = true;
162                                }
163                        }
164                for (Extension e : element.getExtension()) {
165                        if (uri.equals(e.getUrl())) {
166                                        if (modifier)
167                                                throw new FHIRException("Error adding extension \""+uri+"\": found an existing extension, and the extension is marked as a modifier");
168                                        e.setValue(value);
169                                        found = true;
170                        }
171                }
172                if (!found) {
173                        Extension ex = new Extension().setUrl(uri).setValue(value);
174                        if (modifier) {
175                        if (!(element instanceof BackboneElement))
176                                                throw new FHIRException("Error adding extension \""+uri+"\": extension is marked as a modifier, but element is not a backbone element");
177                                ((BackboneElement) element).getModifierExtension().add(ex);
178                                
179                        } else {
180                                element.getExtension().add(ex);
181                        }
182                }
183        }
184  }
185
186  public static boolean hasExtensions(Element element) {
187        if (element instanceof BackboneElement)
188                return element.hasExtension() || ((BackboneElement) element).hasModifierExtension();
189        else
190                return element.hasExtension();
191  }
192
193
194}