001package ca.uhn.fhir.context;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2016 University Health Network
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 */
022import static org.apache.commons.lang3.StringUtils.isBlank;
023
024import java.util.Map;
025
026import org.hl7.fhir.instance.model.api.IBase;
027import org.hl7.fhir.instance.model.api.IBaseDatatype;
028import org.hl7.fhir.instance.model.api.IPrimitiveType;
029
030import ca.uhn.fhir.model.api.annotation.DatatypeDef;
031import ca.uhn.fhir.model.api.annotation.ResourceDef;
032
033public class RuntimePrimitiveDatatypeDefinition extends BaseRuntimeElementDefinition<IPrimitiveType<?>> implements IRuntimeDatatypeDefinition {
034
035        private BaseRuntimeElementDefinition<?> myProfileOf;
036        private Class<? extends IBaseDatatype> myProfileOfType;
037        private boolean mySpecialization;
038
039        public RuntimePrimitiveDatatypeDefinition(DatatypeDef theDef, Class<? extends IPrimitiveType<?>> theImplementingClass, boolean theStandardType) {
040                super(theDef.name(), theImplementingClass, theStandardType);
041                
042                String resourceName = theDef.name();
043                if (isBlank(resourceName)) {
044                        throw new ConfigurationException("Resource type @" + ResourceDef.class.getSimpleName() + " annotation contains no resource name: " + theImplementingClass.getCanonicalName());
045                }
046                
047                mySpecialization = theDef.isSpecialization();
048                myProfileOfType = theDef.profileOf();
049                if (myProfileOfType.equals(IBaseDatatype.class)) {
050                        myProfileOfType = null;
051                }
052        }
053
054        @Override
055        public ca.uhn.fhir.context.BaseRuntimeElementDefinition.ChildTypeEnum getChildType() {
056                return ChildTypeEnum.PRIMITIVE_DATATYPE;
057        }
058
059        @Override
060        public Class<? extends IBaseDatatype> getProfileOf() {
061                return myProfileOfType;
062        }
063
064        @Override
065        public boolean isSpecialization() {
066                return mySpecialization;
067        }
068
069        @Override
070        void sealAndInitialize(FhirContext theContext, Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) {
071                super.sealAndInitialize(theContext, theClassToElementDefinitions);
072                
073                if (myProfileOfType != null) {
074                        myProfileOf = theClassToElementDefinitions.get(myProfileOfType);
075                        if (myProfileOf == null) {
076                                StringBuilder b = new StringBuilder();
077                                b.append("Unknown profileOf value: ");
078                                b.append(myProfileOfType);
079                                b.append(" in type ");
080                                b.append(getImplementingClass().getName());
081                                b.append(" - Valid types: ");
082                                b.append(theClassToElementDefinitions.keySet());
083                                throw new ConfigurationException(b.toString());
084                        }
085                }
086        }
087
088        @Override
089        public boolean isProfileOf(Class<? extends IBaseDatatype> theType) {
090                if (myProfileOfType != null) {
091                        if (myProfileOfType.equals(theType)) {
092                                return true;
093                        } else if (myProfileOf instanceof IRuntimeDatatypeDefinition) {
094                                return ((IRuntimeDatatypeDefinition) myProfileOf).isProfileOf(theType);
095                        }
096                }
097                return false;
098        }
099
100
101}