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 04be15b502..ce2c5e1ce4 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 @@ -44,6 +44,65 @@ public class Operation implements Serializable { private boolean isEnabled; private Object payLoad; + @Override + public boolean equals(Object o) { + + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + Operation operation = (Operation) o; + + if (id != operation.id) { + return false; + } + if (isEnabled != operation.isEnabled) { + return false; + } + if (!code.equals(operation.code)) { + return false; + } + if (createdTimeStamp != null ? + !createdTimeStamp.equals(operation.createdTimeStamp) : + operation.createdTimeStamp != null) { + return false; + } + if (payLoad != null ? !payLoad.equals(operation.payLoad) : operation.payLoad != null) { + return false; + } + if (properties != null ? !properties.equals(operation.properties) : operation.properties != null) { + return false; + } + if (!receivedTimeStamp.equals(operation.receivedTimeStamp)) { + return false; + } + if (status != operation.status) { + return false; + } + if (type != operation.type) { + return false; + } + + return true; + } + + @Override + public int hashCode() { + int result = code.hashCode(); + result = 31 * result + (properties != null ? properties.hashCode() : 0); + result = 31 * result + type.hashCode(); + result = 31 * result + id; + result = 31 * result + status.hashCode(); + result = 31 * result + receivedTimeStamp.hashCode(); + result = 31 * result + (createdTimeStamp != null ? createdTimeStamp.hashCode() : 0); + result = 31 * result + (isEnabled ? 1 : 0); + result = 31 * result + (payLoad != null ? payLoad.hashCode() : 0); + return result; + } + @XmlElement public String getCode() { return code; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationManager.java index 1a8991ba85..aded7d9ace 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationManager.java @@ -71,5 +71,4 @@ public interface OperationManager { public Operation getOperation(int operationId) throws OperationManagementException; - public List getOperationsForStatus(Operation.Status status) throws OperationManagementException; } \ 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/DeviceManagementServiceProviderImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java index dbd410d504..5ad3489644 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java @@ -494,12 +494,6 @@ public class DeviceManagementServiceProviderImpl implements DeviceManagementServ return DeviceManagementDataHolder.getInstance().getOperationManager().getOperation(operationId); } - @Override - public List getOperationsForStatus(Operation.Status status) - throws OperationManagementException { - return DeviceManagementDataHolder.getInstance().getOperationManager().getOperationsForStatus(status); - } - @Override public List getAllDevicesOfUser(String userName) throws DeviceManagementException { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/operation/mgt/Operation.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/operation/mgt/Operation.java index 3320ed17f2..02373c9c61 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/operation/mgt/Operation.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/operation/mgt/Operation.java @@ -27,7 +27,7 @@ import java.util.Properties; public class Operation implements Serializable { public enum Type { - CONFIG, MESSAGE, INFO, COMMAND, PROFILE + CONFIG, MESSAGE, INFO, COMMAND, PROFILE , POLICY } public enum Status { 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 530ca559d7..48a7fc7bc3 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 @@ -26,17 +26,17 @@ import org.wso2.carbon.device.mgt.common.DeviceManagementException; 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.OperationManager; -import org.wso2.carbon.device.mgt.core.dto.operation.mgt.*; -import org.wso2.carbon.device.mgt.core.dto.operation.mgt.PolicyOperation; 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 org.wso2.carbon.device.mgt.core.operation.mgt.util.OperationCreateTimeComparator; import org.wso2.carbon.device.mgt.core.service.DeviceManagementService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementServiceImpl; import java.util.ArrayList; +import java.util.Collections; import java.util.List; /** @@ -51,6 +51,7 @@ public class OperationManagerImpl implements OperationManager { private OperationDAO commandOperationDAO; private OperationDAO configOperationDAO; private OperationDAO profileOperationDAO; + private OperationDAO policyOperationDAO; private OperationMappingDAO operationMappingDAO; private OperationDAO operationDAO; private DeviceManagementService deviceManagementService; @@ -79,7 +80,6 @@ public class OperationManagerImpl implements OperationManager { OperationManagementDAOFactory.beginTransaction(); org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation operationDto = OperationDAOUtil.convertOperation(operation); - operationDto.setStatus(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING); int operationId = this.lookupOperationDAO(operation).addOperation(operationDto); org.wso2.carbon.device.mgt.common.Device device; @@ -163,8 +163,10 @@ public class OperationManagerImpl implements OperationManager { } org.wso2.carbon.device.mgt.common.Device device; - List operations; - List dtoOperationList; + List operations = new ArrayList(); + + List dtoOperationList = + new ArrayList(); try { @@ -174,15 +176,25 @@ public class OperationManagerImpl implements OperationManager { throw new OperationManagementException("Device not found for given device " + "Identifier:" + deviceIdentifier.getId() + " and given type:" + deviceIdentifier.getType()); } - operations = new ArrayList(); - dtoOperationList = operationDAO.getOperationsByDeviceAndStatus(device.getId(), - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING); + + dtoOperationList.addAll(commandOperationDAO.getOperationsByDeviceAndStatus(device.getId(), + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); + + dtoOperationList.addAll(configOperationDAO.getOperationsByDeviceAndStatus(device.getId(), + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); + + dtoOperationList.addAll(profileOperationDAO.getOperationsByDeviceAndStatus(device.getId(), + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); + + dtoOperationList.addAll(policyOperationDAO.getOperationsByDeviceAndStatus(device.getId(), + 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); } + Collections.sort(operations, new OperationCreateTimeComparator()); return operations; } catch (DeviceManagementException deviceMgtException) { String errorMsg = "Error occurred while retrieving the device " + @@ -201,7 +213,8 @@ public class OperationManagerImpl implements OperationManager { public Operation getNextPendingOperation(DeviceIdentifier deviceIdentifier) throws OperationManagementException { if (log.isDebugEnabled()) { - log.debug("device identifier id:[" + deviceIdentifier.getId() + "] type:[" + deviceIdentifier.getType() + "]"); + log.debug("device identifier id:[" + deviceIdentifier.getId() + "] type:[" + deviceIdentifier.getType() + + "]"); } Operation operation = null; Device device; @@ -214,6 +227,25 @@ public class OperationManagerImpl implements OperationManager { } org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO .getNextOperation(device.getId()); + + if (dtoOperation.getType() + .equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND)) { + org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation commandOperation; + commandOperation = (org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation) commandOperationDAO + .getOperation(dtoOperation.getId()); + dtoOperation.setEnabled(commandOperation.isEnabled()); + } else if (dtoOperation.getType() + .equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) { + dtoOperation = configOperationDAO.getOperation(dtoOperation.getId()); + } else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type + .PROFILE)) { + dtoOperation = profileOperationDAO.getOperation(dtoOperation.getId()); + } else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type + .POLICY)) { + + dtoOperation = policyOperationDAO.getOperation(dtoOperation.getId()); + } + if (dtoOperation != null) { operation = OperationDAOUtil.convertOperation(dtoOperation); } @@ -224,7 +256,7 @@ public class OperationManagerImpl implements OperationManager { + deviceIdentifier.getId(); log.error(errorMsg, deviceMgtException); throw new OperationManagementException(errorMsg, deviceMgtException); - } catch (OperationManagementDAOException e) { + } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving next pending operation", e); } } @@ -309,6 +341,24 @@ public class OperationManagerImpl implements OperationManager { org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO .getOperationByDeviceAndId(device.getId(), operationId); + if (dtoOperation.getType() + .equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND)) { + org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation commandOperation; + commandOperation = (org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation) commandOperationDAO + .getOperation(dtoOperation.getId()); + dtoOperation.setEnabled(commandOperation.isEnabled()); + } else if (dtoOperation.getType() + .equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) { + dtoOperation = configOperationDAO.getOperation(dtoOperation.getId()); + } else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type + .PROFILE)) { + dtoOperation = profileOperationDAO.getOperation(dtoOperation.getId()); + } else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type + .POLICY)) { + + dtoOperation = policyOperationDAO.getOperation(dtoOperation.getId()); + } + if (dtoOperation == null) { throw new OperationManagementException("Operation not found for operation Id:" + operationId + " device" + " Id:" + device.getId()); @@ -334,19 +384,30 @@ public class OperationManagerImpl implements OperationManager { try { List operations = new ArrayList(); + List dtoOperationList = + new ArrayList(); + org.wso2.carbon.device.mgt.common.Device device = deviceManagementService.getCoreDevice(identifier); if (device == null) { throw new DeviceManagementException("Device not found for device id:" + identifier.getId() + " " + "type:" + identifier.getType()); } - List dtoOperationList = - operationDAO.getOperationsByDeviceAndStatus(device.getId(), - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status - .valueOf(status.toString())); - Operation operation = null; - PolicyOperation policyOperation; + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status dtoOpStatus = org.wso2.carbon.device + .mgt.core.dto.operation.mgt.Operation.Status.valueOf(status.toString()); + dtoOperationList.addAll(commandOperationDAO.getOperationsByDeviceAndStatus(device.getId(), dtoOpStatus)); + + dtoOperationList.addAll(configOperationDAO.getOperationsByDeviceAndStatus(device.getId(), + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); + + dtoOperationList.addAll(profileOperationDAO.getOperationsByDeviceAndStatus(device.getId(), + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); + + dtoOperationList.addAll(policyOperationDAO.getOperationsByDeviceAndStatus(device.getId(), + 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); @@ -375,6 +436,25 @@ public class OperationManagerImpl implements OperationManager { if (dtoOperation == null) { throw new OperationManagementException("Operation not found for given Id:" + operationId); } + + if (dtoOperation.getType() + .equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND)) { + org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation commandOperation; + commandOperation = (org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation) commandOperationDAO + .getOperation(dtoOperation.getId()); + dtoOperation.setEnabled(commandOperation.isEnabled()); + } else if (dtoOperation.getType() + .equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) { + dtoOperation = configOperationDAO.getOperation(dtoOperation.getId()); + } else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type + .PROFILE)) { + dtoOperation = profileOperationDAO.getOperation(dtoOperation.getId()); + } else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type + .POLICY)) { + + dtoOperation = policyOperationDAO.getOperation(dtoOperation.getId()); + } + operation = OperationDAOUtil.convertOperation(dtoOperation); } catch (OperationManagementDAOException e) { String errorMsg = "Error occurred while retrieving the operation with operation Id '" + operationId; @@ -384,28 +464,6 @@ public class OperationManagerImpl implements OperationManager { return operation; } - @Override - public List getOperationsForStatus(Operation.Status status) - throws OperationManagementException { - - try { - List operations = new ArrayList(); - List dtoOperationList = - operationDAO.getOperationsForStatus( - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status - .valueOf(status.toString())); - Operation operation; - for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : dtoOperationList) { - operation = OperationDAOUtil.convertOperation(dtoOperation); - operations.add(operation); - } - return operations; - } catch (OperationManagementDAOException e) { - throw new OperationManagementException("Error occurred while retrieving the list of " + - "operations for status:'" + status.toString(), e); - } - } - private OperationDAO lookupOperationDAO(Operation operation) { if (operation instanceof CommandOperation) { 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 07572f98c7..8aa4f08c9f 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 @@ -40,8 +40,6 @@ public interface OperationDAO { List getOperationsForDevice(int deviceId) throws OperationManagementDAOException; - List getOperationsForStatus(Operation.Status status) throws OperationManagementDAOException; - Operation getNextOperation(int 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/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 f8d9ffcbfc..8b756f131e 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 @@ -18,6 +18,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.core.dto.operation.mgt.CommandOperation; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; @@ -34,6 +36,7 @@ import java.util.List; public class CommandOperationDAOImpl extends OperationDAOImpl { + private static final Log log = LogFactory.getLog(CommandOperationDAOImpl.class); @Override public int addOperation(Operation operation) throws OperationManagementDAOException { @@ -94,4 +97,79 @@ public class CommandOperationDAOImpl extends OperationDAOImpl { } } + public CommandOperation getOperation(int id) throws OperationManagementDAOException { + + PreparedStatement stmt = null; + ResultSet rs = null; + CommandOperation commandOperation = null; + + try { + Connection conn = OperationManagementDAOFactory.getConnection(); + String sql = "SELECT OPERATION_ID, ENABLED FROM DM_COMMAND_OPERATION WHERE OPERATION_ID=?"; + + stmt = conn.prepareStatement(sql); + stmt.setInt(1, id); + rs = stmt.executeQuery(); + + if (rs.next()) { + commandOperation = new CommandOperation(); + commandOperation.setEnabled(rs.getInt("ENABLED") == 0 ? false : true); + } + + } catch (SQLException e) { + String errorMsg = "SQL Error occurred while retrieving the command operation object " + "available for " + + "the id '" + + id; + log.error(errorMsg, e); + throw new OperationManagementDAOException(errorMsg, e); + } finally { + OperationManagementDAOUtil.cleanupResources(stmt, rs); + OperationManagementDAOFactory.closeConnection(); + } + return commandOperation; + } + + @Override + public List getOperationsByDeviceAndStatus(int deviceId, + Operation.Status status) throws OperationManagementDAOException { + + PreparedStatement stmt = null; + ResultSet rs = null; + Operation operation; + + List operationList = new ArrayList(); + + + try { + Connection conn = OperationManagementDAOFactory.getConnection(); + String sql = "Select co.OPERATION_ID,ENABLED from DM_COMMAND_OPERATION co "+ + "INNER JOIN "+ + "(Select * From DM_DEVICE_OPERATION_MAPPING WHERE DEVICE_ID=? "+ + "AND STATUS=? ) dm ON dm.OPERATION_ID = co.OPERATION_ID"; + + stmt = conn.prepareStatement(sql); + stmt.setInt(1, deviceId); + stmt.setString(2, status.toString()); + + rs = stmt.executeQuery(); + int operationId; + + while (rs.next()) { + + operationId = rs.getInt("ID"); + operation = super.getOperation(operationId); + operation.setEnabled(rs.getInt("ENABLED") == 0?false:true); + operationList.add(operation); + } + } 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; + } } 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 08ac0d702a..d14188c2a6 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 @@ -19,18 +19,28 @@ 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.osgi.service.component.annotations.ConfigurationPolicy; +import org.wso2.carbon.device.mgt.core.dto.operation.mgt.ConfigOperation; 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.dto.operation.mgt.PolicyOperation; 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.*; import java.sql.Connection; import java.sql.PreparedStatement; +import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; public class ConfigOperationDAOImpl extends OperationDAOImpl { + private static final Log log = LogFactory.getLog(ConfigOperationDAOImpl.class); + @Override public int addOperation(Operation operation) throws OperationManagementDAOException { @@ -38,8 +48,9 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl { PreparedStatement stmt = null; try { Connection conn = OperationManagementDAOFactory.getConnection(); - stmt = conn.prepareStatement("INSERT INTO DM_CONFIG_OPERATION(OPERATION_ID) VALUES(?)"); + stmt = conn.prepareStatement("INSERT INTO DM_CONFIG_OPERATION(OPERATION_ID, OPERATION_CONFIG) VALUES(?,?)"); stmt.setInt(1, operationId); + stmt.setObject(2, operation); stmt.executeUpdate(); } catch (SQLException e) { throw new OperationManagementDAOException("Error occurred while adding command operation", e); @@ -65,4 +76,162 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl { OperationManagementDAOUtil.cleanupResources(stmt); } } + + @Override + public void updateOperation(Operation operation) throws OperationManagementDAOException { + + PreparedStatement stmt = null; + ByteArrayOutputStream bao = null; + ObjectOutputStream oos = null; + super.updateOperation(operation); + + try { + Connection connection = OperationManagementDAOFactory.getConnection(); + stmt = connection.prepareStatement("UPDATE DM_CONFIG_OPERATION O SET O.OPERATION_CONFIG=? " + + "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 policy operation metadata", e); + } catch (IOException e) { + throw new OperationManagementDAOException("Error occurred while serializing policy 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 Operation getOperation(int operationId) throws OperationManagementDAOException { + + PreparedStatement stmt = null; + ResultSet rs = null; + ConfigOperation configOperation = null; + + ByteArrayInputStream bais; + ObjectInputStream ois; + + try { + Connection conn = OperationManagementDAOFactory.getConnection(); + String sql = "SELECT OPERATION_ID, ENABLED, OPERATION_CONFIG FROM DM_CONFIG_OPERATION WHERE OPERATION_ID=?"; + + stmt = conn.prepareStatement(sql); + stmt.setInt(1, operationId); + rs = stmt.executeQuery(); + + if (rs.next()) { + byte[] operationDetails = rs.getBytes("OPERATION_DETAILS"); + bais = new ByteArrayInputStream(operationDetails); + ois = new ObjectInputStream(bais); + configOperation = (ConfigOperation) ois.readObject(); + } + + } catch (IOException e) { + String errorMsg = "IO Error occurred while de serialize the policy 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 policy operation object"; + log.error(errorMsg, e); + throw new OperationManagementDAOException(errorMsg, e); + } catch (SQLException e) { + String errorMsg = "SQL Error occurred while retrieving the policy operation object " + "available for " + + "the id '" + + operationId; + log.error(errorMsg, e); + throw new OperationManagementDAOException(errorMsg, e); + } finally { + OperationManagementDAOUtil.cleanupResources(stmt, rs); + OperationManagementDAOFactory.closeConnection(); + } + return configOperation; + } + + @Override + public List getOperationsByDeviceAndStatus(int deviceId, + Operation.Status status) throws OperationManagementDAOException { + + PreparedStatement stmt = null; + ResultSet rs = null; + ConfigOperation configOperation; + + List operationList = new ArrayList(); + + ByteArrayInputStream bais = null; + ObjectInputStream ois = null; + + try { + Connection conn = OperationManagementDAOFactory.getConnection(); + String sql = "Select OPERATION_ID, ENABLED, OPERATION_DETAILS from DM_POLICY_OPERATION po " + + "INNER JOIN " + + "(Select * From DM_DEVICE_OPERATION_MAPPING WHERE DEVICE_ID=? " + + "AND STATUS=?) dm ON dm.OPERATION_ID = po.OPERATION_ID"; + + stmt = conn.prepareStatement(sql); + stmt.setInt(1, deviceId); + stmt.setString(2, status.toString()); + + rs = stmt.executeQuery(); + + while (rs.next()) { + byte[] operationDetails = rs.getBytes("OPERATION_DETAILS"); + bais = new ByteArrayInputStream(operationDetails); + ois = new ObjectInputStream(bais); + configOperation = (ConfigOperation) ois.readObject(); + operationList.add(configOperation); + } + + } catch (IOException e) { + String errorMsg = "IO Error occurred while de serialize the configuration 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 configuration operation object"; + log.error(errorMsg, e); + throw new OperationManagementDAOException(errorMsg, e); + } 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 { + 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(); + } + return operationList; + } } 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 bdb380bae0..a323c8761f 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 @@ -20,18 +20,11 @@ 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.core.dto.operation.mgt.Operation; -import org.wso2.carbon.device.mgt.core.dto.operation.mgt.PolicyOperation; -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; @@ -48,13 +41,12 @@ public class OperationDAOImpl implements OperationDAO { try { Connection connection = OperationManagementDAOFactory.getConnection(); stmt = connection.prepareStatement( - "INSERT INTO DM_OPERATION(TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, STATUS, OPERATION_CODE) " + - "VALUES (?, ?, ?, ?,?)"); + "INSERT INTO DM_OPERATION(TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE) " + + "VALUES (?, ?, ?, ?)"); stmt.setString(1, operation.getType().toString()); stmt.setTimestamp(2, new Timestamp(new Date().getTime())); stmt.setTimestamp(3, null); - stmt.setString(4, Operation.Status.PENDING.toString()); - stmt.setString(5, operation.getCode()); + stmt.setString(4, operation.getCode()); stmt.executeUpdate(); rs = stmt.getGeneratedKeys(); @@ -76,12 +68,11 @@ public class OperationDAOImpl implements OperationDAO { PreparedStatement stmt = null; try { Connection connection = OperationManagementDAOFactory.getConnection(); - stmt = connection.prepareStatement("UPDATE DM_OPERATION O SET O.RECEIVED_TIMESTAMP=?,O.STATUS=? " + + stmt = connection.prepareStatement("UPDATE DM_OPERATION O SET O.RECEIVED_TIMESTAMP=? " + "WHERE O.ID=?"); stmt.setTimestamp(1, new Timestamp(new Date().getTime())); - stmt.setString(2, operation.getStatus().toString()); - stmt.setInt(3, operation.getId()); + stmt.setInt(2, operation.getId()); stmt.executeUpdate(); } catch (SQLException e) { @@ -114,52 +105,28 @@ public class OperationDAOImpl implements OperationDAO { ResultSet rs = null; Operation operation = null; - ByteArrayInputStream bais; - ObjectInputStream ois; - 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, 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"; + String sql = "SELECT ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE FROM " + + "DM_OPERATION WHERE id=?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, id); rs = stmt.executeQuery(); if (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(); + 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 = 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.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString()); } + operation.setCode(rs.getString("OPERATION_CODE")); } - } 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) { String errorMsg = "SQL Error occurred while retrieving the operation object " + "available for the id '" + id; @@ -179,19 +146,13 @@ public class OperationDAOImpl implements OperationDAO { ResultSet rs = null; Operation operation = null; - ByteArrayInputStream bais; - ObjectInputStream ois; - 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, " + + String sql = "SELECT o.ID, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS, o.OPERATION_CODE " + + " 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"; + "ON o.ID = om.OPERATION_ID "; stmt = conn.prepareStatement(sql); stmt.setInt(1, operationId); @@ -200,37 +161,17 @@ public class OperationDAOImpl implements OperationDAO { rs = stmt.executeQuery(); if (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(); + 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 = 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.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString()); } + operation.setCode(rs.getString("OPERATION_CODE")); } - } 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) { String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + deviceId + "' with id '" + operationId; @@ -251,62 +192,34 @@ public class OperationDAOImpl implements OperationDAO { ResultSet rs = null; Operation operation; - 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, " + - "OPERATION_CODE FROM DM_OPERATION WHERE STATUS=?) o " + + String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE " + + "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"; + "where dm.DEVICE_ID=? and dm.STATUS=?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC"; stmt = conn.prepareStatement(sql); - stmt.setString(1, status.toString()); - stmt.setInt(2, deviceId); + stmt.setInt(1, deviceId); + stmt.setString(2, 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(); + 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 = 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")); - if (rs.getObject("ENABLED") != null) { - operation.setEnabled(rs.getBoolean("ENABLED")); - } + operation.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString()); } + 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(); @@ -327,19 +240,14 @@ public class OperationDAOImpl implements OperationDAO { ResultSet rs = null; Operation operation; - 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, " + - "OPERATION_CODE FROM DM_OPERATION) o " + + String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, " + + "OPERATION_CODE,dm.STATUS 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"; + "where dm.DEVICE_ID=?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC"; stmt = conn.prepareStatement(sql); stmt.setInt(1, deviceId); @@ -347,38 +255,19 @@ public class OperationDAOImpl implements OperationDAO { 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(); + 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 = 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.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString()); } + operation.setCode(rs.getString("OPERATION_CODE")); + operation.setStatus(Operation.Status.valueOf(rs.getString("STATUS"))); 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) { String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + deviceId + "' with status '"; @@ -392,145 +281,95 @@ public class OperationDAOImpl implements OperationDAO { } @Override - public List getOperationsForStatus(Operation.Status status) - throws OperationManagementDAOException { + public Operation getNextOperation(int deviceId) throws OperationManagementDAOException { PreparedStatement stmt = null; ResultSet rs = null; - Operation operation; - - ByteArrayInputStream byteArrayInputStream; - 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,"+ - "OPERATION_CODE 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"; + Connection connection = OperationManagementDAOFactory.getConnection(); + stmt = connection.prepareStatement("SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP " + + "OPERATION_CODE FROM DM_OPERATION o " + + "INNER JOIN (Select * from DM_DEVICE_OPERATION_MAPPING dm " + + "where dm.DEVICE_ID=? AND dm.STATUS=?) om ON o.ID = om.OPERATION_ID " + + "ORDER BY o.CREATED_TIMESTAMP ASC LIMIT 1"); + + stmt.setInt(1, deviceId); + stmt.setString(2, Operation.Status.PENDING.toString()); - stmt = conn.prepareStatement(sql); - stmt.setString(1, status.toString()); rs = stmt.executeQuery(); + Operation operation = null; - while (rs.next()) { - if (rs.getBytes("OPERATION_DETAILS") != null) { - byte[] operationDetails; - operationDetails = rs.getBytes("OPERATION_DETAILS"); - byteArrayInputStream = new ByteArrayInputStream(operationDetails); - ois = new ObjectInputStream(byteArrayInputStream); - operation = (ProfileOperation) ois.readObject(); + if (rs.next()) { + operation = new Operation(); + operation.setType(this.getType(rs.getString("TYPE"))); + operation.setStatus(this.getStatus(rs.getString("STATUS"))); + operation.setId(rs.getInt("ID")); + operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString()); + if (rs.getTimestamp("RECEIVED_TIMESTAMP") == null) { + operation.setReceivedTimeStamp(""); } 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")); + operation.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString()); } - operationList.add(operation); + operation.setCode(rs.getString("OPERATION_CODE")); } - } 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); + return operation; } 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); + throw new OperationManagementDAOException("Error occurred while adding operation metadata", e); } finally { OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOFactory.closeConnection(); } - return operationList; } - @Override - public Operation getNextOperation(int deviceId) throws OperationManagementDAOException { + + public List getOperationsByDeviceStatusAndType(int deviceId, + Operation.Status status,Operation.Type type) throws OperationManagementDAOException { PreparedStatement stmt = null; ResultSet rs = null; + Operation operation; - ByteArrayInputStream bais; - ObjectInputStream ois; + List operationList = new ArrayList(); try { - Connection connection = OperationManagementDAOFactory.getConnection(); - stmt = connection.prepareStatement( - "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 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 " + - "ORDER BY o.CREATED_TIMESTAMP ASC LIMIT 1"); - - stmt.setString(1, Operation.Status.PENDING.toString()); + Connection conn = OperationManagementDAOFactory.getConnection(); + String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE FROM "+ + "(SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE " + + "FROM DM_OPERATION o WHERE o.TYPE=?) o " + + "INNER JOIN (Select * from DM_DEVICE_OPERATION_MAPPING dm " + + "where dm.DEVICE_ID=? and dm.STATUS=?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC"; + + stmt = conn.prepareStatement(sql); + stmt.setString(1, type.toString()); stmt.setInt(2, deviceId); + stmt.setString(3, status.toString()); rs = stmt.executeQuery(); - Operation operation = null; - if (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(); + while (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 = new Operation(); - operation.setType(this.getType(rs.getString("TYPE"))); - operation.setStatus(this.getStatus(rs.getString("STATUS"))); - operation.setId(rs.getInt("ID")); - operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString()); - if (rs.getTimestamp("RECEIVED_TIMESTAMP") == null) { - operation.setReceivedTimeStamp(""); - } else { - operation.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString()); - } - operation.setCode(rs.getString("OPERATION_CODE")); - if (rs.getObject("ENABLED") != null) { - operation.setEnabled(rs.getBoolean("ENABLED")); - } + operation.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString()); } + operation.setCode(rs.getString("OPERATION_CODE")); + operationList.add(operation); } - return operation; - } catch (IOException ex) { - String errorMsg = "IO error occurred while de serializing the next 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; + } 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, ex); - - }catch (SQLException e) { - throw new OperationManagementDAOException("Error occurred while adding operation metadata", e); + throw new OperationManagementDAOException(errorMsg, e); } finally { OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOFactory.closeConnection(); } + return operationList; } private Operation.Status getStatus(String status) { 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..96a1b6123f 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 @@ -18,6 +18,7 @@ */ package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl; +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; @@ -34,10 +35,11 @@ public class OperationMappingDAOImpl implements OperationMappingDAO { PreparedStatement stmt = null; try { Connection conn = OperationManagementDAOFactory.getConnection(); - String sql = "INSERT INTO DM_DEVICE_OPERATION_MAPPING(DEVICE_ID, OPERATION_ID) VALUES(?, ?)"; + String sql = "INSERT INTO DM_DEVICE_OPERATION_MAPPING(DEVICE_ID, OPERATION_ID,STATUS) VALUES(?, ?,?)"; stmt = conn.prepareStatement(sql); stmt.setInt(1, deviceId); stmt.setInt(2, operationId); + stmt.setString(3, Operation.Status.PENDING.toString()); stmt.executeUpdate(); } catch (SQLException e) { throw new OperationManagementDAOException("Error occurred while persisting device operation mappings", 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/PolicyOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/PolicyOperationDAOImpl.java new file mode 100644 index 0000000000..591af16ce8 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/PolicyOperationDAOImpl.java @@ -0,0 +1,240 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.core.dto.operation.mgt.PolicyOperation; +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; + +import java.io.*; +import java.sql.*; +import java.util.ArrayList; +import java.util.List; + +public class PolicyOperationDAOImpl extends OperationDAOImpl { + + private static final Log log = LogFactory.getLog(PolicyOperationDAOImpl.class); + + @Override + 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); + PolicyOperation policyOperation = (PolicyOperation) operation; + Connection conn = OperationManagementDAOFactory.getConnection(); + + PreparedStatement stmt = null; + + try { + stmt = conn.prepareStatement("INSERT INTO DM_POLICY_OPERATION(OPERATION_ID, OPERATION_DETAILS) " + + "VALUES(?, ?)"); + stmt.setInt(1, operationId); + stmt.setObject(2, policyOperation); + stmt.executeUpdate(); + } catch (SQLException e) { + throw new OperationManagementDAOException("Error occurred while adding policy operation", e); + } finally { + OperationManagementDAOUtil.cleanupResources(stmt); + } + return operationId; + + } + + @Override + public void updateOperation(Operation operation) throws OperationManagementDAOException { + + PreparedStatement stmt = null; + ByteArrayOutputStream bao = null; + ObjectOutputStream oos = null; + super.updateOperation(operation); + + try { + Connection connection = OperationManagementDAOFactory.getConnection(); + stmt = connection.prepareStatement("UPDATE DM_POLICY_OPERATION O SET O.OPERATION_DETAILS=? " + + "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 policy operation metadata", e); + } catch (IOException e) { + throw new OperationManagementDAOException("Error occurred while serializing policy 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 operationId) throws OperationManagementDAOException { + + super.deleteOperation(operationId); + PreparedStatement stmt = null; + try { + Connection connection = OperationManagementDAOFactory.getConnection(); + stmt = connection.prepareStatement("DELETE DM_POLICY_OPERATION WHERE OPERATION_ID=?"); + stmt.setInt(1, operationId); + stmt.executeUpdate(); + } catch (SQLException e) { + throw new OperationManagementDAOException("Error occurred while deleting operation metadata", e); + } finally { + OperationManagementDAOUtil.cleanupResources(stmt); + } + } + + @Override + public Operation getOperation(int operationId) throws OperationManagementDAOException { + + PreparedStatement stmt = null; + ResultSet rs = null; + PolicyOperation policyOperation = null; + + ByteArrayInputStream bais; + ObjectInputStream ois; + + try { + Connection conn = OperationManagementDAOFactory.getConnection(); + String sql = "SELECT OPERATION_ID, ENABLED, OPERATION_DETAILS FROM DM_POLICY_OPERATION WHERE OPERATION_ID=?"; + + stmt = conn.prepareStatement(sql); + stmt.setInt(1, operationId); + rs = stmt.executeQuery(); + + if (rs.next()) { + byte[] operationDetails = rs.getBytes("OPERATION_DETAILS"); + bais = new ByteArrayInputStream(operationDetails); + ois = new ObjectInputStream(bais); + policyOperation = (PolicyOperation) ois.readObject(); + } + + } catch (IOException e) { + String errorMsg = "IO Error occurred while de serialize the policy 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 policy operation object"; + log.error(errorMsg, e); + throw new OperationManagementDAOException(errorMsg, e); + } catch (SQLException e) { + String errorMsg = "SQL Error occurred while retrieving the policy operation object " + "available for " + + "the id '" + + operationId; + log.error(errorMsg, e); + throw new OperationManagementDAOException(errorMsg, e); + } finally { + OperationManagementDAOUtil.cleanupResources(stmt, rs); + OperationManagementDAOFactory.closeConnection(); + } + return policyOperation; + } + + @Override + public List getOperationsByDeviceAndStatus(int deviceId, + Operation.Status status) throws OperationManagementDAOException { + + PreparedStatement stmt = null; + ResultSet rs = null; + PolicyOperation policyOperation; + + List operationList = new ArrayList(); + + ByteArrayInputStream bais = null; + ObjectInputStream ois = null; + + try { + Connection conn = OperationManagementDAOFactory.getConnection(); + String sql = "Select OPERATION_ID, ENABLED, OPERATION_DETAILS from DM_POLICY_OPERATION po " + + "INNER JOIN " + + "(Select * From DM_DEVICE_OPERATION_MAPPING WHERE DEVICE_ID=? " + + "AND STATUS=?) dm ON dm.OPERATION_ID = po.OPERATION_ID"; + + stmt = conn.prepareStatement(sql); + stmt.setInt(1, deviceId); + stmt.setString(2, status.toString()); + + rs = stmt.executeQuery(); + + while (rs.next()) { + byte[] operationDetails = rs.getBytes("OPERATION_DETAILS"); + bais = new ByteArrayInputStream(operationDetails); + ois = new ObjectInputStream(bais); + policyOperation = (PolicyOperation) ois.readObject(); + operationList.add(policyOperation); + } + + } 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) { + 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 { + 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(); + } + return operationList; + } +} 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 f23e77eafa..036b4131cd 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 @@ -20,7 +20,6 @@ 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.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; @@ -29,6 +28,8 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOU import java.io.*; import java.sql.*; +import java.util.ArrayList; +import java.util.List; public class ProfileOperationDAOImpl extends OperationDAOImpl { @@ -59,67 +60,6 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl { return operationId; } - public Operation getNextOperation(DeviceIdentifier deviceId) throws OperationManagementDAOException { - - PreparedStatement stmt = null; - ResultSet rs = null; - ByteArrayInputStream bais = null; - ObjectInputStream ois = null; - - try { - Connection connection = OperationManagementDAOFactory.getConnection(); - stmt = connection.prepareStatement( - "SELECT po.OPERATION_DETAILS AS OPERATION_DETAILS " + - "FROM DM_OPERATION o " + - "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(2, deviceId.getType()); - stmt.setString(3, deviceId.getId()); - rs = stmt.executeQuery(); - - byte[] operationDetails = new byte[0]; - if (rs.next()) { - operationDetails = rs.getBytes("OPERATION_DETAILS"); - } - bais = new ByteArrayInputStream(operationDetails); - ois = new ObjectInputStream(bais); - return (ProfileOperation) ois.readObject(); - } catch (SQLException e) { - log.error("SQL error occurred while retrieving profile operation", e); - throw new OperationManagementDAOException("Error occurred while adding operation metadata", e); - } catch (ClassNotFoundException e) { - log.error("Class not found error occurred while retrieving profile operation", e); - throw new OperationManagementDAOException("Error occurred while casting retrieved payload as a " + - "ProfileOperation object", e); - } catch (IOException e) { - log.error("IO error occurred while de serialize profile operation", e); - throw new OperationManagementDAOException("Error occurred while serializing profile operation 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 void updateOperation(Operation operation) throws OperationManagementDAOException { @@ -180,4 +120,117 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl { OperationManagementDAOUtil.cleanupResources(stmt); } } + + public Operation getOperation(int id) throws OperationManagementDAOException { + + PreparedStatement stmt = null; + ResultSet rs = null; + ProfileOperation profileOperation = null; + + ByteArrayInputStream bais; + ObjectInputStream ois; + + try { + Connection conn = OperationManagementDAOFactory.getConnection(); + String sql = "SELECT OPERATION_ID, ENABLED, OPERATION_DETAILS FROM DM_PROFILE_OPERATION WHERE OPERATION_ID=?"; + + stmt = conn.prepareStatement(sql); + stmt.setInt(1, id); + rs = stmt.executeQuery(); + + if (rs.next()) { + byte[] operationDetails = rs.getBytes("OPERATION_DETAILS"); + bais = new ByteArrayInputStream(operationDetails); + ois = new ObjectInputStream(bais); + profileOperation = (ProfileOperation) ois.readObject(); + } + + } 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) { + String errorMsg = "SQL Error occurred while retrieving the command operation object " + "available for " + + "the id '" + + id; + log.error(errorMsg, e); + throw new OperationManagementDAOException(errorMsg, e); + } finally { + OperationManagementDAOUtil.cleanupResources(stmt, rs); + OperationManagementDAOFactory.closeConnection(); + } + return profileOperation; + } + + @Override + public List getOperationsByDeviceAndStatus(int deviceId, + Operation.Status status) throws OperationManagementDAOException { + + PreparedStatement stmt = null; + ResultSet rs = null; + ProfileOperation profileOperation; + + List operationList = new ArrayList(); + + ByteArrayInputStream bais = null; + ObjectInputStream ois = null; + + try { + Connection conn = OperationManagementDAOFactory.getConnection(); + String sql = "Select OPERATION_ID, ENABLED, OPERATION_DETAILS from DM_PROFILE_OPERATION po " + + "INNER JOIN " + + "(Select * From DM_DEVICE_OPERATION_MAPPING WHERE DEVICE_ID=? " + + "AND STATUS=?) dm ON dm.OPERATION_ID = po.OPERATION_ID"; + + stmt = conn.prepareStatement(sql); + stmt.setInt(1, deviceId); + stmt.setString(2, status.toString()); + + rs = stmt.executeQuery(); + + while (rs.next()) { + byte[] operationDetails = rs.getBytes("OPERATION_DETAILS"); + bais = new ByteArrayInputStream(operationDetails); + ois = new ObjectInputStream(bais); + profileOperation = (ProfileOperation) ois.readObject(); + operationList.add(profileOperation); + } + + } 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) { + 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 { + 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(); + } + return operationList; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/util/OperationDAOUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/util/OperationDAOUtil.java index 8cabc6d3bd..1f5d31ced4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/util/OperationDAOUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/util/OperationDAOUtil.java @@ -56,6 +56,7 @@ public class OperationDAOUtil { dtoOperation.setId(operation.getId()); dtoOperation.setPayLoad(operation.getPayLoad()); dtoOperation.setReceivedTimeStamp(operation.getReceivedTimeStamp()); + dtoOperation.setProperties(operation.getProperties()); return dtoOperation; } @@ -82,6 +83,7 @@ public class OperationDAOUtil { operation.setPayLoad(dtoOperation.getPayLoad()); operation.setReceivedTimeStamp(dtoOperation.getReceivedTimeStamp()); operation.setEnabled(dtoOperation.isEnabled()); + operation.setProperties(dtoOperation.getProperties()); return operation; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/util/OperationCreateTimeComparator.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/util/OperationCreateTimeComparator.java new file mode 100644 index 0000000000..2e8d633edf --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/util/OperationCreateTimeComparator.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.mgt.core.operation.mgt.util; + +import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; +import java.util.Comparator; + +public class OperationCreateTimeComparator implements Comparator{ + + @Override + public int compare(Operation o1, Operation o2) { + long createdTime1 = java.sql.Timestamp.valueOf(o1.getCreatedTimeStamp()).getTime(); + long createdTime2 = java.sql.Timestamp.valueOf(o1.getCreatedTimeStamp()).getTime(); + return (int) (createdTime1 - createdTime2); + } + + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java index 0a5a9d4c9f..03e50611cb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java @@ -178,11 +178,6 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getOperation(operationId); } - @Override - public List getOperationsForStatus(Operation.Status status) - throws OperationManagementException { - return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getOperationsForStatus(status); - } @Override public void sendEnrolmentInvitation(EmailMessageProperties emailMessageProperties) 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 83a5410439..0093de2bfa 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 @@ -59,6 +59,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_OPERATION_MAPPING ( ID INTEGER AUTO_INCREMENT NOT NULL, DEVICE_ID INTEGER NOT NULL, OPERATION_ID INTEGER NOT NULL, + STATUS VARCHAR(50) NULL, PRIMARY KEY (ID), CONSTRAINT fk_dm_device_operation_mapping_device FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (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 bc58da9eac..c8d332cde4 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 @@ -26,13 +26,13 @@ CREATE TABLE IF NOT EXISTS DM_OPERATION ( TYPE VARCHAR(50) NOT NULL, CREATED_TIMESTAMP TIMESTAMP NOT NULL, RECEIVED_TIMESTAMP TIMESTAMP NULL, - STATUS VARCHAR(50) NULL, OPERATION_CODE VARCHAR(1000) NOT NULL, PRIMARY KEY (ID) ); CREATE TABLE IF NOT EXISTS DM_CONFIG_OPERATION ( OPERATION_ID INTEGER NOT NULL, + OPERATION_CONFIG BLOB DEFAULT NULL, PRIMARY KEY (OPERATION_ID), CONSTRAINT fk_dm_operation_config FOREIGN KEY (OPERATION_ID) REFERENCES DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION @@ -46,6 +46,15 @@ CREATE TABLE IF NOT EXISTS DM_COMMAND_OPERATION ( DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); +CREATE TABLE IF NOT EXISTS DM_POLICY_OPERATION ( + OPERATION_ID INTEGER NOT NULL, + ENABLED INTEGER NOT NULL DEFAULT 0, + OPERATION_DETAILS BLOB DEFAULT NULL, + PRIMARY KEY (OPERATION_ID), + CONSTRAINT fk_dm_operation_policy FOREIGN KEY (OPERATION_ID) REFERENCES + DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION +); + CREATE TABLE IF NOT EXISTS DM_PROFILE_OPERATION ( OPERATION_ID INTEGER NOT NULL, ENABLED INTEGER NOT NULL DEFAULT 0, @@ -59,6 +68,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_OPERATION_MAPPING ( ID INTEGER AUTO_INCREMENT NOT NULL, DEVICE_ID INTEGER NOT NULL, OPERATION_ID INTEGER NOT NULL, + STATUS VARCHAR(50) NULL, PRIMARY KEY (ID), CONSTRAINT fk_dm_device_operation_mapping_device FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, diff --git a/features/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework.server.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework.server.feature/src/main/resources/dbscripts/cdm/h2.sql index 561e4a635c..269942edf9 100644 --- a/features/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework.server.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework.server.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -26,7 +26,6 @@ CREATE TABLE IF NOT EXISTS DM_OPERATION ( TYPE VARCHAR(50) NOT NULL, CREATED_TIMESTAMP TIMESTAMP NOT NULL, RECEIVED_TIMESTAMP TIMESTAMP NULL, - STATUS VARCHAR(50) NULL, PRIMARY KEY (ID) ); @@ -45,10 +44,29 @@ CREATE TABLE IF NOT EXISTS DM_COMMAND_OPERATION ( DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); +CREATE TABLE IF NOT EXISTS DM_PROFILE_OPERATION ( + OPERATION_ID INTEGER 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 +); + +CREATE TABLE IF NOT EXISTS DM_POLICY_OPERATION ( + OPERATION_ID INTEGER NOT NULL, + ENABLED INTEGER NOT NULL DEFAULT 0, + OPERATION_DETAILS BLOB DEFAULT NULL, + PRIMARY KEY (OPERATION_ID), + CONSTRAINT fk_dm_operation_policy FOREIGN KEY (OPERATION_ID) REFERENCES + DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION +); + CREATE TABLE IF NOT EXISTS DM_DEVICE_OPERATION_MAPPING ( ID INTEGER AUTO_INCREMENT NOT NULL, DEVICE_ID INTEGER NOT NULL, OPERATION_ID INTEGER NOT NULL, + STATUS VARCHAR(50) NULL, PRIMARY KEY (ID), CONSTRAINT fk_dm_device_operation_mapping_device FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION,