diff --git a/src/main/java/org/sunbird/assessment/service/AssessmentServiceImpl.java b/src/main/java/org/sunbird/assessment/service/AssessmentServiceImpl.java index 7bd34c107..2fe1e5c48 100644 --- a/src/main/java/org/sunbird/assessment/service/AssessmentServiceImpl.java +++ b/src/main/java/org/sunbird/assessment/service/AssessmentServiceImpl.java @@ -83,18 +83,8 @@ public Map submitAssessment(String rootOrg, AssessmentSubmission Map 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()); @@ -107,22 +97,9 @@ public Map submitAssessment(String rootOrg, AssessmentSubmission if (Boolean.TRUE.equals(data.isAssessment()) && !"".equals(parentId)) { // get parent data for assessment - try { - Map contentResponse = contentService.searchLiveContent(parentId); - if (!ObjectUtils.isEmpty(contentResponse)) { - Map contentResult = (Map) contentResponse.get(Constants.RESULT); - if (0 < (Integer) contentResult.get(Constants.COUNT)) { - List> contentList = (List>) contentResult - .get(Constants.CONTENT); - Map 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()); diff --git a/src/main/java/org/sunbird/common/service/ContentService.java b/src/main/java/org/sunbird/common/service/ContentService.java index 287e60395..64b2a36ac 100644 --- a/src/main/java/org/sunbird/common/service/ContentService.java +++ b/src/main/java/org/sunbird/common/service/ContentService.java @@ -23,5 +23,11 @@ public interface ContentService { public SunbirdApiResp getQuestionListDetails(List questionIdList); public Map searchLiveContent(String contentId); + + public Map getHierarchyResponseMap(String contentId); + + public String getParentIdentifier(String resourceId); + + public String getContentType(String resourceId); } diff --git a/src/main/java/org/sunbird/common/service/ContentServiceImpl.java b/src/main/java/org/sunbird/common/service/ContentServiceImpl.java index e0adbf0de..b8a6a8b03 100644 --- a/src/main/java/org/sunbird/common/service/ContentServiceImpl.java +++ b/src/main/java/org/sunbird/common/service/ContentServiceImpl.java @@ -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; @@ -172,7 +173,7 @@ public SunbirdApiResp getQuestionListDetails(List questionIdList) { return null; } - + public Map searchLiveContent(String contentId) { Map response = null; HashMap headerValues = new HashMap<>(); @@ -184,7 +185,8 @@ public Map searchLiveContent(String contentId) { Map 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 contentRequest = new HashMap<>(); contentRequest.put(Constants.REQUEST, contentRequestValue); response = outboundRequestHandlerService.fetchResultUsingPost( @@ -194,4 +196,46 @@ public Map searchLiveContent(String contentId) { } return null; } + + public Map getHierarchyResponseMap(String contentId) { + StringBuilder url = new StringBuilder(); + url.append(serverConfig.getContentHost()).append(serverConfig.getHierarchyEndPoint()).append("/" + contentId) + .append("?hierarchyType=detail"); + Map response = (Map) outboundRequestHandlerService.fetchResult(url.toString()); + if (ObjectUtils.isEmpty(response)) { + return Collections.EMPTY_MAP; + } + + return response; + } + + public String getParentIdentifier(String resourceId) { + String parentId = ""; + Map response = getHierarchyResponseMap(resourceId); + if (Constants.OK.equalsIgnoreCase((String) response.get(Constants.RESPONSE_CODE))) { + Map resultMap = (Map) response.get(Constants.RESULT); + if (!ObjectUtils.isEmpty(resultMap)) { + Map contentMap = (Map) resultMap.get(Constants.CONTENT); + if (!ObjectUtils.isEmpty(contentMap)) { + parentId = (String) contentMap.get(Constants.PARENT); + } + } + } + return parentId; + } + + public String getContentType(String resourceId) { + String parentContentType = ""; + Map response = getHierarchyResponseMap(resourceId); + if (Constants.OK.equalsIgnoreCase((String) response.get(Constants.RESPONSE_CODE))) { + Map resultMap = (Map) response.get(Constants.RESULT); + if (!ObjectUtils.isEmpty(resultMap)) { + Map contentMap = (Map) resultMap.get(Constants.CONTENT); + if (!ObjectUtils.isEmpty(contentMap)) { + parentContentType = (String) contentMap.get(Constants.CONTENT_TYPE_KEY); + } + } + } + return parentContentType; + } } diff --git a/src/main/java/org/sunbird/common/util/Constants.java b/src/main/java/org/sunbird/common/util/Constants.java index 060a9ccbf..47df4c80e 100644 --- a/src/main/java/org/sunbird/common/util/Constants.java +++ b/src/main/java/org/sunbird/common/util/Constants.java @@ -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"); diff --git a/src/main/java/org/sunbird/course/service/LatestCoursesAlertNotificationService.java b/src/main/java/org/sunbird/course/service/LatestCoursesAlertNotificationService.java index 9df10a603..6e56908a2 100644 --- a/src/main/java/org/sunbird/course/service/LatestCoursesAlertNotificationService.java +++ b/src/main/java/org/sunbird/course/service/LatestCoursesAlertNotificationService.java @@ -133,7 +133,7 @@ public NewCourseData getLatestAddedCourses() { lastUpdatedOn.put(Constants.MAX, maxValue.toString()); Map 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 sortBy = new HashMap<>(); sortBy.put(Constants.LAST_UPDATED_ON, Constants.DESCENDING_ORDER);