From 9f6dd552236c03e13772da19d6f259d8a2eb9289 Mon Sep 17 00:00:00 2001 From: hasuniea Date: Wed, 26 Aug 2015 14:05:21 +0530 Subject: [PATCH 1/2] fixed issue in modify enrollment --- .../carbon/device/mgt/core/dao/DeviceDAO.java | 3 +-- .../mgt/core/dao/impl/DeviceDAOImpl.java | 18 ++++++++---------- .../DeviceManagementProviderServiceImpl.java | 5 +++-- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index 5b5d0c66aa..70c403a2e8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -22,7 +22,6 @@ import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.EnrolmentInfo.Status; -import org.wso2.carbon.device.mgt.common.app.mgt.Application; import java.util.List; @@ -33,7 +32,7 @@ public interface DeviceDAO { int addDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException; - int updateDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException; + boolean updateDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException; int removeDevice(DeviceIdentifier deviceId, int tenantId) throws DeviceManagementDAOException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java index 6d8d4107bf..35e8466712 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java @@ -67,11 +67,11 @@ public class DeviceDAOImpl implements DeviceDAO { } @Override - public int updateDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException { + public boolean updateDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException { Connection conn; PreparedStatement stmt = null; - ResultSet rs = null; - int deviceId = -1; + boolean status = false; + int rows; try { conn = this.getConnection(); String sql = @@ -83,18 +83,16 @@ public class DeviceDAOImpl implements DeviceDAO { stmt.setString(3, device.getDeviceIdentifier()); stmt.setInt(4, typeId); stmt.setInt(5, tenantId); - stmt.executeUpdate(); - - rs = stmt.getGeneratedKeys(); - if (rs.next()) { - deviceId = rs.getInt(1); + rows = stmt.executeUpdate(); + if (rows>0) { + status = true; } - return deviceId; + return status; } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while enrolling device '" + device.getName() + "'", e); } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); + DeviceManagementDAOUtil.cleanupResources(stmt, null); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 197a9321e3..172fa38633 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -139,6 +139,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv EnrolmentInfo newEnrolmentInfo = device.getEnrolmentInfo(); if (existingEnrolmentInfo != null && newEnrolmentInfo != null) { if (existingEnrolmentInfo.equals(newEnrolmentInfo)) { + device.setId(existingDevice.getId()); device.getEnrolmentInfo().setDateOfEnrolment(existingEnrolmentInfo.getDateOfEnrolment()); this.modifyEnrollment(device); status = true; @@ -210,8 +211,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.beginTransaction(); DeviceType type = deviceTypeDAO.getDeviceType(device.getType()); - int deviceId = deviceDAO.updateDevice(type.getId(), device, tenantId); - enrolmentDAO.updateEnrollment(deviceId, device.getEnrolmentInfo(), tenantId); + deviceDAO.updateDevice(type.getId(), device, tenantId); + enrolmentDAO.updateEnrollment(device.getId(), device.getEnrolmentInfo(), tenantId); DeviceManagementDAOFactory.commitTransaction(); } catch (DeviceManagementDAOException | TransactionManagementException e) { From 7642112a9fdae0affe571b8a99ed1d16c1f901c2 Mon Sep 17 00:00:00 2001 From: hasuniea Date: Wed, 2 Sep 2015 17:27:28 +0530 Subject: [PATCH 2/2] code clean up --- .../org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java index 814084aa70..4441c25f2b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java @@ -84,7 +84,7 @@ public class DeviceDAOImpl implements DeviceDAO { stmt.setInt(4, typeId); stmt.setInt(5, tenantId); rows = stmt.executeUpdate(); - if (rows>0) { + if (rows > 0) { status = true; } return status;