From deeeb2ce48949cb529a0c83f1b6063086b9b4961 Mon Sep 17 00:00:00 2001 From: Ace Date: Wed, 10 Oct 2018 11:20:45 +0530 Subject: [PATCH 1/4] Persisting profile operations as string payloads and retriving same as strings, failing which as profile objects to support legacy functonality --- .../mgt/dao/impl/ProfileOperationDAOImpl.java | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) 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 9223b3c030..86463ebca1 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 @@ -53,7 +53,7 @@ public class ProfileOperationDAOImpl extends GenericOperationDAOImpl { bao = new ByteArrayOutputStream(); oos = new ObjectOutputStream(bao); - oos.writeObject(operation); + oos.writeObject(operation.getPayLoad()); stmt.setInt(1, operationId); stmt.setBytes(2, bao.toByteArray()); @@ -101,7 +101,13 @@ public class ProfileOperationDAOImpl extends GenericOperationDAOImpl { byte[] operationDetails = rs.getBytes("OPERATION_DETAILS"); bais = new ByteArrayInputStream(operationDetails); ois = new ObjectInputStream(bais); - profileOperation = (ProfileOperation) ois.readObject(); + Object obj = ois.readObject(); + if(obj instanceof String){ + profileOperation = new ProfileOperation(); + profileOperation.setPayLoad(obj); + } else { + profileOperation = (ProfileOperation) obj; + } } } catch (IOException e) { throw new OperationManagementDAOException("IO Error occurred while de serialize the profile " + @@ -147,9 +153,17 @@ public class ProfileOperationDAOImpl extends GenericOperationDAOImpl { byte[] operationDetails = rs.getBytes("OPERATION_DETAILS"); bais = new ByteArrayInputStream(operationDetails); ois = new ObjectInputStream(bais); - profileOperation = (ProfileOperation) ois.readObject(); - profileOperation.setStatus(status); - operationList.add(profileOperation); + Object obj = ois.readObject(); + if(obj instanceof String){ + profileOperation = new ProfileOperation(); + profileOperation.setPayLoad(obj); + profileOperation.setStatus(status); + operationList.add(profileOperation); + } else { + profileOperation = (ProfileOperation) obj; + profileOperation.setStatus(status); + operationList.add(profileOperation); + }; } } catch (IOException e) { From de9cf67977d61d4843f1fb395ff53b2275bd46b1 Mon Sep 17 00:00:00 2001 From: Ace Date: Wed, 10 Oct 2018 22:35:05 +0530 Subject: [PATCH 2/4] fixing issues with notification status not getting updated --- .../mgt/dao/impl/ProfileOperationDAOImpl.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) 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 86463ebca1..f890544188 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 @@ -99,11 +99,17 @@ public class ProfileOperationDAOImpl extends GenericOperationDAOImpl { if (rs.next()) { byte[] operationDetails = rs.getBytes("OPERATION_DETAILS"); + int oppId = rs.getInt("OPERATION_ID"); bais = new ByteArrayInputStream(operationDetails); ois = new ObjectInputStream(bais); Object obj = ois.readObject(); if(obj instanceof String){ + Operation opp = super.getOperation(oppId); profileOperation = new ProfileOperation(); + profileOperation.setCode(opp.getCode()); + profileOperation.setId(oppId); + profileOperation.setCreatedTimeStamp(opp.getCreatedTimeStamp()); + profileOperation.setId(oppId); profileOperation.setPayLoad(obj); } else { profileOperation = (ProfileOperation) obj; @@ -126,7 +132,7 @@ public class ProfileOperationDAOImpl extends GenericOperationDAOImpl { @Override public List getOperationsByDeviceAndStatus(int enrolmentId, - Operation.Status status) throws OperationManagementDAOException { + Operation.Status status) throws OperationManagementDAOException { PreparedStatement stmt = null; ResultSet rs = null; ProfileOperation profileOperation; @@ -150,12 +156,17 @@ public class ProfileOperationDAOImpl extends GenericOperationDAOImpl { rs = stmt.executeQuery(); while (rs.next()) { + int oppId = rs.getInt("OPERATION_ID"); byte[] operationDetails = rs.getBytes("OPERATION_DETAILS"); bais = new ByteArrayInputStream(operationDetails); ois = new ObjectInputStream(bais); Object obj = ois.readObject(); if(obj instanceof String){ + Operation opp = super.getOperation(oppId); profileOperation = new ProfileOperation(); + profileOperation.setCode(opp.getCode()); + profileOperation.setId(oppId); + profileOperation.setCreatedTimeStamp(opp.getCreatedTimeStamp()); profileOperation.setPayLoad(obj); profileOperation.setStatus(status); operationList.add(profileOperation); From 4b71c6ec4049ae0e3eb2b0b6c0da3d95e44175ee Mon Sep 17 00:00:00 2001 From: Ace Date: Thu, 11 Oct 2018 14:21:26 +0530 Subject: [PATCH 3/4] Operation retrieval logic changed to retrieve operation details via table merge instead of secondary DB call --- .../mgt/dao/impl/ProfileOperationDAOImpl.java | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) 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 f890544188..64ffdf86f3 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 @@ -91,7 +91,8 @@ public class ProfileOperationDAOImpl extends GenericOperationDAOImpl { ObjectInputStream ois; try { Connection conn = OperationManagementDAOFactory.getConnection(); - String sql = "SELECT OPERATION_ID, ENABLED, OPERATION_DETAILS FROM DM_PROFILE_OPERATION WHERE OPERATION_ID=?"; + String sql = "SELECT o.ID, po.ENABLED, po.OPERATION_DETAILS, o.CREATED_TIMESTAMP, o.OPERATION_CODE " + + "FROM DM_PROFILE_OPERATION po INNER JOIN DM_OPERATION o ON po.OPERATION_ID = O.ID WHERE po.OPERATION_ID=?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, id); @@ -99,16 +100,15 @@ public class ProfileOperationDAOImpl extends GenericOperationDAOImpl { if (rs.next()) { byte[] operationDetails = rs.getBytes("OPERATION_DETAILS"); - int oppId = rs.getInt("OPERATION_ID"); + int oppId = rs.getInt("ID"); bais = new ByteArrayInputStream(operationDetails); ois = new ObjectInputStream(bais); Object obj = ois.readObject(); if(obj instanceof String){ - Operation opp = super.getOperation(oppId); profileOperation = new ProfileOperation(); - profileOperation.setCode(opp.getCode()); + profileOperation.setCode(rs.getString("OPERATION_CODE")); profileOperation.setId(oppId); - profileOperation.setCreatedTimeStamp(opp.getCreatedTimeStamp()); + profileOperation.setCreatedTimeStamp(rs.getString("CREATED_TIMESTAMP")); profileOperation.setId(oppId); profileOperation.setPayLoad(obj); } else { @@ -122,7 +122,7 @@ public class ProfileOperationDAOImpl extends GenericOperationDAOImpl { throw new OperationManagementDAOException("Class not found error occurred while de serialize the " + "profile operation object", e); } catch (SQLException e) { - throw new OperationManagementDAOException("SQL Error occurred while retrieving the command " + + throw new OperationManagementDAOException("SQL Error occurred while retrieving the profile " + "operation object " + "available for the id '" + id, e); } finally { OperationManagementDAOUtil.cleanupResources(stmt, rs); @@ -144,10 +144,12 @@ public class ProfileOperationDAOImpl extends GenericOperationDAOImpl { try { Connection conn = OperationManagementDAOFactory.getConnection(); - String sql = "Select po.OPERATION_ID, ENABLED, OPERATION_DETAILS from DM_PROFILE_OPERATION po " + - "INNER JOIN " + - "(Select * From DM_ENROLMENT_OP_MAPPING WHERE ENROLMENT_ID=? " + - "AND STATUS=?) dm ON dm.OPERATION_ID = po.OPERATION_ID"; + String sql = "SELECT o.ID, po1.ENABLED, po1.STATUS, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, " + + "o.OPERATION_CODE, po1.OPERATION_DETAILS " + + "FROM (SELECT po.OPERATION_ID, po.ENABLED, po.OPERATION_DETAILS, dm.STATUS " + + "FROM DM_PROFILE_OPERATION po INNER JOIN (SELECT ENROLMENT_ID, OPERATION_ID, STATUS FROM DM_ENROLMENT_OP_MAPPING " + + "WHERE ENROLMENT_ID = ? AND STATUS = ?) dm ON dm.OPERATION_ID = po.OPERATION_ID) po1 " + + "INNER JOIN DM_OPERATION o ON po1.OPERATION_ID = o.ID "; stmt = conn.prepareStatement(sql); stmt.setInt(1, enrolmentId); @@ -156,17 +158,15 @@ public class ProfileOperationDAOImpl extends GenericOperationDAOImpl { rs = stmt.executeQuery(); while (rs.next()) { - int oppId = rs.getInt("OPERATION_ID"); byte[] operationDetails = rs.getBytes("OPERATION_DETAILS"); bais = new ByteArrayInputStream(operationDetails); ois = new ObjectInputStream(bais); Object obj = ois.readObject(); if(obj instanceof String){ - Operation opp = super.getOperation(oppId); profileOperation = new ProfileOperation(); - profileOperation.setCode(opp.getCode()); - profileOperation.setId(oppId); - profileOperation.setCreatedTimeStamp(opp.getCreatedTimeStamp()); + profileOperation.setCode(rs.getString("OPERATION_CODE")); + profileOperation.setId(rs.getInt("ID")); + profileOperation.setCreatedTimeStamp(rs.getString("CREATED_TIMESTAMP")); profileOperation.setPayLoad(obj); profileOperation.setStatus(status); operationList.add(profileOperation); From cb0fb87b49328470bdbb48b962baf8bbb22c5ed0 Mon Sep 17 00:00:00 2001 From: Ace Date: Thu, 11 Oct 2018 16:37:13 +0530 Subject: [PATCH 4/4] Fixing test failures and adding enhancements --- .../operation/mgt/dao/impl/ProfileOperationDAOImpl.java | 2 +- .../mgt/core/operation/OperationManagementTests.java | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) 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 64ffdf86f3..769537b47b 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 @@ -174,7 +174,7 @@ public class ProfileOperationDAOImpl extends GenericOperationDAOImpl { profileOperation = (ProfileOperation) obj; profileOperation.setStatus(status); operationList.add(profileOperation); - }; + } } } catch (IOException e) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementTests.java index dcfd63b995..3ca10bd90e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementTests.java @@ -67,6 +67,7 @@ public class OperationManagementTests extends BaseDeviceManagementTest { private static final String POLICY_OPERATION_CODE = "POLICY-TEST"; private static final String CONFIG_OPERATION_CODE = "CONFIG-TEST"; private static final String PROFILE_OPERATION_CODE = "PROFILE-TEST"; + private static final String PROFILE_NOTIFICATION_CODE = "NOTIFICATION"; private static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss"; private static final int NO_OF_DEVICES = 5; private static final String ADMIN_USER = "admin"; @@ -205,8 +206,10 @@ public class OperationManagementTests extends BaseDeviceManagementTest { @Test(dependsOnMethods = "addConfigOperation") public void addProfileOperation() throws DeviceManagementException, OperationManagementException, InvalidDeviceException { - Activity activity = this.operationMgtService.addOperation(getOperation(new ProfileOperation(), - Operation.Type.PROFILE, PROFILE_OPERATION_CODE), + Operation opp = getOperation(new ProfileOperation(), + Operation.Type.PROFILE, PROFILE_NOTIFICATION_CODE); + opp.setPayLoad("{\"messageText\":\"xyz\",\"messageTitle\":\"abc\"}"); + Activity activity = this.operationMgtService.addOperation(opp , this.deviceIds); validateOperationResponse(activity, ActivityStatus.Status.PENDING); }