private FhirContext( FhirVersionEnum theVersion, Collection<Class<? extends IBaseResource>> theResourceTypes) { VersionUtil.getVersion(); if (theVersion != null) { if (!theVersion.isPresentOnClasspath()) { throw new IllegalStateException( getLocalizer() .getMessage( FhirContext.class, "noStructuresForSpecifiedVersion", theVersion.name())); } myVersion = theVersion.getVersionImplementation(); } else if (FhirVersionEnum.DSTU1.isPresentOnClasspath()) { myVersion = FhirVersionEnum.DSTU1.getVersionImplementation(); } else if (FhirVersionEnum.DSTU2.isPresentOnClasspath()) { myVersion = FhirVersionEnum.DSTU2.getVersionImplementation(); } else if (FhirVersionEnum.DSTU2_HL7ORG.isPresentOnClasspath()) { myVersion = FhirVersionEnum.DSTU2_HL7ORG.getVersionImplementation(); } else { throw new IllegalStateException(getLocalizer().getMessage(FhirContext.class, "noStructures")); } if (theVersion == null) { ourLog.info( "Creating new FhirContext with auto-detected version [{}]. It is recommended to explicitly select a version for future compatibility by invoking FhirContext.forDstuX()", myVersion.getVersion().name()); } else { ourLog.info("Creating new FHIR context for FHIR version [{}]", myVersion.getVersion().name()); } myResourceTypesToScan = theResourceTypes; }
public RuntimeResourceDefinition getResourceDefinition( FhirVersionEnum theVersion, String theResourceName) { Validate.notNull(theVersion, "theVersion can not be null"); validateInitialized(); if (theVersion.equals(myVersion.getVersion())) { return getResourceDefinition(theResourceName); } Map<String, Class<? extends IBaseResource>> nameToType = myVersionToNameToResourceType.get(theVersion); if (nameToType == null) { nameToType = new HashMap<String, Class<? extends IBaseResource>>(); Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> existing = Collections.emptyMap(); ModelScanner.scanVersionPropertyFile(null, nameToType, theVersion, existing); Map<FhirVersionEnum, Map<String, Class<? extends IBaseResource>>> newVersionToNameToResourceType = new HashMap<FhirVersionEnum, Map<String, Class<? extends IBaseResource>>>(); newVersionToNameToResourceType.putAll(myVersionToNameToResourceType); newVersionToNameToResourceType.put(theVersion, nameToType); myVersionToNameToResourceType = newVersionToNameToResourceType; } Class<? extends IBaseResource> resourceType = nameToType.get(theResourceName.toLowerCase()); if (resourceType == null) { throw new DataFormatException(createUnknownResourceNameError(theResourceName, theVersion)); } return getResourceDefinition(resourceType); }
/** * Returns the scanned runtime model for the given type. This is an advanced feature which is * generally only needed for extending the core library. * * <p>Note that this method is case insensitive! */ @SuppressWarnings("unchecked") public RuntimeResourceDefinition getResourceDefinition(String theResourceName) { validateInitialized(); Validate.notBlank(theResourceName, "theResourceName must not be blank"); String resourceName = theResourceName.toLowerCase(); RuntimeResourceDefinition retVal = myNameToResourceDefinition.get(resourceName); if (retVal == null) { Class<? extends IBaseResource> clazz = myNameToResourceType.get(resourceName.toLowerCase()); if (clazz == null) { throw new DataFormatException( createUnknownResourceNameError(theResourceName, myVersion.getVersion())); } if (IBaseResource.class.isAssignableFrom(clazz)) { retVal = scanResourceType((Class<? extends IResource>) clazz); } } return retVal; }
private synchronized Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> scanResourceTypes(Collection<Class<? extends IElement>> theResourceTypes) { myInitializing = true; List<Class<? extends IBase>> typesToScan = new ArrayList<Class<? extends IBase>>(); if (theResourceTypes != null) { typesToScan.addAll(theResourceTypes); } if (myCustomTypes != null) { typesToScan.addAll(myCustomTypes); myCustomTypes = null; } ModelScanner scanner = new ModelScanner(this, myVersion.getVersion(), myClassToElementDefinition, typesToScan); if (myRuntimeChildUndeclaredExtensionDefinition == null) { myRuntimeChildUndeclaredExtensionDefinition = scanner.getRuntimeChildUndeclaredExtensionDefinition(); } Map<String, BaseRuntimeElementDefinition<?>> nameToElementDefinition = new HashMap<String, BaseRuntimeElementDefinition<?>>(); nameToElementDefinition.putAll(myNameToElementDefinition); for (Entry<String, BaseRuntimeElementDefinition<?>> next : scanner.getNameToElementDefinitions().entrySet()) { if (!nameToElementDefinition.containsKey(next.getKey())) { nameToElementDefinition.put(next.getKey(), next.getValue()); } } Map<String, RuntimeResourceDefinition> nameToResourceDefinition = new HashMap<String, RuntimeResourceDefinition>(); nameToResourceDefinition.putAll(myNameToResourceDefinition); for (Entry<String, RuntimeResourceDefinition> next : scanner.getNameToResourceDefinition().entrySet()) { if (!nameToResourceDefinition.containsKey(next.getKey())) { nameToResourceDefinition.put(next.getKey(), next.getValue()); } } Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> classToElementDefinition = new HashMap<Class<? extends IBase>, BaseRuntimeElementDefinition<?>>(); classToElementDefinition.putAll(myClassToElementDefinition); classToElementDefinition.putAll(scanner.getClassToElementDefinitions()); for (BaseRuntimeElementDefinition<?> next : classToElementDefinition.values()) { if (next instanceof RuntimeResourceDefinition) { if ("Bundle".equals(next.getName())) { if (!IBaseBundle.class.isAssignableFrom(next.getImplementingClass())) { throw new ConfigurationException( "Resource type declares resource name Bundle but does not implement IBaseBundle"); } } } } Map<String, RuntimeResourceDefinition> idToElementDefinition = new HashMap<String, RuntimeResourceDefinition>(); idToElementDefinition.putAll(myIdToResourceDefinition); idToElementDefinition.putAll(scanner.getIdToResourceDefinition()); myNameToElementDefinition = nameToElementDefinition; myClassToElementDefinition = classToElementDefinition; myIdToResourceDefinition = idToElementDefinition; myNameToResourceDefinition = nameToResourceDefinition; myNameToResourceType = scanner.getNameToResourceType(); myInitialized = true; return classToElementDefinition; }
/** * This method should be considered experimental and will likely change in future releases of * HAPI. Use with caution! */ public IVersionSpecificBundleFactory newBundleFactory() { return myVersion.newBundleFactory(this); }