diff --git a/src/main/java/com/wipro/fhir/service/healthID/HealthIDServiceImpl.java b/src/main/java/com/wipro/fhir/service/healthID/HealthIDServiceImpl.java index 4c9e3c2..66f930f 100644 --- a/src/main/java/com/wipro/fhir/service/healthID/HealthIDServiceImpl.java +++ b/src/main/java/com/wipro/fhir/service/healthID/HealthIDServiceImpl.java @@ -61,55 +61,68 @@ public class HealthIDServiceImpl implements HealthIDService { @Autowired HealthIDRepo healthIDRepo; + @Override public String mapHealthIDToBeneficiary(String request) throws FHIRException { BenHealthIDMapping health = InputMapper.gson().fromJson(request, BenHealthIDMapping.class); - health = InputMapper.gson().fromJson(request, BenHealthIDMapping.class); + String[] beneficiaryIdsList = null; try { if (health.getBeneficiaryRegID() == null && health.getBeneficiaryID() == null) throw new FHIRException("Error in mapping request"); - if (health.getBeneficiaryRegID() != null) - health = benHealthIDMappingRepo.save(health); else { - if (health.getBeneficiaryID() != null) { - Long check1 = benHealthIDMappingRepo.getBenRegID(health.getBeneficiaryID()); - health.setBeneficiaryRegID(check1); - health = benHealthIDMappingRepo.save(health); - } - } - // Adding the code to check if the received healthId is present in t_healthId table and add if missing - Integer healthIdCount = healthIDRepo.getCountOfHealthIdNumber(health.getHealthIdNumber()); - if(healthIdCount < 1) { - JsonObject jsonRequest = JsonParser.parseString(request).getAsJsonObject(); - JsonObject abhaProfileJson = jsonRequest.getAsJsonObject("ABHAProfile"); - HealthIDResponse healthID = InputMapper.gson().fromJson(abhaProfileJson, HealthIDResponse.class); - - healthID.setHealthIdNumber(abhaProfileJson.get("ABHANumber").getAsString()); - JsonArray phrAddressArray = abhaProfileJson.getAsJsonArray("phrAddress"); - StringBuilder abhaAddressBuilder = new StringBuilder(); + if (health.getHealthIdNumber() != null) { + beneficiaryIdsList = benHealthIDMappingRepo.getBenIdForHealthId(health.getHealthIdNumber()); - for (int i = 0; i < phrAddressArray.size(); i++) { - abhaAddressBuilder.append(phrAddressArray.get(i).getAsString()); - if (i < phrAddressArray.size() - 1) { - abhaAddressBuilder.append(", "); - } - } - healthID.setHealthId(abhaAddressBuilder.toString()); - healthID.setName( - abhaProfileJson.get("firstName").getAsString() + " " + abhaProfileJson.get("middleName").getAsString() + " " + abhaProfileJson.get("lastName").getAsString()); - SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy"); - Date date = simpleDateFormat.parse(abhaProfileJson.get("dob").getAsString()); - SimpleDateFormat year = new SimpleDateFormat("yyyy"); - SimpleDateFormat month = new SimpleDateFormat("MM"); - SimpleDateFormat day = new SimpleDateFormat("dd"); - healthID.setYearOfBirth(year.format(date)); - healthID.setMonthOfBirth(month.format(date)); - healthID.setDayOfBirth(day.format(date)); - healthID.setCreatedBy(jsonRequest.get("createdBy").getAsString()); - healthID.setProviderServiceMapID(jsonRequest.get("providerServiceMapId").getAsInt()); - healthID.setIsNewAbha(jsonRequest.get("isNew").getAsBoolean()); - healthIDRepo.save(healthID); + if (beneficiaryIdsList != null && beneficiaryIdsList.length > 0) { + return "HealthId is already linked to other beneficiary ID"; + } else { + if (health.getBeneficiaryRegID() != null) + health = benHealthIDMappingRepo.save(health); + else { + if (health.getBeneficiaryID() != null) { + Long benRegId = benHealthIDMappingRepo.getBenRegID(health.getBeneficiaryID()); + health.setBeneficiaryRegID(benRegId); + health = benHealthIDMappingRepo.save(health); + } + } + // Adding the code to check if the received healthId is present in t_healthId + // table and add if missing + Integer healthIdCount = healthIDRepo.getCountOfHealthIdNumber(health.getHealthIdNumber()); + if (healthIdCount < 1) { + JsonObject jsonRequest = JsonParser.parseString(request).getAsJsonObject(); + JsonObject abhaProfileJson = jsonRequest.getAsJsonObject("ABHAProfile"); + HealthIDResponse healthID = InputMapper.gson().fromJson(abhaProfileJson, + HealthIDResponse.class); + + healthID.setHealthIdNumber(abhaProfileJson.get("ABHANumber").getAsString()); + JsonArray phrAddressArray = abhaProfileJson.getAsJsonArray("phrAddress"); + StringBuilder abhaAddressBuilder = new StringBuilder(); + + for (int i = 0; i < phrAddressArray.size(); i++) { + abhaAddressBuilder.append(phrAddressArray.get(i).getAsString()); + if (i < phrAddressArray.size() - 1) { + abhaAddressBuilder.append(", "); + } + } + healthID.setHealthId(abhaAddressBuilder.toString()); + healthID.setName(abhaProfileJson.get("firstName").getAsString() + " " + + abhaProfileJson.get("middleName").getAsString() + " " + + abhaProfileJson.get("lastName").getAsString()); + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy"); + Date date = simpleDateFormat.parse(abhaProfileJson.get("dob").getAsString()); + SimpleDateFormat year = new SimpleDateFormat("yyyy"); + SimpleDateFormat month = new SimpleDateFormat("MM"); + SimpleDateFormat day = new SimpleDateFormat("dd"); + healthID.setYearOfBirth(year.format(date)); + healthID.setMonthOfBirth(month.format(date)); + healthID.setDayOfBirth(day.format(date)); + healthID.setCreatedBy(jsonRequest.get("createdBy").getAsString()); + healthID.setProviderServiceMapID(jsonRequest.get("providerServiceMapId").getAsInt()); + healthID.setIsNewAbha(jsonRequest.get("isNew").getAsBoolean()); + healthIDRepo.save(healthID); + } + } + } } - } catch (Exception e) { throw new FHIRException("Error in saving data"); }