Fix for adding alerts twice with same name bug

merge-requests/1/head
Arcane94 7 years ago
parent 9e64c8971d
commit 27ae7cfed8

@ -327,7 +327,7 @@ public interface GeoLocationBasedService {
}), }),
@ApiResponse( @ApiResponse(
code = 400, 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), response = Response.class),
@ApiResponse( @ApiResponse(
code = 401, code = 401,

@ -48,6 +48,7 @@ import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtUtil;
import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.utils.multitenancy.MultitenantUtils; import org.wso2.carbon.utils.multitenancy.MultitenantUtils;
import javax.persistence.EntityExistsException;
import javax.ws.rs.*; import javax.ws.rs.*;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.util.ArrayList; import java.util.ArrayList;
@ -201,9 +202,13 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
geoService.createGeoAlert(alert, alertType); geoService.createGeoAlert(alert, alertType);
return Response.ok().build(); return Response.ok().build();
} catch (GeoLocationBasedServiceException e) { } catch (GeoLocationBasedServiceException e) {
String error = "Error occurred while creating " + alertType + "alert"; String error = "Error occurred while creating " + alertType + " alert";
log.error(error, e); log.error(error, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build(); 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"; String error = "Error occurred while updating the geo alert for geo clusters";
log.error(error, e); log.error(error, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build(); 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();
} }
} }

@ -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);
}
}

@ -40,13 +40,13 @@ public interface GeoLocationProviderService {
throws GeoLocationBasedServiceException, AlertAlreadyExistException; throws GeoLocationBasedServiceException, AlertAlreadyExistException;
boolean createGeoAlert(Alert alert, String alertType) boolean createGeoAlert(Alert alert, String alertType)
throws GeoLocationBasedServiceException; throws GeoLocationBasedServiceException,AlertAlreadyExist;
boolean updateGeoAlert(Alert alert, DeviceIdentifier identifier, String alertType) boolean updateGeoAlert(Alert alert, DeviceIdentifier identifier, String alertType)
throws GeoLocationBasedServiceException, AlertAlreadyExistException; throws GeoLocationBasedServiceException, AlertAlreadyExistException;
boolean updateGeoAlert(Alert alert, String alertType) boolean updateGeoAlert(Alert alert, String alertType)
throws GeoLocationBasedServiceException; throws GeoLocationBasedServiceException,AlertAlreadyExist;
boolean removeGeoAlert(String alertType, DeviceIdentifier identifier, String queryName) boolean removeGeoAlert(String alertType, DeviceIdentifier identifier, String queryName)
throws GeoLocationBasedServiceException; throws GeoLocationBasedServiceException;

@ -55,6 +55,7 @@ import org.wso2.carbon.registry.api.Resource;
import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory; import javax.net.ssl.TrustManagerFactory;
import javax.persistence.EntityExistsException;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -303,7 +304,7 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
@Override @Override
public boolean createGeoAlert(Alert alert, DeviceIdentifier identifier, String alertType) public boolean createGeoAlert(Alert alert, DeviceIdentifier identifier, String alertType)
throws GeoLocationBasedServiceException, AlertAlreadyExistException { throws GeoLocationBasedServiceException {
return saveGeoAlert(alert, identifier, alertType, false); return saveGeoAlert(alert, identifier, alertType, false);
} }
@ -315,7 +316,7 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
@Override @Override
public boolean updateGeoAlert(Alert alert, DeviceIdentifier identifier, String alertType) public boolean updateGeoAlert(Alert alert, DeviceIdentifier identifier, String alertType)
throws GeoLocationBasedServiceException, AlertAlreadyExistException { throws GeoLocationBasedServiceException {
return saveGeoAlert(alert, identifier, alertType, true); 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) public boolean saveGeoAlert(Alert alert, DeviceIdentifier identifier, String alertType, boolean isUpdate)
throws GeoLocationBasedServiceException, AlertAlreadyExistException { throws GeoLocationBasedServiceException {
Type type = new TypeToken<Map<String, String>>() { Type type = new TypeToken<Map<String, String>>() {
}.getType(); }.getType();
@ -465,38 +466,23 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
"Unrecognized execution plan type: " + alertType + " while creating geo alert"); "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 //deploy alert into event processor
EventProcessorAdminServiceStub eventprocessorStub = null; EventProcessorAdminServiceStub eventprocessorStub = null;
String action = (isUpdate ? "updating" : "creating"); String action = (isUpdate ? "updating" : "creating");
try { try {
ExecutionPlanConfigurationDto[] allActiveExecutionPlanConfigs = null;
String activeExecutionPlan = null;
String executionPlanName = getExecutionPlanName(alertType, alert.getQueryName(),
identifier.getId());
eventprocessorStub = getEventProcessorAdminServiceStub(); eventprocessorStub = getEventProcessorAdminServiceStub();
String parsedTemplate = parseTemplate(alertType, parseMap); String parsedTemplate = parseTemplate(alertType, parseMap);
String validationResponse = eventprocessorStub.validateExecutionPlan(parsedTemplate); String validationResponse = eventprocessorStub.validateExecutionPlan(parsedTemplate);
if (validationResponse.equals("success")) { if (validationResponse.equals("success")) {
allActiveExecutionPlanConfigs = eventprocessorStub.getAllActiveExecutionPlanConfigurations();
if (isUpdate) { if (isUpdate) {
for (ExecutionPlanConfigurationDto activeExectionPlanConfig:allActiveExecutionPlanConfigs) { String executionPlanName = getExecutionPlanName(alertType, alert.getQueryName(),
activeExecutionPlan = activeExectionPlanConfig.getExecutionPlan(); identifier.getId());
if (activeExecutionPlan.contains(executionPlanName)) { eventprocessorStub.editActiveExecutionPlan(parsedTemplate, executionPlanName);
eventprocessorStub.editActiveExecutionPlan(parsedTemplate, executionPlanName);
return true;
}
}
eventprocessorStub.deployExecutionPlan(parsedTemplate);
} else { } 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); eventprocessorStub.deployExecutionPlan(parsedTemplate);
} }
} else { } else {
@ -592,8 +578,6 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
private String getExecutionPlanName(String alertType, String queryName, String deviceId) { private String getExecutionPlanName(String alertType, String queryName, String deviceId) {
if ("Traffic".equals(alertType)) { if ("Traffic".equals(alertType)) {
return "Geo-ExecutionPlan-Traffic_" + queryName + "_alert"; return "Geo-ExecutionPlan-Traffic_" + queryName + "_alert";
} else if ("Speed".equals(alertType)) {
return "Geo-ExecutionPlan-" + alertType + "---" + deviceId + "_alert";
} else { } else {
return "Geo-ExecutionPlan-" + alertType + "_" + queryName + "---_" + deviceId + "_alert"; return "Geo-ExecutionPlan-" + alertType + "_" + queryName + "---_" + deviceId + "_alert";
} }

Loading…
Cancel
Save