From f073ee927633e78cec31c75ce7d9e88dbe5c2ff1 Mon Sep 17 00:00:00 2001 From: Pahansith Date: Thu, 12 Nov 2020 15:11:30 +0530 Subject: [PATCH] Add H2 DAO for event configuration --- .../service/api/GeoLocationBasedService.java | 6 +- .../impl/GeoLocationBasedServiceImpl.java | 7 +- .../core/dao/DeviceManagementDAOFactory.java | 18 +++- ...OImpl.java => AbstractEventConfigDAO.java} | 45 +--------- .../mgt/core/dao/impl/GeofenceDAOImpl.java | 1 - .../impl/event/GenericEventConfigDAOImpl.java | 83 +++++++++++++++++++ .../dao/impl/event/H2EventConfigDAOImpl.java | 80 ++++++++++++++++++ 7 files changed, 192 insertions(+), 48 deletions(-) rename components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/{EventConfigDAOImpl.java => AbstractEventConfigDAO.java} (87%) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/event/GenericEventConfigDAOImpl.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/event/H2EventConfigDAOImpl.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 2121c6135e..5e55acdc77 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 @@ -930,7 +930,11 @@ public interface GeoLocationBasedService { name = "fenceId", value = "Id of the fence", required = true) - @PathParam("fenceId") int fenceId); + @PathParam("fenceId") int fenceId, + @ApiParam( + name = "requireEventData", + value = "Require geofence event data") + @QueryParam("requireEventData") boolean requireEventData); @GET 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 399d24383f..568cdfc41a 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 @@ -647,7 +647,8 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService { @GET @Consumes("application/json") @Produces("application/json") - public Response getGeofence(@PathParam("fenceId") int fenceId) { + public Response getGeofence(@PathParam("fenceId") int fenceId, + @QueryParam("requireEventData") boolean requireEventData) { try { GeoLocationProviderService geoService = DeviceMgtAPIUtils.getGeoService(); GeofenceData geofenceData = geoService.getGeoFences(fenceId); @@ -655,6 +656,10 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService { String msg = "No valid Geofence found for ID " + fenceId; return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); } + if (requireEventData) { + List eventsOfGeoFence = geoService.getEventsOfGeoFence(geofenceData.getId()); + geofenceData.setEventConfig(eventsOfGeoFence); + } return Response.status(Response.Status.OK).entity(getMappedResponseBean(geofenceData)).build(); } catch (GeoLocationBasedServiceException e) { String msg = "Server error occurred while retrieving Geofence for Id " + fenceId; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java index 2fbb922d70..c5c778ca23 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java @@ -29,12 +29,13 @@ import org.wso2.carbon.device.mgt.core.config.datasource.JNDILookupDefinition; import org.wso2.carbon.device.mgt.core.dao.impl.ApplicationDAOImpl; import org.wso2.carbon.device.mgt.core.dao.impl.DeviceTypeDAOImpl; import org.wso2.carbon.device.mgt.core.dao.impl.EnrollmentDAOImpl; -import org.wso2.carbon.device.mgt.core.dao.impl.EventConfigDAOImpl; +import org.wso2.carbon.device.mgt.core.dao.impl.event.GenericEventConfigDAOImpl; import org.wso2.carbon.device.mgt.core.dao.impl.GeofenceDAOImpl; import org.wso2.carbon.device.mgt.core.dao.impl.device.GenericDeviceDAOImpl; import org.wso2.carbon.device.mgt.core.dao.impl.device.OracleDeviceDAOImpl; import org.wso2.carbon.device.mgt.core.dao.impl.device.PostgreSQLDeviceDAOImpl; import org.wso2.carbon.device.mgt.core.dao.impl.device.SQLServerDeviceDAOImpl; +import org.wso2.carbon.device.mgt.core.dao.impl.event.H2EventConfigDAOImpl; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsDAO; import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.impl.DeviceDetailsDAOImpl; @@ -156,7 +157,20 @@ public class DeviceManagementDAOFactory { } public static EventConfigDAO getEventConfigDAO() { - return new EventConfigDAOImpl(); + if (databaseEngine != null) { + switch (databaseEngine) { + case DeviceManagementConstants.DataBaseTypes.DB_TYPE_POSTGRESQL: + case DeviceManagementConstants.DataBaseTypes.DB_TYPE_ORACLE: + case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MSSQL: + case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MYSQL: + return new GenericEventConfigDAOImpl(); + case DeviceManagementConstants.DataBaseTypes.DB_TYPE_H2: + return new H2EventConfigDAOImpl(); + default: + throw new UnsupportedDatabaseEngineException("Unsupported database engine : " + databaseEngine); + } + } + throw new IllegalStateException("Database engine has not initialized properly."); } public static void init(DataSourceConfig config) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/EventConfigDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractEventConfigDAO.java similarity index 87% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/EventConfigDAOImpl.java rename to components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractEventConfigDAO.java index fb03e455ff..e77e9bc9f1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/EventConfigDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractEventConfigDAO.java @@ -21,7 +21,6 @@ package org.wso2.carbon.device.mgt.core.dao.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.event.config.EventConfig; -import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.EventConfigDAO; import org.wso2.carbon.device.mgt.core.dao.EventManagementDAOException; @@ -30,52 +29,12 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.sql.Statement; -import java.sql.Timestamp; import java.util.ArrayList; import java.util.Collections; -import java.util.Date; import java.util.List; -public class EventConfigDAOImpl implements EventConfigDAO { - private static final Log log = LogFactory.getLog(EventConfigDAOImpl.class); - - @Override - public List storeEventRecords(List eventConfigList, int tenantId) throws EventManagementDAOException { - try { - Connection conn = this.getConnection(); - String sql = "INSERT INTO DM_DEVICE_EVENT(" + - "EVENT_SOURCE, " + - "EVENT_LOGIC, " + - "ACTIONS, "+ - "CREATED_TIMESTAMP, " + - "TENANT_ID) " + - "VALUES (?, ?, ?, ?, ?)"; - try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { - for (EventConfig eventConfig : eventConfigList) { - stmt.setString(1, eventConfig.getEventSource()); - stmt.setString(2, eventConfig.getEventLogic()); - stmt.setString(3, eventConfig.getActions()); - stmt.setTimestamp(4, new Timestamp(new Date().getTime())); - stmt.setInt(5, tenantId); - stmt.addBatch(); - } - int[] createdRowCount = stmt.executeBatch(); - List generatedIds = new ArrayList<>(); - ResultSet generatedKeys = stmt.getGeneratedKeys(); - for (int i = 0; i < createdRowCount.length; i++) { - if (generatedKeys.next()) { - generatedIds.add(generatedKeys.getInt(1)); - } - } - return generatedIds; - } - } catch (SQLException e) { - String msg = "Error occurred while creating event configurations for the tenant id " + tenantId; - log.error(msg, e); - throw new EventManagementDAOException(msg, e); - } - } +public abstract class AbstractEventConfigDAO implements EventConfigDAO { + private static final Log log = LogFactory.getLog(AbstractEventConfigDAO.class); @Override public boolean addEventGroupMappingRecords(List eventIds, List groupIds) throws EventManagementDAOException { 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 69e1725492..26e9b9ae99 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 @@ -23,7 +23,6 @@ import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.event.config.EventConfig; -import org.wso2.carbon.device.mgt.common.geo.service.GeoLocationBasedServiceException; import org.wso2.carbon.device.mgt.common.geo.service.GeofenceData; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/event/GenericEventConfigDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/event/GenericEventConfigDAOImpl.java new file mode 100644 index 0000000000..0dfeee16b2 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/event/GenericEventConfigDAOImpl.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.device.mgt.core.dao.impl.event; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.event.config.EventConfig; +import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; +import org.wso2.carbon.device.mgt.core.dao.EventConfigDAO; +import org.wso2.carbon.device.mgt.core.dao.EventManagementDAOException; +import org.wso2.carbon.device.mgt.core.dao.impl.AbstractEventConfigDAO; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.List; + +public class GenericEventConfigDAOImpl extends AbstractEventConfigDAO { + private static final Log log = LogFactory.getLog(GenericEventConfigDAOImpl.class); + + @Override + public List storeEventRecords(List eventConfigList, int tenantId) throws EventManagementDAOException { + try { + Connection conn = this.getConnection(); + String sql = "INSERT INTO DM_DEVICE_EVENT(" + + "EVENT_SOURCE, " + + "EVENT_LOGIC, " + + "ACTIONS, "+ + "CREATED_TIMESTAMP, " + + "TENANT_ID) " + + "VALUES (?, ?, ?, ?, ?)"; + try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { + for (EventConfig eventConfig : eventConfigList) { + stmt.setString(1, eventConfig.getEventSource()); + stmt.setString(2, eventConfig.getEventLogic()); + stmt.setString(3, eventConfig.getActions()); + stmt.setTimestamp(4, new Timestamp(new Date().getTime())); + stmt.setInt(5, tenantId); + stmt.addBatch(); + } + int[] createdRowCount = stmt.executeBatch(); + List generatedIds = new ArrayList<>(); + ResultSet generatedKeys = stmt.getGeneratedKeys(); + for (int i = 0; i < createdRowCount.length; i++) { + if (generatedKeys.next()) { + generatedIds.add(generatedKeys.getInt(1)); + } + } + return generatedIds; + } + } catch (SQLException e) { + String msg = "Error occurred while creating event configurations for the tenant id " + tenantId; + log.error(msg, e); + throw new EventManagementDAOException(msg, e); + } + } + + private Connection getConnection() throws SQLException { + return DeviceManagementDAOFactory.getConnection(); + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/event/H2EventConfigDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/event/H2EventConfigDAOImpl.java new file mode 100644 index 0000000000..fef946649f --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/event/H2EventConfigDAOImpl.java @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.device.mgt.core.dao.impl.event; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.event.config.EventConfig; +import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; +import org.wso2.carbon.device.mgt.core.dao.EventManagementDAOException; +import org.wso2.carbon.device.mgt.core.dao.impl.AbstractEventConfigDAO; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +public class H2EventConfigDAOImpl extends AbstractEventConfigDAO { + private static final Log log = LogFactory.getLog(H2EventConfigDAOImpl.class); + + @Override + public List storeEventRecords(List eventConfigList, int tenantId) throws EventManagementDAOException { + try { + Connection conn = this.getConnection(); + List generatedIds = new ArrayList<>(); + String sql = "INSERT INTO DM_DEVICE_EVENT(" + + "EVENT_SOURCE, " + + "EVENT_LOGIC, " + + "ACTIONS, "+ + "CREATED_TIMESTAMP, " + + "TENANT_ID) " + + "VALUES (?, ?, ?, ?, ?)"; + try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { + for (EventConfig eventConfig : eventConfigList) { + stmt.setString(1, eventConfig.getEventSource()); + stmt.setString(2, eventConfig.getEventLogic()); + stmt.setString(3, eventConfig.getActions()); + stmt.setTimestamp(4, new Timestamp(new Date().getTime())); + stmt.setInt(5, tenantId); + int affectedRawCount = stmt.executeUpdate(); + if (affectedRawCount > 0) { + ResultSet generatedKeys = stmt.getGeneratedKeys(); + if (generatedKeys.next()) { + generatedIds.add(generatedKeys.getInt(1)); + } + } + } + return generatedIds; + } + } catch (SQLException e) { + String msg = "Error occurred while creating event configurations for the tenant id " + tenantId; + log.error(msg, e); + throw new EventManagementDAOException(msg, e); + } + } + + private Connection getConnection() throws SQLException { + return DeviceManagementDAOFactory.getConnection(); + } +}