Refractor Operations to handle Policy Operations and remove joins

revert-70aa11f8
manoj 10 years ago
parent 8ee09793f2
commit 917c0cf965

@ -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;

@ -71,5 +71,4 @@ public interface OperationManager {
public Operation getOperation(int operationId) throws OperationManagementException;
public List<? extends Operation> getOperationsForStatus(Operation.Status status) throws OperationManagementException;
}

@ -484,12 +484,6 @@ public class DeviceManagementServiceProviderImpl implements DeviceManagementServ
return DeviceManagementDataHolder.getInstance().getOperationManager().getOperation(operationId);
}
@Override
public List<? extends Operation> getOperationsForStatus(Operation.Status status)
throws OperationManagementException {
return DeviceManagementDataHolder.getInstance().getOperationManager().getOperationsForStatus(status);
}
@Override
public List<Device> getAllDevicesOfUser(String userName)
throws DeviceManagementException {

@ -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 {

@ -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<Operation> operations;
List<? extends org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation> dtoOperationList;
List<Operation> operations = new ArrayList<Operation>();
List<org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation> dtoOperationList =
new ArrayList<org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation>();
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<Operation>();
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<Operation> operations = new ArrayList<Operation>();
List<org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation> dtoOperationList =
new ArrayList<org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation>();
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<? extends org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation> 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<? extends Operation> getOperationsForStatus(Operation.Status status)
throws OperationManagementException {
try {
List<Operation> operations = new ArrayList<Operation>();
List<? extends org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation> 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) {

@ -40,8 +40,6 @@ public interface OperationDAO {
List<? extends Operation> getOperationsForDevice(int deviceId) throws OperationManagementDAOException;
List<? extends Operation> getOperationsForStatus(Operation.Status status) throws OperationManagementDAOException;
Operation getNextOperation(int deviceId) throws OperationManagementDAOException;
}

@ -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<? extends Operation> getOperationsByDeviceAndStatus(int deviceId,
Operation.Status status) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
Operation operation;
List<Operation> operationList = new ArrayList<Operation>();
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;
}
}

@ -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<? extends Operation> getOperationsByDeviceAndStatus(int deviceId,
Operation.Status status) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
ConfigOperation configOperation;
List<Operation> operationList = new ArrayList<Operation>();
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;
}
}

@ -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<Operation> operationList = new ArrayList<Operation>();
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<Operation> operationList = new ArrayList<Operation>();
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<? extends Operation> 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<Operation> operationList = new ArrayList<Operation>();
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<? extends Operation> getOperationsByDeviceStatusAndType(int deviceId,
Operation.Status status,Operation.Type type) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
Operation operation;
ByteArrayInputStream bais;
ObjectInputStream ois;
List<Operation> operationList = new ArrayList<Operation>();
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) {

@ -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);

@ -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<? extends Operation> getOperationsByDeviceAndStatus(int deviceId,
Operation.Status status) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
ProfileOperation profileOperation;
List<Operation> operationList = new ArrayList<Operation>();
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;
}
}

@ -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;
}

@ -173,11 +173,6 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getOperation(operationId);
}
@Override
public List<? extends Operation> getOperationsForStatus(Operation.Status status)
throws OperationManagementException {
return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getOperationsForStatus(status);
}
@Override
public void sendEnrolmentInvitation(EmailMessageProperties emailMessageProperties)

@ -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,

@ -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,

@ -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,

Loading…
Cancel
Save