All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ca.uhn.fhir.context.RuntimeResourceDefinition Maven / Gradle / Ivy

There is a newer version: 7.6.1
Show newest version
package ca.uhn.fhir.context;

/*
 * #%L
 * HAPI FHIR - Core Library
 * %%
 * Copyright (C) 2014 - 2016 University Health Network
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.hl7.fhir.instance.model.api.IBase;
import org.hl7.fhir.instance.model.api.IBaseResource;

import ca.uhn.fhir.model.api.annotation.ResourceDef;
import ca.uhn.fhir.util.UrlUtil;

public class RuntimeResourceDefinition extends BaseRuntimeElementCompositeDefinition {

	private Class myBaseType;
	private Map> myCompartmentNameToSearchParams;
	private FhirContext myContext;
	private String myId;
	private Map myNameToSearchParam = new LinkedHashMap();
	private IBaseResource myProfileDef;
	private String myResourceProfile;
	private List mySearchParams;
	private final FhirVersionEnum myStructureVersion;
	private volatile RuntimeResourceDefinition myBaseDefinition;
	
	public RuntimeResourceDefinition(FhirContext theContext, String theResourceName, Class theClass, ResourceDef theResourceAnnotation, boolean theStandardType, Map, BaseRuntimeElementDefinition> theClassToElementDefinitions) {
		super(theResourceName, theClass, theStandardType, theContext, theClassToElementDefinitions);
		myContext = theContext;
		myResourceProfile = theResourceAnnotation.profile();
		myId = theResourceAnnotation.id();

		IBaseResource instance;
		try {
			instance = theClass.newInstance();
		} catch (Exception e) {
			throw new ConfigurationException(myContext.getLocalizer().getMessage(getClass(), "nonInstantiableType", theClass.getName(), e.toString()), e);
		}
		myStructureVersion = instance.getStructureFhirVersionEnum();
		if (myStructureVersion != theContext.getVersion().getVersion()) {
			throw new ConfigurationException(myContext.getLocalizer().getMessage(getClass(), "typeWrongVersion", theContext.getVersion().getVersion(), theClass.getName(), myStructureVersion));
		}

	}

	public void addSearchParam(RuntimeSearchParam theParam) {
		myNameToSearchParam.put(theParam.getName(), theParam);
	}

	/**
	 * If this definition refers to a class which extends another resource definition type, this
	 * method will return the definition of the topmost resource. For example, if this definition
	 * refers to MyPatient2, which extends MyPatient, which in turn extends Patient, this method
	 * will return the resource definition for Patient.
	 * 

* If the definition has no parent, returns this *

*/ public RuntimeResourceDefinition getBaseDefinition() { validateSealed(); if (myBaseDefinition == null) { myBaseDefinition = myContext.getResourceDefinition(myBaseType); } return myBaseDefinition; } @Override public ca.uhn.fhir.context.BaseRuntimeElementDefinition.ChildTypeEnum getChildType() { return ChildTypeEnum.RESOURCE; } public String getId() { return myId; } /** * Express {@link #getImplementingClass()} as theClass (to prevent casting warnings) */ @SuppressWarnings("unchecked") public Class getImplementingClass(Class theClass) { if (!theClass.isAssignableFrom(getImplementingClass())) { throw new ConfigurationException("Unable to convert " + getImplementingClass() + " to " + theClass); } return (Class) getImplementingClass(); } @Deprecated public String getResourceProfile() { return myResourceProfile; } public String getResourceProfile(String theServerBase) { validateSealed(); String profile; if (!myResourceProfile.isEmpty()) { profile = myResourceProfile; } else if (!myId.isEmpty()) { profile = myId; } else { return ""; } if (!UrlUtil.isValid(profile)) { String resourceName = "/StructureDefinition/"; if (myContext.getVersion().getVersion() == FhirVersionEnum.DSTU1) { resourceName = "/Profile/"; } String profileWithUrl = theServerBase + resourceName + profile; if (UrlUtil.isValid(profileWithUrl)) { return profileWithUrl; } } return profile; } public RuntimeSearchParam getSearchParam(String theName) { validateSealed(); return myNameToSearchParam.get(theName); } public List getSearchParams() { validateSealed(); return mySearchParams; } /** * Will not return null */ public List getSearchParamsForCompartmentName(String theCompartmentName) { validateSealed(); List retVal = myCompartmentNameToSearchParams.get(theCompartmentName); if (retVal == null) { return Collections.emptyList(); } return retVal; } public FhirVersionEnum getStructureVersion() { return myStructureVersion; } public boolean isBundle() { return "Bundle".equals(getName()); } @SuppressWarnings("unchecked") @Override public void sealAndInitialize(FhirContext theContext, Map, BaseRuntimeElementDefinition> theClassToElementDefinitions) { super.sealAndInitialize(theContext, theClassToElementDefinitions); myNameToSearchParam = Collections.unmodifiableMap(myNameToSearchParam); ArrayList searchParams = new ArrayList(myNameToSearchParam.values()); Collections.sort(searchParams, new Comparator() { @Override public int compare(RuntimeSearchParam theArg0, RuntimeSearchParam theArg1) { return theArg0.getName().compareTo(theArg1.getName()); } }); mySearchParams = Collections.unmodifiableList(searchParams); Map> compartmentNameToSearchParams = new HashMap>(); for (RuntimeSearchParam next : searchParams) { if (next.getProvidesMembershipInCompartments() != null) { for (String nextCompartment : next.getProvidesMembershipInCompartments()) { if (!compartmentNameToSearchParams.containsKey(nextCompartment)) { compartmentNameToSearchParams.put(nextCompartment, new ArrayList()); } compartmentNameToSearchParams.get(nextCompartment).add(next); } } } myCompartmentNameToSearchParams = Collections.unmodifiableMap(compartmentNameToSearchParams); Class target = getImplementingClass(); myBaseType = (Class) target; do { target = target.getSuperclass(); if (IBaseResource.class.isAssignableFrom(target) && target.getAnnotation(ResourceDef.class) != null) { myBaseType = (Class) target; } } while (target.equals(Object.class) == false); } @Deprecated public synchronized IBaseResource toProfile() { validateSealed(); if (myProfileDef != null) { return myProfileDef; } IBaseResource retVal = myContext.getVersion().generateProfile(this, null); myProfileDef = retVal; return retVal; } public synchronized IBaseResource toProfile(String theServerBase) { validateSealed(); if (myProfileDef != null) { return myProfileDef; } IBaseResource retVal = myContext.getVersion().generateProfile(this, theServerBase); myProfileDef = retVal; return retVal; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy