001package org.hl7.fhir.r5.profilemodel;
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
031import java.util.ArrayList;
032import java.util.List;
033
034import org.hl7.fhir.r5.conformance.profile.ProfileUtilities;
035import org.hl7.fhir.r5.model.CanonicalType;
036import org.hl7.fhir.r5.model.ElementDefinition;
037import org.hl7.fhir.r5.model.ElementDefinition.SlicingRules;
038import org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent;
039import org.hl7.fhir.r5.profilemodel.PEDefinition.PEDefinitionElementMode;
040import org.hl7.fhir.utilities.Utilities;
041import org.hl7.fhir.r5.model.StructureDefinition;
042
043public class PEDefinitionExtension extends PEDefinition {
044
045  private StructureDefinition extension;
046  private ElementDefinition sliceDefinition;
047  private ElementDefinition eed;
048  private ElementDefinition ved;
049
050  public PEDefinitionExtension(PEBuilder builder, String name, StructureDefinition profile, ElementDefinition definition, ElementDefinition sliceDefinition, StructureDefinition extension, String ppath) {
051    super(builder, name, profile, definition, ppath);
052    this.sliceDefinition = sliceDefinition;
053    this.extension= extension;
054    eed = extension.getSnapshot().getElementByPath("Extension.extension");
055    ved = extension.getSnapshot().getElementByPath("Extension.value[x]"); 
056  }
057
058  @Override
059  public void listTypes(List<PEType> types) {
060    if (ved.isRequired() || eed.isProhibited()) {
061      for (TypeRefComponent t : ved.getType()) {
062        if (t.hasProfile()) {
063          for (CanonicalType u : t.getProfile()) {
064            types.add(builder.makeType(t, u));
065          }
066        } else {
067          types.add(builder.makeType(t.getWorkingCode()));
068        }
069      }
070    } else if (ProfileUtilities.isComplexExtension(extension)) {
071      types.add(builder.makeType(extension.getName(), extension.getUrl()));
072    } else {
073      types.add(builder.makeType("Extension"));
074    }
075  }
076
077  @Override
078  protected void makeChildren(String typeUrl, List<PEDefinition> children, boolean allFixed) {
079    if (ved.isRequired() || eed.isProhibited()) {
080      children.addAll(builder.listChildren(allFixed, this, extension, ved, typeUrl));
081    } else {
082      List<PEDefinition> slices = builder.listSlices(extension, eed, this);
083      if (eed.getSlicing().getRules() != SlicingRules.CLOSED) {
084        children.addAll(builder.listChildren(allFixed, this, extension, eed, "http://hl7.org/fhir/StructureDefinition/Extension", "value[x]", "url"));
085        if (!children.isEmpty()) {
086           children.get(0).setSlices(slices);
087        }
088      }      
089      children.addAll(slices);
090    }
091  }
092
093  @Override
094  public String fhirpath() {
095    if (ved.isRequired() || eed.isProhibited()) {
096      return "extension('"+extension.getUrl()+"').value";
097    } else {
098      return "extension('"+extension.getUrl()+"')";      
099    }
100  }
101
102  public PEDefinitionElementMode mode() {
103    return PEDefinitionElementMode.Extension;
104  }
105  
106  public List<PEDefinition> directChildren(boolean allFixed) {
107    List<PEDefinition> children = new ArrayList<>();
108    children.addAll(builder.listChildren(allFixed, this, extension, extension.getSnapshot().getElementFirstRep(), "http://hl7.org/fhir/StructureDefinition/Extension"));
109    return children;
110  }
111  
112
113  public boolean isExtension() {
114    return true;
115  }
116
117  
118}