Code cleanup

revert-70aa11f8
prabathabey 9 years ago
parent 54cd6b80f4
commit 134b4ae53c

@ -22,7 +22,6 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException;
@ -30,7 +29,6 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager;
import org.wso2.carbon.device.mgt.core.dao.DeviceDAO;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO;
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;
@ -58,7 +56,6 @@ public class OperationManagerImpl implements OperationManager {
private OperationMappingDAO operationMappingDAO;
private OperationDAO operationDAO;
private DeviceDAO deviceDAO;
private DeviceTypeDAO deviceTypeDAO;
public OperationManagerImpl() {
commandOperationDAO = OperationManagementDAOFactory.getCommandOperationDAO();
@ -68,7 +65,6 @@ public class OperationManagerImpl implements OperationManager {
operationMappingDAO = OperationManagementDAOFactory.getOperationMappingDAO();
operationDAO = OperationManagementDAOFactory.getOperationDAO();
deviceDAO = DeviceManagementDAOFactory.getDeviceDAO();
deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
}
@Override
@ -123,11 +119,8 @@ public class OperationManagerImpl implements OperationManager {
@Override
public List<? extends Operation> getOperations(DeviceIdentifier deviceId) throws OperationManagementException {
List<Operation> operations = new ArrayList<Operation>();
int enrolmentId = -1;
int enrolmentId;
List<Operation> operations = new ArrayList<>();
try {
OperationManagementDAOFactory.getConnection();
@ -138,20 +131,17 @@ public class OperationManagerImpl implements OperationManager {
throw new OperationManagementException("Device not found for given device " +
"Identifier:" + deviceId.getId() + " and given type" + deviceId.getType());
}
List<? extends org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation> operationList = operationDAO
.getOperationsForDevice(enrolmentId);
Operation operation;
List<? extends org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation> operationList =
operationDAO.getOperationsForDevice(enrolmentId);
for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : operationList) {
operation = OperationDAOUtil.convertOperation(dtoOperation);
Operation operation = OperationDAOUtil.convertOperation(dtoOperation);
operations.add(operation);
}
return operations;
} catch (OperationManagementDAOException e) {
throw new OperationManagementException("Error occurred while retrieving the list of " +
"operations assigned for '" + deviceId.getType() + "' device '" + deviceId.getId()
+ "'", e);
"operations assigned for '" + deviceId.getType() + "' device '" + deviceId.getId() + "'", e);
} catch (DeviceManagementDAOException e) {
throw new OperationManagementException("Error occurred while retrieving metadata of '" +
deviceId.getType() + "' device carrying the identifier '" + deviceId.getId() + "'");
@ -168,18 +158,13 @@ public class OperationManagerImpl implements OperationManager {
@Override
public List<? extends Operation> getPendingOperations(
DeviceIdentifier deviceId) throws OperationManagementException {
if (log.isDebugEnabled()) {
log.debug("Device identifier id:[" + deviceId.getId() + "] type:[" + deviceId.getType()
+ "]");
}
int enrolmentId = -1;
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>();
int enrolmentId;
List<Operation> operations = new ArrayList<>();
List<org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation> dtoOperationList = new ArrayList<>();
try {
OperationManagementDAOFactory.getConnection();
@ -215,11 +200,9 @@ public class OperationManagerImpl implements OperationManager {
"pending operations assigned for '" + deviceId.getType() + "' device '" +
deviceId.getId() + "'", e);
} catch (DeviceManagementDAOException e) {
String errorMsg = "Error occurred while retrieving the device " +
throw new OperationManagementException("Error occurred while retrieving the device " +
"for device Identifier type -'" + deviceId.getType() + "' and device Id '"
+ deviceId.getId();
log.error(errorMsg, e);
throw new OperationManagementException(errorMsg, e);
+ deviceId.getId() + "'", e);
} finally {
try {
OperationManagementDAOFactory.closeConnection();
@ -236,7 +219,7 @@ public class OperationManagerImpl implements OperationManager {
+ "]");
}
Operation operation = null;
int enrolmentId = -1;
int enrolmentId;
try {
OperationManagementDAOFactory.getConnection();
@ -365,14 +348,13 @@ public class OperationManagerImpl implements OperationManager {
@Override
public Operation getOperationByDeviceAndOperationId(DeviceIdentifier deviceId, int operationId)
throws OperationManagementException {
int enrolmentId = -1;
int enrolmentId;
Operation operation;
if (log.isDebugEnabled()) {
log.debug("Operation Id:" + operationId + " Device Type:" + deviceId.getType() + " Device Identifier:" +
deviceId.getId());
}
try {
OperationManagementDAOFactory.getConnection();
@ -414,11 +396,9 @@ public class OperationManagerImpl implements OperationManager {
"operations assigned for '" + deviceId.getType() + "' device '" + deviceId.getId()
+ "'", e);
} catch (DeviceManagementDAOException e) {
String errorMsg = "Error occurred while retrieving the device " +
"for device Identifier type -'" + deviceId.getType() + "' and device Id '"
+ deviceId.getId();
log.error(errorMsg, e);
throw new OperationManagementException(errorMsg, e);
throw new OperationManagementException("Error occurred while retrieving the device " +
"for device Identifier type -'" + deviceId.getType() + "' and device Id '" +
deviceId.getId() + "'", e);
} finally {
try {
OperationManagementDAOFactory.closeConnection();
@ -432,9 +412,9 @@ public class OperationManagerImpl implements OperationManager {
@Override
public List<? extends Operation> getOperationsByDeviceAndStatus(
DeviceIdentifier deviceId, Operation.Status status) throws OperationManagementException {
List<Operation> operations = new ArrayList<Operation>();
List<Operation> operations = new ArrayList<>();
List<org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation> dtoOperationList =
new ArrayList<org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation>();
new ArrayList<>();
try {
OperationManagementDAOFactory.getConnection();

@ -18,8 +18,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.core.dto.operation.mgt.CommandOperation;
import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException;
@ -35,15 +33,11 @@ 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 {
int operationId = super.addOperation(operation);
CommandOperation commandOp = (CommandOperation) operation;
PreparedStatement stmt = null;
try {
Connection conn = OperationManagementDAOFactory.getConnection();
stmt = conn.prepareStatement("INSERT INTO DM_COMMAND_OPERATION(OPERATION_ID, ENABLED) VALUES(?, ?)");
@ -60,17 +54,14 @@ public class CommandOperationDAOImpl extends OperationDAOImpl {
@Override
public void updateOperation(Operation operation) throws OperationManagementDAOException {
PreparedStatement stmt = null;
try {
Connection connection = OperationManagementDAOFactory.getConnection();
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 {
@ -81,7 +72,6 @@ public class CommandOperationDAOImpl extends OperationDAOImpl {
@Override
public void deleteOperation(int id) throws OperationManagementDAOException {
super.deleteOperation(id);
PreparedStatement stmt = null;
try {
@ -98,30 +88,23 @@ 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);
commandOperation.setEnabled(rs.getInt("ENABLED") != 0);
}
} 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);
throw new OperationManagementDAOException("SQL Error occurred while retrieving the command operation " +
"object available for the id '" + id, e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
OperationManagementDAOFactory.closeConnection();
@ -132,23 +115,19 @@ public class CommandOperationDAOImpl extends OperationDAOImpl {
@Override
public List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId,
Operation.Status status) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
Operation operation;
List<Operation> operationList = new ArrayList<Operation>();
List<CommandOperation> commandOperationList = new ArrayList<CommandOperation>();
CommandOperation commandOperation = null;
List<Operation> operations = new ArrayList<>();
List<CommandOperation> commandOperations = new ArrayList<>();
CommandOperation commandOperation;
try {
Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "Select co.OPERATION_ID,ENABLED from DM_COMMAND_OPERATION co " +
"INNER JOIN " +
"(Select * From DM_ENROLMENT_OPERATION_MAPPING WHERE ENROLMENT_ID=? " +
String sql = "SELECT co.OPERATION_ID,ENABLED FROM DM_COMMAND_OPERATION co " +
"INNER JOIN (SELECT * FROM DM_ENROLMENT_OPERATION_MAPPING WHERE ENROLMENT_ID=? " +
"AND STATUS=? ) dm ON dm.OPERATION_ID = co.OPERATION_ID";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrolmentId);
stmt.setString(2, status.toString());
@ -156,27 +135,25 @@ public class CommandOperationDAOImpl extends OperationDAOImpl {
rs = stmt.executeQuery();
while (rs.next()) {
commandOperation = new CommandOperation();
commandOperation.setEnabled(rs.getInt("ENABLED") == 0 ? false : true);
commandOperation.setEnabled(rs.getInt("ENABLED") != 0);
commandOperation.setId(rs.getInt("OPERATION_ID"));
commandOperationList.add(commandOperation);
commandOperations.add(commandOperation);
}
for(CommandOperation cmOperation:commandOperationList){
for(CommandOperation cmOperation : commandOperations){
operation = super.getOperation(cmOperation.getId());
operation.setEnabled(cmOperation.isEnabled());
operation.setStatus(status);
operationList.add(operation);
operations.add(operation);
}
} catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + enrolmentId +
"' with status '" + status.toString();
log.error(errorMsg);
throw new OperationManagementDAOException(errorMsg, e);
throw new OperationManagementDAOException("SQL error occurred while retrieving the operation available " +
"for the device'" + enrolmentId + "' with status '" + status.toString(), e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
OperationManagementDAOFactory.closeConnection();
}
return operationList;
return operations;
}
}

@ -21,10 +21,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.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.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;
@ -43,7 +41,6 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl {
@Override
public int addOperation(Operation operation) throws OperationManagementDAOException {
int operationId = super.addOperation(operation);
PreparedStatement stmt = null;
try {
@ -62,7 +59,6 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl {
@Override
public void deleteOperation(int id) throws OperationManagementDAOException {
super.deleteOperation(id);
PreparedStatement stmt = null;
try {
@ -79,17 +75,14 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl {
@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);
@ -97,7 +90,6 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl {
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) {
@ -123,18 +115,15 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl {
@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();
@ -145,21 +134,16 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl {
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);
throw new OperationManagementDAOException("IO Error occurred while de serialize the policy operation " +
"object", 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);
throw new OperationManagementDAOException("Class not found error occurred while de serialize the policy " +
"operation object", 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);
throw new OperationManagementDAOException("SQL Error occurred while retrieving the policy operation " +
"object available for the id '"
+ operationId, e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
OperationManagementDAOFactory.closeConnection();
@ -168,23 +152,19 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl {
}
@Override
public List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId,
Operation.Status status) throws OperationManagementDAOException {
public List<? extends Operation> getOperationsByDeviceAndStatus(
int enrolmentId, Operation.Status status) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
ConfigOperation configOperation;
List<Operation> operationList = new ArrayList<Operation>();
List<Operation> operations = new ArrayList<>();
ByteArrayInputStream bais = null;
ObjectInputStream ois = null;
try {
Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "Select co.OPERATION_ID, co.OPERATION_CONFIG from DM_CONFIG_OPERATION co " +
"INNER JOIN " +
"(Select * From DM_ENROLMENT_OPERATION_MAPPING WHERE ENROLMENT_ID=? " +
String sql = "SELECT co.OPERATION_ID, co.OPERATION_CONFIG FROM DM_CONFIG_OPERATION co " +
"INNER JOIN (SELECT * FROM DM_ENROLMENT_OPERATION_MAPPING WHERE ENROLMENT_ID = ? " +
"AND STATUS = ?) dm ON dm.OPERATION_ID = co.OPERATION_ID";
stmt = conn.prepareStatement(sql);
@ -199,22 +179,17 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl {
ois = new ObjectInputStream(bais);
configOperation = (ConfigOperation) ois.readObject();
configOperation.setStatus(status);
operationList.add(configOperation);
operations.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);
throw new OperationManagementDAOException("IO Error occurred while de serialize the configuration " +
"operation object", 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);
throw new OperationManagementDAOException("Class not found error occurred while de serialize the " +
"configuration operation object", e);
} catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + enrolmentId +
"' with status '" + status.toString();
log.error(errorMsg);
throw new OperationManagementDAOException(errorMsg, e);
throw new OperationManagementDAOException("SQL error occurred while retrieving the operation available " +
"for the device'" + enrolmentId + "' with status '" + status.toString(), e);
} finally {
if (bais != null) {
try {
@ -233,6 +208,7 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
OperationManagementDAOFactory.closeConnection();
}
return operationList;
return operations;
}
}

@ -39,7 +39,6 @@ public class OperationDAOImpl implements OperationDAO {
private static final Log log = LogFactory.getLog(OperationDAOImpl.class);
public int addOperation(Operation operation) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
try {
@ -68,17 +67,14 @@ public class OperationDAOImpl implements OperationDAO {
@Override
public void updateOperation(Operation operation) throws OperationManagementDAOException {
PreparedStatement stmt = null;
try {
Connection connection = OperationManagementDAOFactory.getConnection();
stmt = connection.prepareStatement("UPDATE DM_OPERATION O SET O.RECEIVED_TIMESTAMP = ? " +
"WHERE O.ID = ?");
stmt.setTimestamp(1, new Timestamp(new Date().getTime()));
stmt.setInt(2, operation.getId());
stmt.executeUpdate();
} catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while update operation metadata", e);
} finally {
@ -88,13 +84,11 @@ public class OperationDAOImpl implements OperationDAO {
public void updateOperationStatus(int enrolmentId, int operationId, Operation.Status status)
throws OperationManagementDAOException {
PreparedStatement stmt = null;
try {
Connection connection = OperationManagementDAOFactory.getConnection();
stmt = connection.prepareStatement("UPDATE DM_ENROLMENT_OPERATION_MAPPING O SET O.STATUS = ? " +
"WHERE O.ENROLMENT_ID = ? and O.OPERATION_ID = ?");
stmt.setString(1, status.toString());
stmt.setInt(2, enrolmentId);
stmt.setInt(3, operationId);
@ -102,28 +96,22 @@ public class OperationDAOImpl implements OperationDAO {
} catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while update device mapping operation status " +
"metadata",
e);
"metadata", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt);
}
}
@Override
public void addOperationResponse(int enrolmentId, int operationId, Object operationResponse)
throws OperationManagementDAOException {
PreparedStatement stmt = null;
ByteArrayOutputStream bao = null;
ObjectOutputStream oos = null;
try {
Connection connection = OperationManagementDAOFactory.getConnection();
stmt = connection.prepareStatement("INSERT INTO DM_DEVICE_OPERATION_RESPONSE(OPERATION_ID, DEVICE_ID, " +
"OPERATION_RESPONSE) VALUES(?, ?, ?)");
bao = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bao);
oos.writeObject(operationResponse);
@ -132,7 +120,6 @@ public class OperationDAOImpl implements OperationDAO {
stmt.setInt(2, enrolmentId);
stmt.setBytes(3, bao.toByteArray());
stmt.executeUpdate();
} catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while inserting operation response", e);
} catch (IOException e) {
@ -158,7 +145,6 @@ public class OperationDAOImpl implements OperationDAO {
@Override
public void deleteOperation(int id) throws OperationManagementDAOException {
PreparedStatement stmt = null;
try {
Connection connection = OperationManagementDAOFactory.getConnection();
@ -174,16 +160,13 @@ public class OperationDAOImpl implements OperationDAO {
@Override
public Operation getOperation(int id) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
Operation operation = null;
try {
Connection conn = OperationManagementDAOFactory.getConnection();
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();
@ -202,10 +185,8 @@ public class OperationDAOImpl implements OperationDAO {
}
} catch (SQLException e) {
String errorMsg = "SQL Error occurred while retrieving the operation object " + "available for the id '"
+ id;
log.error(errorMsg, e);
throw new OperationManagementDAOException(errorMsg, e);
throw new OperationManagementDAOException("SQL Error occurred while retrieving the operation object " +
"available for the id '" + id, e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
OperationManagementDAOFactory.closeConnection();
@ -215,19 +196,16 @@ public class OperationDAOImpl implements OperationDAO {
@Override
public Operation getOperationByDeviceAndId(int enrolmentId, int operationId) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
Operation operation = null;
try {
Connection conn = OperationManagementDAOFactory.getConnection();
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 " +
" FROM (SELECT ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, STATUS," +
"OPERATION_CODE FROM DM_OPERATION WHERE id = ?) o INNER JOIN (SELECT * FROM " +
"DM_ENROLMENT_OPERATION_MAPPING dm where dm.OPERATION_ID = ? AND dm.ENROLMENT_ID = ?) om " +
"ON o.ID = om.OPERATION_ID ";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, operationId);
stmt.setInt(2, operationId);
@ -247,10 +225,8 @@ public class OperationDAOImpl implements OperationDAO {
operation.setCode(rs.getString("OPERATION_CODE"));
}
} catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + enrolmentId +
"' with id '" + operationId;
log.error(errorMsg);
throw new OperationManagementDAOException(errorMsg, e);
throw new OperationManagementDAOException("SQL error occurred while retrieving the operation " +
"available for the device'" + enrolmentId + "' with id '" + operationId, e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
OperationManagementDAOFactory.closeConnection();
@ -259,26 +235,21 @@ public class OperationDAOImpl implements OperationDAO {
}
@Override
public List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId,
Operation.Status status) throws OperationManagementDAOException {
public List<? extends Operation> getOperationsByDeviceAndStatus(
int enrolmentId, Operation.Status status) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
Operation operation;
List<Operation> operationList = new ArrayList<Operation>();
List<Operation> operations = new ArrayList<>();
try {
Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE " +
"FROM DM_OPERATION o " +
"INNER JOIN (Select * from DM_ENROLMENT_OPERATION_MAPPING dm " +
"where dm.ENROLMENT_ID=? and dm.STATUS=?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC";
"INNER JOIN (SELECT * FROM DM_ENROLMENT_OPERATION_MAPPING dm " +
"WHERE dm.ENROLMENT_ID = ? AND dm.STATUS = ?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrolmentId);
stmt.setString(2, status.toString());
rs = stmt.executeQuery();
while (rs.next()) {
@ -293,40 +264,32 @@ public class OperationDAOImpl implements OperationDAO {
}
operation.setCode(rs.getString("OPERATION_CODE"));
operation.setStatus(status);
operationList.add(operation);
operations.add(operation);
}
} catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + enrolmentId +
"' with status '" + status.toString();
log.error(errorMsg);
throw new OperationManagementDAOException(errorMsg, e);
throw new OperationManagementDAOException("SQL error occurred while retrieving the operation " +
"available for the device'" + enrolmentId + "' with status '" + status.toString(), e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
OperationManagementDAOFactory.closeConnection();
}
return operationList;
return operations;
}
@Override
public List<? extends Operation> getOperationsForDevice(int enrolmentId)
throws OperationManagementDAOException {
public List<? extends Operation> getOperationsForDevice(int enrolmentId) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
Operation operation;
List<Operation> operationList = new ArrayList<Operation>();
List<Operation> operations = new ArrayList<>();
try {
Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, " +
"OPERATION_CODE, dm.STATUS FROM DM_OPERATION o " +
"INNER JOIN (Select * from DM_ENROLMENT_OPERATION_MAPPING dm " +
"where dm.ENROLMENT_ID=?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC";
"INNER JOIN (SELECT * FROM DM_ENROLMENT_OPERATION_MAPPING dm " +
"WHERE dm.ENROLMENT_ID = ?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrolmentId);
rs = stmt.executeQuery();
while (rs.next()) {
@ -341,40 +304,34 @@ public class OperationDAOImpl implements OperationDAO {
}
operation.setCode(rs.getString("OPERATION_CODE"));
operation.setStatus(Operation.Status.valueOf(rs.getString("STATUS")));
operationList.add(operation);
operations.add(operation);
}
} catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + enrolmentId +
"' with status '";
log.error(errorMsg);
throw new OperationManagementDAOException(errorMsg, e);
throw new OperationManagementDAOException("SQL error occurred while retrieving the operation " +
"available for the device'" + enrolmentId + "' with status '", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
OperationManagementDAOFactory.closeConnection();
}
return operationList;
return operations;
}
@Override
public Operation getNextOperation(int enrolmentId) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
try {
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_ENROLMENT_OPERATION_MAPPING dm " +
"where dm.ENROLMENT_ID=? AND dm.STATUS=?) om ON o.ID = om.OPERATION_ID " +
"INNER JOIN (SELECT * FROM DM_ENROLMENT_OPERATION_MAPPING dm " +
"WHERE dm.ENROLMENT_ID = ? AND dm.STATUS = ?) om ON o.ID = om.OPERATION_ID " +
"ORDER BY o.CREATED_TIMESTAMP ASC LIMIT 1");
stmt.setInt(1, enrolmentId);
stmt.setString(2, Operation.Status.PENDING.toString());
rs = stmt.executeQuery();
Operation operation = null;
Operation operation = null;
if (rs.next()) {
operation = new Operation();
operation.setType(this.getType(rs.getString("TYPE")));
@ -398,28 +355,24 @@ public class OperationDAOImpl implements OperationDAO {
}
public List<? extends Operation> getOperationsByDeviceStatusAndType(int enrolmentId,
Operation.Status status, Operation.Type type) throws OperationManagementDAOException {
public List<? extends Operation> getOperationsByDeviceStatusAndType(
int enrolmentId, Operation.Status status, Operation.Type type) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
Operation operation;
List<Operation> operationList = new ArrayList<Operation>();
List<Operation> operations = new ArrayList<>();
try {
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_ENROLMENT_OPERATION_MAPPING dm " +
"where dm.ENROLMENT_ID=? and dm.STATUS=?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC";
"INNER JOIN (SELECT * FROM DM_ENROLMENT_OPERATION_MAPPING dm " +
"WHERE dm.ENROLMENT_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, enrolmentId);
stmt.setString(3, status.toString());
rs = stmt.executeQuery();
while (rs.next()) {
@ -433,22 +386,16 @@ public class OperationDAOImpl implements OperationDAO {
operation.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString());
}
operation.setCode(rs.getString("OPERATION_CODE"));
operationList.add(operation);
operations.add(operation);
}
} catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + enrolmentId +
"' with status '" + status.toString();
log.error(errorMsg);
throw new OperationManagementDAOException(errorMsg, e);
throw new OperationManagementDAOException("SQL error occurred while retrieving the operation available " +
"for the device'" + enrolmentId + "' with status '" + status.toString(), e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
OperationManagementDAOFactory.closeConnection();
}
return operationList;
}
private Operation.Status getStatus(String status) {
return Operation.Status.valueOf(status);
return operations;
}
private Operation.Type getType(String type) {

@ -22,7 +22,6 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.OperationManagementDAOException;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOUtil;
@ -38,7 +37,6 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl {
@Override
public int addOperation(Operation operation) throws OperationManagementDAOException {
int operationId = super.addOperation(operation);
operation.setCreatedTimeStamp(new Timestamp(new java.util.Date().getTime()).toString());
operation.setId(operationId);
@ -47,7 +45,6 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl {
Connection conn = OperationManagementDAOFactory.getConnection();
PreparedStatement stmt = null;
try {
stmt = conn.prepareStatement("INSERT INTO DM_POLICY_OPERATION(OPERATION_ID, OPERATION_DETAILS) " +
"VALUES(?, ?)");
@ -60,22 +57,18 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl {
OperationManagementDAOUtil.cleanupResources(stmt);
}
return operationId;
}
@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_POLICY_OPERATION O SET O.OPERATION_DETAILS=? " +
"WHERE O.OPERATION_ID=?");
bao = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bao);
oos.writeObject(operation);
@ -83,7 +76,6 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl {
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) {
@ -109,7 +101,6 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl {
@Override
public void deleteOperation(int operationId) throws OperationManagementDAOException {
super.deleteOperation(operationId);
PreparedStatement stmt = null;
try {
@ -126,18 +117,15 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl {
@Override
public Operation getOperation(int operationId) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
PolicyOperation policyOperation = null;
ByteArrayInputStream bais;
ObjectInputStream ois;
try {
Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "SELECT OPERATION_ID, ENABLED, OPERATION_DETAILS FROM DM_POLICY_OPERATION WHERE OPERATION_ID=?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, operationId);
rs = stmt.executeQuery();
@ -148,21 +136,15 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl {
ois = new ObjectInputStream(bais);
policyOperation = (PolicyOperation) 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);
throw new OperationManagementDAOException("IO Error occurred while de serialize the policy operation " +
"object", 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);
throw new OperationManagementDAOException("Class not found error occurred while de serialize the " +
"policy operation object", 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);
throw new OperationManagementDAOException("SQL Error occurred while retrieving the policy operation " +
"object available for the id '" + operationId + "'", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
OperationManagementDAOFactory.closeConnection();
@ -173,27 +155,22 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl {
@Override
public List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId,
Operation.Status status) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
PolicyOperation policyOperation;
List<Operation> operationList = new ArrayList<Operation>();
List<Operation> operations = new ArrayList<>();
ByteArrayInputStream bais = null;
ObjectInputStream ois = null;
try {
Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "Select po.OPERATION_ID, ENABLED, OPERATION_DETAILS from DM_POLICY_OPERATION po " +
"INNER JOIN " +
"(Select * From DM_ENROLMENT_OPERATION_MAPPING WHERE ENROLMENT_ID=? " +
String sql = "SELECT po.OPERATION_ID, ENABLED, OPERATION_DETAILS FROM DM_POLICY_OPERATION po " +
"INNER JOIN (SELECT * FROM DM_ENROLMENT_OPERATION_MAPPING WHERE ENROLMENT_ID = ? " +
"AND STATUS = ?) dm ON dm.OPERATION_ID = po.OPERATION_ID";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrolmentId);
stmt.setString(2, status.toString());
rs = stmt.executeQuery();
while (rs.next()) {
@ -202,22 +179,17 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl {
ois = new ObjectInputStream(bais);
policyOperation = (PolicyOperation) ois.readObject();
policyOperation.setStatus(status);
operationList.add(policyOperation);
operations.add(policyOperation);
}
} catch (IOException e) {
String errorMsg = "IO Error occurred while de serialize the profile operation object";
log.error(errorMsg, e);
throw new OperationManagementDAOException(errorMsg, e);
throw new OperationManagementDAOException("IO Error occurred while de serialize the profile " +
"operation object", 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);
throw new OperationManagementDAOException("Class not found error occurred while de serialize the " +
"profile operation object", e);
} catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + enrolmentId +
"' with status '" + status.toString();
log.error(errorMsg);
throw new OperationManagementDAOException(errorMsg, e);
throw new OperationManagementDAOException("SQL error occurred while retrieving the operation " +
"available for the device'" + enrolmentId + "' with status '" + status.toString(), e);
} finally {
if (bais != null) {
try {
@ -236,6 +208,7 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
OperationManagementDAOFactory.closeConnection();
}
return operationList;
return operations;
}
}

Loading…
Cancel
Save