-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathRegisterBenificiaryServiceImpl.java
More file actions
373 lines (301 loc) · 14 KB
/
RegisterBenificiaryServiceImpl.java
File metadata and controls
373 lines (301 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
* AMRIT – Accessible Medical Records via Integrated Technology
* Integrated EHR (Electronic Health Records) Solution
*
* Copyright (C) "Piramal Swasthya Management and Research Institute"
*
* This file is part of AMRIT.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
/**
*
*/
package com.iemr.common.service.beneficiary;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import com.iemr.common.model.beneficiary.RMNCHBeneficiaryDetailsRmnch;
import com.iemr.common.service.welcomeSms.WelcomeBenificarySmsService;
import com.iemr.common.service.welcomeSms.WelcomeBenificarySmsServiceImpl;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.google.gson.Gson;
import com.iemr.common.data.beneficiary.Beneficiary;
import com.iemr.common.data.mctshistory.MctsOutboundCallDetail;
import com.iemr.common.dto.identity.BeneficiariesDTO;
import com.iemr.common.dto.identity.CommonIdentityDTO;
import com.iemr.common.dto.identity.Identity;
import com.iemr.common.dto.identity.IdentityEditDTO;
import com.iemr.common.mapper.CommonIdentityMapper;
import com.iemr.common.mapper.IdentityBenEditMapper;
import com.iemr.common.mapper.utils.InputMapper;
import com.iemr.common.model.beneficiary.BeneficiaryDemographicsModel;
import com.iemr.common.model.beneficiary.BeneficiaryGenModel;
import com.iemr.common.model.beneficiary.BeneficiaryModel;
import com.iemr.common.repository.mctshistory.OutboundHistoryRepository;
import com.iemr.common.utils.exception.IEMRException;
import com.iemr.common.utils.mapper.OutputMapper;
import com.iemr.common.utils.response.OutputResponse;
import com.iemr.common.utils.validator.Validator;
import jakarta.servlet.http.HttpServletRequest;
/**
* @author WA875423
*
*/
@Service
public class RegisterBenificiaryServiceImpl implements RegisterBenificiaryService {
@Autowired
CommonIdentityMapper identityMapper;
@Autowired
IdentityBeneficiaryService identityBeneficiaryService;
@Autowired
IdentityBenEditMapper identityBenEditMapper;
@Autowired
private WelcomeBenificarySmsService welcomeBenificarySmsService;
@Autowired
Validator validator;
@Autowired
OutboundHistoryRepository outboundHistoryRepository;
/**
* IemrUserRegisterRepository declare
*/
// private BeneficiaryRepository benificiaryRepository;
/**
* Inject IemrUserRegisterRepository
*/
private final Logger logger = LoggerFactory.getLogger(this.getClass().getName());
@Override
public Beneficiary save(Beneficiary benificiaryDetails) {
logger.info("benificiaryDetails: " + benificiaryDetails);
return new Beneficiary(); // savedBenificiaryDetails;
}
@Async
private void updateBeneficiaryID(String beneficiaryID, Long beneficiaryRegID) {
try {
// benificiaryRepository.addBeneficiaryID(beneficiaryID, beneficiaryRegID);
} catch (Exception e) {
logger.error("updateBeneficiaryID failed with error " + e.getMessage(), e);
}
}
@Async
@Override
public Integer updateBenificiary(BeneficiaryModel benificiaryDetails, String auth) throws IEMRException {
Integer updatedRows = 0;
IdentityEditDTO identityEditDTO = identityBenEditMapper.BenToIdentityEditMapper(benificiaryDetails);
setDemographicDetails(identityEditDTO,benificiaryDetails);
if (benificiaryDetails.getBeneficiaryIdentities() != null
&& benificiaryDetails.getBeneficiaryIdentities().size() > 0) {
identityEditDTO.setIdentities(Identity.createIdentity(benificiaryDetails.getBeneficiaryIdentities(),
benificiaryDetails.getCreatedBy()));
}
identityEditDTO.setDob(benificiaryDetails.getDOB());
updatedRows = identityBeneficiaryService.editIdentityEditDTO(identityEditDTO, auth,
benificiaryDetails.getIs1097());
logger.info("updateBen");
//updateDeathOfBenificiary(benificiaryDetails);
return updatedRows;
}
private void setDemographicDetails(IdentityEditDTO identityEditDTO, BeneficiaryModel benificiaryDetails) {
if(null != benificiaryDetails.getI_bendemographics()) {
identityEditDTO.setCommunity(benificiaryDetails.getI_bendemographics().getCommunityName());
identityEditDTO.setCommunityName(benificiaryDetails.getI_bendemographics().getCommunityName());
if(null != benificiaryDetails.getReligion())
identityEditDTO.setReligion(benificiaryDetails.getReligion());
else if(null != benificiaryDetails.getI_bendemographics().getReligion())
identityEditDTO.setReligion(benificiaryDetails.getI_bendemographics().getReligion());
else
identityEditDTO.setReligion(benificiaryDetails.getI_bendemographics().getReligionName());
if (null != benificiaryDetails.getOccupation()) {
identityEditDTO.setOccupationName(benificiaryDetails.getOccupation());
} else if (null != benificiaryDetails.getI_bendemographics() &&
null != benificiaryDetails.getI_bendemographics().getOccupation()) {
identityEditDTO.setOccupationName(benificiaryDetails.getI_bendemographics().getOccupation());
} else {
identityEditDTO.setOccupationName(benificiaryDetails.getOccupationName());
}
if (null != benificiaryDetails.getEducation()) {
identityEditDTO.setEducation(benificiaryDetails.getEducation());
} else if (null != benificiaryDetails.getI_bendemographics() &&
null != benificiaryDetails.getI_bendemographics().getEducationName()) {
identityEditDTO.setEducation(benificiaryDetails.getI_bendemographics().getEducationName());
}
if(null != benificiaryDetails.getIncomeStatus())
identityEditDTO.setIncomeStatus(benificiaryDetails.getIncomeStatus());
else
identityEditDTO.setIncomeStatus(benificiaryDetails.getI_bendemographics().getIncomeStatus());
}
if(benificiaryDetails!=null){
//updateDeathOfBenificiary(benificiaryDetails);
}
}
private int updateDemographics(BeneficiaryDemographicsModel i_BenDemographics) {
int updatedRows = 0;
System.out.println(i_BenDemographics);
return updatedRows;
}
@Override
public String save(BeneficiaryModel beneficiaryModel, HttpServletRequest servletRequest) throws Exception {
logger.info("benificiaryDetails: " + beneficiaryModel);
CommonIdentityDTO identityDTO = identityMapper.beneficiaryModelCommonIdentityDTO(beneficiaryModel);
setSaveDemographicDetails(identityDTO,beneficiaryModel);
// identityDTO.setOtherFields(beneficiaryModel.getOtherFields());
identityDTO.setIsConsent(beneficiaryModel.getIsConsent());
identityDTO.setFaceEmbedding(beneficiaryModel.getFaceEmbedding());
identityDTO.setEmergencyRegistration(beneficiaryModel.isEmergencyRegistration());
identityDTO
.setBenFamilyDTOs(identityMapper.benPhoneMapListToBenFamilyDTOList(beneficiaryModel.getBenPhoneMaps()));
String request = new Gson().toJson(identityDTO);
if (beneficiaryModel.getIs1097() == null)
beneficiaryModel.setIs1097(false);
String identityResponse = identityBeneficiaryService.getIdentityResponse(request,
servletRequest.getHeader("authorization"), beneficiaryModel.getIs1097());
JSONObject responseObj = new JSONObject(identityResponse);
BeneficiaryModel beneficiary = new BeneficiaryModel();
if (responseObj.has("response")) {
OutputResponse response = InputMapper.getInstance()
.fromJson(responseObj.getJSONObject("response").toString(), OutputResponse.class);
if (response.isSuccess()) {
BeneficiariesDTO createdBen = InputMapper.getInstance().fromJson(response.getData(),
BeneficiariesDTO.class);
beneficiaryModel.setBeneficiaryRegID(Long.parseLong(createdBen.getBenRegId().toString()));
beneficiaryModel.setBeneficiaryID(createdBen.getBenId().toString());
beneficiary = beneficiaryModel;
} else {
return response.toString();
}
if (beneficiary != null && beneficiary.getBenPhoneMaps() != null && !beneficiary.getBenPhoneMaps().isEmpty()) {
String phoneNo = beneficiary.getBenPhoneMaps().get(0).getPhoneNo();
if (phoneNo != null && !phoneNo.trim().isEmpty()) {
String beneficiaryName = (beneficiary.getFirstName() != null ? beneficiary.getFirstName() : "") + " " +
(beneficiary.getLastName() != null ? beneficiary.getLastName() : "");
try {
logger.info("[SMS] Attempting to send welcome SMS to: " + phoneNo);
String smsResult = welcomeBenificarySmsService.sendWelcomeSMStoBenificiary(
phoneNo,
beneficiaryName.trim(),
beneficiary.getBeneficiaryID()
);
logger.info("[SMS] Result: " + smsResult);
} catch (Exception smsError) {
// SMS failed but beneficiary is already created - don't fail the request
logger.warn("[SMS] Failed to send SMS: " + smsError.getMessage() +
" - But beneficiary already created successfully");
}
}
}
}
return OutputMapper.gson().toJson(beneficiary);
}
private void setSaveDemographicDetails(CommonIdentityDTO identityDTO, BeneficiaryModel beneficiaryModel) {
if(null != beneficiaryModel.getI_bendemographics()) {
identityDTO.setCommunity(beneficiaryModel.getI_bendemographics().getCommunityName());
if(null != beneficiaryModel.getReligion())
identityDTO.setReligion(beneficiaryModel.getReligion());
else if(null != beneficiaryModel.getI_bendemographics().getReligion())
identityDTO.setReligion(beneficiaryModel.getI_bendemographics().getReligion());
else
identityDTO.setReligion(beneficiaryModel.getI_bendemographics().getReligionName());
if (null != beneficiaryModel.getOccupation()) {
identityDTO.setOccupationName(beneficiaryModel.getOccupation());
} else if (null != beneficiaryModel.getI_bendemographics() &&
null != beneficiaryModel.getI_bendemographics().getOccupation()) {
identityDTO.setOccupationName(beneficiaryModel.getI_bendemographics().getOccupation());
} else {
identityDTO.setOccupationName(beneficiaryModel.getOccupationName());
}
if (null != beneficiaryModel.getEducation()) {
identityDTO.setEducation(beneficiaryModel.getEducation());
} else if (null != beneficiaryModel.getI_bendemographics() &&
null != beneficiaryModel.getI_bendemographics().getEducationName()) {
identityDTO.setEducation(beneficiaryModel.getI_bendemographics().getEducationName());
}
if(null != beneficiaryModel.getIncomeStatus())
identityDTO.setIncomeStatus(beneficiaryModel.getIncomeStatus());
else
identityDTO.setIncomeStatus(beneficiaryModel.getI_bendemographics().getIncomeStatus());
}
if(beneficiaryModel!=null){
// updateDeathOfBenificiary(beneficiaryModel);
}
}
@Override
public Integer updateCommunityorEducation(BeneficiaryModel benificiaryDetails, String auth) throws IEMRException {
IdentityEditDTO identityEditDTO = new IdentityEditDTO();
identityEditDTO.setBeneficiaryRegId(new BigInteger(benificiaryDetails.getBeneficiaryRegID() + ""));
if (benificiaryDetails.getI_bendemographics() != null) {
identityEditDTO.setCommunityId(benificiaryDetails.getI_bendemographics().getCommunityID());
identityEditDTO.setEducationId(benificiaryDetails.getI_bendemographics().getEducationID());
}
Integer updatedRows = 0;
updatedRows = identityBeneficiaryService.editIdentityEditDTOCommunityorEducation(identityEditDTO, auth,
benificiaryDetails.getIs1097());
// updateBeneficiaryPreferredLanguage(identityEditDTO, auth);
return updatedRows;
}
private void updateMCTSRecord(BeneficiaryModel beneficiaryModel) {
List<MctsOutboundCallDetail> mctsOutboundCallDetails = new ArrayList<MctsOutboundCallDetail>();
try {
mctsOutboundCallDetails = outboundHistoryRepository.checkBenExist(beneficiaryModel.getBeneficiaryRegID());
MctsOutboundCallDetail callDetail = mctsOutboundCallDetails.get(0);
if (callDetail != null && callDetail.getIsMother() != null) {
String firstName = beneficiaryModel.getFirstName();
String lastName = beneficiaryModel.getLastName();
String name = "";
if (lastName != null && !lastName.equalsIgnoreCase("")) {
name = firstName + " " + lastName;
} else {
name = firstName;
}
Integer age = beneficiaryModel.getAge();
String districtName = outboundHistoryRepository
.getDistrictName(beneficiaryModel.getI_bendemographics().getDistrictID());
String talukaName = outboundHistoryRepository
.getBlockName(beneficiaryModel.getI_bendemographics().getBlockID());
String villageName = outboundHistoryRepository
.getVillageName(beneficiaryModel.getI_bendemographics().getDistrictBranchID());
String gender = null;
if (beneficiaryModel.getGenderID() == 1) {
gender = "Male";
} else if (beneficiaryModel.getGenderID() == 2) {
gender = "Female";
} else {
gender = "Transgender";
}
Timestamp lastModDate = Timestamp.valueOf(LocalDateTime.now());
if (callDetail.getIsMother() == true) {
int updateRow = outboundHistoryRepository.updateMotherData(name, age, districtName, talukaName,
villageName, lastModDate, beneficiaryModel.getBeneficiaryRegID());
} else {
int updateRow = outboundHistoryRepository.updateChildData(name, gender, districtName, talukaName,
villageName, lastModDate, beneficiaryModel.getBeneficiaryRegID());
}
}
} catch (Exception e) {
}
}
@Override
public String generateBeneficiaryIDs(String request, HttpServletRequest servletRequest) throws Exception {
logger.info("request: " + request.toString());
List<BeneficiaryGenModel> listBen = identityBeneficiaryService.generateBeneficiaryIDs(request,
servletRequest.getHeader("authorization"));
return listBen.toString();
}
}