Fix issues in Event DAO and improvements for the operation task

corrective-policy
Pahansith 4 years ago
parent f073ee9276
commit fed779302a

@ -304,8 +304,8 @@ public abstract class AbstractEventConfigDAO implements EventConfigDAO {
"EVENT_SOURCE " + "EVENT_SOURCE " +
"FROM DM_DEVICE_EVENT E, DM_DEVICE_EVENT_GROUP_MAPPING G " + "FROM DM_DEVICE_EVENT E, DM_DEVICE_EVENT_GROUP_MAPPING G " +
"WHERE G.EVENT_ID = E.ID " + "WHERE G.EVENT_ID = E.ID " +
"AND G.GROUP_ID = ?" + "AND G.GROUP_ID = ? " +
"AND E.TENANT_ID = ?" + "AND E.TENANT_ID = ? " +
"GROUP BY EVENT_SOURCE"; "GROUP BY EVENT_SOURCE";
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, groupId); stmt.setInt(1, groupId);

@ -464,14 +464,13 @@ public class GeofenceDAOImpl implements GeofenceDAO {
List<EventConfig> eventList = new ArrayList<>(); List<EventConfig> eventList = new ArrayList<>();
Connection conn = this.getConnection(); Connection conn = this.getConnection();
String sql = "SELECT " + String sql = "SELECT " +
"ID AS EVENT_ID, " + "E.ID AS EVENT_ID, " +
"EVENT_SOURCE, " + "EVENT_SOURCE, " +
"EVENT_LOGIC, " + "EVENT_LOGIC, " +
"ACTIONS " + "ACTIONS " +
"FROM DM_DEVICE_EVENT " + "FROM DM_DEVICE_EVENT E, DM_GEOFENCE_EVENT_MAPPING G " +
"WHERE ID IN (" + "WHERE E.ID = G.EVENT_ID " +
"SELECT EVENT_ID FROM DM_GEOFENCE_EVENT_MAPPING " + "AND G.FENCE_ID = ?";
"WHERE FENCE_ID = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, geofenceId); stmt.setInt(1, geofenceId);
return getEventConfigs(eventList, stmt); return getEventConfigs(eventList, stmt);

@ -65,6 +65,9 @@ public class DeviceEventOperationExecutor implements Runnable {
EventConfigurationProviderService eventConfigurationService = DeviceManagementDataHolder.getInstance().getEventConfigurationService(); EventConfigurationProviderService eventConfigurationService = DeviceManagementDataHolder.getInstance().getEventConfigurationService();
try { try {
List<String> eventSources = eventConfigurationService.getEventsSourcesOfGroup(groupId, tenantId); List<String> 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) { for (String eventSource : eventSources) {
if (eventSource.equalsIgnoreCase(DeviceManagementConstants.EventServices.GEOFENCE)) { if (eventSource.equalsIgnoreCase(DeviceManagementConstants.EventServices.GEOFENCE)) {
setGeoFenceOperationContent(operation); setGeoFenceOperationContent(operation);

@ -18,6 +18,7 @@
package org.wso2.carbon.device.mgt.core.event.config; 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.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
@ -68,16 +69,6 @@ public class GroupEventOperationExecutor implements Runnable {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Event creation operation started for groups with IDs " + Arrays.toString(groupIds.toArray())); 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.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId, true); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId, true);
GroupManagementProviderService groupManagementService = DeviceManagementDataHolder 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); 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<DeviceIdentifier> deviceIdentifiers = new ArrayList<>(); List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>();
for (Device device : devices) { for (Device device : devices) {
if (device.getType().equalsIgnoreCase("android")) { //TODO introduce a proper mechanism for event handling for each device types 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" + log.error("Creating event operation failed.\n" +
"Could not found device/devices for the defined device identifiers.", e); "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) { private void createGeoFenceOperation(ProfileOperation operation) {
@ -140,11 +142,13 @@ public class GroupEventOperationExecutor implements Runnable {
log.debug("Retrieved event records of Geo Fence " + geoFenceMeta.getId() + log.debug("Retrieved event records of Geo Fence " + geoFenceMeta.getId() +
". events " + Arrays.toString(eventConfigList.toArray())); ". events " + Arrays.toString(eventConfigList.toArray()));
} }
List<EventOperation> eventOperations = new ArrayList<>();
EventOperation eventOperation = new EventOperation(); EventOperation eventOperation = new EventOperation();
eventOperation.setEventDefinition(eventMetaData); eventOperation.setEventDefinition(eventMetaData);
eventOperation.setEventSource(eventSource); eventOperation.setEventSource(eventSource);
eventOperation.setEventTriggers(eventConfigList); eventOperation.setEventTriggers(eventConfigList);
operation.setPayLoad(eventOperation.toJSON()); eventOperations.add(eventOperation);
operation.setPayLoad(new Gson().toJson(eventOperations));
} catch (GeoLocationBasedServiceException e) { } catch (GeoLocationBasedServiceException e) {
log.error("Failed to retrieve event data of Geo fence " + geoFenceMeta.getId() log.error("Failed to retrieve event data of Geo fence " + geoFenceMeta.getId()
+ " : " + geoFenceMeta.getFenceName(), e); + " : " + geoFenceMeta.getFenceName(), e);

Loading…
Cancel
Save