Skip to content

Commit

Permalink
4.8.12 sector api (#523)
Browse files Browse the repository at this point in the history
* Added sector APIs

* Updated the typo is property path

* Fixed error in reading sub sector details

* Fixed error in reading sub sector details

* reverted properties changes
  • Loading branch information
karthik-tarento authored Mar 28, 2024
1 parent 41ae7ea commit f77c668
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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")
Expand All @@ -19,7 +21,13 @@ public class CatalogController {

@GetMapping("/")
public ResponseEntity<Catalog> 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<SBApiResponse> getSectors() {
SBApiResponse response = catalogService.getSectors();
return new ResponseEntity<>(response, response.getResponseCode());
}
}
89 changes: 89 additions & 0 deletions src/main/java/org/sunbird/catalog/service/CatalogServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
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;
import org.sunbird.catalog.model.Catalog;
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;

Expand All @@ -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);
}
Expand Down Expand Up @@ -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<String, Object> requestBody = new HashMap<String, Object>();
Map<String, Object> request = new HashMap<String, Object>();
Map<String, Object> search = new HashMap<String, Object>();
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<String, Object> termResponse = outboundRequestHandlerServiceImpl.fetchResultUsingPost(strUrl.toString(),
requestBody, null);
List<Map<String, Object>> sectors = new ArrayList();
if (termResponse != null
&& "OK".equalsIgnoreCase((String) termResponse.get(Constants.RESPONSE_CODE))) {
Map<String, Object> result = (Map<String, Object>) termResponse.get(Constants.RESULT);
List<Map<String, Object>> terms = (List<Map<String, Object>>) result.get(Constants.TERMS);
if (CollectionUtils.isNotEmpty(terms)) {
for (Map<String, Object> 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<String, Object> sector, List<Map<String, Object>> sectors) {
Map<String, Object> newSector = new HashMap<String, Object>();
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<String, Object> sector, Map<String, Object> newSector) {
List<Map<String, Object>> subSectorList = (List<Map<String, Object>>) sector.get(Constants.CHILDREN);
Set<String> uniqueSubSector = new HashSet<String>();
for (Map<String, Object> subSector : subSectorList) {
if (uniqueSubSector.contains((String) subSector.get(Constants.IDENTIFIER))) {
continue;
} else {
uniqueSubSector.add((String) subSector.get(Constants.IDENTIFIER));
}
Map<String, Object> newSubSector = new HashMap<String, Object>();
for (String field : extServerProperties.getSubSectorFields()) {
if (subSector.containsKey(field)) {
newSubSector.put(field, subSector.get(field));
}
}
((List) newSector.get(Constants.CHILDREN)).add(newSubSector);
}
}
}
44 changes: 44 additions & 0 deletions src/main/java/org/sunbird/common/util/CbExtServerProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<String> getSectorFields() {
return Arrays.asList(sectorFields.split(",", -1));
}

public void setSectorFields(String sectorFields) {
this.sectorFields = sectorFields;
}

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

public void setSubSectorFields(String subSectorFields) {
this.subSectorFields = subSectorFields;
}
}
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 @@ -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");
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down

0 comments on commit f77c668

Please sign in to comment.