From fed779302a327a090e67a9856553c6e35311df6b Mon Sep 17 00:00:00 2001 From: Pahansith Date: Fri, 13 Nov 2020 09:35:37 +0530 Subject: [PATCH] Fix issues in Event DAO and improvements for the operation task --- .../core/dao/impl/AbstractEventConfigDAO.java | 4 +-- .../mgt/core/dao/impl/GeofenceDAOImpl.java | 9 +++--- .../config/DeviceEventOperationExecutor.java | 3 ++ .../config/GroupEventOperationExecutor.java | 28 +++++++++++-------- 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractEventConfigDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractEventConfigDAO.java index e77e9bc9f1..f30b8456e8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractEventConfigDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractEventConfigDAO.java @@ -304,8 +304,8 @@ public abstract class AbstractEventConfigDAO implements EventConfigDAO { "EVENT_SOURCE " + "FROM DM_DEVICE_EVENT E, DM_DEVICE_EVENT_GROUP_MAPPING G " + "WHERE G.EVENT_ID = E.ID " + - "AND G.GROUP_ID = ?" + - "AND E.TENANT_ID = ?" + + "AND G.GROUP_ID = ? " + + "AND E.TENANT_ID = ? " + "GROUP BY EVENT_SOURCE"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, groupId); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GeofenceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GeofenceDAOImpl.java index 26e9b9ae99..b01ead0d5d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GeofenceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GeofenceDAOImpl.java @@ -464,14 +464,13 @@ public class GeofenceDAOImpl implements GeofenceDAO { List eventList = new ArrayList<>(); Connection conn = this.getConnection(); String sql = "SELECT " + - "ID AS EVENT_ID, " + + "E.ID AS EVENT_ID, " + "EVENT_SOURCE, " + "EVENT_LOGIC, " + "ACTIONS " + - "FROM DM_DEVICE_EVENT " + - "WHERE ID IN (" + - "SELECT EVENT_ID FROM DM_GEOFENCE_EVENT_MAPPING " + - "WHERE FENCE_ID = ?"; + "FROM DM_DEVICE_EVENT E, DM_GEOFENCE_EVENT_MAPPING G " + + "WHERE E.ID = G.EVENT_ID " + + "AND G.FENCE_ID = ?"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, geofenceId); return getEventConfigs(eventList, stmt); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/event/config/DeviceEventOperationExecutor.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/event/config/DeviceEventOperationExecutor.java index 9d70ad4646..215c7e5150 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/event/config/DeviceEventOperationExecutor.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/event/config/DeviceEventOperationExecutor.java @@ -65,6 +65,9 @@ public class DeviceEventOperationExecutor implements Runnable { EventConfigurationProviderService eventConfigurationService = DeviceManagementDataHolder.getInstance().getEventConfigurationService(); try { List eventSources = eventConfigurationService.getEventsSourcesOfGroup(groupId, tenantId); + if (eventSources == null || eventSources.isEmpty()) { + log.info("No events applied for queried group with ID " + groupId); + } for (String eventSource : eventSources) { if (eventSource.equalsIgnoreCase(DeviceManagementConstants.EventServices.GEOFENCE)) { setGeoFenceOperationContent(operation); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/event/config/GroupEventOperationExecutor.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/event/config/GroupEventOperationExecutor.java index 882b058f63..380b69f249 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/event/config/GroupEventOperationExecutor.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/event/config/GroupEventOperationExecutor.java @@ -18,6 +18,7 @@ package org.wso2.carbon.device.mgt.core.event.config; +import com.google.gson.Gson; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; @@ -68,16 +69,6 @@ public class GroupEventOperationExecutor implements Runnable { if (log.isDebugEnabled()) { log.debug("Event creation operation started for groups with IDs " + Arrays.toString(groupIds.toArray())); } - ProfileOperation operation = new ProfileOperation(); - operation.setCode(OperationMgtConstants.OperationCodes.EVENT_CONFIG); - operation.setType(Operation.Type.PROFILE); - if (eventSource.equalsIgnoreCase(DeviceManagementConstants.EventServices.GEOFENCE)) { - createGeoFenceOperation(operation); - } //extend with another cases to handle other types of events - - if (log.isDebugEnabled()) { - log.debug("Starting tenant flow for tenant id " + tenantId); - } PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId, true); GroupManagementProviderService groupManagementService = DeviceManagementDataHolder @@ -104,6 +95,17 @@ public class GroupEventOperationExecutor implements Runnable { log.error("Failed to retrieve devices of group with ID " + groupId + " and name " + group.getName(), e); } } + ProfileOperation operation = new ProfileOperation(); + operation.setCode(OperationMgtConstants.OperationCodes.EVENT_CONFIG); + operation.setType(Operation.Type.PROFILE); + if (eventSource.equalsIgnoreCase(DeviceManagementConstants.EventServices.GEOFENCE)) { + createGeoFenceOperation(operation); + } //extend with another cases to handle other types of events + + if (log.isDebugEnabled()) { + log.debug("Starting tenant flow for tenant id " + tenantId); + } + List deviceIdentifiers = new ArrayList<>(); for (Device device : devices) { if (device.getType().equalsIgnoreCase("android")) { //TODO introduce a proper mechanism for event handling for each device types @@ -127,7 +129,7 @@ public class GroupEventOperationExecutor implements Runnable { log.error("Creating event operation failed.\n" + "Could not found device/devices for the defined device identifiers.", e); } - log.info("Event operation creation succeeded"); + log.info("Event operation creation task completed"); } private void createGeoFenceOperation(ProfileOperation operation) { @@ -140,11 +142,13 @@ public class GroupEventOperationExecutor implements Runnable { log.debug("Retrieved event records of Geo Fence " + geoFenceMeta.getId() + ". events " + Arrays.toString(eventConfigList.toArray())); } + List eventOperations = new ArrayList<>(); EventOperation eventOperation = new EventOperation(); eventOperation.setEventDefinition(eventMetaData); eventOperation.setEventSource(eventSource); eventOperation.setEventTriggers(eventConfigList); - operation.setPayLoad(eventOperation.toJSON()); + eventOperations.add(eventOperation); + operation.setPayLoad(new Gson().toJson(eventOperations)); } catch (GeoLocationBasedServiceException e) { log.error("Failed to retrieve event data of Geo fence " + geoFenceMeta.getId() + " : " + geoFenceMeta.getFenceName(), e);