From 9eb2f5df86fba7fb3be8c4e5be0f3c6c482d905b Mon Sep 17 00:00:00 2001 From: mharindu Date: Sat, 4 Jun 2016 11:33:22 +0530 Subject: [PATCH] Fixed foriegn key violation issue when getting pending operations --- .../mgt/core/operation/mgt/OperationManagerImpl.java | 5 +++-- .../mgt/core/operation/mgt/dao/OperationDAO.java | 2 +- .../mgt/dao/impl/GenericOperationDAOImpl.java | 10 +++++++--- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index 7535349ca2..258f3b5948 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -456,12 +456,13 @@ public class OperationManagerImpl implements OperationManager { DeviceManagementDAOFactory.closeConnection(); } OperationManagementDAOFactory.beginTransaction(); + boolean isUpdated = false; if (operation.getStatus() != null) { - operationDAO.updateOperationStatus(enrolmentId, operationId, + isUpdated = operationDAO.updateOperationStatus(enrolmentId, operationId, org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status. valueOf(operation.getStatus().toString())); } - if (operation.getOperationResponse() != null) { + if (isUpdated && operation.getOperationResponse() != null) { operationDAO.addOperationResponse(enrolmentId, operationId, operation.getOperationResponse()); } OperationManagementDAOFactory.commitTransaction(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java index d941679d21..a2d3a20ed6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java @@ -54,7 +54,7 @@ public interface OperationDAO { Operation getNextOperation(int enrolmentId) throws OperationManagementDAOException; - void updateOperationStatus(int enrolmentId, int operationId,Operation.Status status) + boolean updateOperationStatus(int enrolmentId, int operationId,Operation.Status status) throws OperationManagementDAOException; void updateEnrollmentOperationsStatus(int enrolmentId, String operationCode, Operation.Status existingStatus, 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/GenericOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java index 99540d9d18..fc7751cda2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java @@ -89,9 +89,10 @@ public class GenericOperationDAOImpl implements OperationDAO { } } - public void updateOperationStatus(int enrolmentId, int operationId, Operation.Status status) + public boolean updateOperationStatus(int enrolmentId, int operationId, Operation.Status status) throws OperationManagementDAOException { PreparedStatement stmt = null; + boolean isUpdated = false; try { long time = System.currentTimeMillis() / 1000; Connection connection = OperationManagementDAOFactory.getConnection(); @@ -101,14 +102,17 @@ public class GenericOperationDAOImpl implements OperationDAO { stmt.setLong(2, time); stmt.setInt(3, enrolmentId); stmt.setInt(4, operationId); - stmt.executeUpdate(); - + int numOfRecordsUpdated = stmt.executeUpdate(); + if (numOfRecordsUpdated != 0) { + isUpdated = true; + } } catch (SQLException e) { throw new OperationManagementDAOException("Error occurred while update device mapping operation status " + "metadata", e); } finally { OperationManagementDAOUtil.cleanupResources(stmt); } + return isUpdated; } @Override