Check operation status as pending for next operations

4.x.x
manoj 10 years ago
commit 32db7a1011

@ -20,10 +20,11 @@ package org.wso2.carbon.device.mgt.common.operation.mgt;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
import java.util.Properties; import java.util.Properties;
@XmlRootElement @XmlRootElement
public class Operation { public class Operation implements Serializable {
public enum Type { public enum Type {
CONFIG, MESSAGE, INFO, COMMAND, PROFILE CONFIG, MESSAGE, INFO, COMMAND, PROFILE

@ -43,38 +43,16 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl {
Connection conn = OperationManagementDAOFactory.openConnection(); Connection conn = OperationManagementDAOFactory.openConnection();
PreparedStatement stmt = null; PreparedStatement stmt = null;
ByteArrayOutputStream bao = null;
ObjectOutputStream oos = null;
try { try {
bao = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bao);
oos.writeObject(profileOp);
stmt = conn.prepareStatement("INSERT INTO DM_PROFILE_OPERATION(OPERATION_ID, OPERATION_DETAILS) " + stmt = conn.prepareStatement("INSERT INTO DM_PROFILE_OPERATION(OPERATION_ID, OPERATION_DETAILS) " +
"VALUES(?, ?)"); "VALUES(?, ?)");
stmt.setInt(1, operationId); stmt.setInt(1, operationId);
stmt.setObject(2, bao.toByteArray()); stmt.setObject(2, profileOp);
stmt.executeUpdate(); stmt.executeUpdate();
} catch (SQLException e) { } catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while adding profile operation", e); throw new OperationManagementDAOException("Error occurred while adding profile operation", e);
} catch (IOException e) {
throw new OperationManagementDAOException("Error occurred while serializing profile operation object", e);
} finally { } 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); OperationManagementDAOUtil.cleanupResources(stmt);
} }
return operationId; return operationId;
@ -132,38 +110,32 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
ByteArrayInputStream bais; ByteArrayInputStream bais = null;
ObjectOutputStream objectOutputStream; ObjectInputStream ois = null;
try { try {
Connection connection = OperationManagementDAOFactory.openConnection(); Connection connection = OperationManagementDAOFactory.openConnection();
stmt = connection.prepareStatement( stmt = connection.prepareStatement(
"SELECT po.OPERATION_DETAILS AS OPERATIONDETAILS " + "SELECT po.OPERATION_DETAILS AS OPERATIONDETAILS " +
"FROM DM_OPERATION o " + "FROM DM_OPERATION o " +
"INNER JOIN DM_PROFILE_OPERATION po ON o.ID = po.OPERATION_ID AND o.ID IN (" + "INNER JOIN DM_PROFILE_OPERATION po ON o.ID = po.OPERATION_ID AND o.STATUS =? AND o.ID IN (" +
"SELECT dom.OPERATION_ID FROM (SELECT d.ID FROM DM_DEVICE d INNER JOIN " + "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 " + "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 " + "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"); "ON d1.ID = dom.DEVICE_ID) ORDER BY o.CREATED_TIMESTAMP ASC LIMIT 1");
stmt.setString(1, deviceId.getType());
stmt.setString(2, deviceId.getId());
rs = stmt.executeQuery();
byte[] operationObjbyteArr; stmt.setString(1,Operation.Status.PENDING.toString());
Blob operationBlob; stmt.setString(2, deviceId.getType());
ByteArrayInputStream in; stmt.setString(3, deviceId.getId());
ObjectInputStream is; rs = stmt.executeQuery();
byte[] operationDetails = new byte[0];
if (rs.next()) { if (rs.next()) {
operationBlob = rs.getBlob("OPERATIONDETAILS"); operationDetails = rs.getBytes("OPERATIONDETAILS");
operationObjbyteArr = operationBlob.getBytes(1, (int) operationBlob.length());
in = new ByteArrayInputStream(operationObjbyteArr);
is = new ObjectInputStream(in);
return (ProfileOperation) is.readObject();
}else{
return null;
} }
bais = new ByteArrayInputStream(operationDetails);
ois = new ObjectInputStream(bais);
return (ProfileOperation) ois.readObject();
} catch (SQLException e) { } catch (SQLException e) {
log.error("SQL error occurred while retrieving profile operation", e); log.error("SQL error occurred while retrieving profile operation", e);
throw new OperationManagementDAOException("Error occurred while adding operation metadata", e); throw new OperationManagementDAOException("Error occurred while adding operation metadata", e);
@ -175,6 +147,20 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl {
log.error("IO error occurred while de serialize profile operation", e); log.error("IO error occurred while de serialize profile operation", e);
throw new OperationManagementDAOException("Error occurred while serializing profile operation object", e); throw new OperationManagementDAOException("Error occurred while serializing profile operation object", e);
} finally { } 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); OperationManagementDAOUtil.cleanupResources(stmt, rs);
OperationManagementDAOFactory.closeConnection(); OperationManagementDAOFactory.closeConnection();
} }

Loading…
Cancel
Save