Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/org/sunbird/common/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +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 PHONE_OR_EMAIL_EXIST_ERROR = "User with same Email id/Phone already registered";
public static final String BULK_USER_API_FAILED = "Bulk User Update API Failed";

private Constants() {
throw new IllegalStateException("Utility class");
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 All @@ -184,7 +188,7 @@ private void processBulkUpload(HashMap<String, String> inputDataMap) throws IOEx
invalidErrList.add("Invalid External System ID");
}
}
if (nextRow.getCell(6) != null && !StringUtils.isBlank(nextRow.getCell(6).toString())) {
if (nextRow.getCell(6) != null && nextRow.getCell(6).getCellType() != CellType.BLANK) {
userRegistration.setExternalSystem(nextRow.getCell(6).getStringCellValue());
if (!ProjectUtil.validateExternalSystem(userRegistration.getExternalSystem())) {
invalidErrList.add("Invalid External System");
Expand Down Expand Up @@ -212,24 +216,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(StringUtils.isBlank(responseCode) || !responseCode.equalsIgnoreCase("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 +296,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 @@ -22,6 +22,8 @@ public interface UserUtilityService {

boolean createUser(UserRegistration userRegistration);

String createBulkUploadUser(UserRegistration userRegistration);

boolean updateUser(UserRegistration userRegistration);

boolean getActivationLink(UserRegistration userRegistration);
Expand All @@ -40,4 +42,6 @@ public void getUserDetailsFromDB(List<String> userIds, List<String> fields,
boolean isUserExist(String key, String value);

boolean validateGroup(String group);

String updateBulkUploadUser(UserRegistration userRegistration);
}
89 changes: 89 additions & 0 deletions src/main/java/org/sunbird/user/service/UserUtilityServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.client.RestTemplate;
import org.sunbird.cassandra.utils.CassandraOperation;
import org.sunbird.common.model.SearchUserApiContent;
Expand Down Expand Up @@ -562,4 +563,92 @@ 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) {
String responseCode = "";
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> result = (Map<String, Object>) readData.get(Constants.RESULT);
userRegistration.setUserId((String) result.get(Constants.USER_ID));
Map<String, Object> userData = getUsersReadData(userRegistration.getUserId(), StringUtils.EMPTY,
StringUtils.EMPTY);
if (!CollectionUtils.isEmpty(userData)) {
userRegistration.setUserName((String) userData.get(Constants.USER_NAME));
userRegistration.setSbOrgId((String) userData.get(Constants.ROOT_ORG_ID) );
return updateBulkUploadUser(userRegistration);
}
}

} catch (Exception e) {
logger.error("Failed to run the create user flow. UserRegCode : " + userRegistration.getRegistrationCode(),
e);
}
return Constants.PHONE_OR_EMAIL_EXIST_ERROR;
}

@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 (Constants.OK.equalsIgnoreCase((String) readData.get(Constants.RESPONSE_CODE))) {
// if (getActivationLink(userRegistration)) {
return (String) readData.get(Constants.RESPONSE_CODE);
// }
}
return Constants.BULK_USER_API_FAILED;
}
}