Skip to content

Commit

Permalink
Using Serialized object instead of HashMap for positions
Browse files Browse the repository at this point in the history
  • Loading branch information
karthik-tarento committed Jun 10, 2022
1 parent 1496edd commit d6db94a
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 60 deletions.
8 changes: 4 additions & 4 deletions src/main/java/org/sunbird/common/model/FracApiResponse.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.sunbird.common.model;

import java.util.List;
import java.util.Map;

import org.sunbird.searchby.model.FracCommonInfo;
import org.sunbird.workallocation.model.FracStatusInfo;

public class FracApiResponse {
private FracStatusInfo statusInfo;
private List<Map<String, Object>> responseData;
private List<FracCommonInfo> responseData;

public FracStatusInfo getStatusInfo() {
return statusInfo;
Expand All @@ -17,11 +17,11 @@ public void setStatusInfo(FracStatusInfo statusInfo) {
this.statusInfo = statusInfo;
}

public List<Map<String, Object>> getResponseData() {
public List<FracCommonInfo> getResponseData() {
return responseData;
}

public void setResponseData(List<Map<String, Object>> responseData) {
public void setResponseData(List<FracCommonInfo> responseData) {
this.responseData = responseData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public ResponseEntity<?> browseByProvider(@RequestHeader(Constants.X_AUTH_TOKEN)
}

@GetMapping("/v1/listPositions")
public ResponseEntity<?> listDesignations(@RequestHeader(Constants.X_AUTH_TOKEN) String userToken) {
FracApiResponse response = searchByService.listDesignations(userToken);
public ResponseEntity<?> listPositions(@RequestHeader(Constants.X_AUTH_TOKEN) String userToken) {
FracApiResponse response = searchByService.listPositions(userToken);
return new ResponseEntity<>(response, HttpStatus.valueOf(response.getStatusInfo().getStatusCode()));
}
}
30 changes: 2 additions & 28 deletions src/main/java/org/sunbird/searchby/model/CompetencyInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,16 @@

import java.io.Serializable;

public class CompetencyInfo implements Serializable {
public class CompetencyInfo extends FracCommonInfo implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private String id;

private String type;
private String description;
private String status;
private String source;
private String competencyType;
private String competencyArea;
private Integer contentCount;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getContentCount() {
return contentCount;
}
Expand All @@ -30,14 +20,6 @@ public void setContentCount(Integer contentCount) {
this.contentCount = contentCount;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getType() {
return type;
}
Expand All @@ -46,14 +28,6 @@ public void setType(String type) {
this.type = type;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getStatus() {
return status;
}
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/org/sunbird/searchby/model/FracCommonInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.sunbird.searchby.model;

import java.io.Serializable;

public class FracCommonInfo implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private String id;
private String description;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public FracCommonInfo() {
}

public FracCommonInfo(String id, String name, String description) {
this.id = id;
this.name = name;
this.description = description;
}
}
1 change: 1 addition & 0 deletions src/main/java/org/sunbird/searchby/model/ProviderInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.Serializable;

public class ProviderInfo implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private String logoUrl;
private String description;
Expand Down
43 changes: 17 additions & 26 deletions src/main/java/org/sunbird/searchby/service/SearchByService.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.sunbird.common.util.Constants;
import org.sunbird.core.logger.CbExtLogger;
import org.sunbird.searchby.model.CompetencyInfo;
import org.sunbird.searchby.model.FracCommonInfo;
import org.sunbird.searchby.model.ProviderInfo;
import org.sunbird.workallocation.model.FracStatusInfo;

Expand Down Expand Up @@ -65,26 +66,26 @@ public Collection<ProviderInfo> getProviderDetails(String authUserToken) throws
return providerMap.values();
}

public FracApiResponse listDesignations(String userToken) {
public FracApiResponse listPositions(String userToken) {
FracApiResponse response = new FracApiResponse();
response.setStatusInfo(new FracStatusInfo());
response.getStatusInfo().setStatusCode(HttpStatus.OK.value());

Map<String, List<Map<String, Object>>> positionList = (Map<String, List<Map<String, Object>>>) redisCacheMgr
Map<String, List<FracCommonInfo>> positionMap = (Map<String, List<FracCommonInfo>>) redisCacheMgr
.getCache(Constants.POSITIONS_CACHE_NAME);
if (ObjectUtils.isEmpty(positionList)
|| CollectionUtils.isEmpty(positionList.get(Constants.POSITIONS_CACHE_NAME))) {
if (ObjectUtils.isEmpty(positionMap)
|| CollectionUtils.isEmpty(positionMap.get(Constants.POSITIONS_CACHE_NAME))) {
logger.info("Initializing / Refreshing the Cache value for key : " + Constants.POSITIONS_CACHE_NAME);
try {
positionList = updateDesignationDetails(userToken);
response.setResponseData(positionList.get(Constants.POSITIONS_CACHE_NAME));
positionMap = updateDesignationDetails(userToken);
response.setResponseData(positionMap.get(Constants.POSITIONS_CACHE_NAME));
} catch (Exception e) {
logger.error(e);
response.getStatusInfo().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
response.getStatusInfo().setErrorMessage(e.getMessage());
}
} else {
response.setResponseData(positionList.get(Constants.POSITIONS_CACHE_NAME));
response.setResponseData(positionMap.get(Constants.POSITIONS_CACHE_NAME));
}

return response;
Expand Down Expand Up @@ -322,7 +323,7 @@ private Map<String, ProviderInfo> updateProviderDetails(String authUserToken) th
return providerMap;
}

private Map<String, List<Map<String, Object>>> updateDesignationDetails(String authUserToken) throws Exception {
private Map<String, List<FracCommonInfo>> updateDesignationDetails(String authUserToken) throws Exception {
Map<String, String> headers = new HashMap<>();
HashMap<String, Object> reqBody = new HashMap<>();
headers = new HashMap<>();
Expand All @@ -344,7 +345,7 @@ private Map<String, List<Map<String, Object>>> updateDesignationDetails(String a
reqBody.put(Constants.SEARCHES, searchList);

List<String> positionNameList = new ArrayList<String>();
List<Map<String, Object>> positionList = getMasterPositionList(positionNameList);
List<FracCommonInfo> positionList = getMasterPositionList(positionNameList);

Map<String, Object> fracSearchRes = outboundRequestHandlerService.fetchResultUsingPost(
cbExtServerProperties.getFracHost() + cbExtServerProperties.getFracSearchPath(), reqBody, headers);
Expand All @@ -353,11 +354,8 @@ private Map<String, List<Map<String, Object>>> updateDesignationDetails(String a
if (!CollectionUtils.isEmpty(fracResponseList)) {
for (Map<String, Object> respObj : fracResponseList) {
if (!positionNameList.contains((String) respObj.get(Constants.NAME))) {
Map<String, Object> fracInfo = new HashMap<String, Object>();
fracInfo.put(Constants.DESCRIPTION, (String) respObj.get(Constants.DESCRIPTION));
fracInfo.put(Constants.ID, (String) respObj.get(Constants.ID));
fracInfo.put(Constants.NAME, (String) respObj.get(Constants.NAME));
positionList.add(fracInfo);
positionList.add(new FracCommonInfo((String) respObj.get(Constants.ID),
(String) respObj.get(Constants.NAME), (String) respObj.get(Constants.DESCRIPTION)));
positionNameList.add((String) respObj.get(Constants.NAME));
}
}
Expand All @@ -370,14 +368,14 @@ private Map<String, List<Map<String, Object>>> updateDesignationDetails(String a
}
throw err;
}
Map<String, List<Map<String, Object>>> positionMap = new HashMap<String, List<Map<String, Object>>>();
Map<String, List<FracCommonInfo>> positionMap = new HashMap<String, List<FracCommonInfo>>();
positionMap.put(Constants.POSITIONS_CACHE_NAME, positionList);
redisCacheMgr.putCache(Constants.POSITIONS_CACHE_NAME, positionMap);
return positionMap;
}

private List<Map<String, Object>> getMasterPositionList(List<String> positionNameList) throws Exception {
List<Map<String, Object>> positionList = new ArrayList<Map<String, Object>>();
private List<FracCommonInfo> getMasterPositionList(List<String> positionNameList) throws Exception {
List<FracCommonInfo> positionList = new ArrayList<FracCommonInfo>();
JsonNode jsonTree = new ObjectMapper().readTree(this.getClass().getClassLoader()
.getResourceAsStream(cbExtServerProperties.getMasterPositionListFileName()));
JsonNode positionsObj = jsonTree.get(Constants.POSITIONS);
Expand All @@ -386,15 +384,8 @@ private List<Map<String, Object>> getMasterPositionList(List<String> positionNam
while (positionsItr.hasNext()) {
JsonNode position = positionsItr.next();
positionNameList.add(position.get(Constants.NAME).asText());
Map<String, Object> positionObj = new HashMap<String, Object>() {
private static final long serialVersionUID = 1L;
{
put(Constants.ID, position.get(Constants.ID).asText());
put(Constants.NAME, position.get(Constants.NAME).asText());
put(Constants.DESCRIPTION, position.get(Constants.DESCRIPTION).asText());
}
};
positionList.add(positionObj);
positionList.add(new FracCommonInfo(position.get(Constants.ID).asText(),
position.get(Constants.NAME).asText(), position.get(Constants.DESCRIPTION).asText()));
}
return positionList;
}
Expand Down

0 comments on commit d6db94a

Please sign in to comment.