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.model.api.IFhirVersion;
036import ca.uhn.fhir.model.api.IResource;
037import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum;
038import ca.uhn.fhir.model.base.composite.BaseCodingDt;
039import ca.uhn.fhir.model.base.composite.BaseContainedDt;
040import ca.uhn.fhir.model.base.composite.BaseResourceReferenceDt;
041import ca.uhn.fhir.model.dstu2.composite.CodingDt;
042import ca.uhn.fhir.model.dstu2.composite.ContainedDt;
043import ca.uhn.fhir.model.dstu2.composite.ResourceReferenceDt;
044import ca.uhn.fhir.model.dstu2.resource.StructureDefinition;
045import ca.uhn.fhir.model.primitive.IdDt;
046import ca.uhn.fhir.rest.server.IResourceProvider;
047import ca.uhn.fhir.rest.server.IVersionSpecificBundleFactory;
048import ca.uhn.fhir.rest.server.RestfulServer;
049import ca.uhn.fhir.rest.server.provider.dstu2.Dstu2BundleFactory;
050import ca.uhn.fhir.rest.server.provider.dstu2.ServerConformanceProvider;
051import ca.uhn.fhir.rest.server.provider.dstu2.ServerProfileProvider;
052
053public class FhirDstu2 implements IFhirVersion {
054
055        private String myId;
056
057        @Override
058        public ServerConformanceProvider createServerConformanceProvider(RestfulServer theServer) {
059                return new ServerConformanceProvider(theServer);
060        }
061
062        @Override
063        public IResourceProvider createServerProfilesProvider(RestfulServer theRestfulServer) {
064                return new ServerProfileProvider(theRestfulServer);
065        }
066
067        @Override
068        public IResource generateProfile(RuntimeResourceDefinition theRuntimeResourceDefinition, String theServerBase) {
069                StructureDefinition retVal = new StructureDefinition();
070
071                RuntimeResourceDefinition def = theRuntimeResourceDefinition;
072
073                myId = def.getId();
074                if (StringUtils.isBlank(myId)) {
075                        myId = theRuntimeResourceDefinition.getName().toLowerCase();
076                }
077
078                retVal.setId(new IdDt(myId));
079                return retVal;
080        }
081
082        @Override
083        public Class<? extends BaseContainedDt> getContainedType() {
084                return ContainedDt.class;
085        }
086
087        @Override
088        public InputStream getFhirVersionPropertiesFile() {
089                InputStream str = FhirDstu2.class.getResourceAsStream("/ca/uhn/fhir/model/dstu2/fhirversion.properties");
090                if (str == null) {
091                        str = FhirDstu2.class.getResourceAsStream("ca/uhn/fhir/model/dstu2/fhirversion.properties");
092                }
093                if (str == null) {
094                        throw new ConfigurationException("Can not find model property file on classpath: " + "/ca/uhn/fhir/model/dstu2/fhirversion.properties");
095                }
096                return str;
097        }
098
099        @Override
100        public IPrimitiveType<Date> getLastUpdated(IBaseResource theResource) {
101                return ResourceMetadataKeyEnum.UPDATED.get((IResource) theResource);
102        }
103
104        @Override
105        public String getPathToSchemaDefinitions() {
106                return "/org/hl7/fhir/instance/model/schema";
107        }
108
109        @Override
110        public Class<? extends BaseResourceReferenceDt> getResourceReferenceType() {
111                return ResourceReferenceDt.class;
112        }
113
114        @Override
115        public FhirVersionEnum getVersion() {
116                return FhirVersionEnum.DSTU2;
117        }
118
119        @Override
120        public IVersionSpecificBundleFactory newBundleFactory(FhirContext theContext) {
121                return new Dstu2BundleFactory(theContext);
122        }
123
124        @Override
125        public BaseCodingDt newCodingDt() {
126                return new CodingDt();
127        }
128
129        @Override
130        public IIdType newIdType() {
131                return new IdDt();
132        }
133
134}