001package org.hl7.fhir.dstu2.model;
002
003/*-
004 * #%L
005 * org.hl7.fhir.dstu2
006 * %%
007 * Copyright (C) 2014 - 2019 Health Level 7
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023
024import org.hl7.fhir.exceptions.FHIRException;
025
026/**
027 * in a language with helper classes, this would be a helper class (at least, the base exgtension helpers would be)
028 * @author Grahame
029 *
030 */
031public class ExtensionHelper {
032
033  
034  /**
035   * @param name the identity of the extension of interest
036   * @return true if the named extension is on this element. Will check modifier extensions too if appropriate
037   */
038  public static boolean hasExtension(Element element, String name) {
039        if (element != null && element instanceof BackboneElement) 
040                return hasExtension((BackboneElement) element, name);
041        
042    if (name == null || element == null || !element.hasExtension())
043      return false;
044    for (Extension e : element.getExtension()) {
045      if (name.equals(e.getUrl()))
046        return true;
047    }
048    return false;
049  }
050  
051  /**
052   * @param name the identity of the extension of interest
053   * @return true if the named extension is on this element. Will check modifier extensions
054   */
055  public static boolean hasExtension(BackboneElement element, String name) {
056    if (name == null || element == null || !(element.hasExtension() || element.hasModifierExtension()))
057      return false;
058    for (Extension e : element.getModifierExtension()) {
059      if (name.equals(e.getUrl()))
060        return true;
061    }
062    for (Extension e : element.getExtension()) {
063      if (name.equals(e.getUrl()))
064        return true;
065    }
066    return false;
067  }
068  
069  
070  /**
071   * @param name the identity of the extension of interest
072   * @return The extension, if on this element, else null. will check modifier extensions too, if appropriate
073   */
074  public static Extension getExtension(Element element, String name) {
075        if (element != null && element instanceof BackboneElement) 
076                return getExtension((BackboneElement) element, name);
077        
078    if (name == null || element == null || !element.hasExtension())
079      return null;
080    for (Extension e : element.getExtension()) {
081      if (name.equals(e.getUrl()))
082        return e;
083    }
084    return null;
085  }
086  
087  /**
088   * @param name the identity of the extension of interest
089   * @return The extension, if on this element, else null. will check modifier extensions too
090   */
091  public static Extension getExtension(BackboneElement element, String name) {
092    if (name == null || element == null || !element.hasExtension())
093      return null;
094    for (Extension e : element.getModifierExtension()) {
095      if (name.equals(e.getUrl()))
096        return e;
097    }
098    for (Extension e : element.getExtension()) {
099      if (name.equals(e.getUrl()))
100        return e;
101    }
102    return null;
103  }
104
105  /**
106   * set the value of an extension on the element. if value == null, make sure it doesn't exist
107   * 
108   * @param element - the element to act on. Can also be a backbone element 
109   * @param modifier - whether this is a modifier. Note that this is a definitional property of the extension; don't alternate
110   * @param uri - the identifier for the extension
111   * @param value - the value of the extension. Delete if this is null
112   * @- if the modifier logic is incorrect
113   */
114  public static void setExtension(Element element, boolean modifier, String uri, Type value) throws FHIRException {
115        if (value == null) {
116        // deleting the extension
117                if (element instanceof BackboneElement)
118                        for (Extension e : ((BackboneElement) element).getModifierExtension()) {
119                                if (uri.equals(e.getUrl()))
120                                        ((BackboneElement) element).getModifierExtension().remove(e);
121                        }
122                for (Extension e : element.getExtension()) {
123                        if (uri.equals(e.getUrl()))
124                                element.getExtension().remove(e);
125                }
126        } else {
127                // it would probably be easier to delete and then create, but this would re-order the extensions
128                // not that order matters, but we'll preserve it anyway
129                boolean found = false;
130                if (element instanceof BackboneElement)
131                        for (Extension e : ((BackboneElement) element).getModifierExtension()) {
132                                if (uri.equals(e.getUrl())) {
133                                        if (!modifier)
134                                                throw new FHIRException("Error adding extension \""+uri+"\": found an existing modifier extension, and the extension is not marked as a modifier");
135                                        e.setValue(value);
136                                        found = true;
137                                }
138                        }
139                for (Extension e : element.getExtension()) {
140                        if (uri.equals(e.getUrl())) {
141                                        if (modifier)
142                                                throw new FHIRException("Error adding extension \""+uri+"\": found an existing extension, and the extension is marked as a modifier");
143                                        e.setValue(value);
144                                        found = true;
145                        }
146                }
147                if (!found) {
148                        Extension ex = new Extension().setUrl(uri).setValue(value);
149                        if (modifier) {
150                        if (!(element instanceof BackboneElement))
151                                                throw new FHIRException("Error adding extension \""+uri+"\": extension is marked as a modifier, but element is not a backbone element");
152                                ((BackboneElement) element).getModifierExtension().add(ex);
153                                
154                        } else {
155                                element.getExtension().add(ex);
156                        }
157                }
158        }
159  }
160
161  public static boolean hasExtensions(Element element) {
162        if (element instanceof BackboneElement)
163                return element.hasExtension() || ((BackboneElement) element).hasModifierExtension();
164        else
165                return element.hasExtension();
166  }
167
168
169}