From 91d9db1ffaac83552c1bcd8bbc9ec596deed0c2d Mon Sep 17 00:00:00 2001 From: Dileesha Rajapakse Date: Mon, 16 Nov 2015 14:15:54 +0530 Subject: [PATCH] Refactored ApplicationMapping and ProfileOperation DAO classes --- .../dao/impl/ApplicationMappingDAOImpl.java | 2 +- .../mgt/dao/impl/ProfileOperationDAOImpl.java | 28 +++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java index b9dff843a3..45154a54a2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java @@ -73,7 +73,7 @@ public class ApplicationMappingDAOImpl implements ApplicationMappingDAO { Connection conn; PreparedStatement stmt = null; ResultSet rs = null; - List mappingIds = new ArrayList<>(); + try { conn = this.getConnection(); String sql = "INSERT INTO DM_DEVICE_APPLICATION_MAPPING (DEVICE_ID, APPLICATION_ID, " + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ProfileOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ProfileOperationDAOImpl.java index 664ac370da..04e39eef98 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ProfileOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ProfileOperationDAOImpl.java @@ -37,22 +37,46 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl { public int addOperation(Operation operation) throws OperationManagementDAOException { PreparedStatement stmt = null; + ByteArrayOutputStream bao = null; + ObjectOutputStream oos = null; + int operationId; try { operationId = super.addOperation(operation); operation.setCreatedTimeStamp(new Timestamp(new java.util.Date().getTime()).toString()); operation.setId(operationId); operation.setEnabled(true); - ProfileOperation profileOp = (ProfileOperation) operation; + //ProfileOperation profileOp = (ProfileOperation) operation; Connection conn = OperationManagementDAOFactory.getConnection(); stmt = conn.prepareStatement("INSERT INTO DM_PROFILE_OPERATION(OPERATION_ID, OPERATION_DETAILS) " + "VALUES(?, ?)"); + + bao = new ByteArrayOutputStream(); + oos = new ObjectOutputStream(bao); + oos.writeObject(operation); + stmt.setInt(1, operationId); - stmt.setObject(2, profileOp); + stmt.setBytes(2, bao.toByteArray()); stmt.executeUpdate(); } catch (SQLException 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 { + 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); } return operationId;