Refactor operation DAO methods.Add connection close util method

revert-70aa11f8
manoj 10 years ago
parent 32b86c7ff1
commit 4a9a27ab7e

@ -41,6 +41,7 @@ public class Operation {
private Status status; private Status status;
private String receivedTimeStamp; private String receivedTimeStamp;
private String createdTimeStamp; private String createdTimeStamp;
private boolean isEnabled;
@XmlElement @XmlElement
public String getCode() { public String getCode() {
@ -109,4 +110,12 @@ public class Operation {
this.createdTimeStamp = createdTimeStamp; this.createdTimeStamp = createdTimeStamp;
} }
public boolean isEnabled() {
return isEnabled;
}
public void setEnabled(boolean isEnabled) {
this.isEnabled = isEnabled;
}
} }

@ -137,7 +137,7 @@ public class OperationManagerImpl implements OperationManager {
"pending operations assigned for '" + deviceId.getType() + "' device '" + "pending operations assigned for '" + deviceId.getType() + "' device '" +
deviceId.getId() + "'", e); deviceId.getId() + "'", e);
} }
} }
@Override @Override
public Operation getNextPendingOperation(DeviceIdentifier deviceId) throws OperationManagementException { public Operation getNextPendingOperation(DeviceIdentifier deviceId) throws OperationManagementException {
@ -156,7 +156,7 @@ public class OperationManagerImpl implements OperationManager {
@Override @Override
public Operation updateOperation(int id, DeviceIdentifier deviceIdentifier, public Operation updateOperation(int id, DeviceIdentifier deviceIdentifier,
String responsePayLoad) throws OperationManagementException { String responsePayLoad) throws OperationManagementException {
return null; return null;
} }

@ -27,9 +27,9 @@ public interface OperationDAO {
int addOperation(Operation operation) throws OperationManagementDAOException; int addOperation(Operation operation) throws OperationManagementDAOException;
int updateOperation(Operation operation) throws OperationManagementDAOException; void updateOperation(Operation operation) throws OperationManagementDAOException;
int deleteOperation(int operationId) throws OperationManagementDAOException; void deleteOperation(int operationId) throws OperationManagementDAOException;
Operation getOperation(int operationId) throws OperationManagementDAOException; Operation getOperation(int operationId) throws OperationManagementDAOException;

@ -74,20 +74,28 @@ public class OperationManagementDAOFactory {
} }
} }
public static Connection getConnection() throws OperationManagementDAOException { public static Connection openConnection() throws OperationManagementDAOException {
if (currentConnection.get() == null) { if (currentConnection.get() == null) {
synchronized (LOCK) { try {
try { currentConnection.set(dataSource.getConnection());
currentConnection.set(dataSource.getConnection()); } catch (SQLException e) {
} catch (SQLException e) { throw new OperationManagementDAOException("Error occurred while retrieving data source connection",
throw new OperationManagementDAOException("Error occurred while retrieving data source connection", e);
e);
}
} }
} }
return currentConnection.get(); return currentConnection.get();
} }
public static void closeConnection() throws OperationManagementDAOException {
Connection con = currentConnection.get();
try {
con.close();
} catch (SQLException e) {
log.error("Error occurred while close the connection");
}
}
public static void commitTransaction() throws OperationManagementDAOException { public static void commitTransaction() throws OperationManagementDAOException {
try { try {
Connection conn = currentConnection.get(); Connection conn = currentConnection.get();
@ -101,6 +109,8 @@ public class OperationManagementDAOFactory {
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while committing the transaction", e); throw new OperationManagementDAOException("Error occurred while committing the transaction", e);
} finally {
closeConnection();
} }
} }
@ -117,6 +127,8 @@ public class OperationManagementDAOFactory {
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while rollbacking the transaction", e); throw new OperationManagementDAOException("Error occurred while rollbacking the transaction", e);
} finally {
closeConnection();
} }
} }

@ -45,5 +45,13 @@ public class OperationManagementDAOUtil {
} }
} }
} }
public static void cleanupResources(Statement stmt) {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
log.warn("Error occurred while closing the statement", e);
}
}
}
} }

@ -36,13 +36,13 @@ public class CommandOperationDAOImpl extends OperationDAOImpl {
@Override @Override
public int addOperation(Operation operation) throws OperationManagementDAOException { public int addOperation(Operation operation) throws OperationManagementDAOException {
int operationId = super.addOperation(operation); int operationId = super.addOperation(operation);
CommandOperation commandOp = (CommandOperation) operation; CommandOperation commandOp = (CommandOperation) operation;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null;
try { try {
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.openConnection();
stmt = conn.prepareStatement("INSERT INTO DM_COMMAND_OPERATION(OPERATION_ID, ENABLED) VALUES(?, ?)"); stmt = conn.prepareStatement("INSERT INTO DM_COMMAND_OPERATION(OPERATION_ID, ENABLED) VALUES(?, ?)");
stmt.setInt(1, operationId); stmt.setInt(1, operationId);
stmt.setBoolean(2, commandOp.isEnabled()); stmt.setBoolean(2, commandOp.isEnabled());
@ -50,7 +50,7 @@ public class CommandOperationDAOImpl extends OperationDAOImpl {
} catch (SQLException e) { } catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while adding command operation", e); throw new OperationManagementDAOException("Error occurred while adding command operation", e);
} finally { } finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOUtil.cleanupResources(stmt);
} }
return operationId; return operationId;
} }
@ -59,41 +59,56 @@ public class CommandOperationDAOImpl extends OperationDAOImpl {
Operation.Status status) throws OperationManagementDAOException { Operation.Status status) throws OperationManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
List<Operation> operationList = new ArrayList<Operation>();
Operation operation;
try { try {
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.openConnection();
String sql = "SELECT po.OPERATION_ID, po.TYPE, po.CREATED_TIMESTAMP, po.RECEIVED_TIMESTAMP, po.STATUS, " + 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, " + "co.ENABLED,o.OPERATIONCODE 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 (" + "o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS, o.OPERATIONCODE FROM DM_OPERATION o INNER JOIN (" +
"SELECT dom.OPERATION_ID AS OP_ID FROM (SELECT d.ID FROM DM_DEVICE d 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 " + "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 = " + "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 " + "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"; "o.CREATED_TIMESTAMP ASC) po ON co.OPERATION_ID = po.OPERATION_ID";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(1, deviceId.getType()); stmt.setString(1, deviceId.getType());
stmt.setString(1, deviceId.getId()); stmt.setString(2, deviceId.getId());
stmt.setString(1, status.toString()); stmt.setString(3, status.toString());
rs = stmt.executeQuery(sql);
while(rs.next()){
operation = new Operation();
operation.setId(rs.getInt("OPERATION_ID"));
operation.setType(Operation.Type.valueOf(rs.getString("TYPE")));
operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString());
operation.setCreatedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString());
operation.setStatus(Operation.Status.valueOf(rs.getString("STATUS")));
operation.setEnabled(rs.getBoolean("ENABLED"));
operation.setCode(rs.getString("OPERATIONCODE"));
operationList.add(operation);
}
return operationList;
} catch (SQLException e) { } catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while retrieving the list of " + throw new OperationManagementDAOException("Error occurred while retrieving the list of " +
"operations with the status '" + status + "' available for the '" + deviceId.getType() + "operations with the status '" + status + "' available for the '" + deviceId.getType() +
"' device '" + deviceId.getId() + "'"); "' device '" + deviceId.getId() + "'");
} finally { } finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOUtil.cleanupResources(stmt, rs);
OperationManagementDAOFactory.closeConnection();
} }
return null;
} }
public List<? extends Operation> getOperations(DeviceIdentifier deviceId) throws OperationManagementDAOException { public List<? extends Operation> getOperations(DeviceIdentifier deviceId) throws OperationManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
try { try {
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.openConnection();
String sql = "SELECT po.OPERATION_ID, po.TYPE, po.CREATED_TIMESTAMP, po.RECEIVED_TIMESTAMP, po.STATUS, " + 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, " + "co.ENABLED.o.OPERATIONCODE 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 (" + "o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS, o.OPERATIONCODE FROM DM_OPERATION o INNER JOIN (" +
"SELECT dom.OPERATION_ID AS OP_ID FROM (SELECT d.ID FROM DM_DEVICE d 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 " + "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 = " + "d.DEVICE_IDENTIFICATION = ?) d1 INNER JOIN DM_DEVICE_OPERATION_MAPPING dom ON d1.ID = " +
@ -112,6 +127,7 @@ public class CommandOperationDAOImpl extends OperationDAOImpl {
operation.setType(Operation.Type.valueOf(rs.getString("TYPE"))); operation.setType(Operation.Type.valueOf(rs.getString("TYPE")));
operation.setStatus(Operation.Status.valueOf(rs.getString("STATUS"))); operation.setStatus(Operation.Status.valueOf(rs.getString("STATUS")));
operation.setEnabled(Boolean.parseBoolean(rs.getString("ENABLED"))); operation.setEnabled(Boolean.parseBoolean(rs.getString("ENABLED")));
operation.setCode(rs.getString("OPERATIONCODE"));
operations.add(operation); operations.add(operation);
} }
return operations; return operations;
@ -120,6 +136,46 @@ public class CommandOperationDAOImpl extends OperationDAOImpl {
"operations available for the '" + deviceId.getType() + "' device '" + deviceId.getId() + "'"); "operations available for the '" + deviceId.getType() + "' device '" + deviceId.getId() + "'");
} finally { } finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOUtil.cleanupResources(stmt, rs);
OperationManagementDAOFactory.closeConnection();
}
}
@Override
public void updateOperation(Operation operation) throws OperationManagementDAOException {
PreparedStatement stmt = null;
try {
Connection connection = OperationManagementDAOFactory.openConnection();
stmt = connection.prepareStatement(
"UPDATE DM_COMMAND_OPERATION O SET O.ENABLED=? WHERE O.OPERATION_ID=?");
stmt.setBoolean(1,operation.isEnabled());
stmt.setInt(2, operation.getId());
stmt.executeUpdate();
} catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while adding operation metadata", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt);
OperationManagementDAOFactory.closeConnection();
}
}
@Override
public void deleteOperation(int id) throws OperationManagementDAOException {
super.deleteOperation(id);
PreparedStatement stmt = null;
try {
Connection connection = OperationManagementDAOFactory.openConnection();
stmt = connection.prepareStatement("DELETE DM_COMMAND_OPERATION WHERE OPERATION_ID=?") ;
stmt.setInt(1, id);
stmt.executeUpdate();
} catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while deleting operation metadata", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt);
OperationManagementDAOFactory.closeConnection();
} }
} }

@ -26,28 +26,42 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOU
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
public class ConfigOperationDAOImpl extends OperationDAOImpl { public class ConfigOperationDAOImpl extends OperationDAOImpl {
@Override @Override
public int addOperation(Operation operation) throws OperationManagementDAOException { public int addOperation(Operation operation) throws OperationManagementDAOException {
int operationId = super.addOperation(operation);
ConfigOperation commandOp = (ConfigOperation) operation;
int operationId = super.addOperation(operation);
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null;
try { try {
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.openConnection();
stmt = conn.prepareStatement("INSERT INTO DM_CONFIG_OPERATION(OPERATION_ID) VALUES(?)"); stmt = conn.prepareStatement("INSERT INTO DM_CONFIG_OPERATION(OPERATION_ID) VALUES(?)");
stmt.setInt(1, operationId); stmt.setInt(1, operationId);
stmt.executeUpdate(); stmt.executeUpdate();
} catch (SQLException e) { } catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while adding command operation", e); throw new OperationManagementDAOException("Error occurred while adding command operation", e);
} finally { } finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOUtil.cleanupResources(stmt);
} }
return operationId; return operationId;
} }
@Override
public void deleteOperation(int id) throws OperationManagementDAOException {
super.deleteOperation(id);
PreparedStatement stmt = null;
try {
Connection connection = OperationManagementDAOFactory.openConnection();
stmt = connection.prepareStatement("DELETE DM_CONFIG_OPERATION WHERE OPERATION_ID=?") ;
stmt.setInt(1, id);
stmt.executeUpdate();
} catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while deleting operation metadata", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt);
}
}
} }

@ -37,9 +37,9 @@ public class OperationDAOImpl implements OperationDAO {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
try { try {
Connection connection = OperationManagementDAOFactory.getConnection(); Connection connection = OperationManagementDAOFactory.openConnection();
stmt = connection.prepareStatement( stmt = connection.prepareStatement(
"INSERT INTO DM_OPERATION(TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, STATUS,OPERATIONCODE) " + "INSERT INTO DM_OPERATION(TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, STATUS, OPERATIONCODE) " +
"VALUES (?, ?, ?, ?,?)"); "VALUES (?, ?, ?, ?,?)");
stmt.setString(1, operation.getType().toString()); stmt.setString(1, operation.getType().toString());
stmt.setTimestamp(2, new Timestamp(new Date().getTime())); stmt.setTimestamp(2, new Timestamp(new Date().getTime()));
@ -62,12 +62,11 @@ public class OperationDAOImpl implements OperationDAO {
} }
@Override @Override
public int updateOperation(Operation operation) throws OperationManagementDAOException { public void updateOperation(Operation operation) throws OperationManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null;
try { try {
Connection connection = OperationManagementDAOFactory.getConnection(); Connection connection = OperationManagementDAOFactory.openConnection();
stmt = connection.prepareStatement( stmt = connection.prepareStatement(
"UPDATE DM_OPERATION O SET O.RECEIVED_TIMESTAMP=?,O.STATUS=? WHERE O.ID=?"); "UPDATE DM_OPERATION O SET O.RECEIVED_TIMESTAMP=?,O.STATUS=? WHERE O.ID=?");
@ -76,22 +75,26 @@ public class OperationDAOImpl implements OperationDAO {
stmt.setInt(3, operation.getId()); stmt.setInt(3, operation.getId());
stmt.executeUpdate(); stmt.executeUpdate();
rs = stmt.getGeneratedKeys();
int id = -1;
if (rs.next()) {
id = rs.getInt(1);
}
return id;
} catch (SQLException e) { } catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while adding operation metadata", e); throw new OperationManagementDAOException("Error occurred while adding operation metadata", e);
} finally { } finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOUtil.cleanupResources(stmt);
} }
} }
@Override @Override
public int deleteOperation(int id) throws OperationManagementDAOException { public void deleteOperation(int id) throws OperationManagementDAOException {
return 0; PreparedStatement stmt = null;
try {
Connection connection = OperationManagementDAOFactory.openConnection();
stmt = connection.prepareStatement("DELETE DM_OPERATION WHERE ID=?") ;
stmt.setInt(1, id);
stmt.executeUpdate();
} catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while deleting operation metadata", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt);
}
} }
public Operation getOperation(int id) throws OperationManagementDAOException { public Operation getOperation(int id) throws OperationManagementDAOException {
@ -117,9 +120,10 @@ public class OperationDAOImpl implements OperationDAO {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
try { try {
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.openConnection();
String sql = String sql =
"SELECT o.ID, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS, o.OPERATIONCODE FROM DM_OPERATION o " + "SELECT o.ID, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS, o.OPERATIONCODE FROM " +
"DM_OPERATION o " +
"INNER JOIN (SELECT dom.OPERATION_ID AS OP_ID FROM (SELECT d.ID FROM DM_DEVICE d INNER " + "INNER JOIN (SELECT dom.OPERATION_ID AS OP_ID FROM (SELECT d.ID FROM DM_DEVICE d INNER " +
"JOIN " + "JOIN " +
"DM_DEVICE_TYPE dm ON d.DEVICE_TYPE_ID = dm.ID AND dm.NAME = ? AND d" + "DM_DEVICE_TYPE dm ON d.DEVICE_TYPE_ID = dm.ID AND dm.NAME = ? AND d" +
@ -150,6 +154,7 @@ public class OperationDAOImpl implements OperationDAO {
"available for the '" + deviceId.getType() + "' with id '" + deviceId.getId() + "'", e); "available for the '" + deviceId.getType() + "' with id '" + deviceId.getId() + "'", e);
} finally { } finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOUtil.cleanupResources(stmt, rs);
OperationManagementDAOFactory.closeConnection();
} }
return operations; return operations;
} }
@ -170,7 +175,7 @@ public class OperationDAOImpl implements OperationDAO {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
try { try {
Connection connection = OperationManagementDAOFactory.getConnection(); Connection connection = OperationManagementDAOFactory.openConnection();
stmt = connection.prepareStatement( stmt = connection.prepareStatement(
"SELECT o.ID, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS,o.OPERATIONCODE " + "SELECT o.ID, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS,o.OPERATIONCODE " +
" FROM DM_OPERATION o " + " FROM DM_OPERATION o " +
@ -203,6 +208,7 @@ public class OperationDAOImpl implements OperationDAO {
throw new OperationManagementDAOException("Error occurred while adding operation metadata", e); throw new OperationManagementDAOException("Error occurred while adding operation metadata", e);
} finally { } finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOUtil.cleanupResources(stmt, rs);
OperationManagementDAOFactory.closeConnection();
} }
} }

@ -33,7 +33,7 @@ public class OperationMappingDAOImpl implements OperationMappingDAO {
public void addOperationMapping(int operationId, Integer deviceId) throws OperationManagementDAOException { public void addOperationMapping(int operationId, Integer deviceId) throws OperationManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.openConnection();
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) VALUES(?, ?)";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId); stmt.setInt(1, deviceId);
@ -51,7 +51,7 @@ public class OperationMappingDAOImpl implements OperationMappingDAO {
Integer deviceIds) throws OperationManagementDAOException { Integer deviceIds) throws OperationManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.openConnection();
String sql = "DELETE FROM DM_DEVICE_OPERATION_MAPPING WHERE DEVICE_ID = ? AND OPERATION_ID = ?"; String sql = "DELETE FROM DM_DEVICE_OPERATION_MAPPING WHERE DEVICE_ID = ? AND OPERATION_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, 0); stmt.setInt(1, 0);

@ -28,30 +28,31 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOF
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOUtil; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOUtil;
import java.io.*; import java.io.*;
import java.sql.Connection; import java.sql.*;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class ProfileOperationDAOImpl extends OperationDAOImpl { public class ProfileOperationDAOImpl extends OperationDAOImpl {
private static final Log log = LogFactory.getLog(ProfileOperationDAOImpl.class); private static final Log log = LogFactory.getLog(ProfileOperationDAOImpl.class);
public int addOperation(Operation operation) throws OperationManagementDAOException { public int addOperation(Operation operation) throws OperationManagementDAOException {
int operationId = super.addOperation(operation); int operationId = super.addOperation(operation);
operation.setCreatedTimeStamp(new Timestamp(new java.util.Date().getTime()).toString());
operation.setId(operationId);
ProfileOperation profileOp = (ProfileOperation) operation; ProfileOperation profileOp = (ProfileOperation) operation;
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.openConnection();
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null;
ByteArrayOutputStream bao = null; ByteArrayOutputStream bao = null;
ObjectOutputStream oos = null; ObjectOutputStream oos = null;
try { try {
bao = new ByteArrayOutputStream(); bao = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bao); oos = new ObjectOutputStream(bao);
oos.writeObject(profileOp); oos.writeObject(profileOp);
stmt = conn.prepareStatement("INSERT INTO DM_PROFILE_OPERATION(OPERATION_ID, PAYLOAD) VALUES(?, ?)"); stmt = conn.prepareStatement("INSERT INTO DM_PROFILE_OPERATION(OPERATION_ID, OPERATIONDETAILS) " +
"VALUES(?, ?)");
stmt.setInt(1, operationId); stmt.setInt(1, operationId);
stmt.setBytes(2, bao.toByteArray()); stmt.setBytes(2, bao.toByteArray());
stmt.executeUpdate(); stmt.executeUpdate();
@ -74,29 +75,29 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl {
log.warn("Error occurred while closing ObjectOutputStream", e); log.warn("Error occurred while closing ObjectOutputStream", e);
} }
} }
OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOUtil.cleanupResources(stmt);
} }
return operationId; return operationId;
} }
@Override @Override
public Operation getOperation(int operationId) throws OperationManagementDAOException { public Operation getOperation(int operationId) throws OperationManagementDAOException {
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.openConnection();
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
ByteArrayInputStream bais = null; ByteArrayInputStream bais = null;
ObjectInputStream ois = null; ObjectInputStream ois = null;
try { try {
stmt = conn.prepareStatement("SELECT PAYLOAD FROM DM_PROFILE_OPERATION WHERE ID = ?"); stmt = conn.prepareStatement("SELECT OPERATIONDETAILS FROM DM_PROFILE_OPERATION WHERE OPERATION_ID = ?");
stmt.setInt(1, operationId); stmt.setInt(1, operationId);
rs = stmt.executeQuery(); rs = stmt.executeQuery();
byte[] payload = new byte[0]; byte[] operationDetails = new byte[0];
if (rs.next()) { if (rs.next()) {
payload = rs.getBytes("PAYLOAD"); operationDetails = rs.getBytes("OPERATIONDETAILS");
} }
bais = new ByteArrayInputStream(payload); bais = new ByteArrayInputStream(operationDetails);
ois = new ObjectInputStream(bais); ois = new ObjectInputStream(bais);
return (ProfileOperation) ois.readObject(); return (ProfileOperation) ois.readObject();
} catch (SQLException e) { } catch (SQLException e) {
@ -104,7 +105,7 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl {
} catch (IOException e) { } catch (IOException e) {
throw new OperationManagementDAOException("Error occurred while serializing profile operation object", e); throw new OperationManagementDAOException("Error occurred while serializing profile operation object", e);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
throw new OperationManagementDAOException("Error occurred while casting retrieved payload as a " + throw new OperationManagementDAOException("Error occurred while casting retrieved profile operation as a " +
"ProfileOperation object", e); "ProfileOperation object", e);
} finally { } finally {
if (bais != null) { if (bais != null) {
@ -122,6 +123,7 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl {
} }
} }
OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOUtil.cleanupResources(stmt, rs);
OperationManagementDAOFactory.closeConnection();
} }
} }
@ -132,51 +134,28 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl {
ResultSet rs = null; ResultSet rs = null;
ByteArrayInputStream bais; ByteArrayInputStream bais;
ObjectInputStream ois; ObjectInputStream ois;
int operationId = 0;
String operationType = "";
String createdTime = "";
String receivedTime = "";
String operationStatus = "";
String operationCode = "";
try { try {
Connection connection = OperationManagementDAOFactory.getConnection(); Connection connection = OperationManagementDAOFactory.openConnection();
stmt = connection.prepareStatement( stmt = connection.prepareStatement(
"SELECT o.ID AS OPERATION_ID, o.CREATED_TIMESTAMP AS CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP AS " + "SELECT po.OPERATIONDETAILS AS OPERATIONDETAILS " +
"RECEIVED_TIMESTAMP, po.PAYLOAD AS PAYLOAD,o.TYPE AS TYPE,o.STATUS as STATUS,o.OPERATIONCODE " +
"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.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(1, deviceId.getType());
stmt.setString(2, deviceId.getId()); stmt.setString(2, deviceId.getId());
rs = stmt.executeQuery(); rs = stmt.executeQuery();
byte[] payload = new byte[0]; byte[] operationObjbyteArr = new byte[0];
if (rs.next()) { if (rs.next()) {
operationId = rs.getInt("OPERATION_ID"); operationObjbyteArr = rs.getBytes("OPERATIONDETAILS");
createdTime = rs.getTimestamp("CREATED_TIMESTAMP").toString();
receivedTime = rs.getTimestamp("RECEIVED_TIMESTAMP").toString();
payload = rs.getBytes("PAYLOAD");
operationType = rs.getString("TYPE");
operationStatus = rs.getString("STATUS");
operationCode = rs.getString("OPERATIONCODE");
} }
bais = new ByteArrayInputStream(payload); bais = new ByteArrayInputStream(operationObjbyteArr);
ois = new ObjectInputStream(bais); ois = new ObjectInputStream(bais);
ProfileOperation profileOperation = new ProfileOperation(); return (ProfileOperation) ois.readObject();
profileOperation.setPayload(ois.readObject());
profileOperation.setId(operationId);
profileOperation.setType(Operation.Type.valueOf(operationType));
profileOperation.setStatus(Operation.Status.valueOf(operationStatus));
profileOperation.setCreatedTimeStamp(createdTime);
profileOperation.setReceivedTimeStamp(receivedTime);
profileOperation.setCode(operationCode);
return profileOperation;
} catch (SQLException e) { } catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while adding operation metadata", e); throw new OperationManagementDAOException("Error occurred while adding operation metadata", e);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
@ -186,6 +165,67 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl {
throw new OperationManagementDAOException("Error occurred while serializing profile operation object", e); throw new OperationManagementDAOException("Error occurred while serializing profile operation object", e);
} finally { } finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOUtil.cleanupResources(stmt, rs);
OperationManagementDAOFactory.closeConnection();
}
}
@Override
public void updateOperation(Operation operation) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ByteArrayOutputStream bao = null;
ObjectOutputStream oos = null;
try {
Connection connection = OperationManagementDAOFactory.openConnection();
stmt = connection.prepareStatement(
"UPDATE DM_PROFILE_OPERATION O SET O.OPERATIONDETAILS=? 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 operation metadata", e);
} catch (IOException e) {
throw new OperationManagementDAOException("Error occurred while serializing profile operation object", e);
} finally {
if (bao != null) {
try {
bao.close();
} catch (IOException e) {
log.warn("Error occurred while closing ByteArrayOutputStream", e);
}
}
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
log.warn("Error occurred while closing ObjectOutputStream", e);
}
}
OperationManagementDAOUtil.cleanupResources(stmt);
}
}
@Override
public void deleteOperation(int id) throws OperationManagementDAOException {
super.deleteOperation(id);
PreparedStatement stmt = null;
try {
Connection connection = OperationManagementDAOFactory.openConnection();
stmt = connection.prepareStatement("DELETE DM_PROFILE_OPERATION WHERE OPERATION_ID=?");
stmt.setInt(1, id);
stmt.executeUpdate();
} catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while deleting operation metadata", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt);
} }
} }
} }

@ -50,7 +50,7 @@ CREATE TABLE IF NOT EXISTS DM_COMMAND_OPERATION (
CREATE TABLE IF NOT EXISTS DM_PROFILE_OPERATION ( CREATE TABLE IF NOT EXISTS DM_PROFILE_OPERATION (
OPERATION_ID INTEGER NOT NULL, OPERATION_ID INTEGER NOT NULL,
ENABLED INTEGER NOT NULL DEFAULT 0, ENABLED INTEGER NOT NULL DEFAULT 0,
PAYLOAD BLOB DEFAULT NULL, OPERATIONDETAILS BLOB DEFAULT NULL,
PRIMARY KEY (OPERATION_ID), PRIMARY KEY (OPERATION_ID),
CONSTRAINT fk_dm_operation_profile FOREIGN KEY (OPERATION_ID) REFERENCES CONSTRAINT fk_dm_operation_profile FOREIGN KEY (OPERATION_ID) REFERENCES
DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION

Loading…
Cancel
Save