001package ca.uhn.fhir.model.dstu2;
002
003/*
004 * #%L
005 * HAPI FHIR Structures - DSTU2 (FHIR v1.0.0)
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 */
022
023import java.io.InputStream;
024import java.util.Date;
025
026import org.apache.commons.lang3.StringUtils;
027import org.hl7.fhir.instance.model.api.IBaseResource;
028import org.hl7.fhir.instance.model.api.IIdType;
029import org.hl7.fhir.instance.model.api.IPrimitiveType;
030
031import ca.uhn.fhir.context.ConfigurationException;
032import ca.uhn.fhir.context.FhirContext;
033import ca.uhn.fhir.context.FhirVersionEnum;
034import ca.uhn.fhir.context.RuntimeResourceDefinition;
035import ca.uhn.fhir.context.support.IContextValidationSupport;
036import ca.uhn.fhir.fluentpath.IFluentPath;
037import ca.uhn.fhir.model.api.IFhirVersion;
038import ca.uhn.fhir.model.api.IResource;
039import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum;
040import ca.uhn.fhir.model.base.composite.BaseCodingDt;
041import ca.uhn.fhir.model.base.composite.BaseContainedDt;
042import ca.uhn.fhir.model.base.composite.BaseResourceReferenceDt;
043import ca.uhn.fhir.model.dstu2.composite.CodingDt;
044import ca.uhn.fhir.model.dstu2.composite.ContainedDt;
045import ca.uhn.fhir.model.dstu2.composite.ResourceReferenceDt;
046import ca.uhn.fhir.model.dstu2.resource.StructureDefinition;
047import ca.uhn.fhir.model.primitive.IdDt;
048import ca.uhn.fhir.rest.server.IResourceProvider;
049import ca.uhn.fhir.rest.server.IVersionSpecificBundleFactory;
050import ca.uhn.fhir.rest.server.RestfulServer;
051import ca.uhn.fhir.rest.server.provider.dstu2.Dstu2BundleFactory;
052import ca.uhn.fhir.rest.server.provider.dstu2.ServerConformanceProvider;
053import ca.uhn.fhir.rest.server.provider.dstu2.ServerProfileProvider;
054
055public class FhirDstu2 implements IFhirVersion {
056
057        private String myId;
058
059        @Override
060        public IFluentPath createFluentPathExecutor(FhirContext theFhirContext) {
061                throw new UnsupportedOperationException("FluentPath is not supported in DSTU2 contexts");
062        }
063
064        @Override
065        public ServerConformanceProvider createServerConformanceProvider(RestfulServer theServer) {
066                return new ServerConformanceProvider(theServer);
067        }
068
069        @Override
070        public IResourceProvider createServerProfilesProvider(RestfulServer theRestfulServer) {
071                return new ServerProfileProvider(theRestfulServer);
072        }
073
074        @Override
075        public IContextValidationSupport<?, ?, ?, ?, ?, ?> createValidationSupport() {
076                throw new UnsupportedOperationException("Validation support is not supported in DSTU2 contexts");
077        }
078
079        @Override
080        public IResource generateProfile(RuntimeResourceDefinition theRuntimeResourceDefinition, String theServerBase) {
081                StructureDefinition retVal = new StructureDefinition();
082
083                RuntimeResourceDefinition def = theRuntimeResourceDefinition;
084
085                myId = def.getId();
086                if (StringUtils.isBlank(myId)) {
087                        myId = theRuntimeResourceDefinition.getName().toLowerCase();
088                }
089
090                retVal.setId(new IdDt(myId));
091                return retVal;
092        }
093
094        @Override
095        public Class<? extends BaseContainedDt> getContainedType() {
096                return ContainedDt.class;
097        }
098
099        @Override
100        public InputStream getFhirVersionPropertiesFile() {
101                InputStream str = FhirDstu2.class.getResourceAsStream("/ca/uhn/fhir/model/dstu2/fhirversion.properties");
102                if (str == null) {
103                        str = FhirDstu2.class.getResourceAsStream("ca/uhn/fhir/model/dstu2/fhirversion.properties");
104                }
105                if (str == null) {
106                        throw new ConfigurationException("Can not find model property file on classpath: " + "/ca/uhn/fhir/model/dstu2/fhirversion.properties");
107                }
108                return str;
109        }
110
111        @Override
112        public IPrimitiveType<Date> getLastUpdated(IBaseResource theResource) {
113                return ResourceMetadataKeyEnum.UPDATED.get((IResource) theResource);
114        }
115
116        @Override
117        public String getPathToSchemaDefinitions() {
118                return "/org/hl7/fhir/instance/model/schema";
119        }
120
121        @Override
122        public Class<? extends BaseResourceReferenceDt> getResourceReferenceType() {
123                return ResourceReferenceDt.class;
124        }
125
126        @Override
127        public FhirVersionEnum getVersion() {
128                return FhirVersionEnum.DSTU2;
129        }
130
131        @Override
132        public IVersionSpecificBundleFactory newBundleFactory(FhirContext theContext) {
133                return new Dstu2BundleFactory(theContext);
134        }
135
136        @Override
137        public BaseCodingDt newCodingDt() {
138                return new CodingDt();
139        }
140
141        @Override
142        public IIdType newIdType() {
143                return new IdDt();
144        }
145
146
147}