diff --git a/src/main/java/org/sunbird/catalog/controller/CatalogController.java b/src/main/java/org/sunbird/catalog/controller/CatalogController.java index b110dd2a1..7fa625bd6 100644 --- a/src/main/java/org/sunbird/catalog/controller/CatalogController.java +++ b/src/main/java/org/sunbird/catalog/controller/CatalogController.java @@ -1,5 +1,6 @@ package org.sunbird.catalog.controller; +import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -10,6 +11,7 @@ import org.springframework.web.bind.annotation.RestController; import org.sunbird.catalog.model.Catalog; import org.sunbird.catalog.service.CatalogServiceImpl; +import org.sunbird.common.model.SBApiResponse; @RestController @RequestMapping("/v1/catalog") @@ -19,7 +21,13 @@ public class CatalogController { @GetMapping("/") public ResponseEntity getCatalog(@RequestHeader("x-authenticated-user-token") String authUserToken, - @RequestParam(name = "consumption", required = false) boolean isEnrichConsumption){ + @RequestParam(name = "consumption", required = false) boolean isEnrichConsumption) { return new ResponseEntity<>(catalogService.getCatalog(authUserToken, isEnrichConsumption), HttpStatus.OK); } + + @GetMapping("/sector") + public ResponseEntity getSectors() { + SBApiResponse response = catalogService.getSectors(); + return new ResponseEntity<>(response, response.getResponseCode()); + } } diff --git a/src/main/java/org/sunbird/catalog/service/CatalogServiceImpl.java b/src/main/java/org/sunbird/catalog/service/CatalogServiceImpl.java index df032c749..70eb24ae8 100644 --- a/src/main/java/org/sunbird/catalog/service/CatalogServiceImpl.java +++ b/src/main/java/org/sunbird/catalog/service/CatalogServiceImpl.java @@ -1,11 +1,20 @@ package org.sunbird.catalog.service; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.apache.commons.collections.CollectionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @@ -13,7 +22,11 @@ import org.sunbird.catalog.model.Category; import org.sunbird.catalog.model.Framework; import org.sunbird.catalog.model.FrameworkResponse; +import org.sunbird.common.model.SBApiResponse; +import org.sunbird.common.service.OutboundRequestHandlerServiceImpl; import org.sunbird.common.util.CbExtServerProperties; +import org.sunbird.common.util.Constants; +import org.sunbird.common.util.ProjectUtil; import com.fasterxml.jackson.databind.ObjectMapper; @@ -28,6 +41,9 @@ public class CatalogServiceImpl { @Autowired private CbExtServerProperties extServerProperties; + @Autowired + private OutboundRequestHandlerServiceImpl outboundRequestHandlerServiceImpl; + public Catalog getCatalog(String authUserToken, boolean isEnrichConsumption) { return fetchCatalog(authUserToken, isEnrichConsumption); } @@ -68,4 +84,77 @@ private Catalog processResponse(Framework framework, boolean isEnrichConsumption // TODO - Enrich Consumption details for the given term name. return catalog; } + + public SBApiResponse getSectors() { + SBApiResponse response = ProjectUtil.createDefaultResponse(Constants.API_SECTOR_LIST); + try { + Map requestBody = new HashMap(); + Map request = new HashMap(); + Map search = new HashMap(); + search.put(Constants.STATUS, Constants.LIVE); + request.put(Constants.SEARCH, search); + requestBody.put(Constants.REQUEST, request); + + StringBuilder strUrl = new StringBuilder(extServerProperties.getKmBaseHost()); + strUrl.append(extServerProperties.getKmFrameworkTermSearchPath()).append("?framework=") + .append(extServerProperties.getTaxonomyFrameWorkName()).append("&category=") + .append(extServerProperties.getSectorCategoryName()); + + Map termResponse = outboundRequestHandlerServiceImpl.fetchResultUsingPost(strUrl.toString(), + requestBody, null); + List> sectors = new ArrayList(); + if (termResponse != null + && "OK".equalsIgnoreCase((String) termResponse.get(Constants.RESPONSE_CODE))) { + Map result = (Map) termResponse.get(Constants.RESULT); + List> terms = (List>) result.get(Constants.TERMS); + if (CollectionUtils.isNotEmpty(terms)) { + for (Map sector : terms) { + processSector(sector, sectors); + } + } + } + response.getResult().put(Constants.COUNT, sectors.size()); + response.getResult().put(Constants.SECTORS, sectors); + } catch (Exception e) { + String errMsg = "Failed to read sector details. Exception: " + e.getMessage(); + log.error(errMsg, e); + response.setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR); + response.getParams().setErrmsg(errMsg); + response.getParams().setStatus(Constants.FAILED); + } + return response; + } + + private void processSector(Map sector, List> sectors) { + Map newSector = new HashMap(); + for (String field : extServerProperties.getSectorFields()) { + if (sector.containsKey(field)) { + newSector.put(field, sector.get(field)); + } + } + if (sector.containsKey(Constants.CHILDREN)) { + newSector.put(Constants.CHILDREN, new ArrayList()); + processSubSector(sector, newSector); + } + sectors.add(newSector); + } + + private void processSubSector(Map sector, Map newSector) { + List> subSectorList = (List>) sector.get(Constants.CHILDREN); + Set uniqueSubSector = new HashSet(); + for (Map subSector : subSectorList) { + if (uniqueSubSector.contains((String) subSector.get(Constants.IDENTIFIER))) { + continue; + } else { + uniqueSubSector.add((String) subSector.get(Constants.IDENTIFIER)); + } + Map newSubSector = new HashMap(); + for (String field : extServerProperties.getSubSectorFields()) { + if (subSector.containsKey(field)) { + newSubSector.put(field, subSector.get(field)); + } + } + ((List) newSector.get(Constants.CHILDREN)).add(newSubSector); + } + } } diff --git a/src/main/java/org/sunbird/common/util/CbExtServerProperties.java b/src/main/java/org/sunbird/common/util/CbExtServerProperties.java index a3f58aca1..75e888b07 100644 --- a/src/main/java/org/sunbird/common/util/CbExtServerProperties.java +++ b/src/main/java/org/sunbird/common/util/CbExtServerProperties.java @@ -631,6 +631,12 @@ public void setRedisDataPort(String redisDataPort) { @Value("${redis.wheebox.key}") private String redisWheeboxKey; + @Value("${sector.category.fields}") + private String sectorFields; + + @Value("${sub.sector.category.fields}") + private String subSectorFields; + public String getRedisWheeboxKey() { return redisWheeboxKey; } @@ -693,6 +699,12 @@ public void setRedisWheeboxKey(String redisWheeboxKey) { @Value("${pdf-generator-svg-to-pdf-url}") private String pdfGeneratorSvgToPdfUrl; + @Value("${km.framework.term.search.path}") + private String kmFrameworkTermSearchPath; + + @Value("${sector.category.name}") + private String sectorCategoryName; + public boolean qListFromCacheEnabled() { return qListFromCacheEnabled; } @@ -2446,4 +2458,36 @@ public String getPdfGeneratorSvgToPdfUrl() { public void setPdfGeneratorSvgToPdfUrl(String pdfGeneratorSvgToPdfUrl) { this.pdfGeneratorSvgToPdfUrl = pdfGeneratorSvgToPdfUrl; } + + public String getKmFrameworkTermSearchPath() { + return kmFrameworkTermSearchPath; + } + + public void setKmFrameworkTermSearchPath(String kmFrameworkTermSearchPath) { + this.kmFrameworkTermSearchPath = kmFrameworkTermSearchPath; + } + + public String getSectorCategoryName() { + return sectorCategoryName; + } + + public void setSectorCategoryName(String sectorCategoryName) { + this.sectorCategoryName = sectorCategoryName; + } + + public List getSectorFields() { + return Arrays.asList(sectorFields.split(",", -1)); + } + + public void setSectorFields(String sectorFields) { + this.sectorFields = sectorFields; + } + + public List getSubSectorFields() { + return Arrays.asList(subSectorFields.split(",", -1)); + } + + public void setSubSectorFields(String subSectorFields) { + this.subSectorFields = subSectorFields; + } } \ No newline at end of file diff --git a/src/main/java/org/sunbird/common/util/Constants.java b/src/main/java/org/sunbird/common/util/Constants.java index f0e39f55e..db58a0742 100644 --- a/src/main/java/org/sunbird/common/util/Constants.java +++ b/src/main/java/org/sunbird/common/util/Constants.java @@ -1011,6 +1011,9 @@ public class Constants { public static final String USER_REPORT_EXPIRY_DATE = "report_expiry_date"; public static final String REPORT_ACCESS_EXPIRY = "reportAccessExpiry"; public static final String READ_REPORT_ACCESS_API = "api.read.access.report"; + public static final String API_SECTOR_LIST = "api.sector.list"; + public static final String TERMS = "terms"; + public static final String SECTORS = "sectors"; private Constants() { throw new IllegalStateException("Utility class"); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 85ae0598a..a6fe340fd 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -114,10 +114,14 @@ km.base.composite.search.path=v1/search km.base.composite.search.fields=appIcon,channel,contentType,description,duration,idealScreenSize,identifier,instructions,license,mimeType,name,objectType,organisation,pkgVersion,posterImage,primaryCategory,purpose,version,resourceCategory,cloudStorageKey km.base.composite.search.filters.primaryCategory=Course km.base.content.search=v1/content/search +km.framework.term.search.path=v1/framework/term/search #Taxonomy Framework and Category igot.taxonomy.framework.name=igot igot.taxonomy.category.name=Taxonomy +sector.category.name=testsector +sector.category.fields=identifier,code,name,description,appIcon +sub.sector.category.fields=identifier,code,name,description,appIcon #FRAC EndPoints frac.host=http://frac-backend-service:8095/