001/*
002 * #%L
003 * HAPI FHIR Structures - DSTU2 (FHIR v1.0.0)
004 * %%
005 * Copyright (C) 2014 - 2015 University Health Network
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package org.hl7.fhir.r4.hapi.ctx;
021
022import ca.uhn.fhir.context.*;
023import ca.uhn.fhir.fhirpath.IFhirPath;
024import ca.uhn.fhir.i18n.Msg;
025import ca.uhn.fhir.model.api.IFhirVersion;
026import ca.uhn.fhir.model.primitive.IdDt;
027import ca.uhn.fhir.rest.api.IVersionSpecificBundleFactory;
028import ca.uhn.fhir.util.ReflectionUtil;
029import org.apache.commons.lang3.StringUtils;
030import org.hl7.fhir.instance.model.api.*;
031import org.hl7.fhir.r4.hapi.fluentpath.FhirPathR4;
032import org.hl7.fhir.r4.hapi.rest.server.R4BundleFactory;
033import org.hl7.fhir.r4.model.*;
034
035import java.io.InputStream;
036import java.util.Date;
037import java.util.List;
038
039public class FhirR4 implements IFhirVersion {
040
041        private String myId;
042
043        @Override
044        public IFhirPath createFhirPathExecutor(FhirContext theFhirContext) {
045                return new FhirPathR4(theFhirContext);
046        }
047
048        @Override
049        public IBaseResource generateProfile(RuntimeResourceDefinition theRuntimeResourceDefinition, String theServerBase) {
050                StructureDefinition retVal = new StructureDefinition();
051
052                RuntimeResourceDefinition def = theRuntimeResourceDefinition;
053
054                myId = def.getId();
055                if (StringUtils.isBlank(myId)) {
056                        myId = theRuntimeResourceDefinition.getName().toLowerCase();
057                }
058
059                retVal.setId(new IdDt(myId));
060                return retVal;
061        }
062
063        @SuppressWarnings("rawtypes")
064        @Override
065        public Class<List> getContainedType() {
066                return List.class;
067        }
068
069        @Override
070        public InputStream getFhirVersionPropertiesFile() {
071                String path = "org/hl7/fhir/r4/hapi/model/fhirversion.properties";
072                InputStream str = FhirR4.class.getResourceAsStream("/" + path);
073                if (str == null) {
074                        str = FhirR4.class.getResourceAsStream(path);
075                }
076                if (str == null) {
077                        throw new ConfigurationException(Msg.code(257) + "Can not find model property file on classpath: " + path);
078                }
079                return str;
080        }
081
082        @Override
083        public IPrimitiveType<Date> getLastUpdated(IBaseResource theResource) {
084                return ((Resource) theResource).getMeta().getLastUpdatedElement();
085        }
086
087        @Override
088        public String getPathToSchemaDefinitions() {
089                return "/org/hl7/fhir/r4/model/schema";
090        }
091
092        @Override
093        public Class<? extends IBaseReference> getResourceReferenceType() {
094                return Reference.class;
095        }
096
097        @Override
098        public Object getServerVersion() {
099                return ReflectionUtil.newInstanceOfFhirServerType("org.hl7.fhir.r4.hapi.ctx.FhirServerR4");
100        }
101
102        @Override
103        public FhirVersionEnum getVersion() {
104                return FhirVersionEnum.R4;
105        }
106
107        @Override
108        public IVersionSpecificBundleFactory newBundleFactory(FhirContext theContext) {
109                return new R4BundleFactory(theContext);
110        }
111
112        @Override
113        public IBaseCoding newCodingDt() {
114                return new Coding();
115        }
116
117        @Override
118        public IIdType newIdType() {
119                return new IdType();
120        }
121}