001package org.hl7.fhir.dstu2.hapi.ctx;
002
003/*
004 * #%L
005 * HAPI FHIR Structures - HL7.org DSTU2
006 * %%
007 * Copyright (C) 2014 - 2015 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 ca.uhn.fhir.i18n.Msg;
024import ca.uhn.fhir.context.ConfigurationException;
025import ca.uhn.fhir.context.FhirContext;
026import ca.uhn.fhir.context.FhirVersionEnum;
027import ca.uhn.fhir.context.RuntimeResourceDefinition;
028import ca.uhn.fhir.fhirpath.IFhirPath;
029import ca.uhn.fhir.model.api.IFhirVersion;
030import ca.uhn.fhir.model.base.composite.BaseCodingDt;
031import ca.uhn.fhir.rest.api.IVersionSpecificBundleFactory;
032import ca.uhn.fhir.rest.server.provider.dstu2hl7org.Dstu2Hl7OrgBundleFactory;
033import ca.uhn.fhir.util.ReflectionUtil;
034import org.apache.commons.lang3.StringUtils;
035import org.hl7.fhir.dstu2.model.IdType;
036import org.hl7.fhir.dstu2.model.Reference;
037import org.hl7.fhir.dstu2.model.Resource;
038import org.hl7.fhir.dstu2.model.StructureDefinition;
039import org.hl7.fhir.instance.model.api.IBaseResource;
040import org.hl7.fhir.instance.model.api.IIdType;
041import org.hl7.fhir.instance.model.api.IPrimitiveType;
042
043import java.io.InputStream;
044import java.util.ArrayList;
045import java.util.Date;
046
047public class FhirDstu2Hl7Org implements IFhirVersion {
048
049  private String myId;
050
051  @Override
052  public IFhirPath createFhirPathExecutor(FhirContext theFhirContext) {
053    throw new UnsupportedOperationException(Msg.code(586) + "FluentPath is not supported in DSTU2 contexts");
054  }
055
056  @Override
057  public StructureDefinition generateProfile(RuntimeResourceDefinition theRuntimeResourceDefinition, String theServerBase) {
058    StructureDefinition retVal = new StructureDefinition();
059
060    RuntimeResourceDefinition def = theRuntimeResourceDefinition;
061
062    myId = def.getId();
063    if (StringUtils.isBlank(myId)) {
064      myId = theRuntimeResourceDefinition.getName().toLowerCase();
065    }
066
067    retVal.setId(myId);
068    return retVal;
069  }
070
071  @SuppressWarnings("rawtypes")
072  @Override
073  public Class<ArrayList> getContainedType() {
074    return ArrayList.class;
075  }
076
077  @Override
078  public InputStream getFhirVersionPropertiesFile() {
079    String path = "/org/hl7/fhir/instance/model/fhirversion.properties";
080    InputStream str = FhirDstu2Hl7Org.class.getResourceAsStream(path);
081    if (str == null) {
082      str = FhirDstu2Hl7Org.class.getResourceAsStream(path.substring(1));
083    }
084    if (str == null) {
085      throw new ConfigurationException(Msg.code(587) + "Can not find model property file on classpath: " + path);
086    }
087    return str;
088  }
089
090  @Override
091  public IPrimitiveType<Date> getLastUpdated(IBaseResource theResource) {
092    return ((Resource) theResource).getMeta().getLastUpdatedElement();
093  }
094
095  @Override
096  public String getPathToSchemaDefinitions() {
097    return "/org/hl7/fhir/instance/model/schema";
098  }
099
100  @Override
101  public Class<Reference> getResourceReferenceType() {
102    return Reference.class;
103  }
104
105  @Override
106  public Object getServerVersion() {
107    return ReflectionUtil.newInstanceOfFhirServerType("org.hl7.fhir.dstu2.hapi.ctx.FhirServerDstu2Hl7Org2");
108  }
109
110  @Override
111  public FhirVersionEnum getVersion() {
112    return FhirVersionEnum.DSTU2_HL7ORG;
113  }
114
115  @Override
116  public IVersionSpecificBundleFactory newBundleFactory(FhirContext theContext) {
117    return new Dstu2Hl7OrgBundleFactory(theContext);
118  }
119
120  @Override
121  public BaseCodingDt newCodingDt() {
122    throw new UnsupportedOperationException(Msg.code(588));
123  }
124
125  @Override
126  public IIdType newIdType() {
127    return new IdType();
128  }
129
130}