Skip to content

Commit

Permalink
Fixed issue in SearchBy Provider and clean-up based on review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
karthik-tarento committed Jan 27, 2022
1 parent 540420c commit 2bf5f9f
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 125 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package org.sunbird.searchby.controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import org.sunbird.searchby.model.SearchByFilter;
import org.sunbird.searchby.service.SearchByService;

@RestController
Expand All @@ -19,23 +14,13 @@ public class SearchByController {
@Autowired
private SearchByService searchByService;

//v1/readCompetency
@GetMapping("/v1/browseByCompetency")
public ResponseEntity<?> browseByCompetency(@RequestHeader("x-authenticated-user-token") String authUserToken)
throws Exception {
return new ResponseEntity<>(searchByService.getCompetencyDetails(authUserToken), HttpStatus.OK);
}

@PostMapping("/v1/browseByCompetency")
public ResponseEntity<?> browseByCompetencyByFilter(
@RequestHeader("x-authenticated-user-token") String authUserToken,
@Valid @RequestBody SearchByFilter filter) throws Exception {
if (filter == null) {
throw new Exception("Invalid Request");
}
return new ResponseEntity<>(searchByService.getCompetencyDetailsByFilter(authUserToken, filter),
HttpStatus.OK);
}

@GetMapping("/v1/browseByProvider")
public ResponseEntity<?> browseByProvider(@RequestHeader("x-authenticated-user-token") String authUserToken)
throws Exception {
Expand Down
46 changes: 0 additions & 46 deletions src/main/java/org/sunbird/searchby/model/SearchByFilter.java

This file was deleted.

68 changes: 5 additions & 63 deletions src/main/java/org/sunbird/searchby/service/SearchByService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand All @@ -18,7 +17,6 @@
import org.sunbird.core.logger.CbExtLogger;
import org.sunbird.searchby.model.CompetencyInfo;
import org.sunbird.searchby.model.ProviderInfo;
import org.sunbird.searchby.model.SearchByFilter;

import com.fasterxml.jackson.databind.ObjectMapper;

Expand Down Expand Up @@ -48,68 +46,15 @@ public Collection<CompetencyInfo> getCompetencyDetails(String authUserToken) thr
.getCache(Constants.COMPETENCY_CACHE_NAME);
return competencyMap.values();
}

public Collection<CompetencyInfo> getCompetencyDetailsByFilter(String authUserToken, SearchByFilter filter)
throws Exception {

if (filter.isEmptyFilter()) {
return getCompetencyDetails(authUserToken);
}

Map<String, CompetencyInfo> objectNameCache = (Map<String, CompetencyInfo>) redisCacheMgr
.getCache(Constants.COMPETENCY_CACHE_NAME);
Object objectAreaCache = redisCacheMgr.getCache(Constants.COMPETENCY_CACHE_NAME_BY_AREA);
Object objectTypeCache = redisCacheMgr.getCache(Constants.COMPETENCY_CACHE_NAME_BY_TYPE);
if (CollectionUtils.isEmpty(objectNameCache) || objectAreaCache == null || objectTypeCache == null) {
logger.info("Initializing/Refreshing the Cache Value.");
updateCompetencyDetails(authUserToken);
}
objectNameCache = (Map<String, CompetencyInfo>) redisCacheMgr.getCache(Constants.COMPETENCY_CACHE_NAME);
objectAreaCache = redisCacheMgr.getCache(Constants.COMPETENCY_CACHE_NAME_BY_AREA);
objectTypeCache = redisCacheMgr.getCache(Constants.COMPETENCY_CACHE_NAME_BY_TYPE);

// Apply Name filter
Map<String, CompetencyInfo> afterFilter = new HashMap<>();
if (!CollectionUtils.isEmpty(filter.getCompetencyName())) {
List<String> lowerCaseNameFilter = listToLowerCase(filter.getCompetencyName());
for (CompetencyInfo eachInfo : objectNameCache.values()) {
if (lowerCaseNameFilter.contains(eachInfo.getName().toLowerCase().trim())) {
afterFilter.put(eachInfo.getId(), eachInfo);
}
}
}

if (!CollectionUtils.isEmpty(filter.getCompetencyType())) {
List<String> lowerCaseTypeFilter = listToLowerCase(filter.getCompetencyType());
Map<String, List<CompetencyInfo>> typeCache = (Map<String, List<CompetencyInfo>>) objectTypeCache;
for (Map.Entry<String, List<CompetencyInfo>> eachInfo : typeCache.entrySet()) {
if (lowerCaseTypeFilter.contains(eachInfo.getKey().toLowerCase().trim())) {
for (CompetencyInfo competencyInfo : eachInfo.getValue())
afterFilter.put(competencyInfo.getId(), competencyInfo);
}
}
}

if (!CollectionUtils.isEmpty(filter.getCompetencyArea())) {
List<String> lowerCaseAreaFilter = listToLowerCase(filter.getCompetencyArea());
Map<String, List<CompetencyInfo>> areaCache = (Map<String, List<CompetencyInfo>>) objectAreaCache;
for (Map.Entry<String, List<CompetencyInfo>> eachInfo : areaCache.entrySet()) {
if (lowerCaseAreaFilter.contains(eachInfo.getKey().toLowerCase().trim())) {
for (CompetencyInfo competencyInfo : eachInfo.getValue())
afterFilter.put(competencyInfo.getId(), competencyInfo);
}
}
}
return afterFilter.values();
}


public Collection<ProviderInfo> getProviderDetails(String authUserToken) throws Exception {
Object object = redisCacheMgr.getCache(Constants.PROVIDER_CACHE_NAME);
if (object == null) {
logger.info("");
updateProviderDetails(authUserToken);
}
return (Collection<ProviderInfo>) redisCacheMgr.getCache(Constants.PROVIDER_CACHE_NAME);
Map<String, ProviderInfo> providerMap = (Map<String, ProviderInfo>) redisCacheMgr.getCache(Constants.PROVIDER_CACHE_NAME);
return providerMap.values();
}

private void updateCompetencyDetails(String authUserToken) throws Exception {
Expand Down Expand Up @@ -147,6 +92,7 @@ private void updateCompetencyDetails(String authUserToken) throws Exception {
if (!CollectionUtils.isEmpty(facetValueList)) {
for (Map<String, Object> facetValueObj : facetValueList) {
CompetencyInfo compInfo = new CompetencyInfo();
//TODO - Make sure which competency field is unique
compInfo.setContentCount((int) facetValueObj.get("count"));
competencyMap.put((String) facetValueObj.get("name"), compInfo);
}
Expand Down Expand Up @@ -331,10 +277,6 @@ private void updateProviderDetails(String authUserToken) throws Exception {
throw err;
}

redisCacheMgr.putCache(Constants.PROVIDER_CACHE_NAME, providerMap.values());
}

private List<String> listToLowerCase(List<String> convertString) {
return convertString.stream().map(String::toLowerCase).map(String::trim).collect(Collectors.toList());
redisCacheMgr.putCache(Constants.PROVIDER_CACHE_NAME, providerMap);
}
}

0 comments on commit 2bf5f9f

Please sign in to comment.