4949import com .wipro .fhir .utils .exception .FHIRException ;
5050import com .wipro .fhir .utils .http .HttpUtils ;
5151import com .wipro .fhir .utils .mapper .InputMapper ;
52+ import java .time .LocalDate ;
53+ import java .time .format .DateTimeFormatter ;
54+ import java .util .stream .IntStream ;
55+ import java .util .stream .Stream ;
5256
5357@ Service
5458public class HealthIDServiceImpl implements HealthIDService {
@@ -61,73 +65,83 @@ public class HealthIDServiceImpl implements HealthIDService {
6165 @ Autowired
6266 HealthIDRepo healthIDRepo ;
6367
68+
6469 @ Override
6570 public String mapHealthIDToBeneficiary (String request ) throws FHIRException {
66- BenHealthIDMapping health = InputMapper .gson ().fromJson (request , BenHealthIDMapping .class );
67- String [] beneficiaryIdsList = null ;
68- try {
69- if (health .getBeneficiaryRegID () == null && health .getBeneficiaryID () == null )
70- throw new FHIRException ("Error in mapping request" );
71- else {
72- if (health .getHealthIdNumber () != null ) {
73- beneficiaryIdsList = benHealthIDMappingRepo .getBenIdForHealthId (health .getHealthIdNumber ());
74-
75- if (beneficiaryIdsList != null && beneficiaryIdsList .length > 0 ) {
76- return "HealthId is already linked to other beneficiary ID" ;
77- } else {
78- if (health .getBeneficiaryRegID () != null )
79- health = benHealthIDMappingRepo .save (health );
80- else {
81- if (health .getBeneficiaryID () != null ) {
82- Long benRegId = benHealthIDMappingRepo .getBenRegID (health .getBeneficiaryID ());
83- health .setBeneficiaryRegID (benRegId );
84- health = benHealthIDMappingRepo .save (health );
85- }
86- }
87- // Adding the code to check if the received healthId is present in t_healthId
88- // table and add if missing
89- Integer healthIdCount = healthIDRepo .getCountOfHealthIdNumber (health .getHealthIdNumber ());
90- if (healthIdCount < 1 ) {
91- JsonObject jsonRequest = JsonParser .parseString (request ).getAsJsonObject ();
92- JsonObject abhaProfileJson = jsonRequest .getAsJsonObject ("ABHAProfile" );
93- HealthIDResponse healthID = InputMapper .gson ().fromJson (abhaProfileJson ,
94- HealthIDResponse .class );
95-
96- healthID .setHealthIdNumber (abhaProfileJson .get ("ABHANumber" ).getAsString ());
97- JsonArray phrAddressArray = abhaProfileJson .getAsJsonArray ("phrAddress" );
98- StringBuilder abhaAddressBuilder = new StringBuilder ();
99-
100- for (int i = 0 ; i < phrAddressArray .size (); i ++) {
101- abhaAddressBuilder .append (phrAddressArray .get (i ).getAsString ());
102- if (i < phrAddressArray .size () - 1 ) {
103- abhaAddressBuilder .append (", " );
104- }
105- }
106- healthID .setHealthId (abhaAddressBuilder .toString ());
107- healthID .setName (abhaProfileJson .get ("firstName" ).getAsString () + " "
108- + abhaProfileJson .get ("middleName" ).getAsString () + " "
109- + abhaProfileJson .get ("lastName" ).getAsString ());
110- SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("dd-MM-yyyy" );
111- Date date = simpleDateFormat .parse (abhaProfileJson .get ("dob" ).getAsString ());
112- SimpleDateFormat year = new SimpleDateFormat ("yyyy" );
113- SimpleDateFormat month = new SimpleDateFormat ("MM" );
114- SimpleDateFormat day = new SimpleDateFormat ("dd" );
115- healthID .setYearOfBirth (year .format (date ));
116- healthID .setMonthOfBirth (month .format (date ));
117- healthID .setDayOfBirth (day .format (date ));
118- healthID .setCreatedBy (jsonRequest .get ("createdBy" ).getAsString ());
119- healthID .setProviderServiceMapID (jsonRequest .get ("providerServiceMapId" ).getAsInt ());
120- healthID .setIsNewAbha (jsonRequest .get ("isNew" ).getAsBoolean ());
121- healthIDRepo .save (healthID );
122- }
123- }
124- }
71+ BenHealthIDMapping health = null ;
72+ try {
73+ JsonObject jsonRequest = JsonParser .parseString (request ).getAsJsonObject ();
74+ JsonObject abhaProfileJson = jsonRequest .getAsJsonObject ("ABHAProfile" );
75+
76+ health = InputMapper .gson ().fromJson (request , BenHealthIDMapping .class );
77+
78+ if (health .getBeneficiaryRegID () == null && health .getBeneficiaryID () == null ) {
79+ throw new FHIRException ("BeneficiaryRegID or BeneficiaryID must be provided" );
12580 }
126- } catch (Exception e ) {
127- throw new FHIRException ("Error in saving data" );
128- }
129- return new Gson ().toJson (health );
130- }
81+
82+ String healthIdNumber = health .getHealthIdNumber ();
83+ if (healthIdNumber != null && !healthIdNumber .trim ().isEmpty ()) {
84+
85+ // Avoid fetching entire list - use exists check
86+ boolean alreadyLinked = benHealthIDMappingRepo .existsByHealthIdNumber (healthIdNumber );
87+ if (alreadyLinked ) {
88+ return "HealthId is already linked to another beneficiary ID" ;
89+ }
90+
91+ // Save mapping
92+ if (health .getBeneficiaryRegID () != null ) {
93+ health = benHealthIDMappingRepo .save (health );
94+ } else if (health .getBeneficiaryID () != null ) {
95+ Long benRegId = benHealthIDMappingRepo .getBenRegID (health .getBeneficiaryID ());
96+ health .setBeneficiaryRegID (benRegId );
97+ health = benHealthIDMappingRepo .save (health );
98+ }
99+
100+ // Add to healthId table if missing
101+ boolean healthIdExists = healthIDRepo .existsByHealthIdNumber (healthIdNumber );
102+ if (!healthIdExists ) {
103+ HealthIDResponse healthID = InputMapper .gson ().fromJson (abhaProfileJson , HealthIDResponse .class );
104+ healthID .setHealthIdNumber (abhaProfileJson .get ("ABHANumber" ).getAsString ());
105+
106+ // phrAddress as comma-separated
107+ JsonArray phrAddressArray = abhaProfileJson .getAsJsonArray ("phrAddress" );
108+ String abhaAddress = IntStream .range (0 , phrAddressArray .size ())
109+ .mapToObj (i -> phrAddressArray .get (i ).getAsString ())
110+ .collect (Collectors .joining (", " ));
111+ healthID .setHealthId (abhaAddress );
112+
113+ // Full name
114+ String fullName = Stream .of ("firstName" , "middleName" , "lastName" )
115+ .map (field -> abhaProfileJson .has (field ) ? abhaProfileJson .get (field ).getAsString () : "" )
116+ .filter (s -> !s .isEmpty ())
117+ .collect (Collectors .joining (" " ));
118+ healthID .setName (fullName .trim ());
119+
120+ // Parse and split DOB
121+ LocalDate dob = LocalDate .parse (abhaProfileJson .get ("dob" ).getAsString (), DateTimeFormatter .ofPattern ("dd-MM-yyyy" ));
122+ healthID .setYearOfBirth (String .valueOf (dob .getYear ()));
123+ healthID .setMonthOfBirth (String .format ("%02d" , dob .getMonthValue ()));
124+ healthID .setDayOfBirth (String .format ("%02d" , dob .getDayOfMonth ()));
125+
126+ // Other fields
127+ healthID .setCreatedBy (jsonRequest .get ("createdBy" ).getAsString ());
128+ healthID .setProviderServiceMapID (jsonRequest .get ("providerServiceMapId" ).getAsInt ());
129+ healthID .setIsNewAbha (jsonRequest .get ("isNew" ).getAsBoolean ());
130+
131+ healthIDRepo .save (healthID );
132+ }
133+ }
134+
135+ } catch (FHIRException e ) {
136+ throw e ; // already custom
137+ } catch (Exception e ) {
138+ logger .error ("Unexpected error while mapping HealthID" , e );
139+ throw new FHIRException ("Unexpected error: " + e .getMessage ());
140+ }
141+
142+ return new Gson ().toJson (health );
143+ }
144+
131145
132146 public String getBenHealthID (Long benRegID ) {
133147 List <BenHealthIDMapping > healthDetailsList = benHealthIDMappingRepo .getHealthDetails (benRegID );
0 commit comments