From 295daf7899eeced5d9f1b212b9b520db3515e82d Mon Sep 17 00:00:00 2001 From: karthik-tarento Date: Tue, 31 Jan 2023 07:00:45 +0530 Subject: [PATCH] Merged commits from - 09b896d, 0a5e3fe, 1e2bcae, b25ef5f and 4272bcf --- pom.xml | 4 + .../service/AssessmentServiceV2Impl.java | 122 +++++++------ .../service/AssessmentUtilServiceV2Impl.java | 13 +- .../java/org/sunbird/cache/RedisCacheMgr.java | 56 +++--- .../CassandraConnectionManagerImpl.java | 5 +- .../org/sunbird/common/util/Constants.java | 5 + .../org/sunbird/core/config/RedisConfig.java | 43 +++-- .../org/service/ExtendedOrgServiceImpl.java | 166 ++++++++++++++---- .../searchby/service/SearchByService.java | 55 ++++-- .../service/UserRegistrationServiceImpl.java | 8 +- .../resources/cassandratablecolumn.properties | 4 +- 11 files changed, 327 insertions(+), 154 deletions(-) diff --git a/pom.xml b/pom.xml index 93d43dec3..a0501d899 100644 --- a/pom.xml +++ b/pom.xml @@ -171,6 +171,10 @@ poi-ooxml 3.11 + + redis.clients + jedis + diff --git a/src/main/java/org/sunbird/assessment/service/AssessmentServiceV2Impl.java b/src/main/java/org/sunbird/assessment/service/AssessmentServiceV2Impl.java index df6a22f48..4c1377115 100644 --- a/src/main/java/org/sunbird/assessment/service/AssessmentServiceV2Impl.java +++ b/src/main/java/org/sunbird/assessment/service/AssessmentServiceV2Impl.java @@ -1,6 +1,16 @@ package org.sunbird.assessment.service; -import com.fasterxml.jackson.databind.ObjectMapper; +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Collections; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.stream.Collectors; + import org.apache.commons.collections.CollectionUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -17,9 +27,8 @@ import org.sunbird.core.exception.ApplicationLogicError; import org.sunbird.core.logger.CbExtLogger; -import java.sql.Timestamp; -import java.util.*; -import java.util.stream.Collectors; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; @Service @SuppressWarnings("unchecked") @@ -43,52 +52,62 @@ public class AssessmentServiceV2Impl implements AssessmentServiceV2 { @Autowired AssessmentRepository assessmentRepository; - public SBApiResponse readAssessment(String assessmentIdentifier, String token) throws Exception { - SBApiResponse response = new SBApiResponse(); - try { - String userId = RequestInterceptor.fetchUserIdFromAccessToken(token); - if (userId != null) { - Map assessmentAllDetail = (Map) redisCacheMgr - .getCache(Constants.ASSESSMENT_ID + assessmentIdentifier); - boolean isSuccess = true; - if (ObjectUtils.isEmpty(assessmentAllDetail)) { - Map hierarcyReadApiResponse = getReadHierarchyApiResponse(assessmentIdentifier, token); - if (!Constants.OK.equalsIgnoreCase((String) hierarcyReadApiResponse.get(Constants.RESPONSE_CODE))) { - isSuccess = false; - } else { - assessmentAllDetail = (Map) ((Map) hierarcyReadApiResponse - .get(Constants.RESULT)).get(Constants.QUESTION_SET); - redisCacheMgr.putCache(Constants.ASSESSMENT_ID + assessmentIdentifier, assessmentAllDetail); - } - } - response = prepareAssessmentResponse(assessmentAllDetail, isSuccess); - redisCacheMgr.putCache(Constants.USER_ASSESS_REQ + token, response.getResult().get(Constants.QUESTION_SET)); - if (assessmentAllDetail.get(Constants.DURATION) != null) { - boolean resp = assessmentRepository.addUserAssesmentStartTime(userId, Constants.ASSESSMENT_ID + assessmentIdentifier, new Timestamp(new Date().getTime())); - return response; - } - } - } catch (Exception e) { - logger.error(e); - throw new ApplicationLogicError("REQUEST_COULD_NOT_BE_PROCESSED", e); - } - return response; - } + public SBApiResponse readAssessment(String assessmentIdentifier, String token) throws Exception { + SBApiResponse response = new SBApiResponse(); + try { + String userId = RequestInterceptor.fetchUserIdFromAccessToken(token); + if (userId != null) { + String strAssessmentAllDetail = redisCacheMgr.getCache(Constants.ASSESSMENT_ID + assessmentIdentifier); + + Map assessmentAllDetail = mapper.readValue(strAssessmentAllDetail, + new TypeReference>() { + }); + boolean isSuccess = true; + if (ObjectUtils.isEmpty(assessmentAllDetail)) { + Map hierarcyReadApiResponse = getReadHierarchyApiResponse(assessmentIdentifier, + token); + if (!Constants.OK.equalsIgnoreCase((String) hierarcyReadApiResponse.get(Constants.RESPONSE_CODE))) { + isSuccess = false; + } else { + assessmentAllDetail = (Map) ((Map) hierarcyReadApiResponse + .get(Constants.RESULT)).get(Constants.QUESTION_SET); + redisCacheMgr.putCache(Constants.ASSESSMENT_ID + assessmentIdentifier, assessmentAllDetail); + } + } + response = prepareAssessmentResponse(assessmentAllDetail, isSuccess); + redisCacheMgr.putCache(Constants.USER_ASSESS_REQ + token, + response.getResult().get(Constants.QUESTION_SET)); + if (assessmentAllDetail.get(Constants.DURATION) != null) { + boolean resp = assessmentRepository.addUserAssesmentStartTime(userId, + Constants.ASSESSMENT_ID + assessmentIdentifier, new Timestamp(new Date().getTime())); + return response; + } + } + } catch (Exception e) { + logger.error(e); + throw new ApplicationLogicError("REQUEST_COULD_NOT_BE_PROCESSED", e); + } + return response; + } public SBApiResponse readQuestionList(Map requestBody, String authUserToken) throws Exception { try { List identifierList = getQuestionIdList(requestBody); List questionList = new ArrayList<>(); List newIdentifierList = new ArrayList<>(); - List map = redisCacheMgr.mget(identifierList); - int size = map.size(); - for (int i = 0; i < map.size(); i++) { - if (ObjectUtils.isEmpty(map.get(i))) { - newIdentifierList.add(identifierList.get(i)); - } else { - questionList.add(filterQuestionMapDetail((Map) map.get(i))); - } - } + List strQuestionList = redisCacheMgr.mget(identifierList); + + int size = strQuestionList.size(); + for (int i = 0; i < size; i++) { + if (ObjectUtils.isEmpty(strQuestionList.get(i))) { + newIdentifierList.add(identifierList.get(i)); + } else { + Map questionMap = mapper.readValue(strQuestionList.get(i), + new TypeReference>() { + }); + questionList.add(filterQuestionMapDetail(questionMap)); + } + } if (newIdentifierList.size() > 0) { Map questionMapResponse = readQuestionDetails(newIdentifierList); if (questionMapResponse != null && Constants.OK.equalsIgnoreCase((String) questionMapResponse.get(Constants.RESPONSE_CODE))) { @@ -116,14 +135,13 @@ public SBApiResponse readQuestionList(Map requestBody, String au public SBApiResponse submitAssessment(Map data, String authUserToken) throws Exception { SBApiResponse outgoingResponse = new SBApiResponse(); String assessmentId = (String) data.get(Constants.IDENTIFIER); - Map assessmentHierarchy = (Map) redisCacheMgr + String strAssessmentHierarchy = redisCacheMgr .getCache(Constants.ASSESSMENT_ID + assessmentId); - // logger.info("Submit Assessment: userId: " + userId + ", data: " + - // data.toString()); - // Check User exists - // if (!userUtilService.validateUser(userId)) { - // throw new BadRequestException("Invalid UserId."); - // } + + Map assessmentHierarchy = mapper.readValue(strAssessmentHierarchy, + new TypeReference>() { + }); + String userId = RequestInterceptor.fetchUserIdFromAccessToken(authUserToken); if (userId != null) { Date assessmentStartTime = assessmentRepository.fetchUserAssessmentStartTime(userId, Constants.ASSESSMENT_ID + assessmentId); @@ -396,4 +414,4 @@ private void validateAssessmentLevelScore(SBApiResponse outgoingResponse, Map= passPercentage); } -} +} \ No newline at end of file diff --git a/src/main/java/org/sunbird/assessment/service/AssessmentUtilServiceV2Impl.java b/src/main/java/org/sunbird/assessment/service/AssessmentUtilServiceV2Impl.java index e7384b23b..520c710b2 100644 --- a/src/main/java/org/sunbird/assessment/service/AssessmentUtilServiceV2Impl.java +++ b/src/main/java/org/sunbird/assessment/service/AssessmentUtilServiceV2Impl.java @@ -15,11 +15,16 @@ import org.sunbird.core.exception.ApplicationLogicError; import org.sunbird.core.logger.CbExtLogger; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; + @Service public class AssessmentUtilServiceV2Impl implements AssessmentUtilServiceV2 { @Autowired RedisCacheMgr redisCacheMgr; + + ObjectMapper mapper = new ObjectMapper(); private CbExtLogger logger = new CbExtLogger(getClass().getName()); @@ -121,9 +126,9 @@ private Map getQumlAnswers(List questions) throws Except Map ret = new HashMap<>(); for (String questionId : questions) { List correctOption = new ArrayList<>(); - - Map question = (Map) redisCacheMgr - .getCache(Constants.QUESTION_ID + questionId); + String strQuestion = redisCacheMgr.getCache(Constants.QUESTION_ID + questionId); + Map question = mapper.readValue(strQuestion, new TypeReference>() { + }); if (ObjectUtils.isEmpty(question)) { logger.error(new Exception("Failed to get the answer for question: " + questionId)); // TODO - Need to handle this scenario. @@ -168,4 +173,4 @@ private Map getQumlAnswers(List questions) throws Except return ret; } -} +} \ No newline at end of file diff --git a/src/main/java/org/sunbird/cache/RedisCacheMgr.java b/src/main/java/org/sunbird/cache/RedisCacheMgr.java index 318257200..17532964d 100644 --- a/src/main/java/org/sunbird/cache/RedisCacheMgr.java +++ b/src/main/java/org/sunbird/cache/RedisCacheMgr.java @@ -1,28 +1,44 @@ package org.sunbird.cache; -import java.util.*; -import java.util.concurrent.TimeUnit; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import org.sunbird.common.util.CbExtServerProperties; import org.sunbird.common.util.Constants; import org.sunbird.core.logger.CbExtLogger; +import com.fasterxml.jackson.databind.ObjectMapper; + +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPool; + @Component public class RedisCacheMgr { private static final int cache_ttl = 84600; @Autowired - private RedisTemplate redisTemplate; + private JedisPool jedisPool; @Autowired CbExtServerProperties cbExtServerProperties; private CbExtLogger logger = new CbExtLogger(getClass().getName()); + + ObjectMapper objectMapper = new ObjectMapper(); + + public Jedis getJedis() { + try (Jedis jedis = jedisPool.getResource()) { + return jedis; + } + } public void putCache(String key, Object object) { try { @@ -30,8 +46,9 @@ public void putCache(String key, Object object) { if (!StringUtils.isEmpty(cbExtServerProperties.getRedisTimeout())) { ttl = Integer.parseInt(cbExtServerProperties.getRedisTimeout()); } - redisTemplate.opsForValue().set(Constants.REDIS_COMMON_KEY + key, object); - redisTemplate.expire(Constants.REDIS_COMMON_KEY + key, ttl, TimeUnit.SECONDS); + String data = objectMapper.writeValueAsString(object); + getJedis().set(Constants.REDIS_COMMON_KEY + key, data); + getJedis().expire(Constants.REDIS_COMMON_KEY + key, ttl); logger.info("Cache_key_value " + Constants.REDIS_COMMON_KEY + key + " is saved in redis"); } catch (Exception e) { logger.error(e); @@ -40,7 +57,7 @@ public void putCache(String key, Object object) { public boolean deleteKeyByName(String key) { try { - redisTemplate.delete(Constants.REDIS_COMMON_KEY + key); + getJedis().del(Constants.REDIS_COMMON_KEY + key); logger.info("Cache_key_value " + Constants.REDIS_COMMON_KEY + key + " is deleted from redis"); return true; } catch (Exception e) { @@ -52,9 +69,9 @@ public boolean deleteKeyByName(String key) { public boolean deleteAllCBExtKey() { try { String keyPattern = Constants.REDIS_COMMON_KEY + "*"; - Set keys = redisTemplate.keys(keyPattern); + Set keys = getJedis().keys(keyPattern); for (String key : keys) { - redisTemplate.delete(key); + getJedis().del(key); } logger.info("All Keys starts with " + Constants.REDIS_COMMON_KEY + " is deleted from redis"); return true; @@ -64,23 +81,22 @@ public boolean deleteAllCBExtKey() { } } - public Object getCache(String key) { + public String getCache(String key) { try { - return redisTemplate.opsForValue().get(Constants.REDIS_COMMON_KEY + key); + return getJedis().get(Constants.REDIS_COMMON_KEY + key); } catch (Exception e) { logger.error(e); return null; } } - public List mget(List fields) { + public List mget(List fields) { try { - List ls = new ArrayList<>(); + String[] updatedKeys = new String[fields.size()]; for (int i = 0; i < fields.size(); i++) { - ls.add(Constants.REDIS_COMMON_KEY + Constants.QUESTION_ID + fields.get(i)); + updatedKeys[i] = Constants.REDIS_COMMON_KEY + Constants.QUESTION_ID + fields.get(i); } - Collection questionIdList = ls; - return redisTemplate.opsForValue().multiGet(questionIdList); + return getJedis().mget(updatedKeys); } catch (Exception e) { logger.error(e); } @@ -88,15 +104,13 @@ public List mget(List fields) { } public Set getAllKeyNames() { - Set keys = null; try { String keyPattern = Constants.REDIS_COMMON_KEY + "*"; - keys = redisTemplate.keys(keyPattern); + return getJedis().keys(keyPattern); } catch (Exception e) { logger.error(e); return Collections.emptySet(); } - return keys; } public List> getAllKeysAndValues() { @@ -104,11 +118,11 @@ public List> getAllKeysAndValues() { try { String keyPattern = Constants.REDIS_COMMON_KEY + "*"; Map res = new HashMap<>(); - Set keys = redisTemplate.keys(keyPattern); + Set keys = getJedis().keys(keyPattern); if (!keys.isEmpty()) { for (String key : keys) { Object entries; - entries = redisTemplate.opsForValue().get(key); + entries = getJedis().get(key); res.put(key, entries); } result.add(res); diff --git a/src/main/java/org/sunbird/common/helper/cassandra/CassandraConnectionManagerImpl.java b/src/main/java/org/sunbird/common/helper/cassandra/CassandraConnectionManagerImpl.java index 71ad4fea7..a652e4d2b 100644 --- a/src/main/java/org/sunbird/common/helper/cassandra/CassandraConnectionManagerImpl.java +++ b/src/main/java/org/sunbird/common/helper/cassandra/CassandraConnectionManagerImpl.java @@ -62,7 +62,10 @@ private void createCassandraConnection() { poolingOptions.setPoolTimeoutMillis( Integer.parseInt(cache.getProperty(Constants.POOL_TIMEOUT))); String cassandraHost = (cache.getProperty(Constants.CASSANDRA_CONFIG_HOST)); - String[] hosts = new String[]{cassandraHost}; + String[] hosts = null; + if (StringUtils.isNotBlank(cassandraHost)) { + hosts = cassandraHost.split(","); + } cluster = createCluster(hosts, poolingOptions); final Metadata metadata = cluster.getMetadata(); diff --git a/src/main/java/org/sunbird/common/util/Constants.java b/src/main/java/org/sunbird/common/util/Constants.java index 7b9325cbf..4a198c026 100644 --- a/src/main/java/org/sunbird/common/util/Constants.java +++ b/src/main/java/org/sunbird/common/util/Constants.java @@ -553,6 +553,11 @@ public class Constants { public static final String CONTENT_STATUS = "contentStatus"; public static final String ROLE = "role"; public static final String SCOPE = "scope"; + public static final String SB_SUB_ORG_TYPE = "sbSubOrgType"; + public static final String ORG_CODE = "orgCode"; + public static final String MDO = "mdo"; + public static final String BOARD = "board"; + public static final String TRAINING_INSTITUTE = "TrainingInstitute"; public static final List USER_ENROLMENT_REPORT_FIELDS = Arrays.asList(USER_ID, FIRSTNAME, LASTNAME, EMAIL, PHONE, ROOT_ORG_ID, CHANNEL); diff --git a/src/main/java/org/sunbird/core/config/RedisConfig.java b/src/main/java/org/sunbird/core/config/RedisConfig.java index 9ffad7d7c..3c9fdcfb8 100644 --- a/src/main/java/org/sunbird/core/config/RedisConfig.java +++ b/src/main/java/org/sunbird/core/config/RedisConfig.java @@ -4,13 +4,11 @@ import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.data.redis.connection.RedisStandaloneConfiguration; -import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; -import org.springframework.data.redis.serializer.StringRedisSerializer; import org.sunbird.common.util.CbExtServerProperties; +import redis.clients.jedis.JedisPool; +import redis.clients.jedis.JedisPoolConfig; + @Configuration @EnableCaching public class RedisConfig { @@ -19,24 +17,25 @@ public class RedisConfig { CbExtServerProperties cbProperties; @Bean - public JedisConnectionFactory jedisConnectionFactory() { - RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(); - redisStandaloneConfiguration.setHostName(cbProperties.getRedisHostName()); - redisStandaloneConfiguration.setPort(Integer.parseInt(cbProperties.getRedisPort())); - - JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisStandaloneConfiguration); - return jedisConnectionFactory; + public JedisPool jedisPool() { + final JedisPoolConfig poolConfig = buildPoolConfig(); + JedisPool jedisPool = new JedisPool(poolConfig, cbProperties.getRedisHostName(), + Integer.parseInt(cbProperties.getRedisPort())); + return jedisPool; } - @Bean - public RedisTemplate redisTemplate() { - RedisTemplate redisTemplate = new RedisTemplate<>(); - redisTemplate.setConnectionFactory(jedisConnectionFactory()); - redisTemplate.setKeySerializer(new StringRedisSerializer()); - redisTemplate.setHashKeySerializer(new JdkSerializationRedisSerializer()); - redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); - redisTemplate.setEnableTransactionSupport(true); - redisTemplate.afterPropertiesSet(); - return redisTemplate; + private JedisPoolConfig buildPoolConfig() { + final JedisPoolConfig poolConfig = new JedisPoolConfig(); + poolConfig.setMaxIdle(128); + poolConfig.setMaxTotal(3000); + poolConfig.setMinIdle(100); + poolConfig.setTestOnBorrow(true); + poolConfig.setTestOnReturn(true); + poolConfig.setTestWhileIdle(true); + poolConfig.setMinEvictableIdleTimeMillis(120000); + poolConfig.setTimeBetweenEvictionRunsMillis(30000); + poolConfig.setNumTestsPerEvictionRun(3); + poolConfig.setBlockWhenExhausted(true); + return poolConfig; } } diff --git a/src/main/java/org/sunbird/org/service/ExtendedOrgServiceImpl.java b/src/main/java/org/sunbird/org/service/ExtendedOrgServiceImpl.java index e322f132c..6b141b95e 100644 --- a/src/main/java/org/sunbird/org/service/ExtendedOrgServiceImpl.java +++ b/src/main/java/org/sunbird/org/service/ExtendedOrgServiceImpl.java @@ -59,30 +59,57 @@ public SBApiResponse createOrg(Map request, String userToken) { if (!StringUtils.isEmpty(orgId)) { Map updateRequest = new HashMap(); - updateRequest.put(Constants.SB_ORG_ID.toLowerCase(), orgId); String orgType = (String) requestData.get(Constants.ORGANIZATION_TYPE); - if (requestData.containsKey(Constants.SB_ROOT_ORG_ID) - && StringUtils.isNotEmpty((String) requestData.get(Constants.SB_ROOT_ORG_ID))) { - updateRequest.put(Constants.SB_ROOT_ORG_ID, (String) requestData.get(Constants.SB_ROOT_ORG_ID)); - } else if (!(Constants.STATE.equalsIgnoreCase(orgType) - || Constants.MINISTRY.equalsIgnoreCase(orgType))) { - // OrgType is not state / ministry and sbRootOrgId value given as Null... let's - // find the rootOrgId - String sbRootOrgId = findRootOrgId((String) requestData.get(Constants.ORG_NAME), - (String) requestData.get(Constants.MAP_ID)); - if (StringUtils.isNotEmpty(sbRootOrgId)) { - updateRequest.put(Constants.SB_ROOT_ORG_ID, sbRootOrgId); - } + String orgName = (String) requestData.get(Constants.ORG_NAME); + updateRequest.put(Constants.SB_ORG_ID, orgId); + updateRequest.put(Constants.ORG_NAME, orgName); + updateRequest.put(Constants.SB_ORG_TYPE, orgType); + String mapId = (String) requestData.get(Constants.MAP_ID); + if (StringUtils.isEmpty(mapId)) { + // There is a possibility that this Org already exists in table. Get the MapId + // if so. + fetchMapIdFromDB(requestData); + mapId = (String) requestData.get(Constants.MAP_ID); } - Map compositeKey = new HashMap() { - private static final long serialVersionUID = 1L; - { - put(Constants.ORG_NAME, requestData.get(Constants.ORG_NAME)); - put(Constants.MAP_ID, requestData.get(Constants.MAP_ID)); + String orgCode = (String) requestData.get(Constants.ORG_CODE); + String sbRootOrgid = (String) requestData.get(Constants.SB_ROOT_ORG_ID); + updateRequest.put(Constants.SB_SUB_ORG_TYPE, requestData.get(Constants.ORGANIZATION_SUB_TYPE)); + if (!StringUtils.isEmpty(mapId)) { + updateRequest.put(Constants.MAP_ID, mapId); + } else { + mapId = createMapId(requestData); + updateRequest.put(Constants.MAP_ID, mapId); + updateRequest.put(Constants.ORG_CODE, mapId); + } + if (!StringUtils.isEmpty(orgCode)) { + updateRequest.put(Constants.ORG_CODE, orgCode); + } + if (!Constants.STATE.equalsIgnoreCase(orgType) && !Constants.MINISTRY.equalsIgnoreCase(orgType)) { + updateRequest.put(Constants.PARENT_MAP_ID, requestData.get(Constants.PARENT_MAP_ID)); + if (!StringUtils.isEmpty(sbRootOrgid)) { + updateRequest.put(Constants.SB_ROOT_ORG_ID, sbRootOrgid); + } else { + updateRequest.put(Constants.SB_ROOT_ORG_ID, fetchRootOrgId(requestData)); } - }; - cassandraOperation.updateRecord(Constants.SUNBIRD_KEY_SPACE_NAME, Constants.TABLE_ORG_HIERARCHY, - updateRequest, compositeKey); + } else { + updateRequest.put(Constants.PARENT_MAP_ID, Constants.SPV); + } + if (!StringUtils.isEmpty((String) requestData.get(Constants.MAP_ID))) { + Map compositeKey = new HashMap() { + private static final long serialVersionUID = 1L; + { + put(Constants.ORG_NAME, orgName); + put(Constants.MAP_ID, requestData.get(Constants.MAP_ID)); + } + }; + updateRequest.remove(Constants.ORG_NAME); + updateRequest.remove(Constants.MAP_ID); + cassandraOperation.updateRecord(Constants.SUNBIRD_KEY_SPACE_NAME, Constants.TABLE_ORG_HIERARCHY, + updateRequest, compositeKey); + } else { + cassandraOperation.insertRecord(Constants.SUNBIRD_KEY_SPACE_NAME, Constants.TABLE_ORG_HIERARCHY, + updateRequest); + } response.getResult().put(Constants.ORGANIZATION_ID, orgId); response.getResult().put(Constants.RESPONSE, Constants.SUCCESS); } else { @@ -193,36 +220,37 @@ public SBApiResponse orgExtSearch(Map request) throws Exception return response; } - private String validateOrgRequest(Map requestData) { + private String validateOrgRequest(Map request) { List params = new ArrayList(); StringBuilder strBuilder = new StringBuilder(); - Map request = (Map) requestData.get(Constants.REQUEST); - if (ObjectUtils.isEmpty(request)) { + Map requestData = (Map) request.get(Constants.REQUEST); + if (ObjectUtils.isEmpty(requestData)) { strBuilder.append("Request object is empty."); return strBuilder.toString(); } - if (StringUtils.isEmpty((String) request.get(Constants.ORG_NAME))) { + if (StringUtils.isEmpty((String) requestData.get(Constants.ORG_NAME))) { params.add(Constants.ORG_NAME); } - if (StringUtils.isEmpty((String) request.get(Constants.ORGANIZATION_TYPE))) { + String orgType = (String) requestData.get(Constants.ORGANIZATION_TYPE); + if (StringUtils.isEmpty(((String) orgType))) { params.add(Constants.ORGANIZATION_TYPE); + } else if (!Constants.STATE.equalsIgnoreCase(orgType) && !Constants.MINISTRY.equalsIgnoreCase(orgType)) { + if (StringUtils.isEmpty((String) requestData.get(Constants.PARENT_MAP_ID))) { + params.add(Constants.PARENT_MAP_ID); + } } - if (StringUtils.isEmpty((String) request.get(Constants.ORGANIZATION_SUB_TYPE))) { + if (StringUtils.isEmpty((String) requestData.get(Constants.ORGANIZATION_SUB_TYPE))) { params.add(Constants.ORGANIZATION_SUB_TYPE); } - if (StringUtils.isEmpty((String) request.get(Constants.MAP_ID))) { - params.add(Constants.MAP_ID); - } - - if (ObjectUtils.isEmpty(request.get(Constants.IS_TENANT))) { + if (ObjectUtils.isEmpty(requestData.get(Constants.IS_TENANT))) { params.add(Constants.IS_TENANT); } - if (StringUtils.isEmpty((String) request.get(Constants.CHANNEL))) { + if (StringUtils.isEmpty((String) requestData.get(Constants.CHANNEL))) { params.add(Constants.CHANNEL); } @@ -404,4 +432,76 @@ public void getOrgDetailsFromDB(List orgIds, Map orgInfo log.error("Failed to get user details from DB. Exception: ", e); } } + + private String createMapId(Map requestData) { + Map queryRequest = new HashMap<>(); + String prefix = StringUtils.EMPTY; + String mapIdNew = StringUtils.EMPTY; + String orgType = (String) requestData.get(Constants.ORGANIZATION_TYPE); + if (!Constants.STATE.equalsIgnoreCase(orgType) && !Constants.MINISTRY.equalsIgnoreCase(orgType)) { + String orgSubType = (String) requestData.get(Constants.ORGANIZATION_SUB_TYPE); + String parentMapId = (String) requestData.get(Constants.PARENT_MAP_ID); + queryRequest.put(Constants.PARENT_MAP_ID, parentMapId); + if (Constants.DEPARTMENT.equalsIgnoreCase(orgSubType)) { + prefix = "D_"; + } else if (Constants.BOARD.equalsIgnoreCase(orgSubType)) { + prefix = "O_"; + } else if (Constants.TRAINING_INSTITUTE.equalsIgnoreCase(orgSubType)) { + prefix = "T_"; + } else { + prefix = "X_"; + } + prefix = parentMapId + "_" + prefix; + } else { + queryRequest.put(Constants.SB_ORG_TYPE, orgType); + if (Constants.STATE.equalsIgnoreCase(orgType)) { + prefix = "S_"; + } else if (Constants.MINISTRY.equalsIgnoreCase(orgType)) { + prefix = "M_"; + } + } + List> existingDataList = cassandraOperation.getRecordsByProperties( + Constants.KEYSPACE_SUNBIRD, Constants.TABLE_ORG_HIERARCHY, queryRequest, + Arrays.asList(Constants.MAP_ID)); + if (CollectionUtils.isNotEmpty(existingDataList)) { + List mapIdList = new ArrayList<>(); + for (Map map : existingDataList) { + if (((String) map.get(Constants.MAP_ID)).startsWith(prefix)) + mapIdList.add((String) map.get(Constants.MAP_ID)); + } + mapIdNew = prefix + (mapIdList.size() + 1); + } else { + mapIdNew = prefix + "1"; + } + return mapIdNew; + } + + private String fetchRootOrgId(Map requestData) { + String sbOrgId = null; + Map queryRequest = new HashMap<>(); + queryRequest.put(Constants.MAP_ID, requestData.get(Constants.PARENT_MAP_ID)); + List fields = new ArrayList<>(); + fields.add(Constants.SB_ORG_ID); + List> sbRootOrgList = cassandraOperation.getRecordsByProperties(Constants.KEYSPACE_SUNBIRD, + Constants.TABLE_ORG_HIERARCHY, queryRequest, fields); + if (CollectionUtils.isNotEmpty(sbRootOrgList)) { + Map data = sbRootOrgList.get(0); + sbOrgId = (String) data.get(Constants.SB_ORG_ID); + } + return sbOrgId; + } + + private void fetchMapIdFromDB(Map requestData) { + Map queryRequest = new HashMap<>(); + queryRequest.put(Constants.ORG_NAME, requestData.get(Constants.ORG_NAME)); + List fields = new ArrayList<>(); + fields.add(Constants.SB_ORG_ID); + fields.add(Constants.MAP_ID); + List> sbRootOrgList = cassandraOperation.getRecordsByProperties(Constants.KEYSPACE_SUNBIRD, + Constants.TABLE_ORG_HIERARCHY, queryRequest, fields); + if (CollectionUtils.isNotEmpty(sbRootOrgList)) { + Map data = sbRootOrgList.get(0); + requestData.put(Constants.MAP_ID, (String) data.get(Constants.MAP_ID)); + } + } } diff --git a/src/main/java/org/sunbird/searchby/service/SearchByService.java b/src/main/java/org/sunbird/searchby/service/SearchByService.java index 935c889f5..32606edfa 100644 --- a/src/main/java/org/sunbird/searchby/service/SearchByService.java +++ b/src/main/java/org/sunbird/searchby/service/SearchByService.java @@ -25,6 +25,7 @@ import org.sunbird.searchby.model.ProviderInfo; import org.sunbird.workallocation.model.FracStatusInfo; +import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; @@ -39,25 +40,35 @@ public class SearchByService { @Autowired RedisCacheMgr redisCacheMgr; + + @Autowired + ObjectMapper mapper; @Autowired OutboundRequestHandlerServiceImpl outboundRequestHandlerService; public Collection getCompetencyDetails(String authUserToken) throws Exception { - Map competencyMap = (Map) redisCacheMgr - .getCache(Constants.COMPETENCY_CACHE_NAME); + String strCompetencyMap = redisCacheMgr.getCache(Constants.COMPETENCY_CACHE_NAME); + Map competencyMap = new HashMap<>(); + if (!StringUtils.isEmpty(strCompetencyMap)) { + competencyMap = mapper.readValue(strCompetencyMap, new TypeReference>() { + }); + } if (CollectionUtils.isEmpty(competencyMap)) { logger.info("Initializing/Refreshing the Cache Value for Key : " + Constants.COMPETENCY_CACHE_NAME); competencyMap = updateCompetencyDetails(authUserToken); } - return competencyMap.values(); } public Collection getProviderDetails(String authUserToken) throws Exception { - Map providerMap = (Map) redisCacheMgr - .getCache(Constants.PROVIDER_CACHE_NAME); + String strProviderInfo = redisCacheMgr.getCache(Constants.PROVIDER_CACHE_NAME); + Map providerMap = new HashMap<>(); + if (!StringUtils.isEmpty(strProviderInfo)) { + providerMap = mapper.readValue(strProviderInfo, new TypeReference>() { + }); + } if (CollectionUtils.isEmpty(providerMap)) { logger.info("Initializing/Refreshing the Cache Value for Key : " + Constants.PROVIDER_CACHE_NAME); @@ -70,22 +81,30 @@ public FracApiResponse listPositions(String userToken) { FracApiResponse response = new FracApiResponse(); response.setStatusInfo(new FracStatusInfo()); response.getStatusInfo().setStatusCode(HttpStatus.OK.value()); + try { + Map> positionMap = new HashMap<>(); + String strPositionMap = redisCacheMgr.getCache(Constants.POSITIONS_CACHE_NAME); + if (!StringUtils.isEmpty(strPositionMap)) { + positionMap = mapper.readValue(strPositionMap, new TypeReference>>() { + }); + } - Map> positionMap = (Map>) redisCacheMgr - .getCache(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 { - positionMap = updateDesignationDetails(userToken); + 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 { + 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(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(positionMap.get(Constants.POSITIONS_CACHE_NAME)); + } catch (Exception e) { + response.getStatusInfo().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR.value()); } return response; diff --git a/src/main/java/org/sunbird/user/registration/service/UserRegistrationServiceImpl.java b/src/main/java/org/sunbird/user/registration/service/UserRegistrationServiceImpl.java index 592cd9924..28979b190 100644 --- a/src/main/java/org/sunbird/user/registration/service/UserRegistrationServiceImpl.java +++ b/src/main/java/org/sunbird/user/registration/service/UserRegistrationServiceImpl.java @@ -50,6 +50,7 @@ import org.sunbird.user.registration.util.UserRegistrationStatus; import org.sunbird.user.service.UserUtilityService; +import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; @Service @@ -169,8 +170,11 @@ public SBApiResponse getDeptDetails() { SBApiResponse response = createDefaultResponse(Constants.USER_REGISTRATION_DEPT_INFO_API); try { - Map> deptListMap = (Map>) redisCacheMgr - .getCache(Constants.DEPARTMENT_LIST_CACHE_NAME); + String strDeptListMap = redisCacheMgr.getCache(Constants.DEPARTMENT_LIST_CACHE_NAME); + + Map> deptListMap = mapper.readValue(strDeptListMap, + new TypeReference>>() { + }); List orgList = null; if (ObjectUtils.isEmpty(deptListMap) || CollectionUtils.isEmpty(deptListMap.get(Constants.DEPARTMENT_LIST_CACHE_NAME))) { diff --git a/src/main/resources/cassandratablecolumn.properties b/src/main/resources/cassandratablecolumn.properties index ad18cde86..6907d111d 100644 --- a/src/main/resources/cassandratablecolumn.properties +++ b/src/main/resources/cassandratablecolumn.properties @@ -41,4 +41,6 @@ datecreatedon=dateCreatedOn dateupdatedon=dateUpdatedOn filename=fileName rootorgid=rootOrgId -filepath=filePath \ No newline at end of file +filepath=filePath +mapid=mapId +sborgid=sbOrgId \ No newline at end of file