From 1c105e45ec935e5904a86ffa531abc31bf1b2efb Mon Sep 17 00:00:00 2001 From: karthik-tarento Date: Fri, 24 Jun 2022 21:12:32 +0530 Subject: [PATCH] Added validation for org creation --- .../org/service/ExtendedOrgServiceImpl.java | 124 +++++++++++++----- 1 file changed, 89 insertions(+), 35 deletions(-) diff --git a/src/main/java/org/sunbird/org/service/ExtendedOrgServiceImpl.java b/src/main/java/org/sunbird/org/service/ExtendedOrgServiceImpl.java index b87185105..9a131a123 100644 --- a/src/main/java/org/sunbird/org/service/ExtendedOrgServiceImpl.java +++ b/src/main/java/org/sunbird/org/service/ExtendedOrgServiceImpl.java @@ -1,6 +1,7 @@ package org.sunbird.org.service; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -41,44 +42,40 @@ public SBApiResponse createOrg(Map request, String userToken) { SBApiResponse response = ProjectUtil.createDefaultResponse(Constants.API_ORG_EXT_CREATE); try { String errMsg = validateOrgRequest(request); - if (StringUtils.isEmpty(errMsg)) { - Map requestData = (Map) request.get(Constants.REQUEST); - String url = configProperties.getSbUrl() + configProperties.getLmsOrgCreatePath(); - Map headers = new HashMap(); - headers.put(Constants.CONTENT_TYPE, Constants.APPLICATION_JSON); - headers.put(Constants.X_AUTH_TOKEN, userToken); + if (!StringUtils.isEmpty(errMsg)) { + response.getParams().setErrmsg(errMsg); + response.setResponseCode(HttpStatus.BAD_REQUEST); + return response; + } - Map apiResponse = (Map) outboundService.fetchResultUsingPost(url, - request, headers); - if (Constants.OK.equalsIgnoreCase((String) apiResponse.get(Constants.RESPONSE_CODE))) { - Map result = (Map) apiResponse.get(Constants.RESULT); - String orgId = (String) result.get(Constants.ORGANIZATION_ID); - log.info(String.format("Org onboarded successfully for Name: %s, with orgId: %s", - requestData.get(Constants.ORG_NAME), orgId)); - Map updateRequest = new HashMap() { - private static final long serialVersionUID = 1L; - { - put(Constants.SB_ORG_ID, orgId); - if (requestData.containsKey(Constants.SB_ROOT_ORG_ID)) { - put(Constants.SB_ROOT_ORG_ID, (String) requestData.get(Constants.SB_ROOT_ORG_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)); - } - }; - cassandraOperation.updateRecord(Constants.SUNBIRD_KEY_SPACE_NAME, Constants.TABLE_ORG_HIERARCHY, - updateRequest, compositeKey); - response.getResult().put(Constants.ORGANIZATION_ID, orgId); - response.getResult().put(Constants.RESPONSE, Constants.SUCCESS); + Map requestData = (Map) request.get(Constants.REQUEST); + + String orgId = checkOrgExist((String) requestData.get(Constants.CHANNEL), userToken); + + if (StringUtils.isEmpty(orgId)) { + orgId = createOrgInSunbird(request, (String) requestData.get(Constants.CHANNEL), userToken); + } + + if (!StringUtils.isEmpty(orgId)) { + Map updateRequest = new HashMap(); + updateRequest.put(Constants.SB_ORG_ID, orgId); + if (requestData.containsKey(Constants.SB_ROOT_ORG_ID)) { + updateRequest.put(Constants.SB_ROOT_ORG_ID, (String) requestData.get(Constants.SB_ROOT_ORG_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)); + } + }; + cassandraOperation.updateRecord(Constants.SUNBIRD_KEY_SPACE_NAME, Constants.TABLE_ORG_HIERARCHY, + updateRequest, compositeKey); + response.getResult().put(Constants.ORGANIZATION_ID, orgId); + response.getResult().put(Constants.RESPONSE, Constants.SUCCESS); } else { - response.getParams().setErrmsg(errMsg); - response.setResponseCode(HttpStatus.BAD_REQUEST); + response.getParams().setErrmsg("Failed to create organisation in Sunbird."); + response.setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR); } } catch (Exception e) { log.error(e); @@ -244,6 +241,63 @@ private String validateOrgSearchReq(Map requestData) { } return strBuilder.toString(); + } + + private String checkOrgExist(String channel, String userToken) { + Map headers = new HashMap(); + headers.put(Constants.CONTENT_TYPE, Constants.APPLICATION_JSON); + headers.put(Constants.X_AUTH_TOKEN, userToken); + Map filterMap = new HashMap() { + private static final long serialVersionUID = 1L; + { + put(Constants.CHANNEL, channel); + } + }; + Map searchRequest = new HashMap() { + private static final long serialVersionUID = 1L; + { + put(Constants.FILTERS, filterMap); + put(Constants.FIELDS, Arrays.asList(Constants.CHANNEL, Constants.IDENTIFIER)); + } + }; + Map searchRequestBody = new HashMap() { + private static final long serialVersionUID = 1L; + { + put(Constants.REQUEST, searchRequest); + } + }; + String url = configProperties.getSbUrl() + configProperties.getSbOrgSearchPath(); + Map apiResponse = (Map) outboundService.fetchResultUsingPost(url, + searchRequestBody, headers); + if (Constants.OK.equalsIgnoreCase((String) apiResponse.get(Constants.RESPONSE_CODE))) { + Map result = (Map) apiResponse.get(Constants.RESULT); + Map searchResponse = (Map) result.get(Constants.RESPONSE); + int count = (int) searchResponse.get(Constants.COUNT); + if (count > 0) { + // The org is already exist - need to update the org details in org_hierarchy + // table + List> orgList = (List>) searchResponse.get(Constants.CONTENT); + Map existingOrg = orgList.get(0); + return (String) existingOrg.get(Constants.IDENTIFIER); + } + } + return StringUtils.EMPTY; + } + private String createOrgInSunbird(Map request, String channel, String userToken) { + String url = configProperties.getSbUrl() + configProperties.getLmsOrgCreatePath(); + Map headers = new HashMap(); + headers.put(Constants.CONTENT_TYPE, Constants.APPLICATION_JSON); + headers.put(Constants.X_AUTH_TOKEN, userToken); + + Map apiResponse = (Map) outboundService.fetchResultUsingPost(url, request, + headers); + if (Constants.OK.equalsIgnoreCase((String) apiResponse.get(Constants.RESPONSE_CODE))) { + Map result = (Map) apiResponse.get(Constants.RESULT); + log.info(String.format("Org onboarded successfully for Name: %s, with orgId: %s", channel, + result.get(Constants.ORGANIZATION_ID))); + return (String) result.get(Constants.ORGANIZATION_ID); + } + return StringUtils.EMPTY; } }