diff --git a/source/src/main/java/org/cerberus/core/apiprivate/CampaignExecutionPrivateController.java b/source/src/main/java/org/cerberus/core/apiprivate/CampaignExecutionPrivateController.java index a3dc9dc1c..aa1e29dd4 100644 --- a/source/src/main/java/org/cerberus/core/apiprivate/CampaignExecutionPrivateController.java +++ b/source/src/main/java/org/cerberus/core/apiprivate/CampaignExecutionPrivateController.java @@ -78,9 +78,27 @@ public ResponseEntity getTagStatistics( AnswerList daoAnswer; List systemsAllowed; List applicationsAllowed; - JSONObject response = new JSONObject(); + Map mandatoryFilters = new HashMap<>(); + mandatoryFilters.put("System", systems); + mandatoryFilters.put("Application", applications); + mandatoryFilters.put("From date", fromParam); + mandatoryFilters.put("To date", toParam); + + JSONObject response = new JSONObject(); try { + List missingParameters = checkMissingFilters(mandatoryFilters); + if (!missingParameters.isEmpty()) { + MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED); + msg.setDescription(msg.getDescription().replace("%ITEM%", "Campaign Statistics")); + msg.setDescription(msg.getDescription().replace("%OPERATION%", "Get Statistics")); + msg.setDescription(msg.getDescription().replace("%REASON%", String.format("Missing filter(s): %s", missingParameters.toString()))); + response.put("message", msg.getDescription()); + return ResponseEntity + .status(HttpStatus.BAD_REQUEST) + .body(response.toString()); + } + if (request.getUserPrincipal() == null) { MessageEvent message = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNAUTHORISED); message.setDescription(message.getDescription().replace("%ITEM%", "Campaign Statistics")); @@ -95,21 +113,18 @@ public ResponseEntity getTagStatistics( systemsAllowed = tagStatisticService.getSystemsAllowedForUser(request.getUserPrincipal().getName()); applicationsAllowed = tagStatisticService.getApplicationsSystems(systemsAllowed); - if (systems.isEmpty() && applications.isEmpty()) { - tagStatistics = new ArrayList<>(); - } else { - //If user put in filter a system that is has no access, we delete from the list. - systems.removeIf(param -> !systemsAllowed.contains(param)); - applications.removeIf(param -> !applicationsAllowed.contains(param)); - daoAnswer = tagStatisticService.readByCriteria(systems, applications, group1List, fromDateFormatted, toDateFormatted); - - if (daoAnswer.getDataList().isEmpty()) { - response.put("message", daoAnswer.getResultMessage().getDescription()); - return ResponseEntity - .status(HttpStatus.NOT_FOUND) - .body(response.toString()); - } - tagStatistics = daoAnswer.getDataList(); + //If user put in filter a system that is has no access, we delete from the list. + systems.removeIf(param -> !systemsAllowed.contains(param)); + applications.removeIf(param -> !applicationsAllowed.contains(param)); + + daoAnswer = tagStatisticService.readByCriteria(systems, applications, group1List, fromDateFormatted, toDateFormatted); + tagStatistics = daoAnswer.getDataList(); + + if (tagStatistics.isEmpty()) { + response.put("message", daoAnswer.getResultMessage().getDescription()); + return ResponseEntity + .status(HttpStatus.NOT_FOUND) + .body(response.toString()); } Map> aggregateByTag = tagStatisticService.createMapGroupedByTag(tagStatistics, "CAMPAIGN"); @@ -154,7 +169,25 @@ public ResponseEntity getCampaignStatisticsByCountryEnv( List tagStatistics; JSONObject response = new JSONObject(); + Map mandatoryFilters = new HashMap<>(); + mandatoryFilters.put("Campaign", campaign); + mandatoryFilters.put("From date", fromParam); + mandatoryFilters.put("To date", toParam); + try { + List missingParameters = checkMissingFilters(mandatoryFilters); + + if (!missingParameters.isEmpty()) { + MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED); + msg.setDescription(msg.getDescription().replace("%ITEM%", "Campaign Statistics")); + msg.setDescription(msg.getDescription().replace("%OPERATION%", "Get Statistics")); + msg.setDescription(msg.getDescription().replace("%REASON%", String.format("Missing filter(s): %s", missingParameters.toString()))); + response.put("message", msg.getDescription()); + return ResponseEntity + .status(HttpStatus.BAD_REQUEST) + .body(response.toString()); + } + if (request.getUserPrincipal() == null) { MessageEvent message = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNAUTHORISED); message.setDescription(message.getDescription().replace("%ITEM%", "Campaign Statistics")); @@ -167,16 +200,15 @@ public ResponseEntity getCampaignStatisticsByCountryEnv( } AnswerList daoAnswer = tagStatisticService.readByCriteria(campaign, countries, environments, fromDateFormatted, toDateFormatted); + tagStatistics = daoAnswer.getDataList(); - if (daoAnswer.getDataList().isEmpty()) { + if (tagStatistics.isEmpty()) { response.put("message", daoAnswer.getResultMessage().getDescription()); return ResponseEntity .status(HttpStatus.NOT_FOUND) .body(response.toString()); } - tagStatistics = daoAnswer.getDataList(); - boolean userHasRightSystems = tagStatisticService.userHasRightSystems(request.getUserPrincipal().getName(), tagStatistics); if (!userHasRightSystems) { response.put("message", new MessageEvent(MessageEventEnum.DATA_OPERATION_NOT_FOUND_OR_NOT_AUTHORIZE).getDescription()); @@ -243,4 +275,17 @@ public String updateUndeclareFalseNegative( } + private List checkMissingFilters(Map filters) { + List missingParameters = new ArrayList<>(); + for (Map.Entry filter : filters.entrySet()) { + if (filter.getValue() instanceof String && ((String) filter.getValue()).isEmpty()) { + missingParameters.add(filter.getKey()); + } + if (filter.getValue() instanceof ArrayList && (((ArrayList) filter.getValue()).isEmpty())) { + missingParameters.add(filter.getKey()); + } + } + return missingParameters; + } + }