From 0b4f145afbbe3d04bd810ec7eaa531b87465f9b4 Mon Sep 17 00:00:00 2001 From: juhi agarwal Date: Mon, 20 Feb 2023 12:57:49 +0530 Subject: [PATCH] Offensive Data Report APIs (#175) * APIs for Offensive Data Flags --- .../OffensiveDataReportController.java | 40 +++++ .../service/OffensiveDataFlagService.java | 13 ++ .../service/OffensiveDataFlagServiceImpl.java | 165 ++++++++++++++++++ .../utils/CassandraOperationImpl.java | 11 +- .../org/sunbird/common/util/Constants.java | 15 +- .../resources/cassandratablecolumn.properties | 8 +- 6 files changed, 247 insertions(+), 5 deletions(-) create mode 100644 src/main/java/org/sunbird/assessment/controller/OffensiveDataReportController.java create mode 100644 src/main/java/org/sunbird/assessment/service/OffensiveDataFlagService.java create mode 100644 src/main/java/org/sunbird/assessment/service/OffensiveDataFlagServiceImpl.java diff --git a/src/main/java/org/sunbird/assessment/controller/OffensiveDataReportController.java b/src/main/java/org/sunbird/assessment/controller/OffensiveDataReportController.java new file mode 100644 index 000000000..4d2be72f1 --- /dev/null +++ b/src/main/java/org/sunbird/assessment/controller/OffensiveDataReportController.java @@ -0,0 +1,40 @@ +package org.sunbird.assessment.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import org.sunbird.assessment.service.OffensiveDataFlagService; +import org.sunbird.common.model.SBApiResponse; +import org.sunbird.common.util.Constants; + +import javax.validation.Valid; +import java.util.Map; + +@RestController +public class OffensiveDataReportController { + + @Autowired + OffensiveDataFlagService offensiveDataFlagService; + + @PostMapping("/v1/offensive/data/flag") + public ResponseEntity createFlag( + @Valid @RequestBody Map requestBody, @RequestHeader(Constants.X_AUTH_TOKEN) String token) throws Exception { + SBApiResponse readResponse = offensiveDataFlagService.createFlag(requestBody, token); + return new ResponseEntity<>(readResponse, readResponse.getResponseCode()); + } + + @PatchMapping("/v1/offensive/data/flag") + public ResponseEntity updateFlag(@RequestHeader(Constants.X_AUTH_TOKEN) String token, + @Valid @RequestBody Map request) { + SBApiResponse response = offensiveDataFlagService.updateFlag(token, request); + return new ResponseEntity<>(response, response.getResponseCode()); + } + + @GetMapping("/v1/offensive/data/flag/getFlaggedData") + public ResponseEntity getFlaggedData(@RequestHeader(Constants.X_AUTH_TOKEN) String token) throws Exception { + SBApiResponse readResponse = offensiveDataFlagService.getFlaggedData(token); + return new ResponseEntity<>(readResponse, readResponse.getResponseCode()); + } + + +} diff --git a/src/main/java/org/sunbird/assessment/service/OffensiveDataFlagService.java b/src/main/java/org/sunbird/assessment/service/OffensiveDataFlagService.java new file mode 100644 index 000000000..7dfde1abc --- /dev/null +++ b/src/main/java/org/sunbird/assessment/service/OffensiveDataFlagService.java @@ -0,0 +1,13 @@ +package org.sunbird.assessment.service; + +import org.sunbird.common.model.SBApiResponse; + +import java.util.Map; + +public interface OffensiveDataFlagService { + public SBApiResponse createFlag(Map requestBody, String token); + + public SBApiResponse getFlaggedData(String token); + + SBApiResponse updateFlag(String token, Map request); +} diff --git a/src/main/java/org/sunbird/assessment/service/OffensiveDataFlagServiceImpl.java b/src/main/java/org/sunbird/assessment/service/OffensiveDataFlagServiceImpl.java new file mode 100644 index 000000000..684a74a19 --- /dev/null +++ b/src/main/java/org/sunbird/assessment/service/OffensiveDataFlagServiceImpl.java @@ -0,0 +1,165 @@ +package org.sunbird.assessment.service; + +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Service; +import org.springframework.util.ObjectUtils; +import org.sunbird.cassandra.utils.CassandraOperation; +import org.sunbird.common.model.SBApiResponse; +import org.sunbird.common.util.Constants; +import org.sunbird.common.util.ProjectUtil; +import org.sunbird.common.util.RequestInterceptor; + +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Service +@SuppressWarnings("unchecked") +public class OffensiveDataFlagServiceImpl implements OffensiveDataFlagService { + + private final Logger logger = LoggerFactory.getLogger(OffensiveDataFlagServiceImpl.class); + + @Autowired + CassandraOperation cassandraOperation; + + @Override + public SBApiResponse createFlag(Map requestBody, String token) { + String errMsg = ""; + String userId = RequestInterceptor.fetchUserIdFromAccessToken(token); + errMsg = validateRequest(requestBody, userId, Constants.ADD); + SBApiResponse response = ProjectUtil.createDefaultResponse(Constants.ADD_OFFENSIVE_DATA_FLAG); + if (StringUtils.isEmpty(errMsg)) { + Map request = (Map) requestBody.get(Constants.REQUEST); + request.put(Constants.USER_ID, userId); + request.put(Constants.CONTEXT_STATUS, Constants.DRAFT); + request.put(Constants.CREATED_BY, userId); + request.put(Constants.CREATED_DATE, new Timestamp(new java.util.Date().getTime())); + SBApiResponse resp = cassandraOperation.insertRecord(Constants.KEYSPACE_SUNBIRD, + Constants.TABLE_OFFENSIVE_DATA_FLAGS, request); + if (!resp.get(Constants.RESPONSE).equals(Constants.SUCCESS)) { + errMsg = "Failed to add records into DB"; + } else { + response.getResult().put(Constants.STATUS, Constants.CREATED); + } + } + if (StringUtils.isNotBlank(errMsg)) { + response.getParams().setStatus(Constants.FAILED); + response.getParams().setErrmsg(errMsg); + response.setResponseCode(HttpStatus.BAD_REQUEST); + return response; + } + return response; + } + + @Override + public SBApiResponse updateFlag(String token, Map requestBody) { + String errMsg = ""; + SBApiResponse response = ProjectUtil.createDefaultResponse(Constants.UPDATE_OFFENSIVE_DATA_FLAG); + String userId = RequestInterceptor.fetchUserIdFromAccessToken(token); + errMsg = validateRequest(requestBody, userId, Constants.UPDATE); + if (StringUtils.isEmpty(errMsg)) { + try { + Map request = (Map) requestBody.get(Constants.REQUEST); + Map compositeKeys = new HashMap<>(); + compositeKeys.put(Constants.USER_ID, userId); + compositeKeys.put(Constants.CONTEXT_TYPE, request.get(Constants.CONTEXT_TYPE)); + compositeKeys.put(Constants.CONTEXT_TYPE_ID, request.get(Constants.CONTEXT_TYPE_ID)); + Map fieldsToBeUpdated = new HashMap<>(); + fieldsToBeUpdated.put(Constants.CONTEXT_STATUS, request.get(Constants.CONTEXT_STATUS)); + if(!ObjectUtils.isEmpty(request.get(Constants.ADDITIONAL_PARAMS))) { + fieldsToBeUpdated.put(Constants.ADDITIONAL_PARAMS, request.get(Constants.ADDITIONAL_PARAMS)); + } + fieldsToBeUpdated.put(Constants.UPDATED_BY, userId); + fieldsToBeUpdated.put(Constants.UPDATED_DATE, new Timestamp(new java.util.Date().getTime())); + Map resp = cassandraOperation.updateRecord(Constants.KEYSPACE_SUNBIRD, Constants.TABLE_OFFENSIVE_DATA_FLAGS, + fieldsToBeUpdated, compositeKeys); + if (!resp.get(Constants.RESPONSE).equals(Constants.SUCCESS)) { + errMsg = (String) resp.get(Constants.ERROR_MESSAGE); + } else { + response.getResult().put(Constants.STATUS, Constants.UPDATED); + } + } catch (Exception e) { + errMsg = String.format("Failed to update an offensive flag with the details. Exception: ", e.getMessage()); + logger.error(errMsg, e); + } + } + if (StringUtils.isNotBlank(errMsg)) { + response.getParams().setStatus(Constants.FAILED); + response.getParams().setErrmsg(errMsg); + response.setResponseCode(HttpStatus.BAD_REQUEST); + return response; + } + return response; + } + + @Override + public SBApiResponse getFlaggedData(String token) { + String userId = RequestInterceptor.fetchUserIdFromAccessToken(token); + String errMsg = ""; + SBApiResponse response = ProjectUtil.createDefaultResponse(Constants.GET_OFFENSIVE_DATA_FLAG); + if (userId != null) { + Map request = new HashMap<>(); + request.put(Constants.USER_ID, userId); + List> existingDataList = cassandraOperation.getRecordsByProperties( + Constants.KEYSPACE_SUNBIRD, Constants.TABLE_OFFENSIVE_DATA_FLAGS, request, null); + response.getResult().put("Content", existingDataList); + response.getResult().put("Count", existingDataList.size()); + } else { + errMsg = "Invalid User Id"; + } + if (StringUtils.isNotBlank(errMsg)) { + response.getParams().setStatus(Constants.FAILED); + response.getParams().setErrmsg(errMsg); + response.setResponseCode(HttpStatus.BAD_REQUEST); + return response; + } + return response; + } + + private String validateRequest(Map requestBody, String userId, String type) { + if (StringUtils.isEmpty(userId)) { + return "Invalid User Id"; + } + Map request = (Map) requestBody.get(Constants.REQUEST); + if (ObjectUtils.isEmpty(request)) { + return "Invalid Request."; + } + + StringBuilder errMsg = new StringBuilder(); + List missingAttributes = new ArrayList<>(); + List errList = new ArrayList<>(); + + String contextType = (String) request.get(Constants.CONTEXT_TYPE); + if (StringUtils.isBlank(contextType)) { + missingAttributes.add(Constants.CONTEXT_TYPE); + } + + String contextTypeId = (String) request.get(Constants.CONTEXT_TYPE_ID); + if (StringUtils.isBlank(contextTypeId)) { + missingAttributes.add(Constants.CONTEXT_TYPE_ID); + } + + if(type.equalsIgnoreCase(Constants.UPDATE)) { + String contextStatus = (String) request.get(Constants.CONTEXT_STATUS); + if (StringUtils.isBlank(contextStatus)) { + missingAttributes.add(Constants.CONTEXT_STATUS); + } + } + + if (!missingAttributes.isEmpty()) { + errMsg.append("Request doesn't have mandatory parameters - [").append(missingAttributes) + .append("]. "); + } + + if (!errList.isEmpty()) { + errMsg.append(errList.toString()); + } + return errMsg.toString(); + } +} \ No newline at end of file diff --git a/src/main/java/org/sunbird/cassandra/utils/CassandraOperationImpl.java b/src/main/java/org/sunbird/cassandra/utils/CassandraOperationImpl.java index fdcde43e9..cfe2d23f1 100644 --- a/src/main/java/org/sunbird/cassandra/utils/CassandraOperationImpl.java +++ b/src/main/java/org/sunbird/cassandra/utils/CassandraOperationImpl.java @@ -42,8 +42,10 @@ public SBApiResponse insertRecord(String keyspaceName, String tableName, Map updateRecord(String keyspaceName, String tableName, M session.execute(updateQuery); response.put(Constants.RESPONSE, Constants.SUCCESS); } catch (Exception e) { - logger.error(e.getMessage()); + String errMsg = String.format("Exception occurred while updating record to %s %s", tableName, e.getMessage()); + logger.error(errMsg); + response.put(Constants.RESPONSE, Constants.FAILED); + response.put(Constants.ERROR_MESSAGE, errMsg); throw e; } return response; diff --git a/src/main/java/org/sunbird/common/util/Constants.java b/src/main/java/org/sunbird/common/util/Constants.java index 868ce2be6..8b9a6ac85 100644 --- a/src/main/java/org/sunbird/common/util/Constants.java +++ b/src/main/java/org/sunbird/common/util/Constants.java @@ -618,8 +618,21 @@ public class Constants { COURSE_ORG_NAME); public static final List USER_ENROLMENT_COMMON_FIELDS = Arrays.asList(STATUS, COMPLETION_PERCENTAGE); + + public static final String CONTEXT_TYPE = "contextType"; + public static final String CONTEXT_TYPE_ID = "contextTypeId"; + public static final String ADDITIONAL_PARAMS = "additionalParams"; + public static final String CONTEXT_STATUS = "contextStatus"; + public static final String TABLE_OFFENSIVE_DATA_FLAGS = "offensive_data_flags"; + public static final String ADD_OFFENSIVE_DATA_FLAG = "api.add.offensive.data.flag"; + public static final String UPDATE_OFFENSIVE_DATA_FLAG = "api.update.offensive.data.flag"; + public static final String GET_OFFENSIVE_DATA_FLAG = "api.get.offensive.data.flag"; + public static final String DRAFT = "DRAFT"; + public static final Object CREATED = "Created"; + public static final Object UPDATED = "Updated"; + private Constants() { throw new IllegalStateException("Utility class"); } -} +} \ No newline at end of file diff --git a/src/main/resources/cassandratablecolumn.properties b/src/main/resources/cassandratablecolumn.properties index 6907d111d..0a892a777 100644 --- a/src/main/resources/cassandratablecolumn.properties +++ b/src/main/resources/cassandratablecolumn.properties @@ -43,4 +43,10 @@ filename=fileName rootorgid=rootOrgId filepath=filePath mapid=mapId -sborgid=sbOrgId \ No newline at end of file +sborgid=sbOrgId +contexttype=contextType +contexttypeid=contextTypeId +acquiredchannel=acquiredChannel +additionalparams=additionalParams +requestedby=requestedBy +contextstatus=contextStatus \ No newline at end of file