From 3f344fd106e5e2a9fcbf7b5e70e508a57207022f Mon Sep 17 00:00:00 2001 From: Sahil-tarento <140611066+Sahil-tarento@users.noreply.github.com> Date: Thu, 3 Oct 2024 20:31:05 +0530 Subject: [PATCH] Adding the check for adding the mobile number (#10) (#11) --- .../service/UserBulkUploadService.java | 603 ++++++++++-------- .../impl/UserProfileWfServiceImpl.java | 5 +- 2 files changed, 337 insertions(+), 271 deletions(-) diff --git a/src/main/java/org/sunbird/workflow/service/UserBulkUploadService.java b/src/main/java/org/sunbird/workflow/service/UserBulkUploadService.java index 9159a5f..2a8263f 100644 --- a/src/main/java/org/sunbird/workflow/service/UserBulkUploadService.java +++ b/src/main/java/org/sunbird/workflow/service/UserBulkUploadService.java @@ -189,7 +189,11 @@ private void processBulkUpload(HashMap inputDataMap) throws IOEx } Map valuesToBeUpdate = new HashMap<>(); Map userDetails = null; - boolean isEmailOrPhoneNumberValid = false; + Map userDetailsForMobile = null; + Map filters = null; + boolean isEmailOrPhoneNumberExist = false; + boolean isEmailValid = false; + boolean isPhoneNumberValid = false; Cell statusCell = nextRow.getCell(14); if (statusCell == null) { statusCell = nextRow.createCell(14); @@ -198,46 +202,63 @@ private void processBulkUpload(HashMap inputDataMap) throws IOEx if (errorDetails == null) { errorDetails = nextRow.createCell(15); } + String email = ""; if (nextRow.getCell(1) != null && nextRow.getCell(1).getCellType() != CellType.BLANK) { - String email = nextRow.getCell(1).getStringCellValue().trim(); + email = nextRow.getCell(1).getStringCellValue().trim(); if(ValidationUtil.validateEmailPattern(email)){ userDetails = new HashMap<>(); - isEmailOrPhoneNumberValid = this.verifyUserRecordExists(Constants.EMAIL, email, userDetails); + filters = new HashMap<>(); + filters.put(Constants.EMAIL, email); + isEmailValid = this.verifyUserRecordExists(filters, userDetails); } else{ errList.add("The Email provided in Invalid"); } } - if(!isEmailOrPhoneNumberValid){ - String phoneNumber = null; - boolean isValidPhoneNumber = false; - if (nextRow.getCell(2) != null && nextRow.getCell(2).getCellType() != CellType.BLANK) { - if (nextRow.getCell(2).getCellType() == CellType.NUMERIC) { - phoneNumber = NumberToTextConverter.toText(nextRow.getCell(2).getNumericCellValue()); - } else if (nextRow.getCell(2).getCellType() == CellType.STRING) { - phoneNumber = nextRow.getCell(2).getStringCellValue().trim(); - } else { - errList.add("Invalid Value of Mobile Number. Expecting number/string format"); - } + String phoneNumber = null; + boolean isValidPhoneNumber = false; + if (nextRow.getCell(2) != null && nextRow.getCell(2).getCellType() != CellType.BLANK) { + if (nextRow.getCell(2).getCellType() == CellType.NUMERIC) { + phoneNumber = NumberToTextConverter.toText(nextRow.getCell(2).getNumericCellValue()); + } else if (nextRow.getCell(2).getCellType() == CellType.STRING) { + phoneNumber = nextRow.getCell(2).getStringCellValue().trim(); } else { - errList.add("Mobile Number is Missing"); + errList.add("Invalid Value of Mobile Number. Expecting number/string format"); } - if(!StringUtils.isEmpty(phoneNumber)){ - isValidPhoneNumber = ValidationUtil.validateContactPattern(phoneNumber); - if(isValidPhoneNumber){ - userDetails = new HashMap<>(); - isEmailOrPhoneNumberValid = this.verifyUserRecordExists(Constants.PHONE, phoneNumber, userDetails); - } else{ - errList.add("The Mobile Number provided is Invalid"); - } + } else { + errList.add("Mobile Number is Missing"); + } + if (!StringUtils.isEmpty(phoneNumber)) { + isValidPhoneNumber = ValidationUtil.validateContactPattern(phoneNumber); + if (isValidPhoneNumber) { + userDetailsForMobile = new HashMap<>(); + filters = new HashMap<>(); + filters.put(Constants.PHONE, phoneNumber); + isPhoneNumberValid = this.verifyUserRecordExists(filters, userDetailsForMobile); + } else { + errList.add("The Mobile Number provided is Invalid"); } } - if(!isEmailOrPhoneNumberValid){ + if (!StringUtils.isEmpty(phoneNumber) && isEmailValid) { + filters = new HashMap<>(); + filters.put(Constants.EMAIL, email); + filters.put(Constants.PHONE, phoneNumber); + isEmailOrPhoneNumberExist = this.verifyUserRecordExists(filters, userDetails); + } + if (!CollectionUtils.isEmpty(errList)) { + this.setErrorDetails(str, errList, statusCell, errorDetails); + failedRecordsCount++; + totalRecordsCount++; + continue; + } + if(!isEmailValid){ errList.add("User record does not exist with given email and/or Mobile Number"); + } else if (!isEmailOrPhoneNumberExist && isPhoneNumberValid) { + errList.add("Another user record exist with the mobile number"); } else { errList.clear(); String userRootOrgId = (String) userDetails.get(Constants.ROOT_ORG_ID); String mdoAdminRootOrgId = inputDataMap.get(Constants.ROOT_ORG_ID); - if(!mdoAdminRootOrgId.equalsIgnoreCase(userRootOrgId)){ + if (!mdoAdminRootOrgId.equalsIgnoreCase(userRootOrgId)) { errList.add("The User belongs to a different MDO Organisation"); this.setErrorDetails(str, errList, statusCell, errorDetails); failedRecordsCount++; @@ -245,6 +266,12 @@ private void processBulkUpload(HashMap inputDataMap) throws IOEx continue; } } + if(!CollectionUtils.isEmpty(errList)){ + this.setErrorDetails(str, errList, statusCell, errorDetails); + failedRecordsCount++; + totalRecordsCount++; + continue; + } if (nextRow.getCell(0) != null && nextRow.getCell(0).getCellType() != CellType.BLANK) { if (nextRow.getCell(0).getCellType() == CellType.STRING) { valuesToBeUpdate.put(Constants.FIRSTNAME, nextRow.getCell(0).getStringCellValue().trim()); @@ -255,6 +282,9 @@ private void processBulkUpload(HashMap inputDataMap) throws IOEx errList.add("Invalid value for Full Name type. Expecting string format"); } } + if (!isEmailOrPhoneNumberExist) { + valuesToBeUpdate.put(Constants.MOBILE, phoneNumber); + } if (nextRow.getCell(3) != null && nextRow.getCell(3).getCellType() != CellType.BLANK) { String groupValue = null; if (nextRow.getCell(3).getCellType() == CellType.STRING) { @@ -479,6 +509,7 @@ private void processBulkUpload(HashMap inputDataMap) throws IOEx personalDetailsKey.add(Constants.DOMICILE_MEDIUM); personalDetailsKey.add(Constants.CATEGORY); personalDetailsKey.add(Constants.GENDER); + personalDetailsKey.add(Constants.MOBILE); WfRequest wfRequest = this.getWFRequest(valuesToBeUpdate, userId); List> updatedValues = new ArrayList<>(); @@ -574,14 +605,11 @@ private String uploadTheUpdatedFile(File file, XSSFWorkbook wb) } - public boolean verifyUserRecordExists(String field, String fieldValue, Map userRecordDetails) { + public boolean verifyUserRecordExists(Map filters, Map userRecordDetails) { HashMap headersValue = new HashMap<>(); headersValue.put(Constants.CONTENT_TYPE, Constants.APPLICATION_JSON); - Map filters = new HashMap<>(); - filters.put(field, fieldValue); - Map request = new HashMap<>(); request.put("filters", filters); request.put(Constants.FIELDS, Arrays.asList(Constants.USER_ID, Constants.STATUS, Constants.CHANNEL, Constants.ROOT_ORG_ID, Constants.PHONE, Constants.EMAIL)); @@ -731,28 +759,51 @@ public void processBulkUploadV1(HashMap inputDataMap) throws IOE errList.add("Email or Phone is missing"); } + Map userDetailsForMobile = null; + Map filters = null; + boolean isEmailOrPhoneNumberExist = false; + boolean isEmailValid = false; + boolean isPhoneNumberValid = false; // Validate email if (emailExists) { if (ValidationUtil.validateEmailPattern(email)) { userDetails = new HashMap<>(); - isEmailOrPhoneNumberValid = this.verifyUserRecordExists(Constants.EMAIL, email, userDetails); + filters = new HashMap<>(); + filters.put(Constants.EMAIL, email); + isEmailValid = this.verifyUserRecordExists(filters, userDetails); } else { errList.add("Invalid Email format"); } } // Validate phone - if (!isEmailOrPhoneNumberValid) { - if (ValidationUtil.validateContactPattern(phone)) { - userDetails = new HashMap<>(); - isEmailOrPhoneNumberValid = this.verifyUserRecordExists(Constants.PHONE, phone, userDetails); - } else { - errList.add("Invalid Phone number format"); - } - } - if (!isEmailOrPhoneNumberValid) { + if (ValidationUtil.validateContactPattern(phone)) { + userDetails = new HashMap<>(); + userDetailsForMobile = new HashMap<>(); + filters = new HashMap<>(); + filters.put(Constants.PHONE, phone); + isPhoneNumberValid = this.verifyUserRecordExists(filters, userDetailsForMobile); + } else { + errList.add("Invalid Phone number format"); + } + if (!StringUtils.isEmpty(phone) && isEmailValid) { + filters = new HashMap<>(); + filters.put(Constants.EMAIL, email); + filters.put(Constants.PHONE, phone); + isEmailOrPhoneNumberExist = this.verifyUserRecordExists(filters, userDetails); + } + if (!CollectionUtils.isEmpty(errList)) { + csvValues.put("Error Details", String.join(",", errList)); + failedRecordsCount++; + totalRecordsCount++; + updatedRecords.add(csvValues); + continue; + } + if (!isEmailValid) { errList.add("User record does not exist with given email and/or Mobile Number"); + } else if (!isEmailOrPhoneNumberExist && isPhoneNumberValid) { + errList.add("Another user record exist with the mobile number"); } else { errList.clear(); String userRootOrgId = (String) userDetails.get(Constants.ROOT_ORG_ID); @@ -768,6 +819,14 @@ public void processBulkUploadV1(HashMap inputDataMap) throws IOE } } + if (!CollectionUtils.isEmpty(errList)) { + csvValues.put("Error Details", String.join(",", errList)); + failedRecordsCount++; + totalRecordsCount++; + updatedRecords.add(csvValues); + continue; + } + // Validate and update fields if (!record.get(0).isEmpty()) { String fullName = record.get(0).trim(); @@ -777,286 +836,290 @@ public void processBulkUploadV1(HashMap inputDataMap) throws IOE errList.add("Invalid Full Name format"); } } - // Group - if (!record.get(3).isEmpty()) { - String group = record.get(3).trim(); - valuesToBeUpdate.put(Constants.GROUP, group); - if (!this.validateGroupValue(group)) { - errList.add("Invalid value of Group Type, please choose a valid value from the default list"); - } + // Group + if (!record.get(3).isEmpty()) { + String group = record.get(3).trim(); + valuesToBeUpdate.put(Constants.GROUP, group); + if (!this.validateGroupValue(group)) { + errList.add("Invalid value of Group Type, please choose a valid value from the default list"); } + } - // Designation - if (!record.get(4).isEmpty()) { - String designation = record.get(4).trim(); - valuesToBeUpdate.put(Constants.DESIGNATION, designation); - if (!ValidationUtil.validateRegexPatternWithNoSpecialCharacter(designation)) { - errList.add("Invalid Designation: Designation should be added from default list and cannot contain special character"); - } - if (this.validateFieldValue("position", designation)) { - errList.add("Invalid Value of Designation, please choose a valid value from the default list"); - } - } else { - errList.add("Invalid value for Designation type. Expecting string format"); + if (!isEmailOrPhoneNumberExist) { + valuesToBeUpdate.put(Constants.MOBILE, phone); + } + // Designation + if (!record.get(4).isEmpty()) { + String designation = record.get(4).trim(); + valuesToBeUpdate.put(Constants.DESIGNATION, designation); + if (!ValidationUtil.validateRegexPatternWithNoSpecialCharacter(designation)) { + errList.add("Invalid Designation: Designation should be added from default list and cannot contain special character"); } - - // Gender - if (!record.get(5).isEmpty()) { - String gender = record.get(5).trim(); - if (validateGender(gender)) { - valuesToBeUpdate.put(Constants.GENDER, gender); - } else { - errList.add("Invalid Gender : Gender can be only among one of these " + configuration.getBulkUploadGenderValue()); - } + if (this.validateFieldValue("position", designation)) { + errList.add("Invalid Value of Designation, please choose a valid value from the default list"); } + } else { + errList.add("Invalid value for Designation type. Expecting string format"); + } - // Category - if (!record.get(6).isEmpty()) { - String category = record.get(6).trim(); - if (validateCategory(category)) { - valuesToBeUpdate.put(Constants.CATEGORY, category); - } else { - errList.add("Invalid Category : Category can be only among one of these " + configuration.getBulkUploadCategoryValue()); - } + // Gender + if (!record.get(5).isEmpty()) { + String gender = record.get(5).trim(); + if (validateGender(gender)) { + valuesToBeUpdate.put(Constants.GENDER, gender); + } else { + errList.add("Invalid Gender : Gender can be only among one of these " + configuration.getBulkUploadGenderValue()); } + } - // Date of Birth - if (!record.get(7).isEmpty()) { - String dob = record.get(7).trim(); - if (ValidationUtil.validateDate(dob)) { - valuesToBeUpdate.put(Constants.DOB, dob); - } else { - errList.add("Invalid format for Date of Birth type. Expecting in format dd-mm-yyyy"); - } + // Category + if (!record.get(6).isEmpty()) { + String category = record.get(6).trim(); + if (validateCategory(category)) { + valuesToBeUpdate.put(Constants.CATEGORY, category); } else { - errList.add("Invalid value for Date of Birth type. Expecting string format"); + errList.add("Invalid Category : Category can be only among one of these " + configuration.getBulkUploadCategoryValue()); } + } - // Mother Tongue - if (!record.get(8).isEmpty()) { - String motherTongue = record.get(8).trim(); - if (!ValidationUtil.validateRegexPatternWithNoSpecialCharacter(motherTongue) || this.validateFieldValue("languages", motherTongue)) { - errList.add("Invalid Mother Tongue: Mother Tongue should be added from default list and/or cannot contain special character(s)"); - } - valuesToBeUpdate.put(Constants.DOMICILE_MEDIUM, motherTongue); + // Date of Birth + if (!record.get(7).isEmpty()) { + String dob = record.get(7).trim(); + if (ValidationUtil.validateDate(dob)) { + valuesToBeUpdate.put(Constants.DOB, dob); } else { - errList.add("Invalid value for Mother Tongue type. Expecting string format"); + errList.add("Invalid format for Date of Birth type. Expecting in format dd-mm-yyyy"); } + } else { + errList.add("Invalid value for Date of Birth type. Expecting string format"); + } - // Employee ID - if (!record.get(9).isEmpty()) { - String employeeId = record.get(9).trim(); - valuesToBeUpdate.put(Constants.EMPLOYEE_CODE, employeeId); - if (!ValidationUtil.validateEmployeeId((String) valuesToBeUpdate.get(Constants.EMPLOYEE_CODE))) { - errList.add("Invalid Employee ID : Employee ID can contain alphanumeric characters or numeric character and have a max length of 30"); - } + // Mother Tongue + if (!record.get(8).isEmpty()) { + String motherTongue = record.get(8).trim(); + if (!ValidationUtil.validateRegexPatternWithNoSpecialCharacter(motherTongue) || this.validateFieldValue("languages", motherTongue)) { + errList.add("Invalid Mother Tongue: Mother Tongue should be added from default list and/or cannot contain special character(s)"); } + valuesToBeUpdate.put(Constants.DOMICILE_MEDIUM, motherTongue); + } else { + errList.add("Invalid value for Mother Tongue type. Expecting string format"); + } + + // Employee ID + if (!record.get(9).isEmpty()) { + String employeeId = record.get(9).trim(); + valuesToBeUpdate.put(Constants.EMPLOYEE_CODE, employeeId); + if (!ValidationUtil.validateEmployeeId((String) valuesToBeUpdate.get(Constants.EMPLOYEE_CODE))) { + errList.add("Invalid Employee ID : Employee ID can contain alphanumeric characters or numeric character and have a max length of 30"); + } + } - // Office Pin Code - if (!record.get(10).isEmpty()) { - String officePinCode = record.get(10).trim(); - if (!ValidationUtil.validatePinCode(officePinCode)) { - errList.add("Invalid Pin Code: " + officePinCode + - " Pin Code should be numeric and is of 6 digits."); - } - valuesToBeUpdate.put(Constants.PIN_CODE, officePinCode); + // Office Pin Code + if (!record.get(10).isEmpty()) { + String officePinCode = record.get(10).trim(); + if (!ValidationUtil.validatePinCode(officePinCode)) { + errList.add("Invalid Pin Code: " + officePinCode + + " Pin Code should be numeric and is of 6 digits."); } + valuesToBeUpdate.put(Constants.PIN_CODE, officePinCode); + } - // External System ID - if (!record.get(11).isEmpty()) { - String externalSystemId = record.get(11).trim(); - if (!ValidationUtil.validateExternalSystemId(externalSystemId)) { - errList.add("Invalid External System ID: " + externalSystemId + - " External System Id can contain alphanumeric characters and have a max length of 30"); - } - valuesToBeUpdate.put(Constants.EXTERNAL_SYSTEM_ID, externalSystemId); + // External System ID + if (!record.get(11).isEmpty()) { + String externalSystemId = record.get(11).trim(); + if (!ValidationUtil.validateExternalSystemId(externalSystemId)) { + errList.add("Invalid External System ID: " + externalSystemId + + " External System Id can contain alphanumeric characters and have a max length of 30"); } + valuesToBeUpdate.put(Constants.EXTERNAL_SYSTEM_ID, externalSystemId); + } - // External System - if (!record.get(12).isEmpty()) { - String externalSystem = record.get(12).trim(); - if (!ValidationUtil.validateExternalSystem(externalSystem)) { - errList.add("Invalid External System Name: " + externalSystem + - " External System Name can contain only alphabets and can have a max length of 255"); - } - valuesToBeUpdate.put(Constants.EXTERNAL_SYSTEM, externalSystem); + // External System + if (!record.get(12).isEmpty()) { + String externalSystem = record.get(12).trim(); + if (!ValidationUtil.validateExternalSystem(externalSystem)) { + errList.add("Invalid External System Name: " + externalSystem + + " External System Name can contain only alphabets and can have a max length of 255"); } + valuesToBeUpdate.put(Constants.EXTERNAL_SYSTEM, externalSystem); + } - // Tags - if (!record.get(13).isEmpty()) { - String[] tagStrList = record.get(13).split("&"); - List tagList = new ArrayList<>(); - for (String tag : tagStrList) { - tagList.add(tag.trim()); - } - if (!ValidationUtil.validateTag(tagList)) { - errList.add("Invalid Tag: " + tagStrList + - " Tags are separated by '&' and can contain only alphabets with spaces. e.g., Bihar Circle&Patna Division"); - } - valuesToBeUpdate.put(Constants.TAG, tagList); + // Tags + if (!record.get(13).isEmpty()) { + String[] tagStrList = record.get(13).split("&"); + List tagList = new ArrayList<>(); + for (String tag : tagStrList) { + tagList.add(tag.trim()); + } + if (!ValidationUtil.validateTag(tagList)) { + errList.add("Invalid Tag: " + tagStrList + + " Tags are separated by '&' and can contain only alphabets with spaces. e.g., Bihar Circle&Patna Division"); } + valuesToBeUpdate.put(Constants.TAG, tagList); + } - if(!CollectionUtils.isEmpty(errList)) { - String statusValue = errList.isEmpty() ? Constants.SUCCESSFUL_UPERCASE : Constants.FAILED_UPPERCASE; - csvValues.put("Status", statusValue); - csvValues.put("Error Details", errList.isEmpty() ? "" : String.join(", ", errList)); - failedRecordsCount++; - totalRecordsCount++; - updatedRecords.add(csvValues); - continue; - } + if (!CollectionUtils.isEmpty(errList)) { + String statusValue = errList.isEmpty() ? Constants.SUCCESSFUL_UPERCASE : Constants.FAILED_UPPERCASE; + csvValues.put("Status", statusValue); + csvValues.put("Error Details", errList.isEmpty() ? "" : String.join(", ", errList)); + failedRecordsCount++; + totalRecordsCount++; + updatedRecords.add(csvValues); + continue; + } - String userId = null; - if (!CollectionUtils.isEmpty(userDetails)) { - userId = (String) userDetails.get(Constants.USER_ID); - } - logger.info("userId "+userId); - List userPendingRequest = wfStatusRepo.findByUserIdAndCurrentStatus(userId, true); - boolean userRecordUpdate = true; - if (!CollectionUtils.isEmpty(userPendingRequest)) { - for (WfStatusEntity wfStatusEntity : userPendingRequest) { - String updateValuesString = wfStatusEntity.getUpdateFieldValues(); - List> updatedValues = mapper.readValue(updateValuesString, List.class); - Map toValue = (Map) updatedValues.get(0).get(Constants.TO_VALUE); - for (Map.Entry entry : toValue.entrySet()) { - if (valuesToBeUpdate.containsKey(entry.getKey())) { - WfRequest wfRequest = this.getWFRequest(wfStatusEntity, null); - if (entry.getValue().equalsIgnoreCase((String) valuesToBeUpdate.get(entry.getKey()))) { - wfStatusEntity.setCurrentStatus(Constants.APPROVED); + String userId = null; + if (!CollectionUtils.isEmpty(userDetails)) { + userId = (String) userDetails.get(Constants.USER_ID); + } + logger.info("userId " + userId); + List userPendingRequest = wfStatusRepo.findByUserIdAndCurrentStatus(userId, true); + boolean userRecordUpdate = true; + if (!CollectionUtils.isEmpty(userPendingRequest)) { + for (WfStatusEntity wfStatusEntity : userPendingRequest) { + String updateValuesString = wfStatusEntity.getUpdateFieldValues(); + List> updatedValues = mapper.readValue(updateValuesString, List.class); + Map toValue = (Map) updatedValues.get(0).get(Constants.TO_VALUE); + for (Map.Entry entry : toValue.entrySet()) { + if (valuesToBeUpdate.containsKey(entry.getKey())) { + WfRequest wfRequest = this.getWFRequest(wfStatusEntity, null); + if (entry.getValue().equalsIgnoreCase((String) valuesToBeUpdate.get(entry.getKey()))) { + wfStatusEntity.setCurrentStatus(Constants.APPROVED); + } else { + wfStatusEntity.setCurrentStatus(Constants.REJECTED); + } + wfStatusEntity.setInWorkflow(false); + wfStatusRepo.save(wfStatusEntity); + if (Constants.APPROVED.equalsIgnoreCase(wfStatusEntity.getCurrentStatus())) { + userProfileWfService.updateUserProfile(wfRequest); + WfStatusEntity wfStatusEntityFailed = wfStatusRepo.findByWfId(wfRequest.getWfId()); + if (Constants.REJECTED.equalsIgnoreCase(wfStatusEntityFailed.getCurrentStatus())) { + userRecordUpdate = false; + csvValues.put("Status", Constants.FAILED_UPPERCASE); + csvValues.put("Error Details", Constants.UPDATE_FAILED); } else { - wfStatusEntity.setCurrentStatus(Constants.REJECTED); - } - wfStatusEntity.setInWorkflow(false); - wfStatusRepo.save(wfStatusEntity); - if (Constants.APPROVED.equalsIgnoreCase(wfStatusEntity.getCurrentStatus())) { - userProfileWfService.updateUserProfile(wfRequest); - WfStatusEntity wfStatusEntityFailed = wfStatusRepo.findByWfId(wfRequest.getWfId()); - if (Constants.REJECTED.equalsIgnoreCase(wfStatusEntityFailed.getCurrentStatus())) { - userRecordUpdate = false; - csvValues.put("Status", Constants.FAILED_UPPERCASE); - csvValues.put("Error Details", Constants.UPDATE_FAILED); - } else { - csvValues.put("Status", Constants.SUCCESSFUL_UPERCASE); - } - valuesToBeUpdate.remove(entry.getKey()); + csvValues.put("Status", Constants.SUCCESSFUL_UPERCASE); } + valuesToBeUpdate.remove(entry.getKey()); } } } } - if (valuesToBeUpdate.isEmpty()) { - if (userRecordUpdate) - noOfSuccessfulRecords++; - else - failedRecordsCount++; - totalRecordsCount++; - updatedRecords.add(csvValues); - continue; - } - - Set employmentDetailsKey = new HashSet<>(); - employmentDetailsKey.add(Constants.EMPLOYEE_CODE); - employmentDetailsKey.add(Constants.PIN_CODE); - - Set professionalDetailsKey = new HashSet<>(); - professionalDetailsKey.add(Constants.GROUP); - professionalDetailsKey.add(Constants.DESIGNATION); - - Set personalDetailsKey = new HashSet<>(); - personalDetailsKey.add(Constants.FIRSTNAME); - personalDetailsKey.add(Constants.DOB); - personalDetailsKey.add(Constants.DOMICILE_MEDIUM); - personalDetailsKey.add(Constants.CATEGORY); - personalDetailsKey.add(Constants.GENDER); - - WfRequest wfRequest = this.getWFRequest(valuesToBeUpdate, userId); - List> updatedValues = new ArrayList<>(); - for (Map.Entry entry : valuesToBeUpdate.entrySet()) { - String fieldKey; - HashMap updatedValueMap = new HashMap<>(); - updatedValueMap.put(entry.getKey(), entry.getValue()); - HashMap updateValues = new HashMap<>(); - updateValues.put(Constants.FROM_VALUE, new HashMap<>()); - updateValues.put(Constants.TO_VALUE, updatedValueMap); - if (employmentDetailsKey.contains(entry.getKey())) { - fieldKey = Constants.EMPLOYMENT_DETAILS; - } else if (professionalDetailsKey.contains(entry.getKey())) { - fieldKey = Constants.PROFESSIONAL_DETAILS; - } else if (personalDetailsKey.contains(entry.getKey())) { - fieldKey = Constants.PERSONAL_DETAILS; - } else { - fieldKey = Constants.ADDITIONAL_PROPERTIES; - } - updateValues.put(Constants.FIELD_KEY, fieldKey); - updatedValues.add(updateValues); - if (null != wfRequest) { - wfRequest.setUpdateFieldValues(updatedValues); - } - } - userProfileWfService.updateUserProfileForBulkUpload(wfRequest); - WfStatusEntity wfStatusEntityFailed = wfStatusRepo.findByWfId(wfRequest.getWfId()); - if (null != wfStatusEntityFailed && Constants.REJECTED.equalsIgnoreCase(wfStatusEntityFailed.getCurrentStatus())) { - userRecordUpdate = false; - } - if (userRecordUpdate) { + } + if (valuesToBeUpdate.isEmpty()) { + if (userRecordUpdate) noOfSuccessfulRecords++; - csvValues.put("Status", Constants.SUCCESSFUL_UPERCASE); - csvValues.put("Error Details", "NA"); - } else { + else failedRecordsCount++; - csvValues.put("Status", Constants.UPDATE_FAILED); - csvValues.put("Error Details", Constants.UPDATE_FAILED); - } totalRecordsCount++; - duration = System.currentTimeMillis() - startTime; - logger.info("UserBulkUploadService:: Record Completed. Time taken: {} milli-seconds", duration); - updatedRecords.add(csvValues); - } - // Write back updated records to the same CSV file - fileWriter = new FileWriter(file); - bufferedWriter = new BufferedWriter(fileWriter); - csvPrinter = new CSVPrinter(bufferedWriter, CSVFormat.INFORMIX_UNLOAD.withHeader(headers.toArray(new String[0]))); + updatedRecords.add(csvValues); + continue; + } + + Set employmentDetailsKey = new HashSet<>(); + employmentDetailsKey.add(Constants.EMPLOYEE_CODE); + employmentDetailsKey.add(Constants.PIN_CODE); + + Set professionalDetailsKey = new HashSet<>(); + professionalDetailsKey.add(Constants.GROUP); + professionalDetailsKey.add(Constants.DESIGNATION); + Set personalDetailsKey = new HashSet<>(); + personalDetailsKey.add(Constants.FIRSTNAME); + personalDetailsKey.add(Constants.DOB); + personalDetailsKey.add(Constants.DOMICILE_MEDIUM); + personalDetailsKey.add(Constants.CATEGORY); + personalDetailsKey.add(Constants.GENDER); + personalDetailsKey.add(Constants.MOBILE); - for (Map record : updatedRecords) { - List recordValues = new ArrayList<>(); - for (String header : headers) { - recordValues.add((String) record.get(header)); + WfRequest wfRequest = this.getWFRequest(valuesToBeUpdate, userId); + List> updatedValues = new ArrayList<>(); + for (Map.Entry entry : valuesToBeUpdate.entrySet()) { + String fieldKey; + HashMap updatedValueMap = new HashMap<>(); + updatedValueMap.put(entry.getKey(), entry.getValue()); + HashMap updateValues = new HashMap<>(); + updateValues.put(Constants.FROM_VALUE, new HashMap<>()); + updateValues.put(Constants.TO_VALUE, updatedValueMap); + if (employmentDetailsKey.contains(entry.getKey())) { + fieldKey = Constants.EMPLOYMENT_DETAILS; + } else if (professionalDetailsKey.contains(entry.getKey())) { + fieldKey = Constants.PROFESSIONAL_DETAILS; + } else if (personalDetailsKey.contains(entry.getKey())) { + fieldKey = Constants.PERSONAL_DETAILS; + } else { + fieldKey = Constants.ADDITIONAL_PROPERTIES; + } + updateValues.put(Constants.FIELD_KEY, fieldKey); + updatedValues.add(updateValues); + if (null != wfRequest) { + wfRequest.setUpdateFieldValues(updatedValues); } - csvPrinter.printRecord(recordValues); } + userProfileWfService.updateUserProfileForBulkUpload(wfRequest); + WfStatusEntity wfStatusEntityFailed = wfStatusRepo.findByWfId(wfRequest.getWfId()); + if (null != wfStatusEntityFailed && Constants.REJECTED.equalsIgnoreCase(wfStatusEntityFailed.getCurrentStatus())) { + userRecordUpdate = false; + } + if (userRecordUpdate) { + noOfSuccessfulRecords++; + csvValues.put("Status", Constants.SUCCESSFUL_UPERCASE); + csvValues.put("Error Details", "NA"); + } else { + failedRecordsCount++; + csvValues.put("Status", Constants.UPDATE_FAILED); + csvValues.put("Error Details", Constants.UPDATE_FAILED); + } + totalRecordsCount++; + duration = System.currentTimeMillis() - startTime; + logger.info("UserBulkUploadService:: Record Completed. Time taken: {} milli-seconds", duration); + updatedRecords.add(csvValues); + } + // Write back updated records to the same CSV file + fileWriter = new FileWriter(file); + bufferedWriter = new BufferedWriter(fileWriter); + csvPrinter = new CSVPrinter(bufferedWriter, CSVFormat.INFORMIX_UNLOAD.withHeader(headers.toArray(new String[0]))); - if (totalRecordsCount == 0) { - List singleRow = new ArrayList<>(Collections.nCopies(headers.size(), "")); - singleRow.set(headers.indexOf("Status"), Constants.FAILED_UPPERCASE); - singleRow.set(headers.indexOf("Error Details"), Constants.EMPTY_FILE_FAILED); - csvPrinter.printRecord(singleRow); - status = Constants.FAILED_UPPERCASE; + + for (Map record : updatedRecords) { + List recordValues = new ArrayList<>(); + for (String header : headers) { + recordValues.add((String) record.get(header)); } - csvPrinter.flush(); + csvPrinter.printRecord(recordValues); + } + + if (totalRecordsCount == 0) { + List singleRow = new ArrayList<>(Collections.nCopies(headers.size(), "")); + singleRow.set(headers.indexOf("Status"), Constants.FAILED_UPPERCASE); + singleRow.set(headers.indexOf("Error Details"), Constants.EMPTY_FILE_FAILED); + csvPrinter.printRecord(singleRow); + status = Constants.FAILED_UPPERCASE; + } + csvPrinter.flush(); - status = uploadTheUpdatedCSVFile(file); + status = uploadTheUpdatedCSVFile(file); - status = (failedRecordsCount == 0 && totalRecordsCount == noOfSuccessfulRecords && totalRecordsCount >= 1) - ? Constants.SUCCESSFUL_UPERCASE - : Constants.FAILED_UPPERCASE; + status = (failedRecordsCount == 0 && totalRecordsCount == noOfSuccessfulRecords && totalRecordsCount >= 1) + ? Constants.SUCCESSFUL_UPERCASE + : Constants.FAILED_UPPERCASE; - updateUserBulkUploadStatus(inputDataMap.get(Constants.ROOT_ORG_ID), inputDataMap.get(Constants.IDENTIFIER), - status, totalRecordsCount, noOfSuccessfulRecords, failedRecordsCount); + updateUserBulkUploadStatus(inputDataMap.get(Constants.ROOT_ORG_ID), inputDataMap.get(Constants.IDENTIFIER), + status, totalRecordsCount, noOfSuccessfulRecords, failedRecordsCount); - } else{ - logger.error("File does not exist or is empty."); - status = Constants.FAILED_UPPERCASE; - } - } catch (Exception e) { + } else { + logger.error("File does not exist or is empty."); + status = Constants.FAILED_UPPERCASE; + } + } catch (Exception e) { logger.error(String.format("Error in Process Bulk Upload %s", e.getMessage()), e); this.updateUserBulkUploadStatus(inputDataMap.get(Constants.ROOT_ORG_ID), inputDataMap.get(Constants.IDENTIFIER), Constants.FAILED_UPPERCASE, 0, 0, 0); - } finally { + } finally { if (csvParser != null) csvParser.close(); if (csvPrinter != null) @@ -1067,7 +1130,7 @@ public void processBulkUploadV1(HashMap inputDataMap) throws IOE fileWriter.close(); if (file != null) file.delete(); - } + } } private String uploadTheUpdatedCSVFile(File file) diff --git a/src/main/java/org/sunbird/workflow/service/impl/UserProfileWfServiceImpl.java b/src/main/java/org/sunbird/workflow/service/impl/UserProfileWfServiceImpl.java index f389dad..649bcf4 100644 --- a/src/main/java/org/sunbird/workflow/service/impl/UserProfileWfServiceImpl.java +++ b/src/main/java/org/sunbird/workflow/service/impl/UserProfileWfServiceImpl.java @@ -437,9 +437,12 @@ private Map getUpdateRequest(WfRequest wfRequest, Map toValue = (Map) valueToUpdate.get(Constants.TO_VALUE); String keyToUpdate = toValue.keySet().stream().findFirst().get(); - if(Constants.FIRSTNAME.equalsIgnoreCase(keyToUpdate)) { + if (Constants.FIRSTNAME.equalsIgnoreCase(keyToUpdate)) { requestWrapper.put(Constants.FIRST_NAME_CAMEL_CASE, toValue.get(keyToUpdate)); } + if (Constants.MOBILE.equalsIgnoreCase(keyToUpdate)) { + requestWrapper.put(Constants.PHONE, String.valueOf(toValue.get(keyToUpdate))); + } } } requestWrapper.put(Constants.USER_ID, wfRequest.getApplicationId());