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 0ce8dde4a04..450fd923a39 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 @@ -26,17 +26,17 @@ import java.util.Properties; public class Operation { public enum Type { - CONFIG, MESSAGE, INFO, COMMAND + CONFIG, MESSAGE, INFO, COMMAND, PROFILE } public enum Status { - IN_PROGRES, PENDING, COMPLETED, ERROR + IN_PROGRESS, PENDING, COMPLETED, ERROR } private String code; private Properties properties; private Type type; - private Long operationId; + private int id; private String payLoad; private Status status; @@ -67,12 +67,12 @@ public class Operation { this.type = type; } - public Long getOperationId() { - return operationId; + public int getId() { + return id; } - public void setOperationId(Long operationId) { - this.operationId = operationId; + public void setId(int id) { + this.id = id; } public String getPayLoad() { @@ -83,11 +83,11 @@ public class Operation { this.payLoad = payLoad; } - public Status getOperationStates() { + public Status getStatus() { return status; } - public void setOperationStates(Status status) { + public void setStatus(Status status) { this.status = status; } 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 ba36f1eb6c7..0b6f6e0cd69 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 @@ -35,7 +35,7 @@ public interface OperationManager { * operation */ public boolean addOperation(Operation operation, - List devices) throws OperationManagementException; + List devices) throws OperationManagementException; /** * Method to retrieve the list of all operations to a device. @@ -44,7 +44,7 @@ public interface OperationManager { * @throws OperationManagementException If some unusual behaviour is observed while fetching the * operation list. */ - public List getOperations(DeviceIdentifier deviceId) throws OperationManagementException; + public List getOperations(DeviceIdentifier deviceId) throws OperationManagementException; /** * Method to retrieve the list of available operations to a device. @@ -53,11 +53,12 @@ public interface OperationManager { * @throws OperationManagementException If some unusual behaviour is observed while fetching the * operation list. */ - public List getPendingOperations(DeviceIdentifier deviceId) throws OperationManagementException; + public List getPendingOperations( + DeviceIdentifier deviceId) throws OperationManagementException; public Operation getNextPendingOperation(DeviceIdentifier deviceId) throws OperationManagementException; - public Operation updateOperation(Long operationId, DeviceIdentifier deviceIdentifier, - String payLoad) throws OperationManagementException; + public Operation updateOperation( + int id, DeviceIdentifier deviceIdentifier, String payLoad) 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 65508d0fa44..bfdce769452 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 @@ -346,12 +346,12 @@ public class DeviceManagementServiceProviderImpl implements DeviceManagementServ } @Override - public List getOperations(DeviceIdentifier deviceId) throws OperationManagementException { + public List getOperations(DeviceIdentifier deviceId) throws OperationManagementException { return operationManager.getOperations(deviceId); } @Override - public List getPendingOperations(DeviceIdentifier deviceId) throws OperationManagementException { + public List getPendingOperations(DeviceIdentifier deviceId) throws OperationManagementException { return operationManager.getPendingOperations(deviceId); } @@ -361,8 +361,8 @@ public class DeviceManagementServiceProviderImpl implements DeviceManagementServ } @Override - public Operation updateOperation(Long operationId, DeviceIdentifier deviceIdentifier, - String responsePayLoad) throws OperationManagementException { + public Operation updateOperation(int id, DeviceIdentifier deviceIdentifier, + String payLoad) throws OperationManagementException { return null; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java index 92f1a92d99b..9b0331dcac3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java @@ -31,9 +31,11 @@ import org.wso2.carbon.device.mgt.common.spi.DeviceManager; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.DeviceManagementRepository; import org.wso2.carbon.device.mgt.core.DeviceManagementServiceProviderImpl; +import org.wso2.carbon.device.mgt.core.api.mgt.APIConfig; import org.wso2.carbon.device.mgt.core.api.mgt.APIPublisherService; import org.wso2.carbon.device.mgt.core.api.mgt.APIPublisherServiceImpl; import org.wso2.carbon.device.mgt.core.api.mgt.APIRegistrationStartupObserver; +import org.wso2.carbon.device.mgt.core.api.mgt.config.APIPublisherConfig; import org.wso2.carbon.device.mgt.core.app.mgt.AppManagerConnectorException; import org.wso2.carbon.device.mgt.core.app.mgt.RemoteAppManagerConnector; import org.wso2.carbon.device.mgt.core.app.mgt.RemoteAppManagerConnector; @@ -156,6 +158,10 @@ public class DeviceManagementServiceComponent { } } + protected void deactivate(ComponentContext componentContext) { + //do nothing + } + private void initLicenseManager() throws LicenseManagementException { LicenseConfigurationManager.getInstance().initConfig(); LicenseConfig licenseConfig = @@ -175,9 +181,11 @@ public class DeviceManagementServiceComponent { DeviceManagementDataHolder.getInstance().setAppManager(appManager); } - protected void deactivate(ComponentContext componentContext) { - //do nothing - } +// private void initAPIProviders() throws DeviceManagementException { +// for (APIConfig config : APIPublisherConfig.getInstance().getApiConfigs()) { +// config.init(); +// } +// } private void registerServices(ComponentContext componentContext) { if (log.isDebugEnabled()) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/CommandOperation.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/CommandOperation.java index a971ab9d791..852142c1b87 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/CommandOperation.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/CommandOperation.java @@ -32,4 +32,8 @@ public class CommandOperation extends Operation { this.enabled = enabled; } + public Type getType() { + return Type.COMMAND; + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/ConfigOperation.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/ConfigOperation.java index 948902d1bf8..8e12a2ab9a7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/ConfigOperation.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/ConfigOperation.java @@ -75,4 +75,8 @@ public class ConfigOperation extends Operation { } } + public Type getType() { + return Type.CONFIG; + } + } 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 f6719a22a29..f4dae501676 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 @@ -33,6 +33,7 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOE import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationMappingDAO; +import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -91,35 +92,74 @@ public class OperationManagerImpl implements OperationManager { } @Override - public List getOperations(DeviceIdentifier deviceId) throws OperationManagementException { - return null; + 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) { + try { + OperationManagementDAOFactory.rollbackTransaction(); + } catch (OperationManagementDAOException e1) { + log.warn("Error occurred while roll-backing the transaction", e1); + } + throw new OperationManagementException("Error occurred while retrieving the list of " + + "operations assigned for '" + deviceId.getType() + "' device '" + deviceId.getId() + "'", e); + } } @Override - public List getPendingOperations(DeviceIdentifier deviceId) throws OperationManagementException { - return null; + public List getPendingOperations( + DeviceIdentifier deviceId) throws OperationManagementException { + try { + List operations = new ArrayList(); + + OperationManagementDAOFactory.beginTransaction(); + operations.addAll(profileOperationDAO.getOperations(deviceId, Operation.Status.PENDING)); + operations.addAll(configOperationDAO.getOperations(deviceId, Operation.Status.PENDING)); + operations.addAll(commandOperationDAO.getOperations(deviceId, Operation.Status.PENDING)); + OperationManagementDAOFactory.commitTransaction(); + + return operations; + } catch (OperationManagementDAOException e) { + try { + OperationManagementDAOFactory.rollbackTransaction(); + } catch (OperationManagementDAOException e1) { + log.warn("Error occurred while roll-backing the transaction", e1); + } + 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 { OperationManagementDAOFactory.beginTransaction(); - profileOperationDAO.getNextOperation(deviceId); + Operation operation = operationDAO.getNextOperation(deviceId); + operation = this.lookupOperationDAO(operation.getType()).getOperation(operation.getId()); OperationManagementDAOFactory.commitTransaction(); - return null; + return operation; } catch (OperationManagementDAOException e) { try { OperationManagementDAOFactory.rollbackTransaction(); } catch (OperationManagementDAOException e1) { log.warn("Error occurred while roll-backing the transaction", e1); } - throw new OperationManagementException("Error occurred while adding operation", e); + throw new OperationManagementException("Error occurred while retrieving next pending operation", e); } } @Override - public Operation updateOperation(Long operationId, DeviceIdentifier deviceIdentifier, - String responsePayLoad) throws OperationManagementException { + public Operation updateOperation(int id, DeviceIdentifier deviceIdentifier, + String responsePayLoad) throws OperationManagementException { return null; } @@ -133,4 +173,17 @@ public class OperationManagerImpl implements OperationManager { } } + private OperationDAO lookupOperationDAO(Operation.Type type) { + switch (type) { + 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/ProfileOperation.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/ProfileOperation.java index 7138dce8da5..89a9a33e5bb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/ProfileOperation.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/ProfileOperation.java @@ -32,4 +32,8 @@ public class ProfileOperation extends ConfigOperation implements Serializable { this.payload = payload; } + public Type getType() { + return Type.PROFILE; + } + } 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 6a958c0cb60..c0543e66b35 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 @@ -35,9 +35,14 @@ public interface OperationDAO { Operation getOperation(DeviceIdentifier deviceId, int operationId) throws OperationManagementDAOException; - List getOperations(DeviceIdentifier deviceId) throws OperationManagementDAOException; + Operation getOperation(DeviceIdentifier deviceId, Operation.Status status) throws OperationManagementDAOException; - List getOperations(String status) throws OperationManagementDAOException; + List getOperations(DeviceIdentifier deviceId) throws OperationManagementDAOException; + + List getOperations(DeviceIdentifier deviceId, + Operation.Status status) throws OperationManagementDAOException; + + List getOperations(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/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 781da88c7e7..f36d92a3f58 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 @@ -29,6 +29,7 @@ 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 CommandOperationDAOImpl extends OperationDAOImpl { @@ -37,11 +38,11 @@ public class CommandOperationDAOImpl extends OperationDAOImpl { public int addOperation(Operation operation) throws OperationManagementDAOException { int operationId = super.addOperation(operation); CommandOperation commandOp = (CommandOperation) operation; - Connection conn = OperationManagementDAOFactory.getConnection(); PreparedStatement stmt = null; ResultSet rs = null; try { + 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()); @@ -54,39 +55,71 @@ public class CommandOperationDAOImpl extends OperationDAOImpl { return operationId; } - @Override - public int updateOperation(Operation operation) throws OperationManagementDAOException { - return 0; - } - - @Override - public int deleteOperation(int id) throws OperationManagementDAOException { - return 0; - } + public List getOperations(DeviceIdentifier deviceId, + Operation.Status status) throws OperationManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; - @Override - public Operation getOperation(int id) throws OperationManagementDAOException { - return null; - } + try { + Connection conn = OperationManagementDAOFactory.getConnection(); + String sql = "SELECT po.OPERATION_ID, po.TYPE, po.CREATED_TIMESTAMP, po.RECEIVED_TIMESTAMP, po.STATUS, " + + "co.ENABLED FROM DM_COMMAND_OPERATION co INNER JOIN (SELECT o.ID AS OPERATION_ID, o.TYPE, " + + "o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS FROM DM_OPERATION o INNER JOIN (" + + "SELECT dom.OPERATION_ID AS OP_ID FROM (SELECT d.ID FROM DM_DEVICE d INNER JOIN " + + "DM_DEVICE_TYPE dm ON d.DEVICE_TYPE_ID = dm.ID AND dm.NAME = ? AND " + + "d.DEVICE_IDENTIFICATION = ?) d1 INNER JOIN DM_DEVICE_OPERATION_MAPPING dom ON d1.ID = " + + "dom.DEVICE_ID) ois ON o.ID = ois.OP_ID AND o.STATUS = ? ORDER BY " + + "o.CREATED_TIMESTAMP ASC) po ON co.OPERATION_ID = po.OPERATION_ID"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, deviceId.getType()); + stmt.setString(1, deviceId.getId()); + stmt.setString(1, status.toString()); + } 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); + } - @Override - public Operation getOperation(DeviceIdentifier deviceId, int operationId) throws OperationManagementDAOException { return null; } - @Override - public List getOperations(DeviceIdentifier deviceId) throws OperationManagementDAOException { - return null; - } + public List getOperations(DeviceIdentifier deviceId) throws OperationManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; - @Override - public List getOperations(String status) throws OperationManagementDAOException { - return null; - } + try { + Connection conn = OperationManagementDAOFactory.getConnection(); + String sql = "SELECT po.OPERATION_ID, po.TYPE, po.CREATED_TIMESTAMP, po.RECEIVED_TIMESTAMP, po.STATUS, " + + "co.ENABLED FROM DM_COMMAND_OPERATION co INNER JOIN (SELECT o.ID AS OPERATION_ID, o.TYPE, " + + "o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS FROM DM_OPERATION o INNER JOIN (" + + "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(1, deviceId.getId()); + rs = stmt.executeQuery(); - @Override - public Operation getNextOperation(DeviceIdentifier deviceId) throws OperationManagementDAOException { - return null; + 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.setStatus(Operation.Status.valueOf(rs.getString("STATUS"))); + operation.setEnabled(Boolean.parseBoolean(rs.getString("ENABLED"))); + 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); + } } } 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 c44929be658..48febb5158a 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,12 +18,9 @@ */ 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.dao.OperationManagementDAOException; -import java.util.List; - public class ConfigOperationDAOImpl extends OperationDAOImpl { @Override @@ -31,39 +28,4 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl { return 0; } - @Override - public int updateOperation(Operation operation) throws OperationManagementDAOException { - return 0; - } - - @Override - public int deleteOperation(int id) throws OperationManagementDAOException { - return 0; - } - - @Override - public Operation getOperation(int id) throws OperationManagementDAOException { - return null; - } - - @Override - public Operation getOperation(DeviceIdentifier deviceId, int operationId) throws OperationManagementDAOException { - return null; - } - - @Override - public List getOperations(DeviceIdentifier deviceId) throws OperationManagementDAOException { - return null; - } - - @Override - public List getOperations(String status) throws OperationManagementDAOException { - return null; - } - - @Override - public Operation getNextOperation(DeviceIdentifier deviceId) throws OperationManagementDAOException { - return null; - } - } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/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 0571ebb7fa7..0db1da2dbf9 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 @@ -24,6 +24,9 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.*; import javax.sql.DataSource; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.ObjectInputStream; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -82,38 +85,92 @@ public class OperationDAOImpl implements OperationDAO { } @Override - public List getOperations(DeviceIdentifier deviceId) throws OperationManagementDAOException { - Connection conn = OperationManagementDAOFactory.getConnection(); - String sql = "SELECT o.ID, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS 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"; + public Operation getOperation(DeviceIdentifier deviceId, + Operation.Status status) throws OperationManagementDAOException { + return null; + } + + @Override + public List getOperations(DeviceIdentifier deviceId) throws OperationManagementDAOException { + List operations; + + PreparedStatement stmt = null; + ResultSet rs = null; try { - PreparedStatement stmt = conn.prepareStatement(sql); + Connection conn = OperationManagementDAOFactory.getConnection(); + String sql = "SELECT o.ID, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS 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"; + stmt = conn.prepareStatement(sql); stmt.setString(1, deviceId.getType()); stmt.setString(2, deviceId.getId()); + rs = stmt.executeQuery(); - List operations = new ArrayList(); - ResultSet rs = stmt.executeQuery(); + operations = new ArrayList(); while (rs.next()) { Operation operation = new Operation(); - //operation.setType(); + operation.setId(rs.getInt("ID")); } } catch (SQLException e) { throw new OperationManagementDAOException("Error occurred while retrieving the operation list " + - "available for the '" + deviceId.getType() + "' with id '" + deviceId.getId() + "'" , e); + "available for the '" + deviceId.getType() + "' with id '" + deviceId.getId() + "'", e); + } finally { + OperationManagementDAOUtil.cleanupResources(stmt, rs); } + return operations; + } + + @Override + public List getOperations(DeviceIdentifier deviceId, + Operation.Status status) throws OperationManagementDAOException { return null; } @Override - public List getOperations(String status) throws OperationManagementDAOException { + public List getOperations(Operation.Status status) throws OperationManagementDAOException { return null; } @Override public Operation getNextOperation(DeviceIdentifier deviceId) throws OperationManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + try { + Connection connection = OperationManagementDAOFactory.getConnection(); + stmt = connection.prepareStatement( + "SELECT o.ID, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS 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 LIMIT 1"); + stmt.setString(1, deviceId.getType()); + stmt.setString(2, deviceId.getId()); + rs = stmt.executeQuery(); + + Operation operation = null; + 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")); + } + return operation; + } catch (SQLException e) { + throw new OperationManagementDAOException("Error occurred while adding operation metadata", e); + } finally { + OperationManagementDAOUtil.cleanupResources(stmt, rs); + } + } + + private Operation.Status getStatus(String status) { return null; } + private Operation.Type getType(String type) { + return null; + } + + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/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 d716fa3529e..72877487913 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 @@ -80,16 +80,6 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl { return operationId; } - @Override - public int updateOperation(Operation operation) throws OperationManagementDAOException { - return 0; - } - - @Override - public int deleteOperation(int id) throws OperationManagementDAOException { - return 0; - } - @Override public Operation getOperation(int operationId) throws OperationManagementDAOException { Connection conn = OperationManagementDAOFactory.getConnection(); @@ -136,16 +126,6 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl { } } - @Override - public List getOperations(DeviceIdentifier deviceId) throws OperationManagementDAOException { - return null; - } - - @Override - public List getOperations(String status) throws OperationManagementDAOException { - return null; - } - @Override public Operation getNextOperation(DeviceIdentifier deviceId) throws OperationManagementDAOException { PreparedStatement stmt = null; 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 7d9e89d5c2f..7c88e429cce 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 @@ -121,12 +121,13 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { } @Override - public List getOperations(DeviceIdentifier deviceId) throws OperationManagementException { + public List getOperations(DeviceIdentifier deviceId) throws OperationManagementException { return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getOperations(deviceId); } @Override - public List getPendingOperations(DeviceIdentifier deviceId) throws OperationManagementException { + public List getPendingOperations( + DeviceIdentifier deviceId) throws OperationManagementException { return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getPendingOperations(deviceId); } @@ -136,13 +137,14 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { } @Override - public Operation updateOperation(Long operationId, DeviceIdentifier deviceIdentifier, - String responsePayLoad) throws OperationManagementException { + public Operation updateOperation(int operationId, DeviceIdentifier deviceIdentifier, + String responsePayLoad) throws OperationManagementException { return null; } @Override - public void sendEnrolmentInvitation(EmailMessageProperties emailMessageProperties) throws DeviceManagementException { + public void sendEnrolmentInvitation( + EmailMessageProperties emailMessageProperties) throws DeviceManagementException { DeviceManagementDataHolder.getInstance().getDeviceManagementProvider() .sendEnrolmentInvitation(emailMessageProperties); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java index a0ac6ff2dc2..80e2b5b3717 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java @@ -170,18 +170,19 @@ public final class DeviceManagerUtil { public static API getAPI(APIConfig config) throws APIManagementException { APIProvider provider = config.getProvider(); APIIdentifier id = new APIIdentifier(config.getOwner(), config.getName(), config.getVersion()); + API api = new API(id); - if (!provider.isAPIAvailable(id)) { - api.setContext(config.getContext()); - api.setUrl(config.getEndpoint()); - api.setUriTemplates( - getURITemplates(config.getEndpoint(), APIConstants.AUTH_APPLICATION_OR_USER_LEVEL_TOKEN)); - api.setVisibility(APIConstants.API_GLOBAL_VISIBILITY); - api.addAvailableTiers(provider.getTiers()); - api.setEndpointSecured(false); - api.setStatus(APIStatus.PUBLISHED); - api.setTransports(config.getTransports()); - } + api.setApiOwner(config.getOwner()); + api.setContext(config.getContext()); + api.setUrl(config.getEndpoint()); + api.setUriTemplates( + getURITemplates(config.getEndpoint(), APIConstants.AUTH_APPLICATION_OR_USER_LEVEL_TOKEN)); + api.setVisibility(APIConstants.API_GLOBAL_VISIBILITY); + api.addAvailableTiers(provider.getTiers()); + api.setEndpointSecured(false); + api.setStatus(APIStatus.PUBLISHED); + api.setTransports(config.getTransports()); + return api; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceOperationManagementTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceOperationManagementTests.java index e23a291572c..6b7fe6da810 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceOperationManagementTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceOperationManagementTests.java @@ -87,7 +87,7 @@ public class DeviceOperationManagementTests extends DeviceManagementBaseTest { DeviceIdentifier deviceId = new DeviceIdentifier(); deviceId.setId("4892813d-0b18-4a02-b7b1-61775257400e"); deviceId.setType("android"); - List operations = operationManager.getOperations(deviceId); + List operations = operationManager.getOperations(deviceId); Assert.assertNotNull(operations); boolean notEmpty = operations.size() > 0; Assert.assertTrue(notEmpty);