Skip to content

Commit

Permalink
Merge pull request #230 from sunbird-cb/userBulkUploadChanges-4.8.2
Browse files Browse the repository at this point in the history
changes for group and external system
  • Loading branch information
Haritest authored May 29, 2023
2 parents 0cffa05 + ccb9db9 commit 929fd05
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 25 deletions.
12 changes: 12 additions & 0 deletions src/main/java/org/sunbird/common/util/CbExtServerProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,10 @@ public class CbExtServerProperties {
@Value("${org.search.response.default.limit}")
private Integer orgSearchResponseDefaultLimit;

@Value("${user.bulk.upload.group.value}")
private String bulkUploadGroupValue;


public String getAssessmentSubmitTopic() {
return assessmentSubmitTopic;
}
Expand Down Expand Up @@ -1617,4 +1621,12 @@ public Integer getOrgSearchResponseDefaultLimit() {
public void setOrgSearchResponseDefaultLimit(Integer orgSearchResponseDefaultLimit) {
this.orgSearchResponseDefaultLimit = orgSearchResponseDefaultLimit;
}

public List<String> getBulkUploadGroupValue() {
return Arrays.asList(bulkUploadGroupValue.split(",", -1));
}

public void setBulkUploadGroupValue(String bulkUploadGroupValue) {
this.bulkUploadGroupValue = bulkUploadGroupValue;
}
}
4 changes: 4 additions & 0 deletions src/main/java/org/sunbird/common/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,10 @@ public class Constants {
public static final String SUCCESS_UPPERCASE = "SUCCESS";
public static final String EMPTY_FILE_FAILED = "The uploaded file is empty";
public static final String PARENT_TYPE = "parentType";
public static final String TAG = "tag";
public static final String EXTERNAL_SYSTEM_ID = "externalSystemId";
public static final String EXTERNAL_SYSTEM = "externalSystem";
public static final String GROUP = "group";
private Constants() {
throw new IllegalStateException("Utility class");
}
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/org/sunbird/common/util/ProjectUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.sunbird.common.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -149,4 +150,21 @@ public static Boolean validateFirstName( String firstName ) {
public static Boolean validateLastName( String lastName ) {
return lastName.matches( "[a-zA-Z]*" );
}

public static Boolean validateTag(List<String> tags) {
for (String tag : tags) {
if (!tag.matches("[a-zA-Z]*")) {
return false;
}
}
return true;
}

public static Boolean validateExternalSystemId(String externalSystemId) {
return externalSystemId.matches("[a-zA-Z0-9]{0,30}$");
}

public static Boolean validateExternalSystem(String externalSystem) {
return externalSystem.matches("[a-zA-Z ]{0,255}$");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,27 +117,38 @@ private void processBulkUpload(HashMap<String, String> inputDataMap) throws IOEx
Iterator<Row> rowIterator = sheet.iterator();
// incrementing the iterator inorder to skip the headers in the first row
if (rowIterator.hasNext()) {
rowIterator.next();
Row firstRow = rowIterator.next();
Cell statusCell = firstRow.getCell(8);
Cell errorDetails = firstRow.getCell(9);
if (statusCell == null) {
statusCell = firstRow.createCell(8);
}
if (errorDetails == null) {
errorDetails = firstRow.createCell(9);
}
statusCell.setCellValue("Status");
errorDetails.setCellValue("Error Details");
}
while (rowIterator.hasNext()) {
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())) {
errList.add("First Name");
} else {
userRegistration.setFirstName(nextRow.getCell(0).getStringCellValue());
if (!ProjectUtil.validateFirstName(userRegistration.getFirstName())) {
errList.add("Invalid First Name");
invalidErrList.add("Invalid First Name");
}
}
if (nextRow.getCell(1) == null || StringUtils.isBlank(nextRow.getCell(1).toString())) {
errList.add("Last Name");
} else {
userRegistration.setLastName(nextRow.getCell(1).getStringCellValue());
if (!ProjectUtil.validateLastName(userRegistration.getLastName())) {
errList.add("Invalid Last Name");
invalidErrList.add("Invalid Last Name");
}
}
if (nextRow.getCell(2) == null || StringUtils.isBlank(nextRow.getCell(2).toString())) {
Expand All @@ -153,29 +164,60 @@ private void processBulkUpload(HashMap<String, String> inputDataMap) throws IOEx
userRegistration.setPhone(phone);
}
}
if (nextRow.getCell(4) == null || StringUtils.isBlank(nextRow.getCell(4).toString())) {
errList.add("Group");
} else {
userRegistration.setGroup(nextRow.getCell(4).getStringCellValue());
if (!userUtilityService.validateGroup(userRegistration.getGroup())) {
invalidErrList.add("Invalid Group");
}
}
if (nextRow.getCell(5) != null && !StringUtils.isBlank(nextRow.getCell(5).toString())) {
String tagStr = nextRow.getCell(5).getStringCellValue();
List<String> tagList = new ArrayList<String>();
if (!StringUtils.isEmpty(tagStr)) {
tagList = Arrays.asList(tagStr.split(",", -1));
}
userRegistration.setTag(tagList);
if (!ProjectUtil.validateTag(userRegistration.getTag())) {
invalidErrList.add("Invalid Tag");
}
}
if (nextRow.getCell(6) != null && !StringUtils.isBlank(nextRow.getCell(6).toString())) {
userRegistration.setExternalSystemId(nextRow.getCell(6).getStringCellValue());
if (!ProjectUtil.validateExternalSystemId(userRegistration.getExternalSystemId())) {
invalidErrList.add("Invalid External System ID");
}
}
if (nextRow.getCell(7) != null && !StringUtils.isBlank(nextRow.getCell(7).toString())) {
userRegistration.setExternalSystem(nextRow.getCell(7).getStringCellValue());
if (!ProjectUtil.validateExternalSystem(userRegistration.getExternalSystem())) {
invalidErrList.add("Invalid External System");
}
}
userRegistration.setOrgName(inputDataMap.get(Constants.ORG_NAME));
Cell statusCell = nextRow.getCell(4);
Cell errorDetails = nextRow.getCell(5);
Cell statusCell = nextRow.getCell(8);
Cell errorDetails = nextRow.getCell(9);
if (statusCell == null) {
statusCell = nextRow.createCell(4);
statusCell = nextRow.createCell(8);
}
if (errorDetails == null) {
errorDetails = nextRow.createCell(5);
errorDetails = nextRow.createCell(9);
}
if (totalRecordsCount == 0 && errList.size() == 4) {
if (totalRecordsCount == 0 && errList.size() == 5) {
setErrorDetails(str, errList, statusCell, errorDetails);
failedRecordsCount++;
break;
} else if (totalRecordsCount > 0 && errList.size() == 4) {
} else if (totalRecordsCount > 0 && errList.size() == 5) {
break;
}
totalRecordsCount++;
if (!errList.isEmpty()) {
setErrorDetails(str, errList, statusCell, errorDetails);
failedRecordsCount++;
} else {
errList = validateEmailContactAndDomain(userRegistration);
if (errList.isEmpty()) {
invalidErrList = validateEmailContactAndDomain(userRegistration);
if (invalidErrList.isEmpty()) {
boolean isUserCreated = userUtilityService.createUser(userRegistration);
if (isUserCreated) {
noOfSuccessfulRecords++;
Expand All @@ -189,14 +231,14 @@ private void processBulkUpload(HashMap<String, String> inputDataMap) throws IOEx
} else {
failedRecordsCount++;
statusCell.setCellValue(Constants.FAILED_UPPERCASE);
errorDetails.setCellValue(errList.toString());
errorDetails.setCellValue(invalidErrList.toString());
}
}
}
if (totalRecordsCount == 0) {
XSSFRow row = sheet.createRow(sheet.getLastRowNum() + 1);
Cell statusCell = row.createCell(4);
Cell errorDetails = row.createCell(5);
Cell statusCell = row.createCell(8);
Cell errorDetails = row.createCell(9);
statusCell.setCellValue(Constants.FAILED_UPPERCASE);
errorDetails.setCellValue(Constants.EMPTY_FILE_FAILED);

Expand Down Expand Up @@ -252,29 +294,22 @@ private List<String> validateEmailContactAndDomain(UserRegistration userRegistra
List<String> errList = new ArrayList<>();
if (!ProjectUtil.validateEmailPattern(userRegistration.getEmail())) {
errList.add("Invalid Email Id");
}
else
{
} else {
if (!userUtilityService.isDomainAccepted(userRegistration.getEmail())) {
errList.add("Invalid Email Domain");
}
else
{
} 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
{
} 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
@@ -1,11 +1,14 @@
package org.sunbird.user.registration.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
* Model to store user registration details in ES server
*
* @author karthik
*
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class UserRegistration extends UserRegistrationInfo {
private String wfId;
private String priviousStatus;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package org.sunbird.user.registration.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import java.util.List;

/**
* Model object to store user registration details.
*
* @author karthik
*
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class UserRegistrationInfo {
private String registrationCode;
private String firstName;
Expand All @@ -21,6 +26,10 @@ public class UserRegistrationInfo {
private String sbRootOrgId;
private String sbOrgId;
private String phone;
private String group;
private List<String> tag;
private String externalSystemId;
private String externalSystem;

public String getRegistrationCode() {
return registrationCode;
Expand Down Expand Up @@ -133,4 +142,36 @@ public String getPhone() {
public void setPhone(String phone) {
this.phone = phone;
}

public String getGroup() {
return group;
}

public void setGroup(String group) {
this.group = group;
}

public List<String> getTag() {
return tag;
}

public void setTag(List<String> tag) {
this.tag = tag;
}

public String getExternalSystemId() {
return externalSystemId;
}

public void setExternalSystemId(String externalSystemId) {
this.externalSystemId = externalSystemId;
}

public String getExternalSystem() {
return externalSystem;
}

public void setExternalSystem(String externalSystem) {
this.externalSystem = externalSystem;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ public void getUserDetailsFromDB(List<String> userIds, List<String> fields,
boolean isUserExist(String key, String value);

Boolean isDomainAccepted(String email);

boolean validateGroup(String group);
}
21 changes: 21 additions & 0 deletions src/main/java/org/sunbird/user/service/UserUtilityServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,24 @@ public boolean updateUser(UserRegistration userRegistration) {
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 (!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(
Expand Down Expand Up @@ -551,5 +565,12 @@ public Boolean isDomainAccepted(String email) {
String emailDomain = email.split("@")[1];
return props.getUserRegistrationDomain().contains(emailDomain);
}

@Override
public boolean validateGroup(String group) {
return (!CollectionUtils.isEmpty(serverConfig.getBulkUploadGroupValue())) ? serverConfig.getBulkUploadGroupValue().stream().anyMatch(group::equalsIgnoreCase) : false;
}


}

3 changes: 2 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,5 @@ kafka.topics.user.bulk.upload=user.bulk.upload.final
kafka.topics.user.bulk.upload.group=userBulkUpload

org.channel.delimitter=_
org.search.response.default.limit=100
org.search.response.default.limit=100
user.bulk.upload.group.value=GROUP A,GROUP B,GROUP C,GROUP D,Contractual Staff,Others

0 comments on commit 929fd05

Please sign in to comment.