From 27ae7cfed8321739d15dc8efafd9b0559cb791c5 Mon Sep 17 00:00:00 2001 From: Arcane94 Date: Fri, 23 Feb 2018 21:54:55 +0530 Subject: [PATCH] Fix for adding alerts twice with same name bug --- .../service/api/GeoLocationBasedService.java | 2 +- .../impl/GeoLocationBasedServiceImpl.java | 11 +++++- .../common/geo/service/AlertAlreadyExist.java | 25 ++++++++++++ .../service/GeoLocationProviderService.java | 4 +- .../GeoLocationProviderServiceImpl.java | 38 ++++++------------- 5 files changed, 49 insertions(+), 31 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/AlertAlreadyExist.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GeoLocationBasedService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GeoLocationBasedService.java index 23602baf0c..8396457e3f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GeoLocationBasedService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GeoLocationBasedService.java @@ -327,7 +327,7 @@ public interface GeoLocationBasedService { }), @ApiResponse( code = 400, - message = "Bad Request. \n Invalid Device Identifiers found.", + message = "Bad Request. \n A geo alert with this name already exists.", response = Response.class), @ApiResponse( code = 401, diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java index 4f34589157..4bee9d62a8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java @@ -48,6 +48,7 @@ import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtUtil; import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.utils.multitenancy.MultitenantUtils; +import javax.persistence.EntityExistsException; import javax.ws.rs.*; import javax.ws.rs.core.Response; import java.util.ArrayList; @@ -201,9 +202,13 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService { geoService.createGeoAlert(alert, alertType); return Response.ok().build(); } catch (GeoLocationBasedServiceException e) { - String error = "Error occurred while creating " + alertType + "alert"; + String error = "Error occurred while creating " + alertType + " alert"; log.error(error, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build(); + } catch (AlertAlreadyExist e) { + String error = "A geo alert with this name already exists."; + log.error(error,e); + return Response.status(Response.Status.BAD_REQUEST).entity(error).build(); } } @@ -263,6 +268,10 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService { String error = "Error occurred while updating the geo alert for geo clusters"; log.error(error, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build(); + } catch (AlertAlreadyExist e) { + String error = "A geo alert with this name already exists."; + log.error(error,e); + return Response.status(Response.Status.BAD_REQUEST).entity(error).build(); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/AlertAlreadyExist.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/AlertAlreadyExist.java new file mode 100644 index 0000000000..451b8d98d6 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/AlertAlreadyExist.java @@ -0,0 +1,25 @@ +package org.wso2.carbon.device.mgt.common.geo.service; + +/** + * Custom exception class of Geo Service related operations. + */ + +public class AlertAlreadyExist extends Exception { + + private static final long serialVersionUID = 4709355511911265093L; + + private String errorMessage; + + public String getErrorMessage() { + return errorMessage; + } + + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + + public AlertAlreadyExist(String msg) { + super(msg); + setErrorMessage(msg); + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/GeoLocationProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/GeoLocationProviderService.java index 47daac0492..66e5504f95 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/GeoLocationProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/GeoLocationProviderService.java @@ -40,13 +40,13 @@ public interface GeoLocationProviderService { throws GeoLocationBasedServiceException, AlertAlreadyExistException; boolean createGeoAlert(Alert alert, String alertType) - throws GeoLocationBasedServiceException; + throws GeoLocationBasedServiceException,AlertAlreadyExist; boolean updateGeoAlert(Alert alert, DeviceIdentifier identifier, String alertType) throws GeoLocationBasedServiceException, AlertAlreadyExistException; boolean updateGeoAlert(Alert alert, String alertType) - throws GeoLocationBasedServiceException; + throws GeoLocationBasedServiceException,AlertAlreadyExist; boolean removeGeoAlert(String alertType, DeviceIdentifier identifier, String queryName) throws GeoLocationBasedServiceException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java index 7174bea0ae..cf95db9b27 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java @@ -55,6 +55,7 @@ import org.wso2.carbon.registry.api.Resource; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManagerFactory; +import javax.persistence.EntityExistsException; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; @@ -303,7 +304,7 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic @Override public boolean createGeoAlert(Alert alert, DeviceIdentifier identifier, String alertType) - throws GeoLocationBasedServiceException, AlertAlreadyExistException { + throws GeoLocationBasedServiceException { return saveGeoAlert(alert, identifier, alertType, false); } @@ -315,7 +316,7 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic @Override public boolean updateGeoAlert(Alert alert, DeviceIdentifier identifier, String alertType) - throws GeoLocationBasedServiceException, AlertAlreadyExistException { + throws GeoLocationBasedServiceException { return saveGeoAlert(alert, identifier, alertType, true); } @@ -423,7 +424,7 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic } public boolean saveGeoAlert(Alert alert, DeviceIdentifier identifier, String alertType, boolean isUpdate) - throws GeoLocationBasedServiceException, AlertAlreadyExistException { + throws GeoLocationBasedServiceException { Type type = new TypeToken>() { }.getType(); @@ -465,38 +466,23 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic "Unrecognized execution plan type: " + alertType + " while creating geo alert"); } + //persist alert in registry + updateRegistry(getRegistryPath(alertType, identifier, alert.getQueryName()), identifier, content, + options); + //deploy alert into event processor EventProcessorAdminServiceStub eventprocessorStub = null; String action = (isUpdate ? "updating" : "creating"); try { - ExecutionPlanConfigurationDto[] allActiveExecutionPlanConfigs = null; - String activeExecutionPlan = null; - String executionPlanName = getExecutionPlanName(alertType, alert.getQueryName(), - identifier.getId()); eventprocessorStub = getEventProcessorAdminServiceStub(); String parsedTemplate = parseTemplate(alertType, parseMap); String validationResponse = eventprocessorStub.validateExecutionPlan(parsedTemplate); if (validationResponse.equals("success")) { - allActiveExecutionPlanConfigs = eventprocessorStub.getAllActiveExecutionPlanConfigurations(); if (isUpdate) { - for (ExecutionPlanConfigurationDto activeExectionPlanConfig:allActiveExecutionPlanConfigs) { - activeExecutionPlan = activeExectionPlanConfig.getExecutionPlan(); - if (activeExecutionPlan.contains(executionPlanName)) { - eventprocessorStub.editActiveExecutionPlan(parsedTemplate, executionPlanName); - return true; - } - } - eventprocessorStub.deployExecutionPlan(parsedTemplate); + String executionPlanName = getExecutionPlanName(alertType, alert.getQueryName(), + identifier.getId()); + eventprocessorStub.editActiveExecutionPlan(parsedTemplate, executionPlanName); } else { - for (ExecutionPlanConfigurationDto activeExectionPlanConfig:allActiveExecutionPlanConfigs) { - activeExecutionPlan = activeExectionPlanConfig.getExecutionPlan(); - if (activeExecutionPlan.contains(executionPlanName)) { - throw new AlertAlreadyExistException("Execution plan already exists with name " - + executionPlanName); - } - } - updateRegistry(getRegistryPath(alertType, identifier, alert.getQueryName()), identifier, content, - options); eventprocessorStub.deployExecutionPlan(parsedTemplate); } } else { @@ -592,8 +578,6 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic private String getExecutionPlanName(String alertType, String queryName, String deviceId) { if ("Traffic".equals(alertType)) { return "Geo-ExecutionPlan-Traffic_" + queryName + "_alert"; - } else if ("Speed".equals(alertType)) { - return "Geo-ExecutionPlan-" + alertType + "---" + deviceId + "_alert"; } else { return "Geo-ExecutionPlan-" + alertType + "_" + queryName + "---_" + deviceId + "_alert"; }