diff --git a/src/main/java/org/sunbird/org/repository/OrgHierarchyRepository.java b/src/main/java/org/sunbird/org/repository/OrgHierarchyRepository.java index 57283d652..05857d845 100644 --- a/src/main/java/org/sunbird/org/repository/OrgHierarchyRepository.java +++ b/src/main/java/org/sunbird/org/repository/OrgHierarchyRepository.java @@ -38,9 +38,14 @@ public interface OrgHierarchyRepository extends JpaRepository findAllBySbRootOrgId(String sbRootOrgId); + @Query(value = "SELECT sborgid from org_hierarchy_v2 where sbrootorgid in (?1)", nativeQuery = true) + List fetchL2LevelOrgList(List orgIdList); + @Query(value = "SELECT * from org_hierarchy_v2 where mapid in (?1)", nativeQuery = true) List searchOrgForL1MapId(Set l1MapIdSet); + + OrgHierarchy findByOrgNameAndParentMapId(String orgName, String parentMapId); } diff --git a/src/main/java/org/sunbird/org/service/ExtendedOrgServiceImpl.java b/src/main/java/org/sunbird/org/service/ExtendedOrgServiceImpl.java index 65158027c..910b4ad25 100644 --- a/src/main/java/org/sunbird/org/service/ExtendedOrgServiceImpl.java +++ b/src/main/java/org/sunbird/org/service/ExtendedOrgServiceImpl.java @@ -57,35 +57,84 @@ public SBApiResponse createOrg(Map request, String userToken) { } Map requestData = (Map) request.get(Constants.REQUEST); - - String channelName = prepareChannelName((String) requestData.get(Constants.PARENT_MAP_ID), requestData); + String orgId = checkOrgExist((String) requestData.get(Constants.CHANNEL), userToken); String orgType = (String) requestData.get(Constants.ORGANIZATION_TYPE); - if (StringUtils.isBlank(channelName)) { - // We tried to construct channelName but returred empty. - // So orgType could be only ministry / state. + String channelName = null; + boolean dbUpdateRequired = false; + boolean orgCreatedWithNewChannel = false; + + if (StringUtils.isEmpty(orgId)) { + // There is no org exist for given Channel. We can simply create the same in + // system. + orgId = createOrgInSunbird(request, (String) requestData.get(Constants.CHANNEL), userToken); + if (StringUtils.isBlank(orgId)) { + response.getParams().setErrmsg("Failed to create organisation in Sunbird."); + response.setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR); + return response; + } + dbUpdateRequired = true; + } else { + // The channel already exist. We need to check OrgHierarchy table for duplicate. if (Constants.STATE.equalsIgnoreCase(orgType) || Constants.MINISTRY.equalsIgnoreCase(orgType)) { - channelName = (String) requestData.get(Constants.CHANNEL); + // We are not allowing duplicates @ L1 -- Need to throw error + response.getParams().setErrmsg("Organisation is already exist."); + response.setResponseCode(HttpStatus.BAD_REQUEST); + return response; } else { - log.error("Failed to identify channelName.", null); - // Need to treat this as error. + OrgHierarchy existingDBRecord = orgRepository.findByOrgNameAndParentMapId( + (String) requestData.get(Constants.CHANNEL), + (String) requestData.get(Constants.PARENT_MAP_ID)); + if (existingDBRecord == null) { + channelName = prepareChannelName((String) requestData.get(Constants.PARENT_MAP_ID), + requestData); + channelName = channelName + (String) requestData.get(Constants.CHANNEL); + requestData.put(Constants.CHANNEL, channelName); + orgId = createOrgInSunbird(request, (String) requestData.get(Constants.CHANNEL), userToken); + if (StringUtils.isBlank(orgId)) { + response.getParams().setErrmsg("Failed to create organisation in Sunbird."); + response.setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR); + return response; + } + dbUpdateRequired = true; + orgCreatedWithNewChannel = true; + } else if (StringUtils.isBlank(existingDBRecord.getSbOrgId())) { + existingDBRecord.setSbOrgId(orgId); + if (StringUtils.isEmpty(existingDBRecord.getSbRootOrgId())) { + existingDBRecord.setSbRootOrgId(fetchRootOrgId(requestData)); + } + orgRepository.save(existingDBRecord); + } else if (existingDBRecord.getSbOrgId().equalsIgnoreCase(orgId)) { + response.getParams().setErrmsg("Duplicate Record Found in OrgHierarchy. Contact Admin"); + response.setResponseCode(HttpStatus.BAD_REQUEST); + return response; + } } - - } else { - channelName = channelName + (String) requestData.get(Constants.CHANNEL); - requestData.put(Constants.CHANNEL, channelName); } - String orgId = checkOrgExist((String) requestData.get(Constants.CHANNEL), userToken); - - if (StringUtils.isEmpty(orgId)) { - orgId = createOrgInSunbird(request, (String) requestData.get(Constants.CHANNEL), userToken); + if (dbUpdateRequired) { + OrgHierarchy existingDBRecord = orgRepository.findByOrgNameAndParentMapId( + (String) requestData.get(Constants.CHANNEL), (String) requestData.get(Constants.PARENT_MAP_ID)); + if (existingDBRecord != null) { + existingDBRecord.setSbOrgId(orgId); + if (StringUtils.isEmpty(existingDBRecord.getSbRootOrgId())) { + existingDBRecord.setSbRootOrgId(fetchRootOrgId(requestData)); + } + orgRepository.save(existingDBRecord); + } else { + // We just created with given channel name. but this is new record in + // orgHierarchy... + // By calling prepareChannelName we will update L1 and L2 details. + prepareChannelName((String) requestData.get(Constants.PARENT_MAP_ID), requestData); + channelName = (String) requestData.get(Constants.CHANNEL); + orgCreatedWithNewChannel = true; + } } - if (!StringUtils.isEmpty(orgId)) { + if (orgCreatedWithNewChannel) { Map updateRequest = new HashMap(); String orgName = (String) requestData.get(Constants.ORG_NAME); - updateRequest.put(Constants.CHANNEL, channelName); + updateRequest.put(Constants.CHANNEL, (String) requestData.get(Constants.CHANNEL)); updateRequest.put(Constants.SB_ORG_ID, orgId); updateRequest.put(Constants.ORG_NAME, orgName); updateRequest.put(Constants.SB_ORG_TYPE, orgType); @@ -142,9 +191,6 @@ public SBApiResponse createOrg(Map request, String userToken) { } response.getResult().put(Constants.ORGANIZATION_ID, orgId); response.getResult().put(Constants.RESPONSE, Constants.SUCCESS); - } else { - response.getParams().setErrmsg("Failed to create organisation in Sunbird."); - response.setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR); } } catch (Exception e) { log.error(e); @@ -197,8 +243,14 @@ public SBApiResponse orgExtSearch(Map request) throws Exception Map filters = (Map) requestData.get(Constants.FILTERS); String sbRootOrgId = (String) filters.get(Constants.SB_ROOT_ORG_ID); + //sbRootOrgId is State Id. Let's get all the children (i.e. departments) List orgIdList = orgRepository.findAllBySbRootOrgId(sbRootOrgId); + if (CollectionUtils.isNotEmpty(orgIdList)) { + List orgIdChildList = orgRepository.fetchL2LevelOrgList(orgIdList); + if(CollectionUtils.isNotEmpty(orgIdChildList)) { + orgIdList.addAll(orgIdChildList); + } SBApiOrgSearchRequest orgSearchRequest = new SBApiOrgSearchRequest(); orgSearchRequest.getFilters().setId(orgIdList); if (!ProjectUtil.isStringNullOREmpty((String) requestData.get(Constants.QUERY))) { @@ -505,7 +557,7 @@ private void fetchMapIdFromDB(Map requestData) { OrgHierarchy existingOrg = orgList.get(0); // Check given parentMapId is same as existing record parentMapId. if (existingOrg.getParentMapId().equalsIgnoreCase((String) requestData.get(Constants.PARENT_MAP_ID))) { - requestData.put(Constants.MAP_ID, existingOrg.getParentMapId()); + requestData.put(Constants.MAP_ID, existingOrg.getMapId()); } } }