Skip to content

Commit

Permalink
Group changes 4.8.3 (#241)
Browse files Browse the repository at this point in the history
* changes for group and external system
  • Loading branch information
juhiagl8 authored Jun 2, 2023
1 parent 2de65af commit ee746b0
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,15 @@ public Map<String, Object> fetchResultUsingPost(String uri, Object request, Map<
str.append(mapper.writeValueAsString(response)).append(System.lineSeparator());
log.debug(str.toString());
}
} catch (HttpClientErrorException | JsonProcessingException e) {
} catch (HttpClientErrorException hce) {
try {
response = (new ObjectMapper()).readValue(hce.getResponseBodyAsString(),
new TypeReference<HashMap<String, Object>>() {
});
} catch (Exception e1) {
}
log.error("Error received: " + hce.getResponseBodyAsString(), hce);
} catch(JsonProcessingException e) {
log.error(e);
try {
log.warn("Error Response: " + mapper.writeValueAsString(response));
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/sunbird/common/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,8 @@ public class Constants {
public static final String EXTERNAL_SYSTEM_ID = "externalSystemId";
public static final String EXTERNAL_SYSTEM = "externalSystem";
public static final String GROUP = "group";

public static final String BULK_USER_CREATE_API_FAILED = "Bulk User Create API Failed";
public static final String BULK_USER_UPDATE_API_FAILED = "Bulk User Update API Failed";
private Constants() {
throw new IllegalStateException("Utility class");
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/sunbird/common/util/ProjectUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public static Boolean validateContactPattern(String contactNumber) {
}

public static Boolean validateFullName(String firstName ) {
return firstName.matches( "^[a-zA-Z']+\\s[a-zA-Z']+$" );
return firstName.matches("^[a-zA-Z]+(?:['\\s][a-zA-Z]+)*(?<!\\.|\\s)$");
}

public static Boolean validateTag(List<String> tags) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,42 +128,46 @@ private void processBulkUpload(HashMap<String, String> inputDataMap) throws IOEx
statusCell.setCellValue("Status");
errorDetails.setCellValue("Error Details");
}
int count = 0;
while (rowIterator.hasNext()) {
logger.info("UserBulkUploadService:: Record " + count++);
long duration = 0;
long startTime = System.currentTimeMillis();
StringBuffer str = new StringBuffer();
List<String> errList = new ArrayList<>();
List<String> invalidErrList = new ArrayList<>();
Row nextRow = rowIterator.next();
UserRegistration userRegistration = new UserRegistration();
if (nextRow.getCell(0) == null || StringUtils.isBlank(nextRow.getCell(0).toString())) {
if (nextRow.getCell(0) == null || nextRow.getCell(0).getCellType() == CellType.BLANK) {
errList.add("Full Name");
} else {
userRegistration.setFirstName(nextRow.getCell(0).getStringCellValue());
if (!ProjectUtil.validateFullName(userRegistration.getFirstName())) {
invalidErrList.add("Invalid Full Name");
}
}
if (nextRow.getCell(1) == null || StringUtils.isBlank(nextRow.getCell(1).toString())) {
if (nextRow.getCell(1) == null || nextRow.getCell(1).getCellType() == CellType.BLANK) {
errList.add("Email");
} else {
userRegistration.setEmail(nextRow.getCell(1).getStringCellValue());
}
if (nextRow.getCell(2) == null || StringUtils.isBlank(nextRow.getCell(2).toString())) {
if (nextRow.getCell(2) == null || nextRow.getCell(2).getCellType() == CellType.BLANK) {
errList.add("Phone");
} else {
if (nextRow.getCell(2).getCellType() == CellType.NUMERIC) {
phone = NumberToTextConverter.toText(nextRow.getCell(2).getNumericCellValue());
userRegistration.setPhone(phone);
}
}
if (nextRow.getCell(3) == null || StringUtils.isBlank(nextRow.getCell(3).toString())) {
if (nextRow.getCell(3) == null || nextRow.getCell(3).getCellType() == CellType.BLANK) {
errList.add("Group");
} else {
userRegistration.setGroup(nextRow.getCell(3).getStringCellValue());
if (!userUtilityService.validateGroup(userRegistration.getGroup())) {
invalidErrList.add("Invalid Group");
}
}
if (nextRow.getCell(4) != null && !StringUtils.isBlank(nextRow.getCell(4).toString())) {
if (nextRow.getCell(4) != null && nextRow.getCell(4).getCellType() != CellType.BLANK) {
String tagStr = nextRow.getCell(4).getStringCellValue();
List<String> tagList = new ArrayList<String>();
if (!StringUtils.isEmpty(tagStr)) {
Expand Down Expand Up @@ -192,6 +196,7 @@ private void processBulkUpload(HashMap<String, String> inputDataMap) throws IOEx
}
userRegistration.setOrgName(inputDataMap.get(Constants.ORG_NAME));
userRegistration.setChannel(inputDataMap.get(Constants.ORG_NAME));
userRegistration.setSbOrgId(inputDataMap.get(Constants.ROOT_ORG_ID));
Cell statusCell = nextRow.getCell(7);
Cell errorDetails = nextRow.getCell(8);
if (statusCell == null) {
Expand All @@ -212,24 +217,27 @@ private void processBulkUpload(HashMap<String, String> inputDataMap) throws IOEx
setErrorDetails(str, errList, statusCell, errorDetails);
failedRecordsCount++;
} else {
invalidErrList = validateEmailContactAndDomain(userRegistration);
invalidErrList.addAll(validateEmailContactAndDomain(userRegistration));
if (invalidErrList.isEmpty()) {
boolean isUserCreated = userUtilityService.createUser(userRegistration);
if (isUserCreated) {
String responseCode = userUtilityService.createBulkUploadUser(userRegistration);
if (!responseCode.equalsIgnoreCase(Constants.OK)) {
failedRecordsCount++;
statusCell.setCellValue(Constants.FAILED_UPPERCASE);
errorDetails.setCellValue(responseCode);
} else {
noOfSuccessfulRecords++;
statusCell.setCellValue(Constants.SUCCESS_UPPERCASE);
errorDetails.setCellValue("");
} else {
failedRecordsCount++;
statusCell.setCellValue(Constants.FAILED_UPPERCASE);
errorDetails.setCellValue(Constants.USER_CREATION_FAILED);
}
} else {
failedRecordsCount++;
statusCell.setCellValue(Constants.FAILED_UPPERCASE);
errorDetails.setCellValue(invalidErrList.toString());
}
}
duration = System.currentTimeMillis() - startTime;
logger.info("UserBulkUploadService:: Record Completed. Time taken: "
+ duration + " milli-seconds");
}
if (totalRecordsCount == 0) {
XSSFRow row = sheet.createRow(sheet.getLastRowNum() + 1);
Expand Down Expand Up @@ -289,17 +297,9 @@ private List<String> validateEmailContactAndDomain(UserRegistration userRegistra
List<String> errList = new ArrayList<>();
if (!ProjectUtil.validateEmailPattern(userRegistration.getEmail())) {
errList.add("Invalid Email Id");
} else {
if (userUtilityService.isUserExist(Constants.EMAIL, userRegistration.getEmail())) {
errList.add(Constants.EMAIL_EXIST_ERROR);
}
}
if (!ProjectUtil.validateContactPattern(userRegistration.getPhone())) {
errList.add("Invalid Mobile Number");
} else {
if (userUtilityService.isUserExist(Constants.PHONE, String.valueOf(userRegistration.getPhone()))) {
errList.add(Constants.MOBILE_NUMBER_EXIST_ERROR);
}
}
if (!errList.isEmpty()) {
str.append("Failed to Validate User Details. Error Details - [").append(errList).append("]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@ public void getUserDetailsFromDB(List<String> userIds, List<String> fields,
boolean isUserExist(String key, String value);

boolean validateGroup(String group);

String createBulkUploadUser(UserRegistration userRegistration);

String updateBulkUploadUser(UserRegistration userRegistration);
}
90 changes: 90 additions & 0 deletions src/main/java/org/sunbird/user/service/UserUtilityServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -562,4 +562,94 @@ public boolean isUserExist(String key, String value) {
public boolean validateGroup(String group) {
return (!CollectionUtils.isEmpty(serverConfig.getBulkUploadGroupValue())) ? serverConfig.getBulkUploadGroupValue().stream().anyMatch(group::equalsIgnoreCase) : false;
}

@Override
public String createBulkUploadUser(UserRegistration userRegistration) {
Map<String, Object> request = new HashMap<>();
Map<String, Object> requestBody = new HashMap<String, Object>();
requestBody.put(Constants.EMAIL, userRegistration.getEmail());
requestBody.put(Constants.CHANNEL, userRegistration.getChannel());
requestBody.put(Constants.FIRSTNAME, userRegistration.getFirstName());
requestBody.put(Constants.EMAIL_VERIFIED, true);
requestBody.put(Constants.PHONE, userRegistration.getPhone());
requestBody.put(Constants.PHONE_VERIFIED, true);
requestBody.put(Constants.ROLES, Arrays.asList(Constants.PUBLIC));
request.put(Constants.REQUEST, requestBody);
try {
Map<String, Object> readData = (Map<String, Object>) outboundRequestHandlerService.fetchResultUsingPost(
props.getSbUrl() + props.getLmsUserCreatePath(), request, ProjectUtil.getDefaultHeaders());
if (readData != null && !Constants.OK.equalsIgnoreCase((String) readData.get(Constants.RESPONSE_CODE))) {
Map<String, Object> params = (Map<String, Object>) readData.get(Constants.PARAMS);
if (!MapUtils.isEmpty(params)) {
return (String) params.get(Constants.ERROR_MESSAGE);
}
} else if (readData != null && Constants.OK.equalsIgnoreCase((String) readData.get(Constants.RESPONSE_CODE))) {
Map<String, Object> result = (Map<String, Object>) readData.get(Constants.RESULT);
userRegistration.setUserId((String) result.get(Constants.USER_ID));
return updateBulkUploadUser(userRegistration);
}
} catch (Exception e) {
logger.error("Failed to run the create user flow. UserRegCode : " + userRegistration.getRegistrationCode(),
e);
}
return Constants.BULK_USER_CREATE_API_FAILED;
}

@Override
public String updateBulkUploadUser(UserRegistration userRegistration) {
Map<String, Object> request = new HashMap<>();
Map<String, Object> requestBody = new HashMap<String, Object>();
requestBody.put(Constants.USER_ID, userRegistration.getUserId());
Map<String, Object> profileDetails = new HashMap<String, Object>();
profileDetails.put(Constants.MANDATORY_FIELDS_EXISTS, false);
Map<String, Object> employementDetails = new HashMap<String, Object>();
employementDetails.put(Constants.DEPARTMENTNAME, userRegistration.getOrgName());
profileDetails.put(Constants.EMPLOYMENTDETAILS, employementDetails);
Map<String, Object> personalDetails = new HashMap<String, Object>();
personalDetails.put(Constants.FIRSTNAME.toLowerCase(), userRegistration.getFirstName());
personalDetails.put(Constants.PRIMARY_EMAIL, userRegistration.getEmail());
personalDetails.put(Constants.MOBILE, userRegistration.getPhone());
personalDetails.put(Constants.PHONE_VERIFIED, true);
profileDetails.put(Constants.PERSONAL_DETAILS, personalDetails);
Map<String, Object> professionDetailObj = new HashMap<String, Object>();
professionDetailObj.put(Constants.ORGANIZATION_TYPE, Constants.GOVERNMENT);
if (StringUtils.isNotEmpty(userRegistration.getPosition())) {
professionDetailObj.put(Constants.DESIGNATION, userRegistration.getPosition());
}
if (!StringUtils.isEmpty(userRegistration.getGroup())) {
professionDetailObj.put(Constants.GROUP, userRegistration.getGroup());
}
List<Map<String, Object>> professionalDetailsList = new ArrayList<Map<String, Object>>();
professionalDetailsList.add(professionDetailObj);
profileDetails.put(Constants.PROFESSIONAL_DETAILS, professionalDetailsList);
Map<String, Object> additionalProperties = new HashMap<String, Object>();
if (!StringUtils.isEmpty(userRegistration.getGroup())) {
additionalProperties.put(Constants.GROUP, userRegistration.getGroup());
}
if (!CollectionUtils.isEmpty(userRegistration.getTag())) {
additionalProperties.put(Constants.TAG, userRegistration.getTag());
}
if (!StringUtils.isEmpty(userRegistration.getExternalSystemId())) {
additionalProperties.put(Constants.EXTERNAL_SYSTEM_ID, userRegistration.getExternalSystemId());
}
if (!StringUtils.isEmpty(userRegistration.getExternalSystem())) {
additionalProperties.put(Constants.EXTERNAL_SYSTEM, userRegistration.getExternalSystem());
}
profileDetails.put(Constants.ADDITIONAL_PROPERTIES, additionalProperties);
requestBody.put(Constants.PROFILE_DETAILS, profileDetails);
request.put(Constants.REQUEST, requestBody);
Map<String, Object> readData = (Map<String, Object>) outboundRequestHandlerService.fetchResultUsingPatch(
props.getSbUrl() + props.getLmsUserUpdatePath(), request, ProjectUtil.getDefaultHeaders());
if (readData != null && !Constants.OK.equalsIgnoreCase((String) readData.get(Constants.RESPONSE_CODE))) {
Map<String, Object> params = (Map<String, Object>) readData.get(Constants.PARAMS);
if (!MapUtils.isEmpty(params)) {
return (String) params.get(Constants.ERROR_MESSAGE);
}
} else if (readData != null && Constants.OK.equalsIgnoreCase((String) readData.get(Constants.RESPONSE_CODE))) {
// if (getActivationLink(userRegistration)) {
return (String) readData.get(Constants.RESPONSE_CODE);
// }
}
return Constants.BULK_USER_UPDATE_API_FAILED;
}
}

0 comments on commit ee746b0

Please sign in to comment.