From 5f7e4306de3167c9cfbbf511f4eab91e02744aab Mon Sep 17 00:00:00 2001 From: prabathabey Date: Mon, 30 Mar 2015 11:10:07 +0530 Subject: [PATCH] Adding support to retrieve the next available operation to be installed --- .../app/mgt/AppManagementServiceImpl.java | 4 +- .../operation/mgt/OperationManagerImpl.java | 4 +- .../mgt/dao/impl/OperationDAOImpl.java | 1 + .../mgt/dao/impl/ProfileOperationDAOImpl.java | 37 ++++++++++++++++++- 4 files changed, 39 insertions(+), 7 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/AppManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/AppManagementServiceImpl.java index e6a17fa5b7..fb653337ba 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/AppManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/AppManagementServiceImpl.java @@ -27,8 +27,7 @@ public class AppManagementServiceImpl implements AppManagerConnector { @Override public Application[] getApplicationList(String domain, int pageNumber, int size) throws AppManagerConnectorException { - return DeviceManagementDataHolder.getInstance().getAppManager() - .getApplicationList(domain, pageNumber, size); + return DeviceManagementDataHolder.getInstance().getAppManager().getApplicationList(domain, pageNumber, size); } @Override @@ -48,4 +47,5 @@ public class AppManagementServiceImpl implements AppManagerConnector { public Credential getClientCredentials() throws AppManagerConnectorException { return DeviceManagementDataHolder.getInstance().getAppManager().getClientCredentials(); } + } 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 1e533ba592..f6719a22a2 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 @@ -104,9 +104,7 @@ public class OperationManagerImpl implements OperationManager { public Operation getNextPendingOperation(DeviceIdentifier deviceId) throws OperationManagementException { try { OperationManagementDAOFactory.beginTransaction(); - - operationDAO.getNextOperation(deviceId); - + profileOperationDAO.getNextOperation(deviceId); OperationManagementDAOFactory.commitTransaction(); return null; } catch (OperationManagementDAOException 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/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 8a16d533fa..aaa00252ff 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,6 +20,7 @@ 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.ProfileOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.*; import javax.sql.DataSource; 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 4210b3bb6e..d573fc4f9f 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 @@ -107,7 +107,6 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl { if (rs.next()) { payload = rs.getBytes("PAYLOAD"); } - bais = new ByteArrayInputStream(payload); ois = new ObjectInputStream(bais); return (ProfileOperation) ois.readObject(); @@ -149,7 +148,41 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl { @Override public Operation getNextOperation(DeviceIdentifier deviceId) throws OperationManagementDAOException { - return null; + PreparedStatement stmt = null; + ResultSet rs = null; + ByteArrayInputStream bais; + ObjectInputStream ois; + try { + Connection connection = OperationManagementDAOFactory.getConnection(); + stmt = connection.prepareStatement( + "SELECT o.ID AS OPERATION_ID, o.CREATED_TIMESTAMP AS CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP AS " + + "RECEIVED_TIMESTAMP, po.PAYLOAD AS PAYLOAD FROM DM_OPERATION o " + + "INNER JOIN DM_PROFILE_OPERATION po ON o.ID = po.OPERATION_ID AND o.ID IN (" + + "SELECT dom.OPERATION_ID FROM (SELECT d.ID FROM DM_DEVICE d INNER JOIN " + + "DM_DEVICE_TYPE dm ON d.DEVICE_TYPE_ID = dm.ID AND dm.NAME = ? AND " + + "d.DEVICE_IDENTIFICATION = ?) d1 INNER JOIN DM_DEVICE_OPERATION_MAPPING dom " + + "ON d1.ID = dom.DEVICE_ID) ORDER BY o.CREATED_TIMESTAMP ASC LIMIT 1;"); + stmt.setString(1, deviceId.getType()); + stmt.setString(2, deviceId.getId()); + rs = stmt.executeQuery(); + + byte[] payload = new byte[0]; + if (rs.next()) { + payload = rs.getBytes("PAYLOAD"); + } + bais = new ByteArrayInputStream(payload); + ois = new ObjectInputStream(bais); + return (ProfileOperation) ois.readObject(); + } catch (SQLException e) { + throw new OperationManagementDAOException("Error occurred while adding operation metadata", e); + } catch (ClassNotFoundException e) { + throw new OperationManagementDAOException("Error occurred while casting retrieved payload as a " + + "ProfileOperation object", e); + } catch (IOException e) { + throw new OperationManagementDAOException("Error occurred while serializing profile operation object", e); + } finally { + OperationManagementDAOUtil.cleanupResources(stmt, rs); + } } }