From 4a9a27ab7e743ba9deee34750ddd8f16d51d1184 Mon Sep 17 00:00:00 2001 From: manoj Date: Tue, 7 Apr 2015 15:34:10 +0530 Subject: [PATCH] Refactor operation DAO methods.Add connection close util method --- .../mgt/common/operation/mgt/Operation.java | 9 ++ .../operation/mgt/OperationManagerImpl.java | 4 +- .../core/operation/mgt/dao/OperationDAO.java | 4 +- .../dao/OperationManagementDAOFactory.java | 28 ++-- .../mgt/dao/OperationManagementDAOUtil.java | 10 +- .../mgt/dao/impl/CommandOperationDAOImpl.java | 88 +++++++++--- .../mgt/dao/impl/ConfigOperationDAOImpl.java | 26 +++- .../mgt/dao/impl/OperationDAOImpl.java | 40 +++--- .../mgt/dao/impl/OperationMappingDAOImpl.java | 4 +- .../mgt/dao/impl/ProfileOperationDAOImpl.java | 128 ++++++++++++------ .../src/main/resources/dbscripts/cdm/h2.sql | 2 +- 11 files changed, 244 insertions(+), 99 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/Operation.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/Operation.java index 83527c0c0a..809b3ef5ea 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/Operation.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/Operation.java @@ -41,6 +41,7 @@ public class Operation { private Status status; private String receivedTimeStamp; private String createdTimeStamp; + private boolean isEnabled; @XmlElement public String getCode() { @@ -109,4 +110,12 @@ public class Operation { this.createdTimeStamp = createdTimeStamp; } + public boolean isEnabled() { + return isEnabled; + } + + public void setEnabled(boolean isEnabled) { + this.isEnabled = isEnabled; + } + } \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index 03ea37c9f5..eeeca86021 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -137,7 +137,7 @@ public class OperationManagerImpl implements OperationManager { "pending operations assigned for '" + deviceId.getType() + "' device '" + deviceId.getId() + "'", e); } - } +} @Override public Operation getNextPendingOperation(DeviceIdentifier deviceId) throws OperationManagementException { @@ -156,7 +156,7 @@ public class OperationManagerImpl implements OperationManager { @Override public Operation updateOperation(int id, DeviceIdentifier deviceIdentifier, - String responsePayLoad) throws OperationManagementException { + String responsePayLoad) throws OperationManagementException { return null; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java index c0543e66b3..1bfc1563dd 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java @@ -27,9 +27,9 @@ public interface OperationDAO { int addOperation(Operation operation) throws OperationManagementDAOException; - int updateOperation(Operation operation) throws OperationManagementDAOException; + void updateOperation(Operation operation) throws OperationManagementDAOException; - int deleteOperation(int operationId) throws OperationManagementDAOException; + void deleteOperation(int operationId) throws OperationManagementDAOException; Operation getOperation(int operationId) throws OperationManagementDAOException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOFactory.java index 83900177b5..46b96e74f7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOFactory.java @@ -74,20 +74,28 @@ public class OperationManagementDAOFactory { } } - public static Connection getConnection() throws OperationManagementDAOException { + public static Connection openConnection() throws OperationManagementDAOException { if (currentConnection.get() == null) { - synchronized (LOCK) { - try { - currentConnection.set(dataSource.getConnection()); - } catch (SQLException e) { - throw new OperationManagementDAOException("Error occurred while retrieving data source connection", - e); - } + try { + currentConnection.set(dataSource.getConnection()); + } catch (SQLException e) { + throw new OperationManagementDAOException("Error occurred while retrieving data source connection", + e); } } return currentConnection.get(); } + public static void closeConnection() throws OperationManagementDAOException { + + Connection con = currentConnection.get(); + try { + con.close(); + } catch (SQLException e) { + log.error("Error occurred while close the connection"); + } + } + public static void commitTransaction() throws OperationManagementDAOException { try { Connection conn = currentConnection.get(); @@ -101,6 +109,8 @@ public class OperationManagementDAOFactory { } } catch (SQLException e) { throw new OperationManagementDAOException("Error occurred while committing the transaction", e); + } finally { + closeConnection(); } } @@ -117,6 +127,8 @@ public class OperationManagementDAOFactory { } } catch (SQLException e) { throw new OperationManagementDAOException("Error occurred while rollbacking the transaction", e); + } finally { + closeConnection(); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOUtil.java index bd0b5588c5..e39e1dde9b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOUtil.java @@ -45,5 +45,13 @@ public class OperationManagementDAOUtil { } } } - + public static void cleanupResources(Statement stmt) { + if (stmt != null) { + try { + stmt.close(); + } catch (SQLException e) { + log.warn("Error occurred while closing the statement", e); + } + } + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java index 97462105e2..03b2a6aa67 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java @@ -36,13 +36,13 @@ public class CommandOperationDAOImpl extends OperationDAOImpl { @Override public int addOperation(Operation operation) throws OperationManagementDAOException { + int operationId = super.addOperation(operation); CommandOperation commandOp = (CommandOperation) operation; - PreparedStatement stmt = null; - ResultSet rs = null; + try { - Connection conn = OperationManagementDAOFactory.getConnection(); + Connection conn = OperationManagementDAOFactory.openConnection(); stmt = conn.prepareStatement("INSERT INTO DM_COMMAND_OPERATION(OPERATION_ID, ENABLED) VALUES(?, ?)"); stmt.setInt(1, operationId); stmt.setBoolean(2, commandOp.isEnabled()); @@ -50,7 +50,7 @@ public class CommandOperationDAOImpl extends OperationDAOImpl { } catch (SQLException e) { throw new OperationManagementDAOException("Error occurred while adding command operation", e); } finally { - OperationManagementDAOUtil.cleanupResources(stmt, rs); + OperationManagementDAOUtil.cleanupResources(stmt); } return operationId; } @@ -59,41 +59,56 @@ public class CommandOperationDAOImpl extends OperationDAOImpl { Operation.Status status) throws OperationManagementDAOException { PreparedStatement stmt = null; ResultSet rs = null; - + List operationList = new ArrayList(); + Operation operation; try { - Connection conn = OperationManagementDAOFactory.getConnection(); + Connection conn = OperationManagementDAOFactory.openConnection(); String sql = "SELECT po.OPERATION_ID, po.TYPE, po.CREATED_TIMESTAMP, po.RECEIVED_TIMESTAMP, po.STATUS, " + - "co.ENABLED FROM DM_COMMAND_OPERATION co INNER JOIN (SELECT o.ID AS OPERATION_ID, o.TYPE, " + - "o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS FROM DM_OPERATION o INNER JOIN (" + + "co.ENABLED,o.OPERATIONCODE FROM DM_COMMAND_OPERATION co INNER JOIN (SELECT o.ID AS OPERATION_ID, o.TYPE, " + + "o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS, o.OPERATIONCODE FROM DM_OPERATION o INNER JOIN (" + "SELECT dom.OPERATION_ID AS OP_ID FROM (SELECT d.ID FROM DM_DEVICE d INNER JOIN " + "DM_DEVICE_TYPE dm ON d.DEVICE_TYPE_ID = dm.ID AND dm.NAME = ? AND " + "d.DEVICE_IDENTIFICATION = ?) d1 INNER JOIN DM_DEVICE_OPERATION_MAPPING dom ON d1.ID = " + "dom.DEVICE_ID) ois ON o.ID = ois.OP_ID AND o.STATUS = ? ORDER BY " + "o.CREATED_TIMESTAMP ASC) po ON co.OPERATION_ID = po.OPERATION_ID"; + stmt = conn.prepareStatement(sql); stmt.setString(1, deviceId.getType()); - stmt.setString(1, deviceId.getId()); - stmt.setString(1, status.toString()); + stmt.setString(2, deviceId.getId()); + stmt.setString(3, status.toString()); + rs = stmt.executeQuery(sql); + + while(rs.next()){ + operation = new Operation(); + operation.setId(rs.getInt("OPERATION_ID")); + operation.setType(Operation.Type.valueOf(rs.getString("TYPE"))); + operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString()); + operation.setCreatedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString()); + operation.setStatus(Operation.Status.valueOf(rs.getString("STATUS"))); + operation.setEnabled(rs.getBoolean("ENABLED")); + operation.setCode(rs.getString("OPERATIONCODE")); + operationList.add(operation); + } + return operationList; } catch (SQLException e) { throw new OperationManagementDAOException("Error occurred while retrieving the list of " + "operations with the status '" + status + "' available for the '" + deviceId.getType() + "' device '" + deviceId.getId() + "'"); } finally { OperationManagementDAOUtil.cleanupResources(stmt, rs); + OperationManagementDAOFactory.closeConnection(); } - - return null; } public List getOperations(DeviceIdentifier deviceId) throws OperationManagementDAOException { + PreparedStatement stmt = null; ResultSet rs = null; - try { - Connection conn = OperationManagementDAOFactory.getConnection(); + Connection conn = OperationManagementDAOFactory.openConnection(); String sql = "SELECT po.OPERATION_ID, po.TYPE, po.CREATED_TIMESTAMP, po.RECEIVED_TIMESTAMP, po.STATUS, " + - "co.ENABLED FROM DM_COMMAND_OPERATION co INNER JOIN (SELECT o.ID AS OPERATION_ID, o.TYPE, " + - "o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS FROM DM_OPERATION o INNER JOIN (" + + "co.ENABLED.o.OPERATIONCODE FROM DM_COMMAND_OPERATION co INNER JOIN (SELECT o.ID AS OPERATION_ID, o.TYPE, " + + "o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS, o.OPERATIONCODE FROM DM_OPERATION o INNER JOIN (" + "SELECT dom.OPERATION_ID AS OP_ID FROM (SELECT d.ID FROM DM_DEVICE d INNER JOIN " + "DM_DEVICE_TYPE dm ON d.DEVICE_TYPE_ID = dm.ID AND dm.NAME = ? AND " + "d.DEVICE_IDENTIFICATION = ?) d1 INNER JOIN DM_DEVICE_OPERATION_MAPPING dom ON d1.ID = " + @@ -112,6 +127,7 @@ public class CommandOperationDAOImpl extends OperationDAOImpl { operation.setType(Operation.Type.valueOf(rs.getString("TYPE"))); operation.setStatus(Operation.Status.valueOf(rs.getString("STATUS"))); operation.setEnabled(Boolean.parseBoolean(rs.getString("ENABLED"))); + operation.setCode(rs.getString("OPERATIONCODE")); operations.add(operation); } return operations; @@ -120,6 +136,46 @@ public class CommandOperationDAOImpl extends OperationDAOImpl { "operations available for the '" + deviceId.getType() + "' device '" + deviceId.getId() + "'"); } finally { OperationManagementDAOUtil.cleanupResources(stmt, rs); + OperationManagementDAOFactory.closeConnection(); + } + } + + @Override + public void updateOperation(Operation operation) throws OperationManagementDAOException { + + PreparedStatement stmt = null; + try { + Connection connection = OperationManagementDAOFactory.openConnection(); + stmt = connection.prepareStatement( + "UPDATE DM_COMMAND_OPERATION O SET O.ENABLED=? WHERE O.OPERATION_ID=?"); + + stmt.setBoolean(1,operation.isEnabled()); + stmt.setInt(2, operation.getId()); + stmt.executeUpdate(); + + } catch (SQLException e) { + throw new OperationManagementDAOException("Error occurred while adding operation metadata", e); + } finally { + OperationManagementDAOUtil.cleanupResources(stmt); + OperationManagementDAOFactory.closeConnection(); + } + } + + @Override + public void deleteOperation(int id) throws OperationManagementDAOException { + + super.deleteOperation(id); + PreparedStatement stmt = null; + try { + Connection connection = OperationManagementDAOFactory.openConnection(); + stmt = connection.prepareStatement("DELETE DM_COMMAND_OPERATION WHERE OPERATION_ID=?") ; + stmt.setInt(1, id); + stmt.executeUpdate(); + } catch (SQLException e) { + throw new OperationManagementDAOException("Error occurred while deleting operation metadata", e); + } finally { + OperationManagementDAOUtil.cleanupResources(stmt); + OperationManagementDAOFactory.closeConnection(); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ConfigOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ConfigOperationDAOImpl.java index f6eabf6ded..9a53e2fd55 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ConfigOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ConfigOperationDAOImpl.java @@ -26,28 +26,42 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOU import java.sql.Connection; import java.sql.PreparedStatement; -import java.sql.ResultSet; import java.sql.SQLException; public class ConfigOperationDAOImpl extends OperationDAOImpl { @Override public int addOperation(Operation operation) throws OperationManagementDAOException { - int operationId = super.addOperation(operation); - ConfigOperation commandOp = (ConfigOperation) operation; + int operationId = super.addOperation(operation); PreparedStatement stmt = null; - ResultSet rs = null; try { - Connection conn = OperationManagementDAOFactory.getConnection(); + Connection conn = OperationManagementDAOFactory.openConnection(); stmt = conn.prepareStatement("INSERT INTO DM_CONFIG_OPERATION(OPERATION_ID) VALUES(?)"); stmt.setInt(1, operationId); stmt.executeUpdate(); } catch (SQLException e) { throw new OperationManagementDAOException("Error occurred while adding command operation", e); } finally { - OperationManagementDAOUtil.cleanupResources(stmt, rs); + OperationManagementDAOUtil.cleanupResources(stmt); } return operationId; } + + @Override + public void deleteOperation(int id) throws OperationManagementDAOException { + + super.deleteOperation(id); + PreparedStatement stmt = null; + try { + Connection connection = OperationManagementDAOFactory.openConnection(); + stmt = connection.prepareStatement("DELETE DM_CONFIG_OPERATION WHERE OPERATION_ID=?") ; + stmt.setInt(1, id); + stmt.executeUpdate(); + } catch (SQLException e) { + throw new OperationManagementDAOException("Error occurred while deleting operation metadata", e); + } finally { + OperationManagementDAOUtil.cleanupResources(stmt); + } + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationDAOImpl.java index cab41c2913..dcf336e20f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationDAOImpl.java @@ -37,9 +37,9 @@ public class OperationDAOImpl implements OperationDAO { PreparedStatement stmt = null; ResultSet rs = null; try { - Connection connection = OperationManagementDAOFactory.getConnection(); + Connection connection = OperationManagementDAOFactory.openConnection(); stmt = connection.prepareStatement( - "INSERT INTO DM_OPERATION(TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, STATUS,OPERATIONCODE) " + + "INSERT INTO DM_OPERATION(TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, STATUS, OPERATIONCODE) " + "VALUES (?, ?, ?, ?,?)"); stmt.setString(1, operation.getType().toString()); stmt.setTimestamp(2, new Timestamp(new Date().getTime())); @@ -62,12 +62,11 @@ public class OperationDAOImpl implements OperationDAO { } @Override - public int updateOperation(Operation operation) throws OperationManagementDAOException { + public void updateOperation(Operation operation) throws OperationManagementDAOException { PreparedStatement stmt = null; - ResultSet rs = null; try { - Connection connection = OperationManagementDAOFactory.getConnection(); + Connection connection = OperationManagementDAOFactory.openConnection(); stmt = connection.prepareStatement( "UPDATE DM_OPERATION O SET O.RECEIVED_TIMESTAMP=?,O.STATUS=? WHERE O.ID=?"); @@ -76,22 +75,26 @@ public class OperationDAOImpl implements OperationDAO { stmt.setInt(3, operation.getId()); stmt.executeUpdate(); - rs = stmt.getGeneratedKeys(); - int id = -1; - if (rs.next()) { - id = rs.getInt(1); - } - return id; } catch (SQLException e) { throw new OperationManagementDAOException("Error occurred while adding operation metadata", e); } finally { - OperationManagementDAOUtil.cleanupResources(stmt, rs); + OperationManagementDAOUtil.cleanupResources(stmt); } } @Override - public int deleteOperation(int id) throws OperationManagementDAOException { - return 0; + public void deleteOperation(int id) throws OperationManagementDAOException { + PreparedStatement stmt = null; + try { + Connection connection = OperationManagementDAOFactory.openConnection(); + stmt = connection.prepareStatement("DELETE DM_OPERATION WHERE ID=?") ; + stmt.setInt(1, id); + stmt.executeUpdate(); + } catch (SQLException e) { + throw new OperationManagementDAOException("Error occurred while deleting operation metadata", e); + } finally { + OperationManagementDAOUtil.cleanupResources(stmt); + } } public Operation getOperation(int id) throws OperationManagementDAOException { @@ -117,9 +120,10 @@ public class OperationDAOImpl implements OperationDAO { PreparedStatement stmt = null; ResultSet rs = null; try { - Connection conn = OperationManagementDAOFactory.getConnection(); + Connection conn = OperationManagementDAOFactory.openConnection(); String sql = - "SELECT o.ID, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS, o.OPERATIONCODE FROM DM_OPERATION o " + + "SELECT o.ID, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS, o.OPERATIONCODE FROM " + + "DM_OPERATION o " + "INNER JOIN (SELECT dom.OPERATION_ID AS OP_ID FROM (SELECT d.ID FROM DM_DEVICE d INNER " + "JOIN " + "DM_DEVICE_TYPE dm ON d.DEVICE_TYPE_ID = dm.ID AND dm.NAME = ? AND d" + @@ -150,6 +154,7 @@ public class OperationDAOImpl implements OperationDAO { "available for the '" + deviceId.getType() + "' with id '" + deviceId.getId() + "'", e); } finally { OperationManagementDAOUtil.cleanupResources(stmt, rs); + OperationManagementDAOFactory.closeConnection(); } return operations; } @@ -170,7 +175,7 @@ public class OperationDAOImpl implements OperationDAO { PreparedStatement stmt = null; ResultSet rs = null; try { - Connection connection = OperationManagementDAOFactory.getConnection(); + Connection connection = OperationManagementDAOFactory.openConnection(); stmt = connection.prepareStatement( "SELECT o.ID, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS,o.OPERATIONCODE " + " FROM DM_OPERATION o " + @@ -203,6 +208,7 @@ public class OperationDAOImpl implements OperationDAO { throw new OperationManagementDAOException("Error occurred while adding operation metadata", e); } finally { OperationManagementDAOUtil.cleanupResources(stmt, rs); + OperationManagementDAOFactory.closeConnection(); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationMappingDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationMappingDAOImpl.java index 15faacad08..61448a2d36 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationMappingDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationMappingDAOImpl.java @@ -33,7 +33,7 @@ public class OperationMappingDAOImpl implements OperationMappingDAO { public void addOperationMapping(int operationId, Integer deviceId) throws OperationManagementDAOException { PreparedStatement stmt = null; try { - Connection conn = OperationManagementDAOFactory.getConnection(); + Connection conn = OperationManagementDAOFactory.openConnection(); String sql = "INSERT INTO DM_DEVICE_OPERATION_MAPPING(DEVICE_ID, OPERATION_ID) VALUES(?, ?)"; stmt = conn.prepareStatement(sql); stmt.setInt(1, deviceId); @@ -51,7 +51,7 @@ public class OperationMappingDAOImpl implements OperationMappingDAO { Integer deviceIds) throws OperationManagementDAOException { PreparedStatement stmt = null; try { - Connection conn = OperationManagementDAOFactory.getConnection(); + Connection conn = OperationManagementDAOFactory.openConnection(); String sql = "DELETE FROM DM_DEVICE_OPERATION_MAPPING WHERE DEVICE_ID = ? AND OPERATION_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, 0); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ProfileOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ProfileOperationDAOImpl.java index 4dc56db82f..0ebde86dd4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ProfileOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ProfileOperationDAOImpl.java @@ -28,30 +28,31 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOF import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOUtil; import java.io.*; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; +import java.sql.*; public class ProfileOperationDAOImpl extends OperationDAOImpl { private static final Log log = LogFactory.getLog(ProfileOperationDAOImpl.class); 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); ProfileOperation profileOp = (ProfileOperation) operation; - Connection conn = OperationManagementDAOFactory.getConnection(); + Connection conn = OperationManagementDAOFactory.openConnection(); PreparedStatement stmt = null; - ResultSet rs = null; ByteArrayOutputStream bao = null; ObjectOutputStream oos = null; + try { bao = new ByteArrayOutputStream(); oos = new ObjectOutputStream(bao); oos.writeObject(profileOp); - stmt = conn.prepareStatement("INSERT INTO DM_PROFILE_OPERATION(OPERATION_ID, PAYLOAD) VALUES(?, ?)"); + stmt = conn.prepareStatement("INSERT INTO DM_PROFILE_OPERATION(OPERATION_ID, OPERATIONDETAILS) " + + "VALUES(?, ?)"); stmt.setInt(1, operationId); stmt.setBytes(2, bao.toByteArray()); stmt.executeUpdate(); @@ -74,29 +75,29 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl { log.warn("Error occurred while closing ObjectOutputStream", e); } } - OperationManagementDAOUtil.cleanupResources(stmt, rs); + OperationManagementDAOUtil.cleanupResources(stmt); } return operationId; } @Override public Operation getOperation(int operationId) throws OperationManagementDAOException { - Connection conn = OperationManagementDAOFactory.getConnection(); + Connection conn = OperationManagementDAOFactory.openConnection(); PreparedStatement stmt = null; ResultSet rs = null; ByteArrayInputStream bais = null; ObjectInputStream ois = null; try { - stmt = conn.prepareStatement("SELECT PAYLOAD FROM DM_PROFILE_OPERATION WHERE ID = ?"); + stmt = conn.prepareStatement("SELECT OPERATIONDETAILS FROM DM_PROFILE_OPERATION WHERE OPERATION_ID = ?"); stmt.setInt(1, operationId); rs = stmt.executeQuery(); - byte[] payload = new byte[0]; + byte[] operationDetails = new byte[0]; if (rs.next()) { - payload = rs.getBytes("PAYLOAD"); + operationDetails = rs.getBytes("OPERATIONDETAILS"); } - bais = new ByteArrayInputStream(payload); + bais = new ByteArrayInputStream(operationDetails); ois = new ObjectInputStream(bais); return (ProfileOperation) ois.readObject(); } catch (SQLException e) { @@ -104,7 +105,7 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl { } catch (IOException e) { throw new OperationManagementDAOException("Error occurred while serializing profile operation object", e); } catch (ClassNotFoundException e) { - throw new OperationManagementDAOException("Error occurred while casting retrieved payload as a " + + throw new OperationManagementDAOException("Error occurred while casting retrieved profile operation as a " + "ProfileOperation object", e); } finally { if (bais != null) { @@ -122,6 +123,7 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl { } } OperationManagementDAOUtil.cleanupResources(stmt, rs); + OperationManagementDAOFactory.closeConnection(); } } @@ -132,51 +134,28 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl { ResultSet rs = null; ByteArrayInputStream bais; ObjectInputStream ois; - int operationId = 0; - - String operationType = ""; - String createdTime = ""; - String receivedTime = ""; - String operationStatus = ""; - String operationCode = ""; try { - Connection connection = OperationManagementDAOFactory.getConnection(); + Connection connection = OperationManagementDAOFactory.openConnection(); stmt = connection.prepareStatement( - "SELECT o.ID AS OPERATION_ID, o.CREATED_TIMESTAMP AS CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP AS " + - "RECEIVED_TIMESTAMP, po.PAYLOAD AS PAYLOAD,o.TYPE AS TYPE,o.STATUS as STATUS,o.OPERATIONCODE " + + "SELECT po.OPERATIONDETAILS AS OPERATIONDETAILS " + "FROM DM_OPERATION o " + "INNER JOIN DM_PROFILE_OPERATION po ON o.ID = po.OPERATION_ID AND o.ID IN (" + "SELECT dom.OPERATION_ID FROM (SELECT d.ID FROM DM_DEVICE d INNER JOIN " + "DM_DEVICE_TYPE dm ON d.DEVICE_TYPE_ID = dm.ID AND dm.NAME = ? AND " + "d.DEVICE_IDENTIFICATION = ?) d1 INNER JOIN DM_DEVICE_OPERATION_MAPPING dom " + - "ON d1.ID = dom.DEVICE_ID) ORDER BY o.CREATED_TIMESTAMP ASC LIMIT 1;"); + "ON d1.ID = dom.DEVICE_ID) ORDER BY o.CREATED_TIMESTAMP ASC LIMIT 1"); stmt.setString(1, deviceId.getType()); stmt.setString(2, deviceId.getId()); rs = stmt.executeQuery(); - byte[] payload = new byte[0]; + byte[] operationObjbyteArr = new byte[0]; if (rs.next()) { - operationId = rs.getInt("OPERATION_ID"); - createdTime = rs.getTimestamp("CREATED_TIMESTAMP").toString(); - receivedTime = rs.getTimestamp("RECEIVED_TIMESTAMP").toString(); - payload = rs.getBytes("PAYLOAD"); - operationType = rs.getString("TYPE"); - operationStatus = rs.getString("STATUS"); - operationCode = rs.getString("OPERATIONCODE"); + operationObjbyteArr = rs.getBytes("OPERATIONDETAILS"); } - bais = new ByteArrayInputStream(payload); + bais = new ByteArrayInputStream(operationObjbyteArr); ois = new ObjectInputStream(bais); - ProfileOperation profileOperation = new ProfileOperation(); - profileOperation.setPayload(ois.readObject()); - profileOperation.setId(operationId); - profileOperation.setType(Operation.Type.valueOf(operationType)); - profileOperation.setStatus(Operation.Status.valueOf(operationStatus)); - profileOperation.setCreatedTimeStamp(createdTime); - profileOperation.setReceivedTimeStamp(receivedTime); - profileOperation.setCode(operationCode); - return profileOperation; - + return (ProfileOperation) ois.readObject(); } catch (SQLException e) { throw new OperationManagementDAOException("Error occurred while adding operation metadata", e); } catch (ClassNotFoundException e) { @@ -186,6 +165,67 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl { throw new OperationManagementDAOException("Error occurred while serializing profile operation object", e); } finally { OperationManagementDAOUtil.cleanupResources(stmt, rs); + OperationManagementDAOFactory.closeConnection(); + } + } + + @Override + public void updateOperation(Operation operation) throws OperationManagementDAOException { + + PreparedStatement stmt = null; + ByteArrayOutputStream bao = null; + ObjectOutputStream oos = null; + + try { + Connection connection = OperationManagementDAOFactory.openConnection(); + stmt = connection.prepareStatement( + "UPDATE DM_PROFILE_OPERATION O SET O.OPERATIONDETAILS=? WHERE O.OPERATION_ID=?"); + + bao = new ByteArrayOutputStream(); + oos = new ObjectOutputStream(bao); + oos.writeObject(operation); + + stmt.setBytes(1, bao.toByteArray()); + stmt.setInt(2, operation.getId()); + stmt.executeUpdate(); + + } catch (SQLException e) { + throw new OperationManagementDAOException("Error occurred while update operation metadata", e); + } catch (IOException e) { + throw new OperationManagementDAOException("Error occurred while serializing profile operation object", e); + } finally { + if (bao != null) { + try { + bao.close(); + } catch (IOException e) { + log.warn("Error occurred while closing ByteArrayOutputStream", e); + } + } + if (oos != null) { + try { + oos.close(); + } catch (IOException e) { + log.warn("Error occurred while closing ObjectOutputStream", e); + } + } + OperationManagementDAOUtil.cleanupResources(stmt); + } + } + + @Override + public void deleteOperation(int id) throws OperationManagementDAOException { + + super.deleteOperation(id); + PreparedStatement stmt = null; + try { + Connection connection = OperationManagementDAOFactory.openConnection(); + stmt = connection.prepareStatement("DELETE DM_PROFILE_OPERATION WHERE OPERATION_ID=?"); + stmt.setInt(1, id); + stmt.executeUpdate(); + } catch (SQLException e) { + throw new OperationManagementDAOException("Error occurred while deleting operation metadata", e); + } finally { + OperationManagementDAOUtil.cleanupResources(stmt); } } } diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql index a7e39bff60..19bf54c5b6 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -50,7 +50,7 @@ CREATE TABLE IF NOT EXISTS DM_COMMAND_OPERATION ( CREATE TABLE IF NOT EXISTS DM_PROFILE_OPERATION ( OPERATION_ID INTEGER NOT NULL, ENABLED INTEGER NOT NULL DEFAULT 0, - PAYLOAD BLOB DEFAULT NULL, + OPERATIONDETAILS BLOB DEFAULT NULL, PRIMARY KEY (OPERATION_ID), CONSTRAINT fk_dm_operation_profile FOREIGN KEY (OPERATION_ID) REFERENCES DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION