revert-70aa11f8
harshanl 9 years ago
commit 1850a76462

@ -32,6 +32,52 @@ import java.sql.SQLException;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.List; import java.util.List;
/**
* This class intends to act as the primary entity that hides all DAO instantiation related complexities and logic so
* that the business objection handling layer doesn't need to be aware of the same providing seamless plug-ability of
* different data sources, connection acquisition mechanisms as well as different forms of DAO implementations to the
* high-level implementations that require device management related metadata persistence.
* <p/>
* In addition, this also provides means to handle transactions across multiple device management related DAO objects.
* Any high-level business logic that requires transaction handling to be done via utility methods provided in
* DeviceManagementDAOFactory should adhere the following guidelines to avoid any unexpected behaviour that can cause
* as a result of improper use of the aforementioned utility method.
* <p/>
* Any transaction that commits data into the underlying data persistence mechanism MUST follow the sequence of
* operations mentioned below.
* <p/>
* <pre>
* {@code
* try {
* DeviceManagementDAOFactory.beginTransaction();
* .....
* DeviceManagementDAOFactory.commitTransaction();
* return success;
* } catch (Exception e) {
* DeviceManagementDAOFactory.rollbackTransaction();
* throw new DeviceManagementException("Error occurred while ...", e);
* } finally {
* DeviceManagementDAOFactory.closeConnection();
* }
* }
* </pre>
* <p/>
* Any transaction that retrieves data from the underlying data persistence mechanism MUST follow the sequence of
* operations mentioned below.
* <p/>
* <pre>
* {@code
* try {
* DeviceManagementDAOFactory.openConnection();
* .....
* } catch (Exception e) {
* throw new DeviceManagementException("Error occurred while ..., e);
* } finally {
* DeviceManagementDAOFactory.closeConnection();
* }
* }
* </pre>
*/
public class DeviceManagementDAOFactory { public class DeviceManagementDAOFactory {
private static DataSource dataSource; private static DataSource dataSource;
@ -87,7 +133,7 @@ public class DeviceManagementDAOFactory {
return currentConnection.get(); return currentConnection.get();
} }
public static void commitTransaction() throws TransactionManagementException { public static void commitTransaction() {
try { try {
Connection conn = currentConnection.get(); Connection conn = currentConnection.get();
if (conn != null) { if (conn != null) {
@ -99,7 +145,7 @@ public class DeviceManagementDAOFactory {
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new TransactionManagementException("Error occurred while committing the transaction", e); log.error("Error occurred while committing the transaction", e);
} }
} }

@ -58,8 +58,8 @@ public class ApplicationDAOImpl implements ApplicationDAO {
stmt.setString(6, application.getLocationUrl()); stmt.setString(6, application.getLocationUrl());
stmt.setString(7, application.getImageUrl()); stmt.setString(7, application.getImageUrl());
stmt.setInt(8, tenantId); stmt.setInt(8, tenantId);
stmt.setObject(9,application.getAppProperties()); stmt.setObject(9, application.getAppProperties());
stmt.setString(10,application.getApplicationIdentifier()); stmt.setString(10, application.getApplicationIdentifier());
stmt.execute(); stmt.execute();
rs = stmt.getGeneratedKeys(); rs = stmt.getGeneratedKeys();
@ -81,7 +81,7 @@ public class ApplicationDAOImpl implements ApplicationDAO {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs; ResultSet rs;
List<Integer> applicationIds = new ArrayList<Integer>(); List<Integer> applicationIds = new ArrayList<>();
try { try {
conn = this.getConnection(); conn = this.getConnection();
stmt = conn.prepareStatement("INSERT INTO DM_APPLICATION (NAME, PLATFORM, CATEGORY, " + stmt = conn.prepareStatement("INSERT INTO DM_APPLICATION (NAME, PLATFORM, CATEGORY, " +
@ -99,12 +99,12 @@ public class ApplicationDAOImpl implements ApplicationDAO {
stmt.setString(6, application.getLocationUrl()); stmt.setString(6, application.getLocationUrl());
stmt.setString(7, application.getImageUrl()); stmt.setString(7, application.getImageUrl());
stmt.setInt(8, tenantId); stmt.setInt(8, tenantId);
stmt.setObject(9,application.getAppProperties()); stmt.setObject(9, application.getAppProperties());
stmt.setString(10,application.getApplicationIdentifier()); stmt.setString(10, application.getApplicationIdentifier());
stmt.executeUpdate(); stmt.executeUpdate();
rs = stmt.getGeneratedKeys(); rs = stmt.getGeneratedKeys();
if (rs.next()){ if (rs.next()) {
applicationIds.add(rs.getInt(1)); applicationIds.add(rs.getInt(1));
} }
} }
@ -118,20 +118,17 @@ public class ApplicationDAOImpl implements ApplicationDAO {
@Override @Override
public List<Integer> removeApplications(List<Application> apps, int tenantId) throws DeviceManagementDAOException { public List<Integer> removeApplications(List<Application> apps, int tenantId) throws DeviceManagementDAOException {
Connection conn = null; Connection conn = null;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
int applicationId = -1; List<Integer> applicationIds = new ArrayList<>();
List<Integer> applicationIds = new ArrayList<Integer>();
try { try {
conn = this.getConnection(); conn = this.getConnection();
conn.setAutoCommit(false); conn.setAutoCommit(false);
stmt = conn.prepareStatement("DELETE DM_APPLICATION WHERE APP_IDENTIFIER = ? AND TENANT_ID = ?", stmt = conn.prepareStatement("DELETE DM_APPLICATION WHERE APP_IDENTIFIER = ? AND TENANT_ID = ?",
Statement.RETURN_GENERATED_KEYS); Statement.RETURN_GENERATED_KEYS);
for(Application app:apps){ for (Application app : apps) {
stmt.setString(1, app.getApplicationIdentifier()); stmt.setString(1, app.getApplicationIdentifier());
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
stmt.addBatch(); stmt.addBatch();
@ -144,7 +141,9 @@ public class ApplicationDAOImpl implements ApplicationDAO {
return applicationIds; return applicationIds;
} catch (SQLException e) { } catch (SQLException e) {
try { try {
conn.rollback(); if (conn != null) {
conn.rollback();
}
} catch (SQLException e1) { } catch (SQLException e1) {
log.warn("Error occurred while roll-backing the transaction", e); log.warn("Error occurred while roll-backing the transaction", e);
} }
@ -189,21 +188,18 @@ public class ApplicationDAOImpl implements ApplicationDAO {
public List<Application> getInstalledApplications(int deviceId) throws DeviceManagementDAOException { public List<Application> getInstalledApplications(int deviceId) throws DeviceManagementDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
List<Application> applications = new ArrayList<Application>(); List<Application> applications = new ArrayList<>();
Application application; Application application;
ByteArrayInputStream bais;
ObjectInputStream ois;
try { try {
conn = this.getConnection(); conn = this.getConnection();
stmt = conn.prepareStatement("Select ID, NAME, APP_IDENTIFIER, PLATFORM, CATEGORY, VERSION, TYPE, " + stmt = conn.prepareStatement("Select ID, NAME, APP_IDENTIFIER, PLATFORM, CATEGORY, VERSION, TYPE, " +
"LOCATION_URL, IMAGE_URL, APP_PROPERTIES, TENANT_ID From DM_APPLICATION app " + "LOCATION_URL, IMAGE_URL, APP_PROPERTIES, TENANT_ID From DM_APPLICATION app " +
"INNER JOIN "+ "INNER JOIN " +
"(Select APPLICATION_ID From DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID=?) APPMAP " + "(Select APPLICATION_ID From DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID=?) APPMAP " +
"ON "+ "ON " +
"app.ID = APPMAP.APPLICATION_ID "); "app.ID = APPMAP.APPLICATION_ID ");
stmt.setInt(1,deviceId); stmt.setInt(1, deviceId);
ResultSet rs = stmt.executeQuery(); ResultSet rs = stmt.executeQuery();
while (rs.next()) { while (rs.next()) {
@ -219,8 +215,7 @@ public class ApplicationDAOImpl implements ApplicationDAO {
return applications; return applications;
} }
private Application loadApplication(ResultSet rs) throws DeviceManagementDAOException{ private Application loadApplication(ResultSet rs) throws DeviceManagementDAOException {
ByteArrayInputStream bais; ByteArrayInputStream bais;
ObjectInputStream ois; ObjectInputStream ois;
Properties properties; Properties properties;
@ -231,7 +226,7 @@ public class ApplicationDAOImpl implements ApplicationDAO {
application.setName(rs.getString("NAME")); application.setName(rs.getString("NAME"));
application.setType(rs.getString("TYPE")); application.setType(rs.getString("TYPE"));
if (rs.getBytes("APP_PROPERTIES") !=null) { if (rs.getBytes("APP_PROPERTIES") != null) {
byte[] appProperties = rs.getBytes("APP_PROPERTIES"); byte[] appProperties = rs.getBytes("APP_PROPERTIES");
bais = new ByteArrayInputStream(appProperties); bais = new ByteArrayInputStream(appProperties);
@ -246,18 +241,12 @@ public class ApplicationDAOImpl implements ApplicationDAO {
application.setVersion(rs.getString("VERSION")); application.setVersion(rs.getString("VERSION"));
application.setApplicationIdentifier(rs.getString("APP_IDENTIFIER")); application.setApplicationIdentifier(rs.getString("APP_IDENTIFIER"));
}catch (IOException ex){ } catch (IOException e) {
String errorMsg = "IO error occurred fetch at app properties"; throw new DeviceManagementDAOException("IO error occurred fetch at app properties", e);
log.error(errorMsg, ex); } catch (ClassNotFoundException e) {
throw new DeviceManagementDAOException(errorMsg, ex); throw new DeviceManagementDAOException("Class not found error occurred fetch at app properties", e);
}catch (ClassNotFoundException cex){ } catch (SQLException e) {
String errorMsg = "Class not found error occurred fetch at app properties"; throw new DeviceManagementDAOException("SQL error occurred fetch at application", e);
log.error(errorMsg, cex);
throw new DeviceManagementDAOException(errorMsg, cex);
}catch (SQLException ex){
String errorMsg = "SQL error occurred fetch at application";
log.error(errorMsg, ex);
throw new DeviceManagementDAOException(errorMsg, ex);
} }
return application; return application;

@ -38,7 +38,6 @@ import java.util.Properties;
public class ApplicationMappingDAOImpl implements ApplicationMappingDAO { public class ApplicationMappingDAOImpl implements ApplicationMappingDAO {
private static final Log log = LogFactory.getLog(ApplicationMappingDAOImpl.class);
@Override @Override
public int addApplicationMapping(int deviceId, int applicationId, public int addApplicationMapping(int deviceId, int applicationId,
int tenantId) throws DeviceManagementDAOException { int tenantId) throws DeviceManagementDAOException {
@ -74,7 +73,7 @@ public class ApplicationMappingDAOImpl implements ApplicationMappingDAO {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
List<Integer> mappingIds = new ArrayList<Integer>(); List<Integer> mappingIds = new ArrayList<>();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String sql = "INSERT INTO DM_DEVICE_APPLICATION_MAPPING (DEVICE_ID, APPLICATION_ID, " + String sql = "INSERT INTO DM_DEVICE_APPLICATION_MAPPING (DEVICE_ID, APPLICATION_ID, " +
@ -102,12 +101,9 @@ public class ApplicationMappingDAOImpl implements ApplicationMappingDAO {
} }
@Override @Override
public void removeApplicationMapping(int deviceId, List<Integer> appIdList, int tenantId) public void removeApplicationMapping(int deviceId, List<Integer> appIdList,
throws DeviceManagementDAOException { int tenantId) throws DeviceManagementDAOException {
Connection conn; Connection conn;
ResultSet rs;
int mappingId = -1;
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
conn = this.getConnection(); conn = this.getConnection();

@ -18,8 +18,6 @@
package org.wso2.carbon.device.mgt.core.dao.impl; 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.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
@ -36,8 +34,6 @@ import java.util.List;
public class DeviceDAOImpl implements DeviceDAO { public class DeviceDAOImpl implements DeviceDAO {
private static final Log log = LogFactory.getLog(DeviceDAOImpl.class);
@Override @Override
public int addDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException { public int addDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException {
Connection conn; Connection conn;
@ -48,7 +44,7 @@ public class DeviceDAOImpl implements DeviceDAO {
conn = this.getConnection(); conn = this.getConnection();
String sql = String sql =
"INSERT INTO DM_DEVICE(DESCRIPTION, NAME, DEVICE_TYPE_ID, DEVICE_IDENTIFICATION, TENANT_ID) " + "INSERT INTO DM_DEVICE(DESCRIPTION, NAME, DEVICE_TYPE_ID, DEVICE_IDENTIFICATION, TENANT_ID) " +
"VALUES (?, ?, ?, ?, ?)"; "VALUES (?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
stmt.setString(1, device.getDescription()); stmt.setString(1, device.getDescription());
stmt.setString(2, device.getName()); stmt.setString(2, device.getName());
@ -63,8 +59,8 @@ public class DeviceDAOImpl implements DeviceDAO {
} }
return deviceId; return deviceId;
} catch (SQLException e) { } catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while enrolling device " + throw new DeviceManagementDAOException("Error occurred while enrolling device '" + device.getName() +
"'" + device.getName() + "'", e); "'", e);
} finally { } finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs); DeviceManagementDAOUtil.cleanupResources(stmt, rs);
} }
@ -80,7 +76,7 @@ public class DeviceDAOImpl implements DeviceDAO {
conn = this.getConnection(); conn = this.getConnection();
String sql = String sql =
"UPDATE DM_DEVICE SET DESCRIPTION = ?, NAME = ? WHERE DEVICE_IDENTIFICATION = ? AND " + "UPDATE DM_DEVICE SET DESCRIPTION = ?, NAME = ? WHERE DEVICE_IDENTIFICATION = ? AND " +
"DEVICE_TYPE_ID = ? AND TENANT_ID = ?"; "DEVICE_TYPE_ID = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
stmt.setString(1, device.getDescription()); stmt.setString(1, device.getDescription());
stmt.setString(2, device.getName()); stmt.setString(2, device.getName());
@ -96,7 +92,7 @@ public class DeviceDAOImpl implements DeviceDAO {
return deviceId; return deviceId;
} catch (SQLException e) { } catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while enrolling device '" + throw new DeviceManagementDAOException("Error occurred while enrolling device '" +
device.getName() + "'", e); device.getName() + "'", e);
} finally { } finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs); DeviceManagementDAOUtil.cleanupResources(stmt, rs);
} }
@ -117,11 +113,11 @@ public class DeviceDAOImpl implements DeviceDAO {
conn = this.getConnection(); conn = this.getConnection();
String sql = String sql =
"SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, d1.DEVICE_IDENTIFICATION, " + "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, d1.DEVICE_IDENTIFICATION, " +
"e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT " + "e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT " +
"FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME, " + "FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME, " +
"t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE " + "t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE " +
"t.NAME = ? AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID " + "t.NAME = ? AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID " +
"AND TENANT_ID = ?"; "AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(1, deviceId.getType()); stmt.setString(1, deviceId.getType());
stmt.setString(2, deviceId.getId()); stmt.setString(2, deviceId.getId());
@ -133,7 +129,7 @@ public class DeviceDAOImpl implements DeviceDAO {
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while listing devices for type " + throw new DeviceManagementDAOException("Error occurred while listing devices for type " +
"'" + deviceId.getType() + "'", e); "'" + deviceId.getType() + "'", e);
} finally { } finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs); DeviceManagementDAOUtil.cleanupResources(stmt, rs);
} }
@ -150,22 +146,22 @@ public class DeviceDAOImpl implements DeviceDAO {
conn = this.getConnection(); conn = this.getConnection();
String sql = String sql =
"SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, d1.DEVICE_IDENTIFICATION, " + "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, d1.DEVICE_IDENTIFICATION, " +
"e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID " + "e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID " +
"FROM DM_ENROLMENT e, (SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME," + "FROM DM_ENROLMENT e, (SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME," +
"d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, DM_DEVICE_TYPE t " + "d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, DM_DEVICE_TYPE t " +
"WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?"; "WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId); stmt.setInt(1, tenantId);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
rs = stmt.executeQuery(); rs = stmt.executeQuery();
devices = new ArrayList<Device>(); devices = new ArrayList<>();
while (rs.next()) { while (rs.next()) {
Device device = this.loadDevice(rs); Device device = this.loadDevice(rs);
devices.add(device); devices.add(device);
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving information of all " + throw new DeviceManagementDAOException("Error occurred while retrieving information of all " +
"registered devices", e); "registered devices", e);
} finally { } finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs); DeviceManagementDAOUtil.cleanupResources(stmt, rs);
} }
@ -182,16 +178,16 @@ public class DeviceDAOImpl implements DeviceDAO {
conn = this.getConnection(); conn = this.getConnection();
String sql = String sql =
"SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, d1.DEVICE_IDENTIFICATION, " + "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, d1.DEVICE_IDENTIFICATION, " +
"e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID " + "e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID " +
"FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, d.OWNER, t.NAME " + "FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, d.OWNER, t.NAME " +
"AS DEVICE_TYPE FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " + "AS DEVICE_TYPE FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " +
"AND d.TENANT_ID = ?) d1 WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?"; "AND d.TENANT_ID = ?) d1 WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(1, type); stmt.setString(1, type);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId); stmt.setInt(3, tenantId);
rs = stmt.executeQuery(); rs = stmt.executeQuery();
devices = new ArrayList<Device>(); devices = new ArrayList<>();
while (rs.next()) { while (rs.next()) {
Device device = this.loadDevice(rs); Device device = this.loadDevice(rs);
devices.add(device); devices.add(device);
@ -208,16 +204,16 @@ public class DeviceDAOImpl implements DeviceDAO {
public List<Device> getDevicesOfUser(String username, int tenantId) throws DeviceManagementDAOException { public List<Device> getDevicesOfUser(String username, int tenantId) throws DeviceManagementDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
List<Device> devices = new ArrayList<Device>(); List<Device> devices = new ArrayList<>();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String sql = String sql =
"SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " +
"d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_ENROLMENT FROM DM_ENROLMENT e, (SELECT t.NAME AS DEVICE_TYPE, d.ID, d.DESCRIPTION, " + "e.DATE_OF_ENROLMENT FROM DM_ENROLMENT e, (SELECT t.NAME AS DEVICE_TYPE, d.ID, d.DESCRIPTION, " +
"d.NAME, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t " + "d.NAME, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t " +
"WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 " + "WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 " +
"WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? AND e.OWNER = ?"; "WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? AND e.OWNER = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId); stmt.setInt(1, tenantId);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
@ -230,7 +226,7 @@ public class DeviceDAOImpl implements DeviceDAO {
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices belongs to '" + throw new DeviceManagementDAOException("Error occurred while fetching the list of devices belongs to '" +
username + "'", e); username + "'", e);
} finally { } finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null); DeviceManagementDAOUtil.cleanupResources(stmt, null);
} }
@ -282,15 +278,15 @@ public class DeviceDAOImpl implements DeviceDAO {
public List<Device> getDevicesByName(String deviceName, int tenantId) throws DeviceManagementDAOException { public List<Device> getDevicesByName(String deviceName, int tenantId) throws DeviceManagementDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
List<Device> devices = new ArrayList<Device>(); List<Device> devices = new ArrayList<>();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String sql = String sql =
"SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, d1.DEVICE_IDENTIFICATION, " + "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, d1.DEVICE_IDENTIFICATION, " +
"e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID " + "e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID " +
"FROM DM_ENROLMENT e, (SELECT d.ID, d.NAME, d.DESCRIPTION, t.NAME AS DEVICE_TYPE, " + "FROM DM_ENROLMENT e, (SELECT d.ID, d.NAME, d.DESCRIPTION, t.NAME AS DEVICE_TYPE, " +
"d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID " + "d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID " +
"AND d.NAME LIKE ? AND d.TENANT_ID = ?) d1 WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?"; "AND d.NAME LIKE ? AND d.TENANT_ID = ?) d1 WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(1, deviceName + "%"); stmt.setString(1, deviceName + "%");
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
@ -303,7 +299,7 @@ public class DeviceDAOImpl implements DeviceDAO {
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches " + throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches " +
"'" + deviceName + "'", e); "'" + deviceName + "'", e);
} finally { } finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null); DeviceManagementDAOUtil.cleanupResources(stmt, null);
} }
@ -320,7 +316,7 @@ public class DeviceDAOImpl implements DeviceDAO {
conn = this.getConnection(); conn = this.getConnection();
String sql = String sql =
"INSERT INTO DM_ENROLMENT(DEVICE_ID, OWNER, OWNERSHIP, STATUS,DATE_OF_ENROLMENT, DATE_OF_LAST_UPDATE, " + "INSERT INTO DM_ENROLMENT(DEVICE_ID, OWNER, OWNERSHIP, STATUS,DATE_OF_ENROLMENT, DATE_OF_LAST_UPDATE, " +
"TENANT_ID) VALUES(?, ?, ?, ?, ?, ?, ?)"; "TENANT_ID) VALUES(?, ?, ?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
stmt.setInt(1, device.getId()); stmt.setInt(1, device.getId());
stmt.setString(2, device.getEnrolmentInfo().getOwner()); stmt.setString(2, device.getEnrolmentInfo().getOwner());
@ -352,8 +348,8 @@ public class DeviceDAOImpl implements DeviceDAO {
conn = this.getConnection(); conn = this.getConnection();
String sql = String sql =
"UPDATE DM_ENROLMENT SET STATUS = ? WHERE DEVICE_ID = (SELECT d.ID FROM DM_DEVICE d, DM_DEVICE_TYPE t " + "UPDATE DM_ENROLMENT SET STATUS = ? WHERE DEVICE_ID = (SELECT d.ID FROM DM_DEVICE d, DM_DEVICE_TYPE t " +
"WHERE d.DEVICE_TYPE_ID = t.ID AND d.DEVICE_IDENTIFICATION = ? AND t.NAME = ? AND d.TENANT_ID = ?) " + "WHERE d.DEVICE_TYPE_ID = t.ID AND d.DEVICE_IDENTIFICATION = ? AND t.NAME = ? AND d.TENANT_ID = ?) " +
"AND OWNER = ? AND TENANT_ID = ?"; "AND OWNER = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(1, status.toString()); stmt.setString(1, status.toString());
stmt.setString(2, deviceId.getId()); stmt.setString(2, deviceId.getId());
@ -381,8 +377,8 @@ public class DeviceDAOImpl implements DeviceDAO {
conn = this.getConnection(); conn = this.getConnection();
String sql = String sql =
"SELECT STATUS FROM DM_ENROLMENT WHERE DEVICE_ID = " + "SELECT STATUS FROM DM_ENROLMENT WHERE DEVICE_ID = " +
"(SELECT d.ID FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND " + "(SELECT d.ID FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND " +
"d.DEVICE_IDENTIFICATION = ? AND t.NAME = ? AND d.TENANT_ID = ?) AND OWNER = ? AND TENANT_ID = ?"; "d.DEVICE_IDENTIFICATION = ? AND t.NAME = ? AND d.TENANT_ID = ?) AND OWNER = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(1, deviceId.getId()); stmt.setString(1, deviceId.getId());
stmt.setString(2, deviceId.getType()); stmt.setString(2, deviceId.getType());
@ -413,9 +409,9 @@ public class DeviceDAOImpl implements DeviceDAO {
conn = this.getConnection(); conn = this.getConnection();
String sql = String sql =
"SELECT ID AS ENROLMENT_ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, DATE_OF_ENROLMENT, DATE_OF_LAST_UPDATE, " + "SELECT ID AS ENROLMENT_ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, DATE_OF_ENROLMENT, DATE_OF_LAST_UPDATE, " +
"TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = (SELECT d.ID FROM DM_DEVICE d, DM_DEVICE_TYPE t " + "TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = (SELECT d.ID FROM DM_DEVICE d, DM_DEVICE_TYPE t " +
"WHERE d.DEVICE_TYPE_ID = t.ID AND d.DEVICE_IDENTIFICATION = ? AND t.NAME = ? AND d.TENANT_ID = ?) " + "WHERE d.DEVICE_TYPE_ID = t.ID AND d.DEVICE_IDENTIFICATION = ? AND t.NAME = ? AND d.TENANT_ID = ?) " +
"AND OWNER = ? AND TENANT_ID = ?"; "AND OWNER = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(1, deviceId.getId()); stmt.setString(1, deviceId.getId());
stmt.setString(2, deviceId.getType()); stmt.setString(2, deviceId.getType());
@ -429,7 +425,7 @@ public class DeviceDAOImpl implements DeviceDAO {
return enrolmentInfo; return enrolmentInfo;
} catch (SQLException e) { } catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " + throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " +
"information of user '" + currentOwner + "' upon device '" + deviceId + "'", e); "information of user '" + currentOwner + "' upon device '" + deviceId + "'", e);
} finally { } finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs); DeviceManagementDAOUtil.cleanupResources(stmt, rs);
} }
@ -444,8 +440,8 @@ public class DeviceDAOImpl implements DeviceDAO {
conn = this.getConnection(); conn = this.getConnection();
String sql = String sql =
"SELECT ID AS ENROLMENT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = (SELECT d.ID FROM DM_DEVICE d, " + "SELECT ID AS ENROLMENT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = (SELECT d.ID FROM DM_DEVICE d, " +
"DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.DEVICE_IDENTIFICATION = ? AND t.NAME = ? " + "DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.DEVICE_IDENTIFICATION = ? AND t.NAME = ? " +
"AND d.TENANT_ID = ?) AND STATUS = ? AND TENANT_ID = ?"; "AND d.TENANT_ID = ?) AND STATUS = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(1, deviceId.getId()); stmt.setString(1, deviceId.getId());
stmt.setString(2, deviceId.getType()); stmt.setString(2, deviceId.getType());
@ -460,7 +456,7 @@ public class DeviceDAOImpl implements DeviceDAO {
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " + throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " +
"id of device '" + deviceId + "'", e); "id of device '" + deviceId + "'", e);
} finally { } finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs); DeviceManagementDAOUtil.cleanupResources(stmt, rs);
} }
@ -491,16 +487,16 @@ public class DeviceDAOImpl implements DeviceDAO {
throws DeviceManagementDAOException { throws DeviceManagementDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
List<Device> devices = new ArrayList<Device>(); List<Device> devices = new ArrayList<>();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String sql = String sql =
"SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, t.NAME AS DEVICE_TYPE, " + "SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, t.NAME AS DEVICE_TYPE, " +
"d.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + "d.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_ENROLMENT FROM (SELECT e.ID, e.DEVICE_ID, e.OWNER, e.OWNERSHIP, e.STATUS, " + "e.DATE_OF_ENROLMENT FROM (SELECT e.ID, e.DEVICE_ID, e.OWNER, e.OWNERSHIP, e.STATUS, " +
"e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE FROM DM_ENROLMENT e WHERE TENANT_ID = ? " + "e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE FROM DM_ENROLMENT e WHERE TENANT_ID = ? " +
"AND STATUS = ?) e, DM_DEVICE d, DM_DEVICE_TYPE t WHERE DEVICE_ID = e.DEVICE_ID " + "AND STATUS = ?) e, DM_DEVICE d, DM_DEVICE_TYPE t WHERE DEVICE_ID = e.DEVICE_ID " +
"AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?"; "AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId); stmt.setInt(1, tenantId);
stmt.setString(2, status.toString()); stmt.setString(2, status.toString());
@ -513,7 +509,7 @@ public class DeviceDAOImpl implements DeviceDAO {
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches to status " + throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches to status " +
"'" + status + "'", e); "'" + status + "'", e);
} finally { } finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null); DeviceManagementDAOUtil.cleanupResources(stmt, null);
} }

@ -17,16 +17,12 @@
*/ */
package org.wso2.carbon.device.mgt.core.dao.impl; 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.TransactionManagementException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; 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.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import javax.sql.DataSource;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;

@ -23,6 +23,7 @@ import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.TransactionManagementException;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager;
@ -36,6 +37,7 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationMappingDAO;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.util.OperationDAOUtil; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.util.OperationDAOUtil;
import org.wso2.carbon.device.mgt.core.operation.mgt.util.OperationCreateTimeComparator; import org.wso2.carbon.device.mgt.core.operation.mgt.util.OperationCreateTimeComparator;
import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -70,7 +72,6 @@ public class OperationManagerImpl implements OperationManager {
@Override @Override
public int addOperation(Operation operation, public int addOperation(Operation operation,
List<DeviceIdentifier> deviceIds) throws OperationManagementException { List<DeviceIdentifier> deviceIds) throws OperationManagementException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("operation:[" + operation.toString() + "]"); log.debug("operation:[" + operation.toString() + "]");
for (DeviceIdentifier deviceIdentifier : deviceIds) { for (DeviceIdentifier deviceIdentifier : deviceIds) {
@ -101,19 +102,15 @@ public class OperationManagerImpl implements OperationManager {
OperationManagementDAOFactory.commitTransaction(); OperationManagementDAOFactory.commitTransaction();
return operationId; return operationId;
} catch (OperationManagementDAOException e) { } catch (OperationManagementDAOException e) {
try { OperationManagementDAOFactory.rollbackTransaction();
OperationManagementDAOFactory.rollbackTransaction();
} catch (OperationManagementDAOException e1) {
log.warn("Error occurred while roll-backing the transaction", e1);
}
throw new OperationManagementException("Error occurred while adding operation", e); throw new OperationManagementException("Error occurred while adding operation", e);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
try { OperationManagementDAOFactory.rollbackTransaction();
OperationManagementDAOFactory.rollbackTransaction();
} catch (OperationManagementDAOException e1) {
log.warn("Error occurred while roll-backing the transaction", e1);
}
throw new OperationManagementException("Error occurred while retrieving device metadata", e); throw new OperationManagementException("Error occurred while retrieving device metadata", e);
} catch (TransactionManagementException e) {
throw new OperationManagementException("Error occurred while initiating the transaction", e);
} finally {
OperationManagementDAOFactory.closeConnection();
} }
} }
@ -122,7 +119,7 @@ public class OperationManagerImpl implements OperationManager {
int enrolmentId; int enrolmentId;
List<Operation> operations = new ArrayList<>(); List<Operation> operations = new ArrayList<>();
try { try {
OperationManagementDAOFactory.getConnection(); OperationManagementDAOFactory.openConnection();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId);
@ -145,12 +142,10 @@ public class OperationManagerImpl implements OperationManager {
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
throw new OperationManagementException("Error occurred while retrieving metadata of '" + throw new OperationManagementException("Error occurred while retrieving metadata of '" +
deviceId.getType() + "' device carrying the identifier '" + deviceId.getId() + "'"); deviceId.getType() + "' device carrying the identifier '" + deviceId.getId() + "'");
} catch (SQLException e) {
throw new OperationManagementException("Error occurred while opening a connection to the data source", e);
} finally { } finally {
try { OperationManagementDAOFactory.closeConnection();
OperationManagementDAOFactory.closeConnection();
} catch (OperationManagementDAOException e) {
log.warn("Error occurred while closing data source connection", e);
}
} }
} }
@ -165,7 +160,7 @@ public class OperationManagerImpl implements OperationManager {
List<Operation> operations = new ArrayList<>(); List<Operation> operations = new ArrayList<>();
List<org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation> dtoOperationList = new ArrayList<>(); List<org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation> dtoOperationList = new ArrayList<>();
try { try {
OperationManagementDAOFactory.getConnection(); OperationManagementDAOFactory.openConnection();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId);
@ -202,12 +197,10 @@ public class OperationManagerImpl implements OperationManager {
throw new OperationManagementException("Error occurred while retrieving the device " + throw new OperationManagementException("Error occurred while retrieving the device " +
"for device Identifier type -'" + deviceId.getType() + "' and device Id '" "for device Identifier type -'" + deviceId.getType() + "' and device Id '"
+ deviceId.getId() + "'", e); + deviceId.getId() + "'", e);
} catch (SQLException e) {
throw new OperationManagementException("Error occurred while opening a connection to the data source", e);
} finally { } finally {
try { OperationManagementDAOFactory.closeConnection();
OperationManagementDAOFactory.closeConnection();
} catch (OperationManagementDAOException e) {
log.warn("Error occurred while closing data source connection", e);
}
} }
} }
@ -220,7 +213,7 @@ public class OperationManagerImpl implements OperationManager {
Operation operation = null; Operation operation = null;
int enrolmentId; int enrolmentId;
try { try {
OperationManagementDAOFactory.getConnection(); OperationManagementDAOFactory.openConnection();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId);
@ -258,23 +251,19 @@ public class OperationManagerImpl implements OperationManager {
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
throw new OperationManagementException("Error occurred while retrieving the device " + throw new OperationManagementException("Error occurred while retrieving the device " +
"for device Identifier type -'" + deviceId.getType() + "' and device Id '" + deviceId.getId(), e); "for device Identifier type -'" + deviceId.getType() + "' and device Id '" + deviceId.getId(), e);
} catch (SQLException e) {
throw new OperationManagementException("Error occurred while opening a connection to the data source", e);
} finally { } finally {
try { OperationManagementDAOFactory.closeConnection();
OperationManagementDAOFactory.closeConnection();
} catch (OperationManagementDAOException e) {
log.warn("Error occurred while closing data source connection", e);
}
} }
} }
@Override @Override
public void updateOperation(DeviceIdentifier deviceId, Operation operation) throws OperationManagementException { public void updateOperation(DeviceIdentifier deviceId, Operation operation) throws OperationManagementException {
int operationId = operation.getId(); int operationId = operation.getId();
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("operation Id:" + operationId + " status:" + operation.getStatus()); log.debug("operation Id:" + operationId + " status:" + operation.getStatus());
} }
try { try {
OperationManagementDAOFactory.beginTransaction(); OperationManagementDAOFactory.beginTransaction();
@ -282,65 +271,49 @@ public class OperationManagerImpl implements OperationManager {
int enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); int enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId);
if (operation.getStatus() != null) { if (operation.getStatus() != null) {
OperationManagementDAOFactory.beginTransaction();
operationDAO.updateOperationStatus(enrolmentId, operationId, operationDAO.updateOperationStatus(enrolmentId, operationId,
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status
.valueOf(operation.getStatus().toString())); .valueOf(operation.getStatus().toString()));
} }
if (operation.getOperationResponse() != null) { if (operation.getOperationResponse() != null) {
OperationManagementDAOFactory.beginTransaction();
operationDAO.addOperationResponse(enrolmentId, operationId, operation.getOperationResponse()); operationDAO.addOperationResponse(enrolmentId, operationId, operation.getOperationResponse());
OperationManagementDAOFactory.commitTransaction();
} }
OperationManagementDAOFactory.commitTransaction();
} catch (OperationManagementDAOException e) { } catch (OperationManagementDAOException e) {
try { OperationManagementDAOFactory.rollbackTransaction();
OperationManagementDAOFactory.rollbackTransaction();
} catch (OperationManagementDAOException e1) {
log.warn("Error occurred while roll-backing the update operation transaction", e1);
}
throw new OperationManagementException("Error occurred while updating the operation: " + operationId + throw new OperationManagementException("Error occurred while updating the operation: " + operationId +
" status:" + operation.getStatus(), e); " status:" + operation.getStatus(), e);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
try { OperationManagementDAOFactory.rollbackTransaction();
OperationManagementDAOFactory.rollbackTransaction();
} catch (OperationManagementDAOException e1) {
log.warn("Error occurred while roll-backing the update operation transaction", e1);
}
throw new OperationManagementException("Error occurred while fetching the device for device identifier: " + throw new OperationManagementException("Error occurred while fetching the device for device identifier: " +
deviceId.getId() + "type:" + deviceId.getType(), e); deviceId.getId() + "type:" + deviceId.getType(), e);
} catch (TransactionManagementException e) {
throw new OperationManagementException("Error occurred while initiating a transaction", e);
} finally { } finally {
try { OperationManagementDAOFactory.closeConnection();
OperationManagementDAOFactory.closeConnection();
} catch (OperationManagementDAOException e) {
log.warn("Error occurred while closing data source connection", e);
}
} }
} }
@Override @Override
public void deleteOperation(int operationId) throws OperationManagementException { public void deleteOperation(int operationId) throws OperationManagementException {
try { try {
OperationManagementDAOFactory.beginTransaction(); OperationManagementDAOFactory.beginTransaction();
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation operation = operationDAO.getOperation
(operationId);
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation operation =
operationDAO.getOperation(operationId);
if (operation == null) { if (operation == null) {
throw new OperationManagementException("Operation not found for operation id:" + operationId); throw new OperationManagementException("Operation not found for operation id:" + operationId);
} }
lookupOperationDAO(operation).deleteOperation(operationId); lookupOperationDAO(operation).deleteOperation(operationId);
OperationManagementDAOFactory.commitTransaction();
OperationManagementDAOFactory.commitTransaction();
} catch (OperationManagementDAOException e) { } catch (OperationManagementDAOException e) {
try { OperationManagementDAOFactory.rollbackTransaction();
OperationManagementDAOFactory.rollbackTransaction();
} catch (OperationManagementDAOException e1) {
log.warn("Error occurred while roll-backing the delete operation transaction", e1);
}
throw new OperationManagementException("Error occurred while deleting the operation: " + operationId, e); throw new OperationManagementException("Error occurred while deleting the operation: " + operationId, e);
} catch (TransactionManagementException e) {
throw new OperationManagementException("Error occurred while initiating a transaction", e);
} finally {
OperationManagementDAOFactory.closeConnection();
} }
} }
@ -349,13 +322,12 @@ public class OperationManagerImpl implements OperationManager {
throws OperationManagementException { throws OperationManagementException {
int enrolmentId; int enrolmentId;
Operation operation; Operation operation;
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Operation Id:" + operationId + " Device Type:" + deviceId.getType() + " Device Identifier:" + log.debug("Operation Id:" + operationId + " Device Type:" + deviceId.getType() + " Device Identifier:" +
deviceId.getId()); deviceId.getId());
} }
try { try {
OperationManagementDAOFactory.getConnection(); OperationManagementDAOFactory.openConnection();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId);
@ -376,12 +348,11 @@ public class OperationManagerImpl implements OperationManager {
} else if (dtoOperation.getType() } else if (dtoOperation.getType()
.equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) { .equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) {
dtoOperation = configOperationDAO.getOperation(dtoOperation.getId()); dtoOperation = configOperationDAO.getOperation(dtoOperation.getId());
} else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type } else if (dtoOperation.getType().equals(
.PROFILE)) { org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.PROFILE)) {
dtoOperation = profileOperationDAO.getOperation(dtoOperation.getId()); dtoOperation = profileOperationDAO.getOperation(dtoOperation.getId());
} else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type } else if (dtoOperation.getType().equals(
.POLICY)) { org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.POLICY)) {
dtoOperation = policyOperationDAO.getOperation(dtoOperation.getId()); dtoOperation = policyOperationDAO.getOperation(dtoOperation.getId());
} }
@ -398,12 +369,10 @@ public class OperationManagerImpl implements OperationManager {
throw new OperationManagementException("Error occurred while retrieving the device " + throw new OperationManagementException("Error occurred while retrieving the device " +
"for device Identifier type -'" + deviceId.getType() + "' and device Id '" + "for device Identifier type -'" + deviceId.getType() + "' and device Id '" +
deviceId.getId() + "'", e); deviceId.getId() + "'", e);
} catch (SQLException e) {
throw new OperationManagementException("Error occurred while opening connection to the data source", e);
} finally { } finally {
try { OperationManagementDAOFactory.closeConnection();
OperationManagementDAOFactory.closeConnection();
} catch (OperationManagementDAOException e) {
log.warn("Error occurred while closing data source connection", e);
}
} }
return operation; return operation;
} }
@ -415,7 +384,7 @@ public class OperationManagerImpl implements OperationManager {
List<org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation> dtoOperationList = List<org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation> dtoOperationList =
new ArrayList<>(); new ArrayList<>();
try { try {
OperationManagementDAOFactory.getConnection(); OperationManagementDAOFactory.openConnection();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
int enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); int enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId);
@ -455,18 +424,15 @@ public class OperationManagerImpl implements OperationManager {
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
throw new OperationManagementException("Error occurred while retrieving the device " + throw new OperationManagementException("Error occurred while retrieving the device " +
"for device Identifier type -'" + deviceId.getType() + "' and device Id '" + deviceId.getId(), e); "for device Identifier type -'" + deviceId.getType() + "' and device Id '" + deviceId.getId(), e);
} catch (SQLException e) {
throw new OperationManagementException("Error occurred while opening a connection to the data source", e);
} finally { } finally {
try { OperationManagementDAOFactory.closeConnection();
OperationManagementDAOFactory.closeConnection();
} catch (OperationManagementDAOException e) {
log.warn("Error occurred while closing data source connection", e);
}
} }
} }
@Override @Override
public Operation getOperation(int operationId) throws OperationManagementException { public Operation getOperation(int operationId) throws OperationManagementException {
Operation operation; Operation operation;
try { try {
OperationManagementDAOFactory.getConnection(); OperationManagementDAOFactory.getConnection();
@ -480,8 +446,9 @@ public class OperationManagerImpl implements OperationManager {
if (dtoOperation.getType() if (dtoOperation.getType()
.equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND)) { .equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND)) {
org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation commandOperation; org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation commandOperation;
commandOperation = (org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation) commandOperationDAO commandOperation =
.getOperation(dtoOperation.getId()); (org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation) commandOperationDAO.
getOperation(dtoOperation.getId());
dtoOperation.setEnabled(commandOperation.isEnabled()); dtoOperation.setEnabled(commandOperation.isEnabled());
} else if (dtoOperation.getType() } else if (dtoOperation.getType()
.equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) { .equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) {
@ -496,15 +463,12 @@ public class OperationManagerImpl implements OperationManager {
} }
operation = OperationDAOUtil.convertOperation(dtoOperation); operation = OperationDAOUtil.convertOperation(dtoOperation);
} catch (OperationManagementDAOException e) { } catch (OperationManagementDAOException e) {
String errorMsg = "Error occurred while retrieving the operation with operation Id '" + operationId; throw new OperationManagementException("Error occurred while retrieving the operation with operation Id '" +
log.error(errorMsg, e); operationId, e);
throw new OperationManagementException(errorMsg, e); } catch (SQLException e) {
throw new OperationManagementException("Error occurred while opening a connection to the data source", e);
} finally { } finally {
try { OperationManagementDAOFactory.closeConnection();
OperationManagementDAOFactory.closeConnection();
} catch (OperationManagementDAOException e) {
log.warn("Error occurred while closing data source connection", e);
}
} }
return operation; return operation;
} }

@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.core.operation.mgt.dao;
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.device.mgt.common.TransactionManagementException;
import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig; 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.config.datasource.JNDILookupDefinition;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
@ -69,27 +70,29 @@ public class OperationManagementDAOFactory {
dataSource = resolveDataSource(config); dataSource = resolveDataSource(config);
} }
public static void beginTransaction() throws OperationManagementDAOException { public static void beginTransaction() throws TransactionManagementException {
try { try {
currentConnection.set(dataSource.getConnection()); Connection conn = dataSource.getConnection();
conn.setAutoCommit(false);
currentConnection.set(conn);
} catch (SQLException e) { } catch (SQLException e) {
throw new OperationManagementDAOException( throw new TransactionManagementException(
"Error occurred while retrieving config.datasource connection", e); "Error occurred while retrieving config.datasource connection", e);
} }
} }
public static Connection getConnection() throws OperationManagementDAOException { public static void openConnection() throws SQLException {
currentConnection.set(dataSource.getConnection());
}
public static Connection getConnection() throws SQLException {
if (currentConnection.get() == null) { if (currentConnection.get() == null) {
try { currentConnection.set(dataSource.getConnection());
currentConnection.set(dataSource.getConnection());
} catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while retrieving data source connection", e);
}
} }
return currentConnection.get(); return currentConnection.get();
} }
public static void closeConnection() throws OperationManagementDAOException { public static void closeConnection() {
Connection con = currentConnection.get(); Connection con = currentConnection.get();
if (con != null) { if (con != null) {
try { try {
@ -101,7 +104,7 @@ public class OperationManagementDAOFactory {
} }
} }
public static void commitTransaction() throws OperationManagementDAOException { public static void commitTransaction() {
try { try {
Connection conn = currentConnection.get(); Connection conn = currentConnection.get();
if (conn != null) { if (conn != null) {
@ -113,13 +116,11 @@ public class OperationManagementDAOFactory {
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while committing the transaction", e); log.error("Error occurred while committing the transaction", e);
} finally {
closeConnection();
} }
} }
public static void rollbackTransaction() throws OperationManagementDAOException { public static void rollbackTransaction() {
try { try {
Connection conn = currentConnection.get(); Connection conn = currentConnection.get();
if (conn != null) { if (conn != null) {
@ -127,13 +128,11 @@ public class OperationManagementDAOFactory {
} else { } else {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Datasource connection associated with the current thread is null, hence rollback " + log.debug("Datasource connection associated with the current thread is null, hence rollback " +
"has not been attempted"); "has not been attempted");
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while roll-backing the transaction", e); log.error("Error occurred while roll-backing the transaction", e);
} finally {
closeConnection();
} }
} }
@ -147,7 +146,7 @@ public class OperationManagementDAOFactory {
DataSource dataSource = null; DataSource dataSource = null;
if (config == null) { if (config == null) {
throw new RuntimeException("Device Management Repository data source configuration is null and " + throw new RuntimeException("Device Management Repository data source configuration is null and " +
"thus, is not initialized"); "thus, is not initialized");
} }
JNDILookupDefinition jndiConfig = config.getJndiLookupDefinition(); JNDILookupDefinition jndiConfig = config.getJndiLookupDefinition();
if (jndiConfig != null) { if (jndiConfig != null) {

@ -35,10 +35,11 @@ public class CommandOperationDAOImpl extends OperationDAOImpl {
@Override @Override
public int addOperation(Operation operation) throws OperationManagementDAOException { public int addOperation(Operation operation) throws OperationManagementDAOException {
int operationId = super.addOperation(operation); int operationId;
CommandOperation commandOp = (CommandOperation) operation; CommandOperation commandOp = (CommandOperation) operation;
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
operationId = super.addOperation(operation);
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.getConnection();
stmt = conn.prepareStatement("INSERT INTO DM_COMMAND_OPERATION(OPERATION_ID, ENABLED) VALUES(?, ?)"); stmt = conn.prepareStatement("INSERT INTO DM_COMMAND_OPERATION(OPERATION_ID, ENABLED) VALUES(?, ?)");
stmt.setInt(1, operationId); stmt.setInt(1, operationId);
@ -66,7 +67,6 @@ public class CommandOperationDAOImpl extends OperationDAOImpl {
throw new OperationManagementDAOException("Error occurred while adding operation metadata", e); throw new OperationManagementDAOException("Error occurred while adding operation metadata", e);
} finally { } finally {
OperationManagementDAOUtil.cleanupResources(stmt); OperationManagementDAOUtil.cleanupResources(stmt);
OperationManagementDAOFactory.closeConnection();
} }
} }
@ -75,7 +75,6 @@ public class CommandOperationDAOImpl extends OperationDAOImpl {
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
super.deleteOperation(id); super.deleteOperation(id);
Connection connection = OperationManagementDAOFactory.getConnection(); Connection connection = OperationManagementDAOFactory.getConnection();
stmt = connection.prepareStatement("DELETE DM_COMMAND_OPERATION WHERE OPERATION_ID = ?"); stmt = connection.prepareStatement("DELETE DM_COMMAND_OPERATION WHERE OPERATION_ID = ?");
stmt.setInt(1, id); stmt.setInt(1, id);
@ -84,7 +83,6 @@ public class CommandOperationDAOImpl extends OperationDAOImpl {
throw new OperationManagementDAOException("Error occurred while deleting operation metadata", e); throw new OperationManagementDAOException("Error occurred while deleting operation metadata", e);
} finally { } finally {
OperationManagementDAOUtil.cleanupResources(stmt); OperationManagementDAOUtil.cleanupResources(stmt);
OperationManagementDAOFactory.closeConnection();
} }
} }
@ -108,7 +106,6 @@ public class CommandOperationDAOImpl extends OperationDAOImpl {
"object available for the id '" + id, e); "object available for the id '" + id, e);
} finally { } finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOUtil.cleanupResources(stmt, rs);
OperationManagementDAOFactory.closeConnection();
} }
return commandOperation; return commandOperation;
} }
@ -148,7 +145,6 @@ public class CommandOperationDAOImpl extends OperationDAOImpl {
"for the device'" + enrolmentId + "' with status '" + status.toString(), e); "for the device'" + enrolmentId + "' with status '" + status.toString(), e);
} finally { } finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOUtil.cleanupResources(stmt, rs);
OperationManagementDAOFactory.closeConnection();
} }
return commandOperations; return commandOperations;
} }

@ -41,9 +41,10 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl {
@Override @Override
public int addOperation(Operation operation) throws OperationManagementDAOException { public int addOperation(Operation operation) throws OperationManagementDAOException {
int operationId = super.addOperation(operation); int operationId;
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
operationId = super.addOperation(operation);
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.getConnection();
stmt = conn.prepareStatement("INSERT INTO DM_CONFIG_OPERATION(OPERATION_ID, OPERATION_CONFIG) VALUES(?, ?)"); stmt = conn.prepareStatement("INSERT INTO DM_CONFIG_OPERATION(OPERATION_ID, OPERATION_CONFIG) VALUES(?, ?)");
stmt.setInt(1, operationId); stmt.setInt(1, operationId);
@ -59,9 +60,9 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl {
@Override @Override
public void deleteOperation(int id) throws OperationManagementDAOException { public void deleteOperation(int id) throws OperationManagementDAOException {
super.deleteOperation(id);
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
super.deleteOperation(id);
Connection connection = OperationManagementDAOFactory.getConnection(); Connection connection = OperationManagementDAOFactory.getConnection();
stmt = connection.prepareStatement("DELETE DM_CONFIG_OPERATION WHERE OPERATION_ID = ?") ; stmt = connection.prepareStatement("DELETE DM_CONFIG_OPERATION WHERE OPERATION_ID = ?") ;
stmt.setInt(1, id); stmt.setInt(1, id);
@ -78,8 +79,8 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ByteArrayOutputStream bao = null; ByteArrayOutputStream bao = null;
ObjectOutputStream oos = null; ObjectOutputStream oos = null;
super.updateOperation(operation);
try { try {
super.updateOperation(operation);
Connection connection = OperationManagementDAOFactory.getConnection(); Connection connection = OperationManagementDAOFactory.getConnection();
stmt = connection.prepareStatement("UPDATE DM_CONFIG_OPERATION O SET O.OPERATION_CONFIG = ? " + stmt = connection.prepareStatement("UPDATE DM_CONFIG_OPERATION O SET O.OPERATION_CONFIG = ? " +
"WHERE O.OPERATION_ID = ?"); "WHERE O.OPERATION_ID = ?");
@ -146,7 +147,6 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl {
+ operationId, e); + operationId, e);
} finally { } finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOUtil.cleanupResources(stmt, rs);
OperationManagementDAOFactory.closeConnection();
} }
return configOperation; return configOperation;
} }
@ -205,7 +205,6 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl {
} }
} }
OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOUtil.cleanupResources(stmt, rs);
OperationManagementDAOFactory.closeConnection();
} }
return operations; return operations;
} }

@ -189,7 +189,6 @@ public class OperationDAOImpl implements OperationDAO {
"available for the id '" + id, e); "available for the id '" + id, e);
} finally { } finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOUtil.cleanupResources(stmt, rs);
OperationManagementDAOFactory.closeConnection();
} }
return operation; return operation;
} }
@ -229,7 +228,6 @@ public class OperationDAOImpl implements OperationDAO {
"available for the device'" + enrolmentId + "' with id '" + operationId, e); "available for the device'" + enrolmentId + "' with id '" + operationId, e);
} finally { } finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOUtil.cleanupResources(stmt, rs);
OperationManagementDAOFactory.closeConnection();
} }
return operation; return operation;
} }
@ -271,7 +269,6 @@ public class OperationDAOImpl implements OperationDAO {
"available for the device'" + enrolmentId + "' with status '" + status.toString(), e); "available for the device'" + enrolmentId + "' with status '" + status.toString(), e);
} finally { } finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOUtil.cleanupResources(stmt, rs);
OperationManagementDAOFactory.closeConnection();
} }
return operations; return operations;
} }
@ -311,7 +308,6 @@ public class OperationDAOImpl implements OperationDAO {
"available for the device'" + enrolmentId + "' with status '", e); "available for the device'" + enrolmentId + "' with status '", e);
} finally { } finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOUtil.cleanupResources(stmt, rs);
OperationManagementDAOFactory.closeConnection();
} }
return operations; return operations;
} }
@ -350,7 +346,6 @@ public class OperationDAOImpl implements OperationDAO {
throw new OperationManagementDAOException("Error occurred while adding operation metadata", e); throw new OperationManagementDAOException("Error occurred while adding operation metadata", e);
} finally { } finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOUtil.cleanupResources(stmt, rs);
OperationManagementDAOFactory.closeConnection();
} }
} }
@ -393,7 +388,6 @@ public class OperationDAOImpl implements OperationDAO {
"for the device'" + enrolmentId + "' with status '" + status.toString(), e); "for the device'" + enrolmentId + "' with status '" + status.toString(), e);
} finally { } finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOUtil.cleanupResources(stmt, rs);
OperationManagementDAOFactory.closeConnection();
} }
return operations; return operations;
} }

@ -37,15 +37,15 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl {
@Override @Override
public int addOperation(Operation operation) throws OperationManagementDAOException { public int addOperation(Operation operation) throws OperationManagementDAOException {
int operationId = super.addOperation(operation); int operationId;
operation.setCreatedTimeStamp(new Timestamp(new java.util.Date().getTime()).toString());
operation.setId(operationId);
operation.setEnabled(true);
PolicyOperation policyOperation = (PolicyOperation) operation;
Connection conn = OperationManagementDAOFactory.getConnection();
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
operationId = super.addOperation(operation);
operation.setCreatedTimeStamp(new Timestamp(new java.util.Date().getTime()).toString());
operation.setId(operationId);
operation.setEnabled(true);
PolicyOperation policyOperation = (PolicyOperation) operation;
Connection conn = OperationManagementDAOFactory.getConnection();
stmt = conn.prepareStatement("INSERT INTO DM_POLICY_OPERATION(OPERATION_ID, OPERATION_DETAILS) " + stmt = conn.prepareStatement("INSERT INTO DM_POLICY_OPERATION(OPERATION_ID, OPERATION_DETAILS) " +
"VALUES(?, ?)"); "VALUES(?, ?)");
stmt.setInt(1, operationId); stmt.setInt(1, operationId);
@ -64,8 +64,8 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ByteArrayOutputStream bao = null; ByteArrayOutputStream bao = null;
ObjectOutputStream oos = null; ObjectOutputStream oos = null;
super.updateOperation(operation);
try { try {
super.updateOperation(operation);
Connection connection = OperationManagementDAOFactory.getConnection(); Connection connection = OperationManagementDAOFactory.getConnection();
stmt = connection.prepareStatement("UPDATE DM_POLICY_OPERATION O SET O.OPERATION_DETAILS=? " + stmt = connection.prepareStatement("UPDATE DM_POLICY_OPERATION O SET O.OPERATION_DETAILS=? " +
"WHERE O.OPERATION_ID=?"); "WHERE O.OPERATION_ID=?");
@ -101,9 +101,9 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl {
@Override @Override
public void deleteOperation(int operationId) throws OperationManagementDAOException { public void deleteOperation(int operationId) throws OperationManagementDAOException {
super.deleteOperation(operationId);
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
super.deleteOperation(operationId);
Connection connection = OperationManagementDAOFactory.getConnection(); Connection connection = OperationManagementDAOFactory.getConnection();
stmt = connection.prepareStatement("DELETE DM_POLICY_OPERATION WHERE OPERATION_ID=?"); stmt = connection.prepareStatement("DELETE DM_POLICY_OPERATION WHERE OPERATION_ID=?");
stmt.setInt(1, operationId); stmt.setInt(1, operationId);
@ -147,7 +147,6 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl {
"object available for the id '" + operationId + "'", e); "object available for the id '" + operationId + "'", e);
} finally { } finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOUtil.cleanupResources(stmt, rs);
OperationManagementDAOFactory.closeConnection();
} }
return policyOperation; return policyOperation;
} }
@ -206,7 +205,6 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl {
} }
} }
OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOUtil.cleanupResources(stmt, rs);
OperationManagementDAOFactory.closeConnection();
} }
return operations; return operations;
} }

@ -36,17 +36,15 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl {
private static final Log log = LogFactory.getLog(ProfileOperationDAOImpl.class); private static final Log log = LogFactory.getLog(ProfileOperationDAOImpl.class);
public int addOperation(Operation operation) throws OperationManagementDAOException { public int addOperation(Operation operation) throws OperationManagementDAOException {
int operationId = super.addOperation(operation);
operation.setCreatedTimeStamp(new Timestamp(new java.util.Date().getTime()).toString());
operation.setId(operationId);
operation.setEnabled(true);
ProfileOperation profileOp = (ProfileOperation) operation;
Connection conn = OperationManagementDAOFactory.getConnection();
PreparedStatement stmt = null; PreparedStatement stmt = null;
int operationId;
try { try {
operationId = super.addOperation(operation);
operation.setCreatedTimeStamp(new Timestamp(new java.util.Date().getTime()).toString());
operation.setId(operationId);
operation.setEnabled(true);
ProfileOperation profileOp = (ProfileOperation) operation;
Connection conn = OperationManagementDAOFactory.getConnection();
stmt = conn.prepareStatement("INSERT INTO DM_PROFILE_OPERATION(OPERATION_ID, OPERATION_DETAILS) " + stmt = conn.prepareStatement("INSERT INTO DM_PROFILE_OPERATION(OPERATION_ID, OPERATION_DETAILS) " +
"VALUES(?, ?)"); "VALUES(?, ?)");
stmt.setInt(1, operationId); stmt.setInt(1, operationId);
@ -62,13 +60,11 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl {
@Override @Override
public void updateOperation(Operation operation) throws OperationManagementDAOException { public void updateOperation(Operation operation) throws OperationManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ByteArrayOutputStream bao = null; ByteArrayOutputStream bao = null;
ObjectOutputStream oos = null; ObjectOutputStream oos = null;
super.updateOperation(operation);
try { try {
super.updateOperation(operation);
Connection connection = OperationManagementDAOFactory.getConnection(); Connection connection = OperationManagementDAOFactory.getConnection();
stmt = connection.prepareStatement("UPDATE DM_PROFILE_OPERATION O SET O.OPERATION_DETAILS=? " + stmt = connection.prepareStatement("UPDATE DM_PROFILE_OPERATION O SET O.OPERATION_DETAILS=? " +
"WHERE O.OPERATION_ID=?"); "WHERE O.OPERATION_ID=?");
@ -80,7 +76,6 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl {
stmt.setBytes(1, bao.toByteArray()); stmt.setBytes(1, bao.toByteArray());
stmt.setInt(2, operation.getId()); stmt.setInt(2, operation.getId());
stmt.executeUpdate(); stmt.executeUpdate();
} catch (SQLException e) { } catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while update operation metadata", e); throw new OperationManagementDAOException("Error occurred while update operation metadata", e);
} catch (IOException e) { } catch (IOException e) {
@ -106,10 +101,9 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl {
@Override @Override
public void deleteOperation(int id) throws OperationManagementDAOException { public void deleteOperation(int id) throws OperationManagementDAOException {
super.deleteOperation(id);
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
super.deleteOperation(id);
Connection connection = OperationManagementDAOFactory.getConnection(); Connection connection = OperationManagementDAOFactory.getConnection();
stmt = connection.prepareStatement("DELETE DM_PROFILE_OPERATION WHERE OPERATION_ID=?"); stmt = connection.prepareStatement("DELETE DM_PROFILE_OPERATION WHERE OPERATION_ID=?");
stmt.setInt(1, id); stmt.setInt(1, id);
@ -122,14 +116,12 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl {
} }
public Operation getOperation(int id) throws OperationManagementDAOException { public Operation getOperation(int id) throws OperationManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
ProfileOperation profileOperation = null; ProfileOperation profileOperation = null;
ByteArrayInputStream bais; ByteArrayInputStream bais;
ObjectInputStream ois; ObjectInputStream ois;
try { try {
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "SELECT OPERATION_ID, ENABLED, OPERATION_DETAILS FROM DM_PROFILE_OPERATION WHERE OPERATION_ID=?"; String sql = "SELECT OPERATION_ID, ENABLED, OPERATION_DETAILS FROM DM_PROFILE_OPERATION WHERE OPERATION_ID=?";
@ -144,24 +136,17 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl {
ois = new ObjectInputStream(bais); ois = new ObjectInputStream(bais);
profileOperation = (ProfileOperation) ois.readObject(); profileOperation = (ProfileOperation) ois.readObject();
} }
} catch (IOException e) { } catch (IOException e) {
String errorMsg = "IO Error occurred while de serialize the profile operation object"; throw new OperationManagementDAOException("IO Error occurred while de serialize the profile " +
log.error(errorMsg, e); "operation object", e);
throw new OperationManagementDAOException(errorMsg, e);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
String errorMsg = "Class not found error occurred while de serialize the profile operation object"; throw new OperationManagementDAOException("Class not found error occurred while de serialize the " +
log.error(errorMsg, e); "profile operation object", e);
throw new OperationManagementDAOException(errorMsg, e);
} catch (SQLException e) { } catch (SQLException e) {
String errorMsg = "SQL Error occurred while retrieving the command operation object " + "available for " + throw new OperationManagementDAOException("SQL Error occurred while retrieving the command " +
"the id '" "operation object " + "available for the id '" + id, e);
+ id;
log.error(errorMsg, e);
throw new OperationManagementDAOException(errorMsg, e);
} finally { } finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOUtil.cleanupResources(stmt, rs);
OperationManagementDAOFactory.closeConnection();
} }
return profileOperation; return profileOperation;
} }
@ -169,7 +154,6 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl {
@Override @Override
public List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId, public List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId,
Operation.Status status) throws OperationManagementDAOException { Operation.Status status) throws OperationManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
ProfileOperation profileOperation; ProfileOperation profileOperation;
@ -202,18 +186,14 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl {
} }
} catch (IOException e) { } catch (IOException e) {
String errorMsg = "IO Error occurred while de serialize the profile operation object"; throw new OperationManagementDAOException("IO Error occurred while de serialize the profile " +
log.error(errorMsg, e); "operation object", e);
throw new OperationManagementDAOException(errorMsg, e);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
String errorMsg = "Class not found error occurred while de serialize the profile operation object"; throw new OperationManagementDAOException("Class not found error occurred while de serialize the " +
log.error(errorMsg, e); "profile operation object", e);
throw new OperationManagementDAOException(errorMsg, e);
} catch (SQLException e) { } catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + enrolmentId + throw new OperationManagementDAOException("SQL error occurred while retrieving the operation " +
"' with status '" + status.toString(); "available for the device'" + enrolmentId + "' with status '" + status.toString(), e);
log.error(errorMsg);
throw new OperationManagementDAOException(errorMsg, e);
} finally { } finally {
if (bais != null) { if (bais != null) {
try { try {
@ -230,8 +210,8 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl {
} }
} }
OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOUtil.cleanupResources(stmt, rs);
OperationManagementDAOFactory.closeConnection();
} }
return operationList; return operationList;
} }
} }

Loading…
Cancel
Save