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

io.github.linuxforhealth.fhir.FHIRResourceMapper Maven / Gradle / Ivy

/*
 * (C) Copyright IBM Corp. 2020
 *
 * SPDX-License-Identifier: Apache-2.0
 */
package io.github.linuxforhealth.fhir;

import java.util.Map;
import org.apache.commons.lang3.ClassUtils;
import org.hl7.fhir.r4.model.Resource;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.github.linuxforhealth.core.Constants;
import io.github.linuxforhealth.core.ObjectMapperUtil;
import io.github.linuxforhealth.hl7.resource.ResourceReader;

public class FHIRResourceMapper {

  private static FHIRResourceMapper fhirResourceMapper;

  private Map resourceMapping;

  private FHIRResourceMapper() {
    String resource = ResourceReader.getInstance().getResource(Constants.RESOURCE_MAPPING_PATH);
    try {
      resourceMapping = ObjectMapperUtil.getYAMLInstance().readValue(resource, Map.class);
    } catch (JsonProcessingException e) {
      throw new IllegalArgumentException(
          "Cannot read resource mapping file fhir/resourcemapping.yml ", e);
    }
  }



  public static Class getResourceClass(String name) {
    if (fhirResourceMapper == null) {
      fhirResourceMapper = new FHIRResourceMapper();
    }
    String resourceName = fhirResourceMapper.resourceMapping.get(name);

    if (resourceName != null) {
      try {
        return (Class) ClassUtils.getClass(resourceName);
      } catch (ClassNotFoundException e) {
        throw new IllegalStateException(
            "Resource type not mapped in FHIRResourceMapper , resource name: " + name, e);
      }
    } else {

      throw new IllegalStateException(
          "Resource type not mapped in FHIRResourceMapper , resource name: " + name);
    }

  }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy