001package org.hl7.fhir.dstu2016may.metamodel;
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.dstu2016may.formats.FormatUtilities;
035import org.hl7.fhir.dstu2016may.model.ElementDefinition;
036import org.hl7.fhir.dstu2016may.model.ElementDefinition.TypeRefComponent;
037import org.hl7.fhir.dstu2016may.model.StructureDefinition;
038import org.hl7.fhir.dstu2016may.model.StructureDefinition.StructureDefinitionKind;
039import org.hl7.fhir.dstu2016may.utils.IWorkerContext;
040import org.hl7.fhir.dstu2016may.utils.ToolingExtensions;
041
042public class Property {
043
044        private IWorkerContext context;
045        private ElementDefinition definition;
046        private StructureDefinition structure;
047        private Boolean canBePrimitive; 
048
049        public Property(IWorkerContext context, ElementDefinition definition, StructureDefinition structure) {
050                this.context = context;
051                this.definition = definition;
052                this.structure = structure;
053        }
054
055        public String getName() {
056                return definition.getPath().substring(definition.getPath().lastIndexOf(".")+1);
057        }
058
059        public ElementDefinition getDefinition() {
060                return definition;
061        }
062
063        public String getType() {
064                if (definition.getType().size() == 0)
065                        return null;
066                else if (definition.getType().size() > 1) {
067                        String tn = definition.getType().get(0).getCode();
068                        for (int i = 1; i < definition.getType().size(); i++) {
069                                if (!tn.equals(definition.getType().get(i).getCode()))
070                        throw new Error("logic error, gettype when types > 1");
071                        }
072                        return tn;
073                } else
074                        return definition.getType().get(0).getCode();
075        }
076
077  public String getType(String elementName) {
078    if (definition.getType().size() == 0)
079      return null;
080    else if (definition.getType().size() > 1) {
081      String t = definition.getType().get(0).getCode();
082      boolean all = true;
083      for (TypeRefComponent tr : definition.getType()) {
084        if (!t.equals(tr.getCode()))
085          all = false;
086      }
087      if (all)
088        return t;
089      String tail = definition.getPath().substring(definition.getPath().lastIndexOf(".")+1);
090      if (tail.endsWith("[x]") && elementName != null && elementName.startsWith(tail.substring(0, tail.length()-3))) {
091        String name = elementName.substring(tail.length()-3);
092        return ParserBase.isPrimitive(lowFirst(name)) ? lowFirst(name) : name;        
093      } else
094        throw new Error("logic error, gettype when types > 1, name mismatch for "+elementName+" on at "+definition.getPath());
095    } else if (definition.getType().get(0).getCode() == null) {
096      return structure.getId();
097    } else
098      return definition.getType().get(0).getCode();
099  }
100
101  public boolean hasType(String elementName) {
102    if (definition.getType().size() == 0)
103      return false;
104    else if (definition.getType().size() > 1) {
105      String t = definition.getType().get(0).getCode();
106      boolean all = true;
107      for (TypeRefComponent tr : definition.getType()) {
108        if (!t.equals(tr.getCode()))
109          all = false;
110      }
111      if (all)
112        return true;
113      String tail = definition.getPath().substring(definition.getPath().lastIndexOf(".")+1);
114      if (tail.endsWith("[x]") && elementName.startsWith(tail.substring(0, tail.length()-3))) {
115        String name = elementName.substring(tail.length()-3);
116        return true;        
117      } else
118        return false;
119    } else
120      return true;
121  }
122
123        public StructureDefinition getStructure() {
124                return structure;
125        }
126
127        public boolean isPrimitive(String name) {
128    return ParserBase.isPrimitive(getType(name));
129        }
130
131        private String lowFirst(String t) {
132                return t.substring(0, 1).toLowerCase()+t.substring(1);
133        }
134
135        public boolean isResource() {
136                return definition.getType().size() == 1 && ("Resource".equals(definition.getType().get(0).getCode()) || "DomainResource".equals(definition.getType().get(0).getCode()));
137        }
138
139        public boolean isList() {
140          return !definition.getMax().equals("1");
141        }
142
143  public String getScopedPropertyName() {
144    return definition.getBase().getPath();
145  }
146
147  public String getNamespace() {
148    if (ToolingExtensions.hasExtension(definition, "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace"))
149      return ToolingExtensions.readStringExtension(definition, "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace");
150    if (ToolingExtensions.hasExtension(structure, "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace"))
151      return ToolingExtensions.readStringExtension(structure, "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace");
152    return FormatUtilities.FHIR_NS;
153  }
154
155        public boolean IsLogicalAndHasPrimitiveValue(String name) {
156                if (canBePrimitive!= null)
157                        return canBePrimitive;
158                
159                canBePrimitive = false;
160        if (structure.getKind() != StructureDefinitionKind.LOGICAL)
161                return false;
162        if (!hasType(name))
163                return false;
164        StructureDefinition sd = context.fetchResource(StructureDefinition.class, structure.getUrl().substring(0, structure.getUrl().lastIndexOf("/")+1)+getType(name));
165        if (sd == null || sd.getKind() != StructureDefinitionKind.LOGICAL)
166                return false;
167        for (ElementDefinition ed : sd.getSnapshot().getElement()) {
168                if (ed.getPath().equals(sd.getId()+".value") && ed.getType().size() == 1 && ParserBase.isPrimitive(ed.getType().get(0).getCode())) {
169                        canBePrimitive = true;
170                        return true;
171                }
172        }
173        return false;
174        }
175
176  public boolean isChoice() {
177    if (definition.getType().size() <= 1)
178      return false;
179    String tn = definition.getType().get(0).getCode();
180    for (int i = 1; i < definition.getType().size(); i++) 
181      if (!definition.getType().get(i).getCode().equals(tn))
182        return true;
183    return false;
184  }
185
186
187
188}