Skip to content

Commit

Permalink
Committed the code from PR #165 to 4.8.0 version (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
karthik-tarento authored Jan 20, 2023
1 parent 09b896d commit 0a5e3fe
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 25 deletions.
3 changes: 3 additions & 0 deletions src/main/java/org/sunbird/common/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,9 @@ public class Constants {
public static final String PUBLIC_COURSE_LIST = "exploreOpenCourseList";
public static final String LAST_UPDATED_ON = "lastUpdatedOn";
public static final String DESCENDING_ORDER = "desc";
public static final String SB_SUB_ORG_TYPE = "sbSubOrgType";
public static final String ORG_CODE = "orgCode";
public static final String MDO = "mdo";

private Constants() {
throw new IllegalStateException("Utility class");
Expand Down
124 changes: 99 additions & 25 deletions src/main/java/org/sunbird/org/service/ExtendedOrgServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,28 +60,48 @@ public SBApiResponse createOrg(Map<String, Object> request, String userToken) {
Map<String, Object> updateRequest = new HashMap<String, Object>();
updateRequest.put(Constants.SB_ORG_ID.toLowerCase(), orgId);
String orgType = (String) requestData.get(Constants.ORGANIZATION_TYPE);
if (requestData.containsKey(Constants.SB_ROOT_ORG_ID)
&& StringUtils.isNotEmpty((String) requestData.get(Constants.SB_ROOT_ORG_ID))) {
updateRequest.put(Constants.SB_ROOT_ORG_ID, (String) requestData.get(Constants.SB_ROOT_ORG_ID));
} else if (!(Constants.STATE.equalsIgnoreCase(orgType)
|| Constants.MINISTRY.equalsIgnoreCase(orgType))) {
// OrgType is not state / ministry and sbRootOrgId value given as Null... let's
// find the rootOrgId
String sbRootOrgId = findRootOrgId((String) requestData.get(Constants.ORG_NAME),
(String) requestData.get(Constants.MAP_ID));
if (StringUtils.isNotEmpty(sbRootOrgId)) {
updateRequest.put(Constants.SB_ROOT_ORG_ID, sbRootOrgId);
}
String orgName = (String) requestData.get(Constants.ORG_NAME);
updateRequest.put(Constants.SB_ORG_ID, orgId);
updateRequest.put(Constants.ORG_NAME, orgName);
updateRequest.put(Constants.SB_ORG_TYPE, orgType);
String mapId = (String) requestData.get(Constants.MAP_ID);
String orgCode = (String) requestData.get(Constants.ORG_CODE);
String sbRootOrgid = (String) requestData.get(Constants.SB_ROOT_ORG_ID);
updateRequest.put(Constants.SB_SUB_ORG_TYPE, requestData.get(Constants.ORGANIZATION_SUB_TYPE));
if (!StringUtils.isEmpty(mapId)) {
updateRequest.put(Constants.MAP_ID, mapId);
} else {
mapId = createMapId(requestData);
updateRequest.put(Constants.MAP_ID, mapId);
updateRequest.put(Constants.ORG_CODE, mapId);
}
Map<String, Object> compositeKey = new HashMap<String, Object>() {
private static final long serialVersionUID = 1L;
{
put(Constants.ORG_NAME, requestData.get(Constants.ORG_NAME));
put(Constants.MAP_ID, requestData.get(Constants.MAP_ID));
if (!StringUtils.isEmpty(orgCode)) {
updateRequest.put(Constants.ORG_CODE, orgCode);
}
if (!Constants.STATE.equalsIgnoreCase(orgType) && !Constants.MINISTRY.equalsIgnoreCase(orgType)) {
updateRequest.put(Constants.PARENT_MAP_ID, requestData.get(Constants.PARENT_MAP_ID));
if (!StringUtils.isEmpty(sbRootOrgid)) {
updateRequest.put(Constants.SB_ROOT_ORG_ID, sbRootOrgid);
} else {
updateRequest.put(Constants.SB_ROOT_ORG_ID, fetchRootOrgId(requestData));
}
};
cassandraOperation.updateRecord(Constants.SUNBIRD_KEY_SPACE_NAME, Constants.TABLE_ORG_HIERARCHY,
updateRequest, compositeKey);
} else {
updateRequest.put(Constants.PARENT_MAP_ID, Constants.SPV);
}
if (!StringUtils.isEmpty((String) requestData.get(Constants.MAP_ID))) {
Map<String, Object> compositeKey = new HashMap<String, Object>() {
private static final long serialVersionUID = 1L;
{
put(Constants.ORG_NAME, orgName);
put(Constants.MAP_ID, requestData.get(Constants.MAP_ID));
}
};
cassandraOperation.updateRecord(Constants.SUNBIRD_KEY_SPACE_NAME, Constants.TABLE_ORG_HIERARCHY,
updateRequest, compositeKey);
} else {
cassandraOperation.insertRecord(Constants.SUNBIRD_KEY_SPACE_NAME, Constants.TABLE_ORG_HIERARCHY,
updateRequest);
}
response.getResult().put(Constants.ORGANIZATION_ID, orgId);
response.getResult().put(Constants.RESPONSE, Constants.SUCCESS);
} else {
Expand Down Expand Up @@ -195,6 +215,7 @@ public SBApiResponse orgExtSearch(Map<String, Object> request) throws Exception
private String validateOrgRequest(Map<String, Object> requestData) {
List<String> params = new ArrayList<String>();
StringBuilder strBuilder = new StringBuilder();
String orgType = (String) requestData.get(Constants.ORGANIZATION_TYPE);
Map<String, Object> request = (Map<String, Object>) requestData.get(Constants.REQUEST);
if (ObjectUtils.isEmpty(request)) {
strBuilder.append("Request object is empty.");
Expand All @@ -205,18 +226,18 @@ private String validateOrgRequest(Map<String, Object> requestData) {
params.add(Constants.ORG_NAME);
}

if (StringUtils.isEmpty((String) request.get(Constants.ORGANIZATION_TYPE))) {
if (StringUtils.isEmpty(((String) orgType))) {
params.add(Constants.ORGANIZATION_TYPE);
} else if (!Constants.STATE.equalsIgnoreCase(orgType) && !Constants.MINISTRY.equalsIgnoreCase(orgType)) {
if (StringUtils.isEmpty((String) request.get(Constants.PARENT_MAP_ID))) {
params.add(Constants.PARENT_MAP_ID);
}
}

if (StringUtils.isEmpty((String) request.get(Constants.ORGANIZATION_SUB_TYPE))) {
params.add(Constants.ORGANIZATION_SUB_TYPE);
}

if (StringUtils.isEmpty((String) request.get(Constants.MAP_ID))) {
params.add(Constants.MAP_ID);
}

if (ObjectUtils.isEmpty(request.get(Constants.IS_TENANT))) {
params.add(Constants.IS_TENANT);
}
Expand Down Expand Up @@ -346,4 +367,57 @@ private String findRootOrgId(String orgName, String mapId) {

return StringUtils.EMPTY;
}

private String createMapId(Map<String, Object> requestData) {
Map<String, Object> queryRequest = new HashMap<>();
String prefix = StringUtils.EMPTY;
String mapIdNew = StringUtils.EMPTY;
String orgType = (String) requestData.get(Constants.ORGANIZATION_TYPE);
if (!Constants.STATE.equalsIgnoreCase(orgType) && !Constants.MINISTRY.equalsIgnoreCase(orgType)) {
queryRequest.put(Constants.PARENT_MAP_ID, requestData.get(Constants.PARENT_MAP_ID));
if (Constants.MDO.equalsIgnoreCase(orgType)) {
prefix = "D_";
} else if (Constants.ORG.equalsIgnoreCase(orgType)) {
prefix = "O_";
} else {
prefix = "X_";
}
} else {
queryRequest.put(Constants.SB_ORG_TYPE, orgType);
if (Constants.STATE.equalsIgnoreCase(orgType)) {
prefix = "S_";
} else if (Constants.MINISTRY.equalsIgnoreCase(orgType)) {
prefix = "M_";
}
}
List<Map<String, Object>> existingDataList = cassandraOperation.getRecordsByProperties(
Constants.KEYSPACE_SUNBIRD, Constants.TABLE_ORG_HIERARCHY, queryRequest,
Arrays.asList(Constants.MAP_ID));
if (CollectionUtils.isNotEmpty(existingDataList)) {
List<String> mapIdList = new ArrayList<>();
for (Map<String, Object> map : existingDataList) {
if (((String) map.get(Constants.MAP_ID)).startsWith(prefix))
mapIdList.add((String) map.get(Constants.MAP_ID));
}
mapIdNew = prefix + (mapIdList.size() + 1);
} else {
mapIdNew = prefix + "1";
}
return mapIdNew;
}

private String fetchRootOrgId(Map<String, Object> requestData) {
String sbOrgId = null;
Map<String, Object> queryRequest = new HashMap<>();
queryRequest.put(Constants.MAP_ID, requestData.get(Constants.PARENT_MAP_ID));
List<String> fields = new ArrayList<>();
fields.add(Constants.SB_ORG_ID);
List<Map<String, Object>> sbRootOrgList = cassandraOperation.getRecordsByProperties(Constants.KEYSPACE_SUNBIRD,
Constants.TABLE_ORG_HIERARCHY, queryRequest, fields);
if (CollectionUtils.isNotEmpty(sbRootOrgList)) {
Map<String, Object> data = sbRootOrgList.get(0);
sbOrgId = (String) data.get(Constants.SB_ORG_ID);
}
return sbOrgId;
}
}
2 changes: 2 additions & 0 deletions src/main/resources/cassandratablecolumn.properties
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ enddate=endDate
startdate=startDate
firstname=firstName
lastname=lastName
mapid=mapId
sborgid=sbOrgId

0 comments on commit 0a5e3fe

Please sign in to comment.