Add separate DAO factory for event management

feature/traccar-sync
Pahansith Gunathilake 3 years ago committed by Charitha Goonetilleke
parent 777392e36c
commit 35e0b9189e

@ -151,27 +151,6 @@ public class DeviceManagementDAOFactory {
return new PrivacyComplianceDAOImpl();
}
public static GeofenceDAO getGeofenceDAO() {
return new GeofenceDAOImpl();
}
public static EventConfigDAO getEventConfigDAO() {
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) {
dataSource = resolveDataSource(config);
try {

@ -0,0 +1,200 @@
/*
* Copyright (c) 2022, 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;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.common.exceptions.IllegalTransactionStateException;
import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException;
import org.wso2.carbon.device.mgt.common.exceptions.UnsupportedDatabaseEngineException;
import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig;
import org.wso2.carbon.device.mgt.core.config.datasource.JNDILookupDefinition;
import org.wso2.carbon.device.mgt.core.dao.impl.*;
import org.wso2.carbon.device.mgt.core.dao.impl.event.GenericEventConfigDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.impl.event.H2EventConfigDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Hashtable;
import java.util.List;
public class EventManagementDAOFactory {
private static DataSource dataSource;
private static String databaseEngine;
private static final Log log = LogFactory.getLog(EventManagementDAOFactory.class);
private static ThreadLocal<Connection> currentConnection = new ThreadLocal<>();
public static GeofenceDAO getGeofenceDAO() {
return new GeofenceDAOImpl();
}
public static EventConfigDAO getEventConfigDAO() {
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) {
dataSource = resolveDataSource(config);
try {
databaseEngine = dataSource.getConnection().getMetaData().getDatabaseProductName();
} catch (SQLException e) {
log.error("Error occurred while retrieving config.datasource connection", e);
}
}
public static void init(DataSource dtSource) {
dataSource = dtSource;
try {
databaseEngine = dataSource.getConnection().getMetaData().getDatabaseProductName();
} catch (SQLException e) {
log.error("Error occurred while retrieving config.datasource connection", e);
}
}
public static void beginTransaction() throws TransactionManagementException {
Connection conn = currentConnection.get();
if (conn != null) {
throw new IllegalTransactionStateException("A transaction is already active within the context of " +
"this particular thread. Therefore, calling 'beginTransaction/openConnection' while another " +
"transaction is already active is a sign of improper transaction handling");
}
try {
conn = dataSource.getConnection();
conn.setAutoCommit(false);
currentConnection.set(conn);
} catch (SQLException e) {
throw new TransactionManagementException("Error occurred while retrieving config.datasource connection", e);
}
}
public static void openConnection() throws SQLException {
Connection conn = currentConnection.get();
if (conn != null) {
throw new IllegalTransactionStateException("A transaction is already active within the context of " +
"this particular thread. Therefore, calling 'beginTransaction/openConnection' while another " +
"transaction is already active is a sign of improper transaction handling");
}
conn = dataSource.getConnection();
currentConnection.set(conn);
}
public static Connection getConnection() throws SQLException {
Connection conn = currentConnection.get();
if (conn == null) {
throw new IllegalTransactionStateException("No connection is associated with the current transaction. " +
"This might have ideally been caused by not properly initiating the transaction via " +
"'beginTransaction'/'openConnection' methods");
}
return conn;
}
public static void commitTransaction() {
Connection conn = currentConnection.get();
if (conn == null) {
throw new IllegalTransactionStateException("No connection is associated with the current transaction. " +
"This might have ideally been caused by not properly initiating the transaction via " +
"'beginTransaction'/'openConnection' methods");
}
try {
conn.commit();
} catch (SQLException e) {
log.error("Error occurred while committing the transaction", e);
}
}
public static void rollbackTransaction() {
Connection conn = currentConnection.get();
if (conn == null) {
throw new IllegalTransactionStateException("No connection is associated with the current transaction. " +
"This might have ideally been caused by not properly initiating the transaction via " +
"'beginTransaction'/'openConnection' methods");
}
try {
conn.rollback();
} catch (SQLException e) {
log.warn("Error occurred while roll-backing the transaction", e);
}
}
public static void closeConnection() {
Connection conn = currentConnection.get();
if (conn == null) {
throw new IllegalTransactionStateException("No connection is associated with the current transaction. " +
"This might have ideally been caused by not properly initiating the transaction via " +
"'beginTransaction'/'openConnection' methods");
}
try {
conn.close();
} catch (SQLException e) {
log.warn("Error occurred while close the connection");
}
currentConnection.remove();
}
/**
* Resolve data source from the data source definition
*
* @param config data source configuration
* @return data source resolved from the data source definition
*/
private static DataSource resolveDataSource(DataSourceConfig config) {
DataSource dataSource = null;
if (config == null) {
throw new RuntimeException(
"Device Management Repository data source configuration " + "is null and " +
"thus, is not initialized");
}
JNDILookupDefinition jndiConfig = config.getJndiLookupDefinition();
if (jndiConfig != null) {
if (log.isDebugEnabled()) {
log.debug("Initializing Device Management Repository data source using the JNDI " +
"Lookup Definition");
}
List<JNDILookupDefinition.JNDIProperty> jndiPropertyList =
jndiConfig.getJndiProperties();
if (jndiPropertyList != null) {
Hashtable<Object, Object> jndiProperties = new Hashtable<Object, Object>();
for (JNDILookupDefinition.JNDIProperty prop : jndiPropertyList) {
jndiProperties.put(prop.getName(), prop.getValue());
}
dataSource = DeviceManagementDAOUtil.lookupDataSource(jndiConfig.getJndiName(), jndiProperties);
} else {
dataSource = DeviceManagementDAOUtil.lookupDataSource(jndiConfig.getJndiName(), null);
}
}
return dataSource;
}
}

@ -24,6 +24,7 @@ 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.EventManagementDAOFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
@ -342,6 +343,6 @@ public abstract class AbstractEventConfigDAO implements EventConfigDAO {
}
private Connection getConnection() throws SQLException {
return DeviceManagementDAOFactory.getConnection();
return EventManagementDAOFactory.getConnection();
}
}

@ -26,6 +26,7 @@ import org.wso2.carbon.device.mgt.common.event.config.EventConfig;
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;
import org.wso2.carbon.device.mgt.core.dao.EventManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.GeofenceDAO;
import org.wso2.carbon.device.mgt.core.dto.event.config.GeoFenceGroupMap;
@ -308,7 +309,7 @@ public class GeofenceDAOImpl implements GeofenceDAO {
}
private Connection getConnection() throws SQLException {
return DeviceManagementDAOFactory.getConnection();
return EventManagementDAOFactory.getConnection();
}
private List<GeofenceData> extractGeofenceData(ResultSet rst) throws SQLException {

@ -27,10 +27,7 @@ import org.wso2.carbon.device.mgt.common.event.config.EventConfigurationProvider
import org.wso2.carbon.device.mgt.common.event.config.EventMetaData;
import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException;
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
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;
import org.wso2.carbon.device.mgt.core.dao.*;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.geo.task.GeoFenceEventOperationManager;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
@ -49,7 +46,7 @@ public class EventConfigurationProviderServiceImpl implements EventConfiguration
private final EventConfigDAO eventConfigDAO;
public EventConfigurationProviderServiceImpl() {
eventConfigDAO = DeviceManagementDAOFactory.getEventConfigDAO();
eventConfigDAO = EventManagementDAOFactory.getEventConfigDAO();
}
@Override
@ -65,7 +62,7 @@ public class EventConfigurationProviderServiceImpl implements EventConfiguration
}
try {
DeviceManagementDAOFactory.beginTransaction();
EventManagementDAOFactory.beginTransaction();
if (log.isDebugEnabled()) {
log.debug("Creating event records of tenant " + tenantId);
}
@ -75,7 +72,7 @@ public class EventConfigurationProviderServiceImpl implements EventConfiguration
log.debug("Creating event group mapping for created events with group ids : " + groupIds.toString());
}
eventConfigDAO.addEventGroupMappingRecords(generatedEventIds, groupIds);
DeviceManagementDAOFactory.commitTransaction();
EventManagementDAOFactory.commitTransaction();
if (log.isDebugEnabled()) {
log.debug("Event configuration added successfully for the tenant " + tenantId);
}
@ -86,10 +83,10 @@ public class EventConfigurationProviderServiceImpl implements EventConfiguration
} catch (EventManagementDAOException e) {
String msg = "Error occurred while saving event records";
log.error(msg, e);
DeviceManagementDAOFactory.rollbackTransaction();
EventManagementDAOFactory.rollbackTransaction();
throw new EventConfigurationException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
EventManagementDAOFactory.closeConnection();
}
}
@ -103,7 +100,7 @@ public class EventConfigurationProviderServiceImpl implements EventConfiguration
}
List<EventConfig> eventsToAdd;
try {
DeviceManagementDAOFactory.beginTransaction();
EventManagementDAOFactory.beginTransaction();
eventsToAdd = new ArrayList<>();
List<EventConfig> eventsToUpdate = new ArrayList<>();
List<Integer> updateEventIdList = new ArrayList<>();
@ -162,7 +159,7 @@ public class EventConfigurationProviderServiceImpl implements EventConfiguration
}
eventConfigDAO.deleteEventRecords(removedEventIdList);
}
DeviceManagementDAOFactory.commitTransaction();
EventManagementDAOFactory.commitTransaction();
} catch (TransactionManagementException e) {
String msg = "Failed to start/open transaction to store device event configurations";
log.error(msg, e);
@ -170,10 +167,10 @@ public class EventConfigurationProviderServiceImpl implements EventConfiguration
} catch (EventManagementDAOException e) {
String msg = "Error occurred while saving event records";
log.error(msg, e);
DeviceManagementDAOFactory.rollbackTransaction();
EventManagementDAOFactory.rollbackTransaction();
throw new EventConfigurationException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
EventManagementDAOFactory.closeConnection();
}
if (log.isDebugEnabled()) {
@ -185,7 +182,7 @@ public class EventConfigurationProviderServiceImpl implements EventConfiguration
@Override
public List<EventConfig> getEvents(List<Integer> createdEventIds) throws EventConfigurationException {
try {
DeviceManagementDAOFactory.openConnection();
EventManagementDAOFactory.openConnection();
return eventConfigDAO.getEventsById(createdEventIds);
} catch (EventManagementDAOException e) {
String msg = "Error occurred while retrieving event by IDs : " + Arrays.toString(createdEventIds.toArray());
@ -196,14 +193,14 @@ public class EventConfigurationProviderServiceImpl implements EventConfiguration
log.error(msg, e);
throw new EventConfigurationException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
EventManagementDAOFactory.closeConnection();
}
}
@Override
public List<String> getEventsSourcesOfGroup(int groupId, int tenantId) throws EventConfigurationException {
try {
DeviceManagementDAOFactory.openConnection();
EventManagementDAOFactory.openConnection();
return eventConfigDAO.getEventSourcesOfGroups(groupId, tenantId);
} catch (EventManagementDAOException e) {
String msg = "Error occurred while retrieving events of group " + groupId + " and tenant " + tenantId;
@ -214,14 +211,14 @@ public class EventConfigurationProviderServiceImpl implements EventConfiguration
log.error(msg, e);
throw new EventConfigurationException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
EventManagementDAOFactory.closeConnection();
}
}
@Override
public void deleteEvents(List<EventConfig> events) throws EventConfigurationException {
try {
DeviceManagementDAOFactory.beginTransaction();
EventManagementDAOFactory.beginTransaction();
Set<Integer> eventIdSet = new HashSet<>();
for (EventConfig eventConfig : events) {
eventIdSet.add(eventConfig.getEventId());
@ -230,13 +227,13 @@ public class EventConfigurationProviderServiceImpl implements EventConfiguration
eventConfigDAO.deleteEventGroupMappingRecordsByEventIds(Lists.newArrayList(eventIdSet));
eventConfigDAO.deleteEventRecords(Lists.newArrayList(eventIdSet));
}
DeviceManagementDAOFactory.commitTransaction();
EventManagementDAOFactory.commitTransaction();
} catch (TransactionManagementException e) {
String msg = "Failed to start/open transaction to delete device event configurations";
log.error(msg, e);
throw new EventConfigurationException(msg, e);
} catch (EventManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction();
EventManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while deleting event records";
log.error(msg, e);
throw new EventConfigurationException(msg, e);

@ -53,6 +53,7 @@ import org.wso2.carbon.device.mgt.common.geo.service.GeofenceData;
import org.wso2.carbon.device.mgt.core.cache.impl.GeoCacheManagerImpl;
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.EventManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.GeofenceDAO;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.dto.event.config.GeoFenceGroupMap;
@ -133,7 +134,7 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
private final GeofenceDAO geofenceDAO;
public GeoLocationProviderServiceImpl() {
this.geofenceDAO = DeviceManagementDAOFactory.getGeofenceDAO();
this.geofenceDAO = EventManagementDAOFactory.getGeofenceDAO();
}
public static JWTClientManagerService getJWTClientManagerService() {
@ -1269,32 +1270,32 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
}
try {
DeviceManagementDAOFactory.beginTransaction();
EventManagementDAOFactory.beginTransaction();
geofenceData = geofenceDAO.saveGeofence(geofenceData);
GeoCacheManagerImpl.getInstance()
.addFenceToCache(geofenceData, geofenceData.getId(), tenantId);
geofenceDAO.createGeofenceGroupMapping(geofenceData, geofenceData.getGroupIds());
DeviceManagementDAOFactory.commitTransaction();
EventManagementDAOFactory.commitTransaction();
} catch (TransactionManagementException e) {
String msg = "Failed to begin transaction for saving geofence";
log.error(msg, e);
throw new GeoLocationBasedServiceException(msg, e);
} catch (DeviceManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction();
EventManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while saving geofence";
log.error(msg, e);
throw new GeoLocationBasedServiceException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
EventManagementDAOFactory.closeConnection();
}
List<Integer> createdEventIds;
try {
setEventSource(geofenceData.getEventConfig());
eventConfigService = DeviceManagementDataHolder.getInstance().getEventConfigurationService();
createdEventIds = eventConfigService.createEventsOfDeviceGroup(geofenceData.getEventConfig(), geofenceData.getGroupIds());
DeviceManagementDAOFactory.beginTransaction();
EventManagementDAOFactory.beginTransaction();
geofenceDAO.createGeofenceEventMapping(geofenceData.getId(), createdEventIds);
DeviceManagementDAOFactory.commitTransaction();
EventManagementDAOFactory.commitTransaction();
} catch (EventConfigurationException e) {
String msg = "Failed to store Geofence event configurations";
log.error(msg, e);
@ -1309,12 +1310,12 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
log.error(msg, e);
throw new GeoLocationBasedServiceException(msg, e);
} catch (DeviceManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction();
EventManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while creating geofence event mapping records";
log.error(msg, e);
throw new GeoLocationBasedServiceException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
EventManagementDAOFactory.closeConnection();
}
try {
@ -1358,7 +1359,7 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
}
try {
DeviceManagementDAOFactory.openConnection();
EventManagementDAOFactory.openConnection();
GeofenceData geofence = geofenceDAO.getGeofence(fenceId, true);
if (geofence != null) {
GeoCacheManagerImpl.getInstance().addFenceToCache(geofence, fenceId, tenantId);
@ -1373,7 +1374,7 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
log.error(msg, e);
throw new GeoLocationBasedServiceException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
EventManagementDAOFactory.closeConnection();
}
}
@ -1392,7 +1393,7 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
if (log.isDebugEnabled()) {
log.debug("Retrieving geofence data for the tenant " + tenantId);
}
DeviceManagementDAOFactory.openConnection();
EventManagementDAOFactory.openConnection();
return geofenceDAO.getGeoFencesOfTenant(request, tenantId);
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while retrieving geofence data for the tenant " + tenantId;
@ -1403,7 +1404,7 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
log.error(msg, e);
throw new GeoLocationBasedServiceException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
EventManagementDAOFactory.closeConnection();
}
}
@ -1422,7 +1423,7 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
if (log.isDebugEnabled()) {
log.debug("Retrieving geofence data for the tenant " + tenantId);
}
DeviceManagementDAOFactory.openConnection();
EventManagementDAOFactory.openConnection();
return geofenceDAO.getGeoFencesOfTenant(fenceName, tenantId);
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while retrieving geofence data for the tenant " + tenantId;
@ -1433,7 +1434,7 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
log.error(msg, e);
throw new GeoLocationBasedServiceException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
EventManagementDAOFactory.closeConnection();
}
}
@ -1452,7 +1453,7 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
if (log.isDebugEnabled()) {
log.debug("Retrieving all fence data for the tenant " + tenantId);
}
DeviceManagementDAOFactory.openConnection();
EventManagementDAOFactory.openConnection();
List<GeofenceData> geoFencesOfTenant = geofenceDAO.getGeoFencesOfTenant(tenantId);
for (GeofenceData geofenceData : geoFencesOfTenant) {
GeoCacheManagerImpl.getInstance()
@ -1469,7 +1470,7 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
log.error(msg, e);
throw new GeoLocationBasedServiceException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
EventManagementDAOFactory.closeConnection();
}
}
@ -1488,7 +1489,7 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
}
try {
DeviceManagementDAOFactory.beginTransaction();
EventManagementDAOFactory.beginTransaction();
geofence = geofenceDAO.getGeofence(fenceId, true);
if (geofence == null) {
return false;
@ -1509,7 +1510,7 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
DeviceManagementDAOFactory.commitTransaction();
GeoCacheManagerImpl.getInstance().removeFenceFromCache(fenceId, tenantId);
} catch (DeviceManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction();
EventManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while deleting geofence";
log.error(msg, e);
throw new GeoLocationBasedServiceException(msg, e);
@ -1518,7 +1519,7 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
log.error(msg, e);
throw new GeoLocationBasedServiceException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
EventManagementDAOFactory.closeConnection();
}
this.deleteGeoFenceEvents(geofence, eventsOfGeoFence);
return true;
@ -1538,7 +1539,7 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
List<Integer> savedGroupIds;
try {
DeviceManagementDAOFactory.beginTransaction();
EventManagementDAOFactory.beginTransaction();
int updatedRowCount = geofenceDAO.updateGeofence(geofenceData, fenceId);
savedGroupIds = geofenceDAO.getGroupIdsOfGeoFence(fenceId);
geofenceData.setId(fenceId);
@ -1556,7 +1557,7 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
}
geofenceDAO.deleteGeofenceGroupMapping(groupIdsToDelete, fenceId);
geofenceDAO.createGeofenceGroupMapping(geofenceData, groupIdsToAdd);
DeviceManagementDAOFactory.commitTransaction();
EventManagementDAOFactory.commitTransaction();
if (updatedRowCount > 0) {
GeoCacheManagerImpl.getInstance().updateGeoFenceInCache(geofenceData, fenceId, tenantId);
}
@ -1587,11 +1588,11 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
if (log.isDebugEnabled()) {
log.debug("Deleting geofence event mapping records of geofence " + fenceId);
}
DeviceManagementDAOFactory.beginTransaction();
EventManagementDAOFactory.beginTransaction();
geofenceDAO.deleteGeofenceEventMapping(removedEventIdList);
DeviceManagementDAOFactory.commitTransaction();
EventManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction();
EventManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while deleting geofence event mapping of fence " + fenceId;
log.error(msg, e);
throw new GeoLocationBasedServiceException(msg, e);
@ -1600,7 +1601,7 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
log.error(msg, e);
throw new GeoLocationBasedServiceException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
EventManagementDAOFactory.closeConnection();
}
List<Integer> createdEventIds;
@ -1630,11 +1631,11 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
+ fenceId + ". created events " + createdEventIds.toString());
}
try {
DeviceManagementDAOFactory.beginTransaction();
EventManagementDAOFactory.beginTransaction();
geofenceDAO.createGeofenceEventMapping(fenceId, createdEventIds);
DeviceManagementDAOFactory.commitTransaction();
EventManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction();
EventManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while creating geofence event mapping records of geofence " + fenceId;
log.error(msg, e);
throw new GeoLocationBasedServiceException(msg, e);
@ -1643,7 +1644,7 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
log.error(msg, e);
throw new GeoLocationBasedServiceException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
EventManagementDAOFactory.closeConnection();
}
if (log.isDebugEnabled()) {
log.debug("Update geofence event completed.");
@ -1664,7 +1665,7 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
@Override
public List<GeofenceData> attachEventObjects(List<GeofenceData> geoFences) throws GeoLocationBasedServiceException {
try {
DeviceManagementDAOFactory.openConnection();
EventManagementDAOFactory.openConnection();
List<Integer> fenceIds = new ArrayList<>();
for (GeofenceData geoFence : geoFences) {
fenceIds.add(geoFence.getId());
@ -1696,14 +1697,14 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
log.error(msg, e);
throw new GeoLocationBasedServiceException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
EventManagementDAOFactory.closeConnection();
}
}
@Override
public List<GeofenceData> getGeoFencesOfGroup(int groupId, int tenantId, boolean requireEventData) throws GeoLocationBasedServiceException {
try {
DeviceManagementDAOFactory.openConnection();
EventManagementDAOFactory.openConnection();
List<GeofenceData> geofenceDataList = geofenceDAO.getGeoFences(groupId, tenantId);
if (requireEventData) {
for (GeofenceData geoFenceData : geofenceDataList) {
@ -1711,7 +1712,6 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
geoFenceData.setEventConfig(eventsOfGeoFence);
}
}
DeviceManagementDAOFactory.closeConnection();
return geofenceDataList;
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while retrieving geo fences of group " + groupId
@ -1723,13 +1723,15 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
+ groupId + " and tenant " + tenantId;
log.error(msg, e);
throw new GeoLocationBasedServiceException(msg, e);
} finally {
EventManagementDAOFactory.closeConnection();
}
}
@Override
public List<EventConfig> getEventsOfGeoFence(int geoFenceId) throws GeoLocationBasedServiceException {
try {
DeviceManagementDAOFactory.openConnection();
EventManagementDAOFactory.openConnection();
return geofenceDAO.getEventsOfGeoFence(geoFenceId);
} catch (SQLException e) {
String msg = "Failed to obtain connection while retrieving event data of geo fence "
@ -1741,7 +1743,7 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
log.error(msg, e);
throw new GeoLocationBasedServiceException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
EventManagementDAOFactory.closeConnection();
}
}

@ -51,6 +51,7 @@ import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig;
import org.wso2.carbon.device.mgt.core.config.tenant.PlatformConfigurationManagementServiceImpl;
import org.wso2.carbon.device.mgt.core.config.ui.UIConfigurationManager;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.EventManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManager;
import org.wso2.carbon.device.mgt.core.device.details.mgt.impl.DeviceInformationManagerImpl;
@ -192,6 +193,7 @@ public class DeviceManagementServiceComponent {
NotificationManagementDAOFactory.init(dsConfig);
OperationManagementDAOFactory.init(dsConfig);
MetadataManagementDAOFactory.init(dsConfig);
EventManagementDAOFactory.init(dsConfig);
OTPManagementDAOFactory.init(dsConfig.getJndiLookupDefinition().getJndiName());
/*Initialize the device cache*/
DeviceManagerUtil.initializeDeviceCache();

@ -994,17 +994,29 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
if (log.isDebugEnabled()) {
log.debug("Group devices to the group: " + groupId);
}
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
List<Device> devicesList = null;
try {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
List<String> deviceIdentifierList = deviceIdentifiers.stream()
.map(DeviceIdentifier::getId)
.collect(Collectors.toCollection(ArrayList::new));
List<Device> devicesList = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider()
devicesList = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider()
.getDeviceByIdList(deviceIdentifierList);
if (devicesList == null || devicesList.isEmpty()) {
throw new DeviceNotFoundException("Couldn't find any devices for the given deviceIdentifiers '" + deviceIdentifiers + "'");
}
} catch (DeviceManagementException e) {
String msg = "Error occurred while retrieving devices using device identifiers";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred in addDevices for groupId " + groupId;
log.error(msg, e);
throw new GroupManagementException(msg, e);
}
try {
GroupManagementDAOFactory.beginTransaction();
for (Device device : devicesList) {
if (!this.groupDAO.isDeviceMappedToGroup(groupId, device.getId(), tenantId)) {
@ -1013,10 +1025,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
}
GroupManagementDAOFactory.commitTransaction();
createEventTask(OperationMgtConstants.OperationCodes.EVENT_CONFIG, groupId, deviceIdentifiers, tenantId);
} catch (DeviceManagementException e) {
String msg = "Error occurred while retrieving device.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while adding device to group.";

Loading…
Cancel
Save