From 41edb530bcec76bfcedb4ad5c11c748c9607410c Mon Sep 17 00:00:00 2001 From: manoj Date: Wed, 15 Apr 2015 21:40:13 +0530 Subject: [PATCH] Refactor Operations --- .../carbon/device/mgt/core/dao/DeviceDAO.java | 1 + .../operation/mgt/OperationManagerImpl.java | 87 ++-- .../core/operation/mgt/dao/OperationDAO.java | 15 +- .../dao/OperationManagementDAOFactory.java | 2 +- .../mgt/dao/impl/CommandOperationDAOImpl.java | 149 +------ .../mgt/dao/impl/ConfigOperationDAOImpl.java | 7 +- .../mgt/dao/impl/OperationDAOImpl.java | 384 ++++++++++++++---- .../mgt/dao/impl/OperationMappingDAOImpl.java | 4 +- .../mgt/dao/impl/ProfileOperationDAOImpl.java | 65 +-- .../src/test/resources/sql/h2.sql | 7 +- .../src/main/resources/dbscripts/cdm/h2.sql | 2 +- 11 files changed, 382 insertions(+), 341 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index fb27efe523..2588668b53 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -60,4 +60,5 @@ public interface DeviceDAO { */ List getDeviceListOfUser(String username , int tenantId) throws DeviceManagementDAOException; + } 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 238a61c9f0..6ada7c3e7f 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 @@ -32,9 +32,9 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationDAO; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationMappingDAO; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.util.OperationDAOUtil; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; /** @@ -64,10 +64,14 @@ public class OperationManagerImpl implements OperationManager { @Override public boolean addOperation(Operation operation, - List devices) throws OperationManagementException { + List devices) throws OperationManagementException { try { OperationManagementDAOFactory.beginTransaction(); - int operationId = this.lookupOperationDAO(operation).addOperation(operation); + + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation operationDto = OperationDAOUtil + .convertOperation(operation); + int operationId = this.lookupOperationDAO(operation).addOperation(operationDto); + for (DeviceIdentifier deviceIdentifier : devices) { Device device = deviceDAO.getDevice(deviceIdentifier); operationMappingDAO.addOperationMapping(operationId, device.getId()); @@ -95,20 +99,24 @@ public class OperationManagerImpl implements OperationManager { public List getOperations(DeviceIdentifier deviceId) throws OperationManagementException { try { List operations = new ArrayList(); - - OperationManagementDAOFactory.beginTransaction(); - operations.addAll(profileOperationDAO.getOperations(deviceId)); - operations.addAll(configOperationDAO.getOperations(deviceId)); - operations.addAll(commandOperationDAO.getOperations(deviceId)); - OperationManagementDAOFactory.commitTransaction(); - - return operations; - } catch (OperationManagementDAOException e) { + Device device; try { - OperationManagementDAOFactory.rollbackTransaction(); - } catch (OperationManagementDAOException e1) { - log.warn("Error occurred while roll-backing the transaction", e1); + device = deviceDAO.getDevice(deviceId); + } catch (DeviceManagementDAOException deviceDAOException) { + String errorMsg = "Error occurred while retrieving the device " + + "for device Identifier type -'" + deviceId.getType() + "' and device Id '" + deviceId.getId(); + log.error(errorMsg, deviceDAOException); + throw new OperationManagementException(errorMsg, deviceDAOException); } + List operationList = operationDAO + .getOperationsForDevice(device.getId()); + Operation operation; + for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : operationList) { + operation = OperationDAOUtil.convertOperation(dtoOperation); + operations.add(operation); + } + return operations; + } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving the list of " + "operations assigned for '" + deviceId.getType() + "' device '" + deviceId.getId() + "'", e); } @@ -119,25 +127,28 @@ public class OperationManagerImpl implements OperationManager { DeviceIdentifier deviceId) throws OperationManagementException { try { List operations = new ArrayList(); + List dtoOperationList = operationDAO + .getOperationsForStatus(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING); + Operation operation; + for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : dtoOperationList) { + operation = OperationDAOUtil.convertOperation(dtoOperation); + operations.add(operation); + } - operations.addAll(profileOperationDAO.getOperations(deviceId, Operation.Status.PENDING)); - operations.addAll(configOperationDAO.getOperations(deviceId, Operation.Status.PENDING)); - operations.addAll(commandOperationDAO.getOperations(deviceId, Operation.Status.PENDING)); return operations; } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving the list of " + "pending operations assigned for '" + deviceId.getType() + "' device '" + deviceId.getId() + "'", e); } -} + } @Override public Operation getNextPendingOperation(DeviceIdentifier deviceId) throws OperationManagementException { try { - Operation operation = operationDAO.getNextOperation(deviceId); - if (operation!=null && operation.getType().equals(Operation.Type.PROFILE)){ - operation = profileOperationDAO.getNextOperation(deviceId); - } + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO + .getNextOperation(deviceId); + Operation operation = OperationDAOUtil.convertOperation(dtoOperation); return operation; } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving next pending operation", e); @@ -149,18 +160,20 @@ public class OperationManagerImpl implements OperationManager { throws OperationManagementException { try { - Operation operation = operationDAO.getOperation(operationId); - operation.setStatus(operationStatus); + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO.getOperation + (operationId); + dtoOperation.setStatus(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.valueOf + (operationStatus.toString())); OperationManagementDAOFactory.beginTransaction(); - operationDAO.updateOperation(operation); + operationDAO.updateOperation(dtoOperation); OperationManagementDAOFactory.commitTransaction(); - }catch(OperationManagementDAOException ex){ + } catch (OperationManagementDAOException ex) { try { OperationManagementDAOFactory.rollbackTransaction(); } catch (OperationManagementDAOException e1) { log.warn("Error occurred while roll-backing the update operation transaction", e1); } - log.error("Error occurred while updating the operation: "+operationId); + log.error("Error occurred while updating the operation: " + operationId); throw new OperationManagementException("Error occurred while update operation", ex); } @@ -174,21 +187,21 @@ public class OperationManagerImpl implements OperationManager { return profileOperationDAO; } else if (operation instanceof ConfigOperation) { return configOperationDAO; - }else{ + } else { return operationDAO; } } private OperationDAO lookupOperationDAO(Operation.Type type) { switch (type) { - case CONFIG: - return configOperationDAO; - case PROFILE: - return profileOperationDAO; - case COMMAND: - return commandOperationDAO; - default: - return commandOperationDAO; + case CONFIG: + return configOperationDAO; + case PROFILE: + return profileOperationDAO; + case COMMAND: + return commandOperationDAO; + default: + return commandOperationDAO; } } 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 77bb066b0a..8883b71c86 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 @@ -19,7 +19,7 @@ package org.wso2.carbon.device.mgt.core.operation.mgt.dao; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; import java.util.List; @@ -33,16 +33,15 @@ public interface OperationDAO { Operation getOperation(int operationId) throws OperationManagementDAOException; - Operation getOperation(DeviceIdentifier deviceId, int operationId) throws OperationManagementDAOException; + Operation getOperationByDeviceAndId(int deviceId, + int operationId) throws OperationManagementDAOException; - Operation getOperation(DeviceIdentifier deviceIdentifier, Operation.Status status) throws OperationManagementDAOException; + List getOperationsByDeviceAndStatus(int deviceId, Operation.Status status) throws + OperationManagementDAOException; - List getOperations(DeviceIdentifier deviceId) throws OperationManagementDAOException; + List getOperationsForDevice(int deviceId) throws OperationManagementDAOException; - List getOperations(DeviceIdentifier deviceId, - Operation.Status status) throws OperationManagementDAOException; - - List getOperations(Operation.Status status) throws OperationManagementDAOException; + List getOperationsForStatus(Operation.Status status) throws OperationManagementDAOException; Operation getNextOperation(DeviceIdentifier deviceId) 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 ed2066871e..960e5b8850 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,7 +74,7 @@ public class OperationManagementDAOFactory { } } - public static Connection openConnection() throws OperationManagementDAOException { + public static Connection getConnection() throws OperationManagementDAOException { if (currentConnection.get() == null) { try { currentConnection.set(dataSource.getConnection()); 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 ac248dea8f..f8d9ffcbfc 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 @@ -19,8 +19,8 @@ package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; -import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation; +import org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation; +import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOUtil; @@ -42,7 +42,7 @@ public class CommandOperationDAOImpl extends OperationDAOImpl { PreparedStatement stmt = null; try { - Connection conn = OperationManagementDAOFactory.openConnection(); + Connection conn = OperationManagementDAOFactory.getConnection(); stmt = conn.prepareStatement("INSERT INTO DM_COMMAND_OPERATION(OPERATION_ID, ENABLED) VALUES(?, ?)"); stmt.setInt(1, operationId); stmt.setBoolean(2, commandOp.isEnabled()); @@ -55,151 +55,12 @@ public class CommandOperationDAOImpl extends OperationDAOImpl { return operationId; } - public Operation getOperation(int id) throws OperationManagementDAOException { - - PreparedStatement stmt = null; - ResultSet rs = null; - Operation operation = null; - try { - Connection conn = OperationManagementDAOFactory.openConnection(); - String sql = "SELECT o.ID, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS, o.OPERATIONCODE," + - "co.ENABLED FROM DM_OPERATION o INNER JOIN DM_COMMAND_OPERATION co ON " + - "co.OPERATION_ID = o.ID WHERE o.ID=?"; - - stmt = conn.prepareStatement(sql); - stmt.setInt(1, id); - rs = stmt.executeQuery(); - - if (rs.next()) { - operation = new Operation(); - operation.setId(rs.getInt("ID")); - operation.setType(Operation.Type.valueOf(rs.getString("TYPE"))); - operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString()); - if (rs.getTimestamp("RECEIVED_TIMESTAMP") == null) { - operation.setReceivedTimeStamp(""); - } else { - operation.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString()); - } - operation.setStatus(Operation.Status.valueOf(rs.getString("STATUS"))); - operation.setCode(rs.getString("OPERATIONCODE")); - } - } catch (SQLException e) { - throw new OperationManagementDAOException("Error occurred while retrieving the operation object " + - "available for the id '" + id + "'", e); - } finally { - OperationManagementDAOUtil.cleanupResources(stmt, rs); - OperationManagementDAOFactory.closeConnection(); - } - return operation; - } - - public List getOperations(DeviceIdentifier deviceId, - Operation.Status status) throws OperationManagementDAOException { - PreparedStatement stmt = null; - ResultSet rs = null; - List operationList = new ArrayList(); - Operation operation; - try { - Connection conn = OperationManagementDAOFactory.openConnection(); - String sql = "SELECT po.OPERATION_ID, po.TYPE, po.CREATED_TIMESTAMP, po.RECEIVED_TIMESTAMP, po.STATUS, " + - "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(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()); - - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) { - operation.setCreatedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString()); - } else { - operation.setCreatedTimeStamp(null); - } - 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(); - } - } - - public List getOperations(DeviceIdentifier deviceId) throws OperationManagementDAOException { - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - Connection conn = OperationManagementDAOFactory.openConnection(); - String sql = "SELECT po.OPERATION_ID, po.TYPE, po.CREATED_TIMESTAMP, po.RECEIVED_TIMESTAMP, po.STATUS, " + - "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 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(2, deviceId.getId()); - rs = stmt.executeQuery(); - - List operations = new ArrayList(); - while (rs.next()) { - CommandOperation operation = new CommandOperation(); - operation.setId(rs.getInt("ID")); - operation.setType(Operation.Type.valueOf(rs.getString("TYPE"))); - operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString()); - - if (rs.getTimestamp("RECEIVED_TIMESTAMP") == null) { - operation.setReceivedTimeStamp(null); - } else { - operation.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString()); - } - 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; - } catch (SQLException e) { - throw new OperationManagementDAOException("Error occurred while retrieving the list of " + - "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(); + Connection connection = OperationManagementDAOFactory.getConnection(); stmt = connection.prepareStatement( "UPDATE DM_COMMAND_OPERATION O SET O.ENABLED=? WHERE O.OPERATION_ID=?"); @@ -221,7 +82,7 @@ public class CommandOperationDAOImpl extends OperationDAOImpl { super.deleteOperation(id); PreparedStatement stmt = null; try { - Connection connection = OperationManagementDAOFactory.openConnection(); + Connection connection = OperationManagementDAOFactory.getConnection(); stmt = connection.prepareStatement("DELETE DM_COMMAND_OPERATION WHERE OPERATION_ID=?"); stmt.setInt(1, id); stmt.executeUpdate(); 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 9a53e2fd55..08ac0d702a 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 @@ -18,7 +18,8 @@ */ package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl; -import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; + +import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; import org.wso2.carbon.device.mgt.core.operation.mgt.ConfigOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; @@ -36,7 +37,7 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl { int operationId = super.addOperation(operation); PreparedStatement stmt = null; try { - Connection conn = OperationManagementDAOFactory.openConnection(); + Connection conn = OperationManagementDAOFactory.getConnection(); stmt = conn.prepareStatement("INSERT INTO DM_CONFIG_OPERATION(OPERATION_ID) VALUES(?)"); stmt.setInt(1, operationId); stmt.executeUpdate(); @@ -54,7 +55,7 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl { super.deleteOperation(id); PreparedStatement stmt = null; try { - Connection connection = OperationManagementDAOFactory.openConnection(); + Connection connection = OperationManagementDAOFactory.getConnection(); stmt = connection.prepareStatement("DELETE DM_CONFIG_OPERATION WHERE OPERATION_ID=?") ; stmt.setInt(1, id); stmt.executeUpdate(); 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 b93739b459..e6d8ac0c41 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 @@ -18,13 +18,19 @@ */ package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.core.dto.operation.mgt.ProfileOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationDAO; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOUtil; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.ObjectInputStream; import java.sql.*; import java.util.ArrayList; import java.util.Date; @@ -32,14 +38,16 @@ import java.util.List; public class OperationDAOImpl implements OperationDAO { + private static final Log log = LogFactory.getLog(OperationDAOImpl.class); + public int addOperation(Operation operation) throws OperationManagementDAOException { PreparedStatement stmt = null; ResultSet rs = null; try { - Connection connection = OperationManagementDAOFactory.openConnection(); + Connection connection = OperationManagementDAOFactory.getConnection(); stmt = connection.prepareStatement( - "INSERT INTO DM_OPERATION(TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, STATUS, OPERATIONCODE) " + + "INSERT INTO DM_OPERATION(TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, STATUS, OPERATION_CODE) " + "VALUES (?, ?, ?, ?,?)"); stmt.setString(1, operation.getType().toString()); stmt.setTimestamp(2, new Timestamp(new Date().getTime())); @@ -66,7 +74,7 @@ public class OperationDAOImpl implements OperationDAO { PreparedStatement stmt = null; try { - Connection connection = OperationManagementDAOFactory.openConnection(); + Connection connection = OperationManagementDAOFactory.getConnection(); stmt = connection.prepareStatement("UPDATE DM_OPERATION O SET O.RECEIVED_TIMESTAMP=?,O.STATUS=? " + "WHERE O.ID=?"); @@ -87,7 +95,7 @@ public class OperationDAOImpl implements OperationDAO { PreparedStatement stmt = null; try { - Connection connection = OperationManagementDAOFactory.openConnection(); + Connection connection = OperationManagementDAOFactory.getConnection(); stmt = connection.prepareStatement("DELETE DM_OPERATION WHERE ID=?"); stmt.setInt(1, id); stmt.executeUpdate(); @@ -98,37 +106,64 @@ public class OperationDAOImpl implements OperationDAO { } } + @Override public Operation getOperation(int id) throws OperationManagementDAOException { PreparedStatement stmt = null; ResultSet rs = null; Operation operation = null; + + ByteArrayInputStream bais; + ObjectInputStream ois; + try { - 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 WHERE o.ID=?"; + Connection conn = OperationManagementDAOFactory.getConnection(); + String sql = "SELECT o.ID, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS, o.OPERATION_CODE," + + "po.OPERATION_DETAILS,co.ENABLED from " + + "(SELECT ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, STATUS, OPERATION_CODE FROM " + + " DM_OPERATION WHERE id=?) o" + + "LEFT OUTER JOIN DM_PROFILE_OPERATION po on o.ID =po.OPERATION_ID" + + "LEFT OUTER JOIN DM_COMMAND_OPERATION co on co.OPERATION_ID=o.ID"; stmt = conn.prepareStatement(sql); stmt.setInt(1, id); rs = stmt.executeQuery(); if (rs.next()) { - operation = new Operation(); - operation.setId(rs.getInt("ID")); - operation.setType(Operation.Type.valueOf(rs.getString("TYPE"))); - operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString()); - if (rs.getTimestamp("RECEIVED_TIMESTAMP") == null) { - operation.setReceivedTimeStamp(""); + if (rs.getBytes("OPERATION_DETAILS") != null) { + byte[] operationDetails; + operationDetails = rs.getBytes("OPERATION_DETAILS"); + bais = new ByteArrayInputStream(operationDetails); + ois = new ObjectInputStream(bais); + operation = (ProfileOperation) ois.readObject(); } else { - operation.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString()); + operation = new Operation(); + operation.setId(rs.getInt("ID")); + operation.setType(Operation.Type.valueOf(rs.getString("TYPE"))); + operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString()); + if (rs.getTimestamp("RECEIVED_TIMESTAMP") == null) { + operation.setReceivedTimeStamp(""); + } else { + operation.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString()); + } + operation.setEnabled(rs.getBoolean("ENABLED")); + operation.setStatus(Operation.Status.valueOf(rs.getString("STATUS"))); + operation.setCode(rs.getString("OPERATION_CODE")); } - operation.setStatus(Operation.Status.valueOf(rs.getString("STATUS"))); - operation.setCode(rs.getString("OPERATIONCODE")); } + } catch (IOException e) { + String errorMsg = "IO Error occurred while de serialize the profile operation object"; + log.error(errorMsg, e); + throw new OperationManagementDAOException(errorMsg, e); + } catch (ClassNotFoundException e) { + String errorMsg = "Class not found error occurred while de serialize the profile operation object"; + log.error(errorMsg, e); + throw new OperationManagementDAOException(errorMsg, e); } catch (SQLException e) { - throw new OperationManagementDAOException("Error occurred while retrieving the operation object " + - "available for the id '" + id + "'", e); + String errorMsg = "SQL Error occurred while retrieving the operation object " + + "available for the id '" + id; + log.error(errorMsg, e); + throw new OperationManagementDAOException(errorMsg, e); } finally { OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOFactory.closeConnection(); @@ -137,47 +172,69 @@ public class OperationDAOImpl implements OperationDAO { } @Override - public Operation getOperation(DeviceIdentifier deviceIdentifier, int operationId) - throws OperationManagementDAOException { + public Operation getOperationByDeviceAndId(int deviceId, int operationId) throws OperationManagementDAOException { PreparedStatement stmt = null; ResultSet rs = null; Operation operation = null; + ByteArrayInputStream bais; + ObjectInputStream ois; + try { - 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 " + - "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 WHERE o.ID=?"; + Connection conn = OperationManagementDAOFactory.getConnection(); + String sql = "SELECT o.ID, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS, o.OPERATION_CODE," + + "po.OPERATION_DETAILS,co.ENABLED from " + + "(SELECT ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, STATUS, " + + "OPERATION_CODE FROM DM_OPERATION WHERE id=?) o INNER JOIN (Select * from " + + "DM_DEVICE_OPERATION_MAPPING dm where dm.OPERATION_ID=? AND dm.DEVICE_ID=?) om " + + "ON o.ID = om.OPERATION_ID " + + "LEFT OUTER JOIN DM_PROFILE_OPERATION po on o.ID = po.OPERATION_ID " + + "LEFT OUTER JOIN DM_COMMAND_OPERATION co on co.OPERATION_ID=o.ID"; + stmt = conn.prepareStatement(sql); - stmt.setString(1, deviceIdentifier.getType()); - stmt.setString(2, deviceIdentifier.getId()); - stmt.setInt(3, operationId); + stmt.setInt(1, operationId); + stmt.setInt(2, operationId); + stmt.setInt(3, deviceId); rs = stmt.executeQuery(); if (rs.next()) { - operation = new Operation(); - operation.setId(rs.getInt("ID")); - operation.setType(Operation.Type.valueOf(rs.getString("TYPE"))); - operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString()); - if (rs.getTimestamp("CREATED_TIMESTAMP") == null) { - operation.setReceivedTimeStamp(""); + if (rs.getBytes("OPERATION_DETAILS") != null) { + byte[] operationDetails; + operationDetails = rs.getBytes("OPERATION_DETAILS"); + bais = new ByteArrayInputStream(operationDetails); + ois = new ObjectInputStream(bais); + operation = (ProfileOperation) ois.readObject(); } else { - operation.setReceivedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString()); + operation = new Operation(); + operation.setId(rs.getInt("ID")); + operation.setType(Operation.Type.valueOf(rs.getString("TYPE"))); + operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString()); + if (rs.getTimestamp("CREATED_TIMESTAMP") == null) { + operation.setReceivedTimeStamp(""); + } else { + operation.setReceivedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString()); + } + operation.setStatus(Operation.Status.valueOf(rs.getString("STATUS"))); + operation.setCode(rs.getString("OPERATION_CODE")); } - operation.setStatus(Operation.Status.valueOf(rs.getString("STATUS"))); - operation.setCode(rs.getString("OPERATIONCODE")); } + } catch (IOException ex) { + String errorMsg = "IO error occurred while de serializing the profile operation available for the " + + "device:" + deviceId + "' with id '" + operationId; + log.error(errorMsg); + throw new OperationManagementDAOException(errorMsg, ex); + } catch (ClassNotFoundException ex) { + String errorMsg = + "class not found error occurred while de serializing the profile operation available for " + + "the device:" + deviceId + "' with id '" + operationId; + log.error(errorMsg); + throw new OperationManagementDAOException(errorMsg, ex); } catch (SQLException e) { - throw new OperationManagementDAOException("Error occurred while retrieving the operation list " + - "available for the '" + deviceIdentifier.getType() + "' with id '" + deviceIdentifier.getId() + "'", e); + String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + deviceId + + "' with id '" + operationId; + log.error(errorMsg); + throw new OperationManagementDAOException(errorMsg, e); } finally { OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOFactory.closeConnection(); @@ -186,66 +243,218 @@ public class OperationDAOImpl implements OperationDAO { } @Override - public Operation getOperation(DeviceIdentifier deviceId, + public List getOperationsByDeviceAndStatus(int deviceId, Operation.Status status) throws OperationManagementDAOException { - return null; + + PreparedStatement stmt = null; + ResultSet rs = null; + Operation operation = null; + + ByteArrayInputStream bais; + ObjectInputStream ois; + List operationList = new ArrayList(); + + try { + Connection conn = OperationManagementDAOFactory.getConnection(); + String sql = "SELECT o.ID, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS, o.OPERATIONCODE, " + + "po.OPERATIONDETAILS,co.ENABLED from " + + "(SELECT ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, STATUS, " + + "OPERATIONCODE FROM DM_OPERATION WHERE STATUS=?) o " + + "INNER JOIN (Select * from DM_DEVICE_OPERATION_MAPPING dm " + + "where dm.DEVICE_ID=?) om ON o.ID = om.OPERATION_ID LEFT OUTER JOIN DM_PROFILE_OPERATION po ON " + + "o.ID =po.OPERATION_ID LEFT OUTER JOIN DM_COMMAND_OPERATION co ON co.OPERATION_ID=o.ID"; + + stmt = conn.prepareStatement(sql); + stmt.setString(1, status.toString()); + stmt.setInt(2, deviceId); + + rs = stmt.executeQuery(); + + while (rs.next()) { + if (rs.getBytes("OPERATION_DETAILS") != null) { + byte[] operationDetails; + operationDetails = rs.getBytes("OPERATION_DETAILS"); + bais = new ByteArrayInputStream(operationDetails); + ois = new ObjectInputStream(bais); + operation = (ProfileOperation) ois.readObject(); + } else { + operation = new Operation(); + operation.setId(rs.getInt("ID")); + operation.setType(Operation.Type.valueOf(rs.getString("TYPE"))); + operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString()); + if (rs.getTimestamp("CREATED_TIMESTAMP") == null) { + operation.setReceivedTimeStamp(""); + } else { + operation.setReceivedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString()); + } + operation.setStatus(Operation.Status.valueOf(rs.getString("STATUS"))); + operation.setCode(rs.getString("OPERATION_CODE")); + } + operationList.add(operation); + } + } catch (IOException ex) { + String errorMsg = "IO error occurred while de serializing the profile operation available for the " + + "device:" + deviceId + "' and status '" + status.toString(); + log.error(errorMsg); + throw new OperationManagementDAOException(errorMsg, ex); + } catch (ClassNotFoundException ex) { + String errorMsg = + "class not found error occurred while de serializing the profile operation available for " + + "the device:" + deviceId + "' with status '" + status.toString(); + log.error(errorMsg); + throw new OperationManagementDAOException(errorMsg, ex); + } catch (SQLException e) { + String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + deviceId + + "' with status '" + status.toString(); + log.error(errorMsg); + throw new OperationManagementDAOException(errorMsg, e); + } finally { + OperationManagementDAOUtil.cleanupResources(stmt, rs); + OperationManagementDAOFactory.closeConnection(); + } + return operationList; } @Override - public List getOperations(DeviceIdentifier deviceId) throws OperationManagementDAOException { + public List getOperationsForDevice(int deviceId) + throws OperationManagementDAOException { - List operations; PreparedStatement stmt = null; ResultSet rs = null; + Operation operation = null; + + ByteArrayInputStream bais; + ObjectInputStream ois; + List operationList = new ArrayList(); + try { - 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 " + - "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"; + Connection conn = OperationManagementDAOFactory.getConnection(); + String sql = "SELECT o.ID, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS, o.OPERATIONCODE, " + + "po.OPERATIONDETAILS,co.ENABLED from " + + "(SELECT ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, STATUS, " + + "OPERATIONCODE FROM DM_OPERATION) o " + + "INNER JOIN (Select * from DM_DEVICE_OPERATION_MAPPING dm " + + "where dm.DEVICE_ID=?) om ON o.ID = om.OPERATION_ID LEFT OUTER JOIN DM_PROFILE_OPERATION po ON " + + "o.ID =po.OPERATION_ID LEFT OUTER JOIN DM_COMMAND_OPERATION co ON co.OPERATION_ID=o.ID"; + stmt = conn.prepareStatement(sql); - stmt.setString(1, deviceId.getType()); - stmt.setString(2, deviceId.getId()); + stmt.setInt(1, deviceId); + rs = stmt.executeQuery(); - operations = new ArrayList(); while (rs.next()) { - Operation operation = new Operation(); - operation.setId(rs.getInt("ID")); - operation.setType(Operation.Type.valueOf(rs.getString("TYPE"))); - operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString()); - if (rs.getTimestamp("CREATED_TIMESTAMP") == null) { - operation.setReceivedTimeStamp(""); + if (rs.getBytes("OPERATION_DETAILS") != null) { + byte[] operationDetails; + operationDetails = rs.getBytes("OPERATION_DETAILS"); + bais = new ByteArrayInputStream(operationDetails); + ois = new ObjectInputStream(bais); + operation = (ProfileOperation) ois.readObject(); } else { - operation.setReceivedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString()); + operation = new Operation(); + operation.setId(rs.getInt("ID")); + operation.setType(Operation.Type.valueOf(rs.getString("TYPE"))); + operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString()); + if (rs.getTimestamp("CREATED_TIMESTAMP") == null) { + operation.setReceivedTimeStamp(""); + } else { + operation.setReceivedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString()); + } + operation.setStatus(Operation.Status.valueOf(rs.getString("STATUS"))); + operation.setCode(rs.getString("OPERATION_CODE")); } - operation.setStatus(Operation.Status.valueOf(rs.getString("STATUS"))); - operation.setCode(rs.getString("OPERATIONCODE")); + operationList.add(operation); } + } catch (IOException ex) { + String errorMsg = "IO error occurred while de serializing the profile operation available for the " + + "device:" + deviceId; + log.error(errorMsg); + throw new OperationManagementDAOException(errorMsg, ex); + } catch (ClassNotFoundException ex) { + String errorMsg = + "class not found error occurred while de serializing the profile operation available for " + + "the device:" + deviceId; + log.error(errorMsg); + throw new OperationManagementDAOException(errorMsg, ex); } catch (SQLException e) { - throw new OperationManagementDAOException("Error occurred while retrieving the operation list " + - "available for the '" + deviceId.getType() + "' with id '" + deviceId.getId() + "'", e); + String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + deviceId + + "' with status '"; + log.error(errorMsg); + throw new OperationManagementDAOException(errorMsg, e); } finally { OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOFactory.closeConnection(); } - return operations; + return operationList; } @Override - public List getOperations(DeviceIdentifier deviceId, - Operation.Status status) throws OperationManagementDAOException { - return null; - } + public List getOperationsForStatus(Operation.Status status) + throws OperationManagementDAOException { - @Override - public List getOperations(Operation.Status status) throws OperationManagementDAOException { - return null; + PreparedStatement stmt = null; + ResultSet rs = null; + Operation operation = null; + + ByteArrayInputStream bais; + ObjectInputStream ois; + List operationList = new ArrayList(); + + try { + Connection conn = OperationManagementDAOFactory.getConnection(); + String sql = "SELECT o.ID, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS, o.OPERATION_CODE,"+ + "po.OPERATION_DETAILS,co.ENABLED from "+ + "(SELECT ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, STATUS,"+ + "OPERATIONCODE FROM DM_OPERATION WHERE STATUS=?) o "+ + "LEFT OUTER JOIN DM_PROFILE_OPERATION po ON "+ + "o.ID =po.OPERATION_ID LEFT OUTER JOIN DM_COMMAND_OPERATION co ON co.OPERATION_ID=o.ID"; + + stmt = conn.prepareStatement(sql); + stmt.setString(1, status.toString()); + rs = stmt.executeQuery(); + + while (rs.next()) { + if (rs.getBytes("OPERATION_DETAILS") != null) { + byte[] operationDetails; + operationDetails = rs.getBytes("OPERATION_DETAILS"); + bais = new ByteArrayInputStream(operationDetails); + ois = new ObjectInputStream(bais); + operation = (ProfileOperation) ois.readObject(); + } else { + operation = new Operation(); + operation.setId(rs.getInt("ID")); + operation.setType(Operation.Type.valueOf(rs.getString("TYPE"))); + operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString()); + if (rs.getTimestamp("CREATED_TIMESTAMP") == null) { + operation.setReceivedTimeStamp(""); + } else { + operation.setReceivedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString()); + } + operation.setStatus(Operation.Status.valueOf(rs.getString("STATUS"))); + operation.setCode(rs.getString("OPERATION_CODE")); + } + operationList.add(operation); + } + } catch (IOException ex) { + String errorMsg = "IO error occurred while de serializing the profile operation available for the " + + "status:" + status.toString(); + log.error(errorMsg); + throw new OperationManagementDAOException(errorMsg, ex); + } catch (ClassNotFoundException ex) { + String errorMsg = + "class not found error occurred while de serializing the profile operation available for " + + "the status:" + status.toString(); + log.error(errorMsg); + throw new OperationManagementDAOException(errorMsg, ex); + } catch (SQLException e) { + String errorMsg = "SQL error occurred while retrieving the operation available for the status:'" + + status.toString(); + log.error(errorMsg); + throw new OperationManagementDAOException(errorMsg, e); + } finally { + OperationManagementDAOUtil.cleanupResources(stmt, rs); + OperationManagementDAOFactory.closeConnection(); + } + return operationList; } @Override @@ -253,14 +462,15 @@ public class OperationDAOImpl implements OperationDAO { PreparedStatement stmt = null; ResultSet rs = null; try { - Connection connection = OperationManagementDAOFactory.openConnection(); + Connection connection = OperationManagementDAOFactory.getConnection(); stmt = connection.prepareStatement( - "SELECT o.ID, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS,o.OPERATIONCODE " + + "SELECT o.ID, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS,o.OPERATION_CODE " + " 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.STATUS=? AND o.ID = ois.OP_ID " + + "DM_DEVICE_OPERATION_MAPPING dom ON d1.ID = dom.DEVICE_ID) ois ON o.STATUS=? AND" + + " o.ID = ois.OP_ID " + "ORDER BY o.CREATED_TIMESTAMP ASC LIMIT 1"); stmt.setString(1, deviceId.getType()); stmt.setString(2, deviceId.getId()); @@ -280,7 +490,7 @@ public class OperationDAOImpl implements OperationDAO { } else { operation.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString()); } - operation.setCode(rs.getString("OPERATIONCODE")); + operation.setCode(rs.getString("OPERATION_CODE")); } return operation; } catch (SQLException 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/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 61448a2d36..15faacad08 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.openConnection(); + Connection conn = OperationManagementDAOFactory.getConnection(); 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.openConnection(); + Connection conn = OperationManagementDAOFactory.getConnection(); 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 494b3d3336..22a459533f 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 @@ -21,8 +21,8 @@ package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; -import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation; +import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.core.dto.operation.mgt.ProfileOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOUtil; @@ -39,8 +39,9 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl { 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.openConnection(); + Connection conn = OperationManagementDAOFactory.getConnection(); PreparedStatement stmt = null; @@ -58,53 +59,6 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl { return operationId; } - @Override - public Operation getOperation(int operationId) throws OperationManagementDAOException { - Connection conn = OperationManagementDAOFactory.openConnection(); - - PreparedStatement stmt = null; - ResultSet rs = null; - ByteArrayInputStream bais = null; - ObjectInputStream ois = null; - try { - stmt = conn.prepareStatement("SELECT OPERATION_DETAILS FROM DM_PROFILE_OPERATION WHERE OPERATION_ID = ?"); - stmt.setInt(1, operationId); - rs = stmt.executeQuery(); - - byte[] operationDetails = new byte[0]; - if (rs.next()) { - operationDetails = rs.getBytes("OPERATIONDETAILS"); - } - bais = new ByteArrayInputStream(operationDetails); - ois = new ObjectInputStream(bais); - return (ProfileOperation) ois.readObject(); - } catch (SQLException e) { - throw new OperationManagementDAOException("Error occurred while adding profile operation", e); - } 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 profile operation as a " + - "ProfileOperation object", e); - } finally { - if (bais != null) { - try { - bais.close(); - } catch (IOException e) { - log.warn("Error occurred while closing ByteArrayOutputStream", e); - } - } - if (ois != null) { - try { - ois.close(); - } catch (IOException e) { - log.warn("Error occurred while closing ObjectOutputStream", e); - } - } - OperationManagementDAOUtil.cleanupResources(stmt, rs); - OperationManagementDAOFactory.closeConnection(); - } - } - @Override public Operation getNextOperation(DeviceIdentifier deviceId) throws OperationManagementDAOException { @@ -114,17 +68,18 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl { ObjectInputStream ois = null; try { - Connection connection = OperationManagementDAOFactory.openConnection(); + Connection connection = OperationManagementDAOFactory.getConnection(); stmt = connection.prepareStatement( "SELECT po.OPERATION_DETAILS AS OPERATIONDETAILS " + "FROM DM_OPERATION o " + - "INNER JOIN DM_PROFILE_OPERATION po ON o.ID = po.OPERATION_ID AND o.STATUS =? AND o.ID IN (" + + "INNER JOIN DM_PROFILE_OPERATION po ON o.ID = po.OPERATION_ID AND o.STATUS =? 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"); - stmt.setString(1,Operation.Status.PENDING.toString()); + stmt.setString(1, Operation.Status.PENDING.toString()); stmt.setString(2, deviceId.getType()); stmt.setString(3, deviceId.getId()); rs = stmt.executeQuery(); @@ -174,7 +129,7 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl { ObjectOutputStream oos = null; try { - Connection connection = OperationManagementDAOFactory.openConnection(); + Connection connection = OperationManagementDAOFactory.getConnection(); stmt = connection.prepareStatement("UPDATE DM_PROFILE_OPERATION O SET O.OPERATION_DETAILS=? " + "WHERE O.OPERATION_ID=?"); @@ -215,7 +170,7 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl { super.deleteOperation(id); PreparedStatement stmt = null; try { - Connection connection = OperationManagementDAOFactory.openConnection(); + Connection connection = OperationManagementDAOFactory.getConnection(); stmt = connection.prepareStatement("DELETE DM_PROFILE_OPERATION WHERE OPERATION_ID=?"); stmt.setInt(1, id); stmt.executeUpdate(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql index e0fd152ac6..d15a958212 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql @@ -27,7 +27,7 @@ CREATE TABLE IF NOT EXISTS DM_OPERATION ( CREATED_TIMESTAMP TIMESTAMP NOT NULL, RECEIVED_TIMESTAMP TIMESTAMP NULL, STATUS VARCHAR(50) NULL, - OPERATIONCODE VARCHAR(25) NOT NULL, + OPERATION_CODE VARCHAR(25) NOT NULL, PRIMARY KEY (ID) ); @@ -40,7 +40,7 @@ CREATE TABLE IF NOT EXISTS DM_CONFIG_OPERATION ( CREATE TABLE IF NOT EXISTS DM_COMMAND_OPERATION ( OPERATION_ID INTEGER NOT NULL, - ENABLED INTEGER NOT NULL DEFAULT 0, + ENABLED BOOLEAN NOT NULL DEFAULT FALSE, PRIMARY KEY (OPERATION_ID), CONSTRAINT fk_dm_operation_command FOREIGN KEY (OPERATION_ID) REFERENCES DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION @@ -48,7 +48,8 @@ CREATE TABLE IF NOT EXISTS DM_COMMAND_OPERATION ( CREATE TABLE IF NOT EXISTS DM_PROFILE_OPERATION ( OPERATION_ID INTEGER NOT NULL, - PAYLOAD BLOB NOT NULL, + ENABLED INTEGER NOT NULL DEFAULT 0, + OPERATION_DETAILS 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 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 8964e01b32..b14d3a6156 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 @@ -27,7 +27,7 @@ CREATE TABLE IF NOT EXISTS DM_OPERATION ( CREATED_TIMESTAMP TIMESTAMP NOT NULL, RECEIVED_TIMESTAMP TIMESTAMP NULL, STATUS VARCHAR(50) NULL, - OPERATIONCODE VARCHAR(25) NOT NULL, + OPERATION_CODE VARCHAR(25) NOT NULL, PRIMARY KEY (ID) );