From 4a06ebd0538b22b30cff5be3ee84d7bde65667ca Mon Sep 17 00:00:00 2001 From: karthik-tarento Date: Wed, 24 Apr 2024 18:51:28 +0530 Subject: [PATCH] Added validation and error messages for sector existing or not --- .../catalog/service/CatalogServiceImpl.java | 98 +++++++++++++------ 1 file changed, 66 insertions(+), 32 deletions(-) diff --git a/src/main/java/org/sunbird/catalog/service/CatalogServiceImpl.java b/src/main/java/org/sunbird/catalog/service/CatalogServiceImpl.java index 747233ce2..abb748e81 100644 --- a/src/main/java/org/sunbird/catalog/service/CatalogServiceImpl.java +++ b/src/main/java/org/sunbird/catalog/service/CatalogServiceImpl.java @@ -201,12 +201,12 @@ public SBApiResponse createSector(Map request) { return response; } - //Check sector exists + // Check sector exists Map reqBody = (Map) request.get(Constants.REQUEST); String name = (String) reqBody.get(Constants.NAME); SBApiResponse readResponse = readSector(getCodeValue(name)); if (readResponse == null) { - //We have failed to read the sector details before creating... through error + // We have failed to read the sector details before creating... through error response.getParams().setErrmsg("Failed to validate sector exists or not."); response.setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR); response.getParams().setStatus(Constants.FAILED); @@ -214,7 +214,8 @@ public SBApiResponse createSector(Map request) { // Resource is not found... we can create sector. reqBody.put(Constants.CODE, name); Map parentObj = new HashMap<>(); - parentObj.put(Constants.IDENTIFIER, extServerProperties.getSectorFrameworkName() + "_" + extServerProperties.getSectorCategoryName()); + parentObj.put(Constants.IDENTIFIER, + extServerProperties.getSectorFrameworkName() + "_" + extServerProperties.getSectorCategoryName()); reqBody.put(Constants.PARENTS, Arrays.asList(parentObj)); Map termReq = new HashMap(); termReq.put(Constants.TERM, reqBody); @@ -237,6 +238,12 @@ public SBApiResponse createSector(Map request) { response.setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR); response.getParams().setStatus(Constants.FAILED); } + } else if (HttpStatus.OK.equals(readResponse.getResponseCode())) { + errMsg = "Sector already exists with name: " + name; + log.error(errMsg); + response.getParams().setErrmsg(errMsg); + response.setResponseCode(HttpStatus.BAD_REQUEST); + response.getParams().setStatus(Constants.FAILED); } else { log.error("Failed to validate sector exists while creating with name: " + name); response.getParams().setErrmsg("Failed to create sector."); @@ -273,41 +280,68 @@ public SBApiResponse createSubSector(Map request) { response.getParams().setStatus(Constants.FAILED); } else if (HttpStatus.OK.equals(readResponse.getResponseCode())) { List> subSectorList = (List>) reqBody.get(Constants.SUB_SECTORS); + List requiredSubSector = new ArrayList(); + Map readTermResponse = (Map) readResponse.getResult(); + Map existingTerm = (Map) readTermResponse.get(Constants.SECTOR); + List> existingChildren = (List>) existingTerm + .get(Constants.CHILDREN); + for (Map subSector : subSectorList) { + String name = (String) subSector.get(Constants.NAME); if (subSector.containsKey(Constants.IDENTIFIER)) { continue; } else { - String name = (String) subSector.get(Constants.NAME); - Map requestBody = new HashMap<>(); - Map termReq = new HashMap<>(); - termReq.put(Constants.CODE, name); - termReq.put(Constants.NAME, name); - - Map parentObj = new HashMap<>(); - parentObj.put(Constants.IDENTIFIER, extServerProperties.getSectorFrameworkName() + "_" + extServerProperties.getSectorCategoryName() + "_" + id); - termReq.put(Constants.PARENTS, Arrays.asList(parentObj)); - requestBody.put(Constants.TERM, termReq); - Map createReq = new HashMap(); - createReq.put(Constants.REQUEST, requestBody); - - StringBuilder strUrl = new StringBuilder(extServerProperties.getKmBaseHost()); - strUrl.append(extServerProperties.getKmFrameworkTermCreatePath()).append("?framework=") - .append(extServerProperties.getSectorFrameworkName()).append("&category=") - .append(extServerProperties.getSectorCategoryName()); - Map termResponse = outboundRequestHandlerServiceImpl.fetchResultUsingPost(strUrl.toString(), - createReq, null); - List> sectors = new ArrayList<>(); - if (termResponse != null - && Constants.OK.equalsIgnoreCase((String) termResponse.get(Constants.RESPONSE_CODE))) { - log.info("Created sub sector successfully with name : " + name); - } else { - log.error("Failed to create the sector object with name: " + name); - response.getParams().setErrmsg("Failed to create sector."); - response.setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR); - response.getParams().setStatus(Constants.FAILED); - break; + for (Map existingSubSector : existingChildren) { + if (name.equalsIgnoreCase((String) existingSubSector.get(Constants.NAME))) { + errMsg = "Failed to create SubSector. Name '" + name + "' already exists in Sector."; + break; + } else { + requiredSubSector.add(name); + } } } + if (StringUtils.isNotBlank(errMsg)) { + break; + } + } + if (StringUtils.isNotBlank(errMsg)) { + response.getParams().setErrmsg(errMsg); + response.setResponseCode(HttpStatus.BAD_REQUEST); + response.getParams().setStatus(Constants.FAILED); + return response; + } + for (String name : requiredSubSector) { + Map requestBody = new HashMap<>(); + Map termReq = new HashMap<>(); + termReq.put(Constants.CODE, name); + termReq.put(Constants.NAME, name); + + Map parentObj = new HashMap<>(); + parentObj.put(Constants.IDENTIFIER, extServerProperties.getSectorFrameworkName() + "_" + + extServerProperties.getSectorCategoryName() + "_" + id); + termReq.put(Constants.PARENTS, Arrays.asList(parentObj)); + requestBody.put(Constants.TERM, termReq); + Map createReq = new HashMap(); + createReq.put(Constants.REQUEST, requestBody); + + StringBuilder strUrl = new StringBuilder(extServerProperties.getKmBaseHost()); + strUrl.append(extServerProperties.getKmFrameworkTermCreatePath()).append("?framework=") + .append(extServerProperties.getSectorFrameworkName()).append("&category=") + .append(extServerProperties.getSectorCategoryName()); + Map termResponse = outboundRequestHandlerServiceImpl.fetchResultUsingPost( + strUrl.toString(), + createReq, null); + List> sectors = new ArrayList<>(); + if (termResponse != null + && Constants.OK.equalsIgnoreCase((String) termResponse.get(Constants.RESPONSE_CODE))) { + log.info("Created sub sector successfully with name : " + name); + } else { + log.error("Failed to create the sector object with name: " + name); + response.getParams().setErrmsg("Failed to create sector."); + response.setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR); + response.getParams().setStatus(Constants.FAILED); + break; + } } } else { //We have failed to read the sector details before creating... through error