Skip to content

Commit

Permalink
Fixes for getting v1 assessment data. (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
karthik-tarento authored Dec 6, 2022
1 parent 5aaa156 commit b539cd7
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,8 @@ public Map<String, Object> submitAssessment(String rootOrg, AssessmentSubmission
Map<String, Object> persist = new HashMap<>();

// Fetch parent of an assessment with status live
String parentId = "";
try {
SunbirdApiResp contentHierarchy = contentService.getHeirarchyResponse(data.getIdentifier());
if (contentHierarchy != null) {
parentId = contentHierarchy.getResult().getContent().getParent();
}
} catch (Exception e) {
logger.error(e);
}
if (parentId == null) {
parentId = "";
}
String parentId = contentService.getParentIdentifier(data.getIdentifier());

persist.put("parent", parentId);
persist.put(RESULT, result);
persist.put("sourceId", data.getIdentifier());
Expand All @@ -107,22 +97,9 @@ public Map<String, Object> submitAssessment(String rootOrg, AssessmentSubmission

if (Boolean.TRUE.equals(data.isAssessment()) && !"".equals(parentId)) {
// get parent data for assessment
try {
Map<String, Object> contentResponse = contentService.searchLiveContent(parentId);
if (!ObjectUtils.isEmpty(contentResponse)) {
Map<String, Object> contentResult = (Map<String, Object>) contentResponse.get(Constants.RESULT);
if (0 < (Integer) contentResult.get(Constants.COUNT)) {
List<Map<String, Object>> contentList = (List<Map<String, Object>>) contentResult
.get(Constants.CONTENT);
Map<String, Object> content = contentList.get(0);
persist.put(Constants.PARENT_CONTENT_SEARCH, (String) content.get(Constants.CONTENT_TYPE_SEARCH));
}
}
} catch (Exception e) {
logger.error(e);
}
persist.put(Constants.PARENT_CONTENT_TYPE, contentService.getContentType(parentId));
} else {
persist.put("parentContentType", "");
persist.put(Constants.PARENT_CONTENT_TYPE, "");
}

logger.info("Trying to persist assessment data -> " + persist.toString());
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/sunbird/common/service/ContentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,11 @@ public interface ContentService {
public SunbirdApiResp getQuestionListDetails(List<String> questionIdList);

public Map<String, Object> searchLiveContent(String contentId);

public Map<String, Object> getHierarchyResponseMap(String contentId);

public String getParentIdentifier(String resourceId);

public String getContentType(String resourceId);
}

48 changes: 46 additions & 2 deletions src/main/java/org/sunbird/common/service/ContentServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.sunbird.common.model.SunbirdApiHierarchyResultBatch;
import org.sunbird.common.model.SunbirdApiResp;
import org.sunbird.common.model.SunbirdApiUserCourseListResp;
Expand Down Expand Up @@ -172,7 +173,7 @@ public SunbirdApiResp getQuestionListDetails(List<String> questionIdList) {

return null;
}

public Map<String, Object> searchLiveContent(String contentId) {
Map<String, Object> response = null;
HashMap<String, String> headerValues = new HashMap<>();
Expand All @@ -184,7 +185,8 @@ public Map<String, Object> searchLiveContent(String contentId) {
Map<String, Object> contentRequestValue = new HashMap<>();
contentRequestValue.put(Constants.FILTERS, filters);
contentRequestValue.put(Constants.FIELDS,
Arrays.asList(Constants.IDENTIFIER, Constants.NAME, Constants.PRIMARY_CATEGORY, Constants.BATCHES, Constants.LEAF_NODES_COUNT, Constants.CONTENT_TYPE_SEARCH));
Arrays.asList(Constants.IDENTIFIER, Constants.NAME, Constants.PRIMARY_CATEGORY, Constants.BATCHES,
Constants.LEAF_NODES_COUNT, Constants.CONTENT_TYPE_KEY));
Map<String, Object> contentRequest = new HashMap<>();
contentRequest.put(Constants.REQUEST, contentRequestValue);
response = outboundRequestHandlerService.fetchResultUsingPost(
Expand All @@ -194,4 +196,46 @@ public Map<String, Object> searchLiveContent(String contentId) {
}
return null;
}

public Map<String, Object> getHierarchyResponseMap(String contentId) {
StringBuilder url = new StringBuilder();
url.append(serverConfig.getContentHost()).append(serverConfig.getHierarchyEndPoint()).append("/" + contentId)
.append("?hierarchyType=detail");
Map<String, Object> response = (Map<String, Object>) outboundRequestHandlerService.fetchResult(url.toString());
if (ObjectUtils.isEmpty(response)) {
return Collections.EMPTY_MAP;
}

return response;
}

public String getParentIdentifier(String resourceId) {
String parentId = "";
Map<String, Object> response = getHierarchyResponseMap(resourceId);
if (Constants.OK.equalsIgnoreCase((String) response.get(Constants.RESPONSE_CODE))) {
Map<String, Object> resultMap = (Map<String, Object>) response.get(Constants.RESULT);
if (!ObjectUtils.isEmpty(resultMap)) {
Map<String, Object> contentMap = (Map<String, Object>) resultMap.get(Constants.CONTENT);
if (!ObjectUtils.isEmpty(contentMap)) {
parentId = (String) contentMap.get(Constants.PARENT);
}
}
}
return parentId;
}

public String getContentType(String resourceId) {
String parentContentType = "";
Map<String, Object> response = getHierarchyResponseMap(resourceId);
if (Constants.OK.equalsIgnoreCase((String) response.get(Constants.RESPONSE_CODE))) {
Map<String, Object> resultMap = (Map<String, Object>) response.get(Constants.RESULT);
if (!ObjectUtils.isEmpty(resultMap)) {
Map<String, Object> contentMap = (Map<String, Object>) resultMap.get(Constants.CONTENT);
if (!ObjectUtils.isEmpty(contentMap)) {
parentContentType = (String) contentMap.get(Constants.CONTENT_TYPE_KEY);
}
}
}
return parentContentType;
}
}
5 changes: 3 additions & 2 deletions src/main/java/org/sunbird/common/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -527,12 +527,13 @@ public class Constants {
public static final String LAST_SENT_DATE = "lastsentdate";
public static final String MIN = "min";
public static final String MAX = "max";
public static final String CONTENT_TYPE_SEARCH = "contentType";
public static final String PARENT_CONTENT_SEARCH="parentContentType";
public static final String CONTENT_TYPE_KEY = "contentType";
public static final String PARENT_CONTENT_TYPE = "parentContentType";
public static final String NEW_COURSES = "newcourses";
public static final String OVERVIEW_BATCH_KEY = "/overview?batchId=";
public static final String LEAF_NODES_COUNT = "leafNodesCount";
public static final String CLIENT_ERROR = "CLIENT_ERROR";
public static final String PARENT = "parent";

private Constants() {
throw new IllegalStateException("Utility class");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public NewCourseData getLatestAddedCourses() {
lastUpdatedOn.put(Constants.MAX, maxValue.toString());
Map<String, Object> filters = new HashMap<>();
filters.put(Constants.PRIMARY_CATEGORY, Collections.singletonList(Constants.COURSE));
filters.put(Constants.CONTENT_TYPE_SEARCH, Collections.singletonList(Constants.COURSE));
filters.put(Constants.CONTENT_TYPE_KEY, Collections.singletonList(Constants.COURSE));
filters.put(Constants.LAST_UPDATED_ON, lastUpdatedOn);
Map<String, Object> sortBy = new HashMap<>();
sortBy.put(Constants.LAST_UPDATED_ON, Constants.DESCENDING_ORDER);
Expand Down

0 comments on commit b539cd7

Please sign in to comment.