|
| 1 | +package org.openmrs.module.initializer.api.fhir.cpm; |
| 2 | + |
| 3 | +import org.openmrs.PersonAttributeType; |
| 4 | +import org.openmrs.annotation.OpenmrsProfile; |
| 5 | +import org.openmrs.api.LocationService; |
| 6 | +import org.openmrs.api.PersonService; |
| 7 | +import org.openmrs.api.ProviderService; |
| 8 | +import org.openmrs.attribute.BaseAttributeType; |
| 9 | +import org.openmrs.module.fhir2.api.FhirContactPointMapService; |
| 10 | +import org.openmrs.module.initializer.Domain; |
| 11 | +import org.openmrs.module.fhir2.model.FhirContactPointMap; |
| 12 | +import org.openmrs.module.initializer.api.BaseLineProcessor; |
| 13 | +import org.openmrs.module.initializer.api.CsvLine; |
| 14 | +import org.openmrs.module.initializer.api.CsvParser; |
| 15 | +import org.springframework.beans.factory.annotation.Autowired; |
| 16 | + |
| 17 | +@OpenmrsProfile(modules = { "fhir2:1.11.* - 9.*" }, openmrsPlatformVersion = "2.5.13 - 2.5.*, 2.6.2 - 2.6.*, 2.7.* - 9.*") |
| 18 | +public class FhirContactPointMapCsvParser extends CsvParser<FhirContactPointMap, BaseLineProcessor<FhirContactPointMap>> { |
| 19 | + |
| 20 | + public static final String ATTRIBUTE_TYPE_DOMAIN_HEADER = "Entity name"; |
| 21 | + |
| 22 | + public static final String ATTRIBUTE_TYPE = "Attribute type"; |
| 23 | + |
| 24 | + private static final String LOCATION = "location"; |
| 25 | + |
| 26 | + private static final String PERSON = "person"; |
| 27 | + |
| 28 | + private static final String PROVIDER = "provider"; |
| 29 | + |
| 30 | + private final LocationService locationService; |
| 31 | + |
| 32 | + private final PersonService personService; |
| 33 | + |
| 34 | + private final ProviderService providerService; |
| 35 | + |
| 36 | + private final FhirContactPointMapService fhirContactPointMapService; |
| 37 | + |
| 38 | + @Autowired |
| 39 | + protected FhirContactPointMapCsvParser(FhirContactPointMapService fhirContactPointMapService,BaseLineProcessor<FhirContactPointMap> lineProcessor, |
| 40 | + LocationService locationService, PersonService personService, ProviderService providerService) { |
| 41 | + super(lineProcessor); |
| 42 | + this.fhirContactPointMapService = fhirContactPointMapService; |
| 43 | + this.locationService = locationService; |
| 44 | + this.personService = personService; |
| 45 | + this.providerService = providerService; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public FhirContactPointMap bootstrap(CsvLine line) throws IllegalArgumentException { |
| 50 | + FhirContactPointMap contactPointMap = null; |
| 51 | + if (line.getUuid() != null) { |
| 52 | + contactPointMap = fhirContactPointMapService.getFhirContactPointMapByUuid(line.getUuid()) |
| 53 | + .orElse(null); |
| 54 | + } |
| 55 | + |
| 56 | + if (contactPointMap != null) { |
| 57 | + return contactPointMap; |
| 58 | + } |
| 59 | + |
| 60 | + String attributeTypeDomain = line.get(ATTRIBUTE_TYPE_DOMAIN_HEADER, true); |
| 61 | + String attributeType = line.get(ATTRIBUTE_TYPE, true); |
| 62 | + |
| 63 | + if (attributeTypeDomain.equals(PERSON)) { |
| 64 | + PersonAttributeType personAttributeType = getPersonAttributeType(attributeType); |
| 65 | + |
| 66 | + if (personAttributeType == null) { |
| 67 | + throw new IllegalArgumentException("PersonAttributeType " + attributeType |
| 68 | + + " does not exist. Please ensure your Initializer configuration contains this attribute type."); |
| 69 | + } |
| 70 | + |
| 71 | + contactPointMap = fhirContactPointMapService.getFhirContactPointMapForPersonAttributeType(personAttributeType) |
| 72 | + .orElse(null); |
| 73 | + } else { |
| 74 | + BaseAttributeType<?> baseAttributeType = getBaseAttributeType(attributeTypeDomain, attributeType); |
| 75 | + |
| 76 | + if (baseAttributeType == null) { |
| 77 | + throw new IllegalArgumentException( |
| 78 | + "Could not find attribute type " + attributeType + " for attribute domain " + attributeTypeDomain); |
| 79 | + } |
| 80 | + |
| 81 | + contactPointMap = fhirContactPointMapService.getFhirContactPointMapForAttributeType(baseAttributeType) |
| 82 | + .orElse(null); |
| 83 | + } |
| 84 | + |
| 85 | + if (contactPointMap != null) { |
| 86 | + return contactPointMap; |
| 87 | + } |
| 88 | + |
| 89 | + return new FhirContactPointMap(); |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + @Override |
| 94 | + public FhirContactPointMap save(FhirContactPointMap instance) { |
| 95 | + return fhirContactPointMapService.saveFhirContactPointMap(instance); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public Domain getDomain() { |
| 100 | + return Domain.FHIR_CONTACT_POINT_MAP; |
| 101 | + } |
| 102 | + |
| 103 | + protected PersonAttributeType getPersonAttributeType(String attributeType) { |
| 104 | + PersonAttributeType personAttributeType = personService.getPersonAttributeTypeByName(attributeType); |
| 105 | + |
| 106 | + if (personAttributeType != null) { |
| 107 | + return personAttributeType; |
| 108 | + } |
| 109 | + |
| 110 | + personAttributeType = personService.getPersonAttributeTypeByUuid(attributeType); |
| 111 | + |
| 112 | + return personAttributeType; |
| 113 | + } |
| 114 | + |
| 115 | + protected BaseAttributeType<?> getBaseAttributeType(String attributeDomain, String attributeType) { |
| 116 | + BaseAttributeType<?> baseAttributeType = null; |
| 117 | + |
| 118 | + switch (attributeDomain) { |
| 119 | + case LOCATION: |
| 120 | + baseAttributeType = locationService.getLocationAttributeTypeByName(attributeType); |
| 121 | + |
| 122 | + if (baseAttributeType != null) { |
| 123 | + return baseAttributeType; |
| 124 | + } |
| 125 | + |
| 126 | + return locationService.getLocationAttributeTypeByUuid(attributeType); |
| 127 | + break; |
| 128 | + case PROVIDER: |
| 129 | + baseAttributeType = providerService.getProviderAttributeTypeByName(attributeType); |
| 130 | + |
| 131 | + if (baseAttributeType != null) { |
| 132 | + return baseAttributeType; |
| 133 | + } |
| 134 | + |
| 135 | + return providerService.getProviderAttributeTypeByUuid(attributeType); |
| 136 | + break; |
| 137 | + } |
| 138 | + return baseAttributeType; |
| 139 | + } |
| 140 | +} |
0 commit comments