From ac3438e2cd98d9b6f0d2e9f821c529ec533eb1b7 Mon Sep 17 00:00:00 2001 From: manoj Date: Thu, 16 Jul 2015 13:17:43 +0530 Subject: [PATCH] Code refactor application update --- .../mgt/common/app/mgt/Application.java | 37 ---------------- ...ApplicationManagerProviderServiceImpl.java | 43 ++++++++++++++++--- .../mgt/core/dao/impl/ApplicationDAOImpl.java | 12 +++--- .../dao/impl/ApplicationMappingDAOImpl.java | 5 ++- ...licationManagementProviderServiceTest.java | 8 +++- 5 files changed, 54 insertions(+), 51 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/Application.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/Application.java index 535b45a185b..6a53d75c087 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/Application.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/Application.java @@ -118,35 +118,6 @@ public class Application implements Serializable { } Application that = (Application) o; - - if (id != that.id) { - return false; - } - if (appProperties != null ? !appProperties.equals(that.appProperties) : that.appProperties != null) { - return false; - } - - if (category != null ? !category.equals(that.category) : that.category != null) { - return false; - } - if (imageUrl != null ? !imageUrl.equals(that.imageUrl) : that.imageUrl != null) { - return false; - } - if (locationUrl != null ? !locationUrl.equals(that.locationUrl) : that.locationUrl != null) { - return false; - } - if (name != null ? !name.equals(that.name) : that.name != null) { - return false; - } - if (platform != null ? !platform.equals(that.platform) : that.platform != null) { - return false; - } - if (type != null ? !type.equals(that.type) : that.type != null) { - return false; - } - if (version != null ? !version.equals(that.version) : that.version != null) { - return false; - } if (applicationIdentifier != null ? !applicationIdentifier.equals(that.applicationIdentifier) : that.applicationIdentifier != null) { return false; } @@ -156,14 +127,6 @@ public class Application implements Serializable { @Override public int hashCode() { int result = id; - result = 31 * result + (platform != null ? platform.hashCode() : 0); - result = 31 * result + (category != null ? category.hashCode() : 0); - result = 31 * result + (name != null ? name.hashCode() : 0); - result = 31 * result + (locationUrl != null ? locationUrl.hashCode() : 0); - result = 31 * result + (imageUrl != null ? imageUrl.hashCode() : 0); - result = 31 * result + (version != null ? version.hashCode() : 0); - result = 31 * result + (type != null ? type.hashCode() : 0); - result = 31 * result + (appProperties != null ? appProperties.hashCode() : 0); result = 31 * result + (applicationIdentifier != null ? applicationIdentifier.hashCode() : 0); return result; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java index d9cab6e58a6..6dbf00e15bb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java @@ -184,33 +184,66 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem int tenantId = getTenantId(); try { + DeviceManagementDAOFactory.beginTransaction(); Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); + if (log.isDebugEnabled()) { + log.debug("Device:" + device.getId() + ":identifier:" + deviceIdentifier.getId()); + } + List installedAppList = getApplicationListForDevice(deviceIdentifier); + + if (log.isDebugEnabled()) { + log.debug("num of apps installed:" + installedAppList.size()); + } List appsToAdd = new ArrayList(); List appIdsToRemove = new ArrayList(); for(Application installedApp:installedAppList){ if (!applications.contains(installedApp)){ + if (log.isDebugEnabled()) { + log.debug("Remove app Id:" + installedApp.getId()); + } appIdsToRemove.add(installedApp.getId()); } } + Application installedApp; + List applicationIds = new ArrayList<>(); + for(Application application:applications){ - if (!installedAppList.contains(application)){ - appsToAdd.add(application); + if (!installedAppList.contains(application)) { + installedApp = applicationDAO.getApplication(application.getApplicationIdentifier(), tenantId); + if (installedApp == null){ + appsToAdd.add(application); + }else{ + applicationIds.add(installedApp.getId()); + } } } + if (log.isDebugEnabled()) { + log.debug("num of apps add:" + appsToAdd.size()); + } + applicationIds.addAll(applicationDAO.addApplications(appsToAdd, tenantId)); - - List applicationIds = applicationDAO.addApplications(appsToAdd, tenantId); + if (log.isDebugEnabled()) { + log.debug("num of app Ids:" + applicationIds.size()); + } applicationMappingDAO.addApplicationMappings(device.getId(), applicationIds, tenantId); + if (log.isDebugEnabled()) { + log.debug("num of remove app Ids:" + appIdsToRemove.size()); + } applicationMappingDAO.removeApplicationMapping(device.getId(), appIdsToRemove,tenantId); - + DeviceManagementDAOFactory.commitTransaction(); } catch (DeviceManagementDAOException deviceDaoEx) { String errorMsg = "Error occurred saving application list to the device"; log.error(errorMsg + ":" + deviceIdentifier.toString()); + try { + DeviceManagementDAOFactory.rollbackTransaction(); + } catch (DeviceManagementDAOException e) { + log.error("Error occurred while roll back transaction",e); + } throw new ApplicationManagementException(errorMsg, deviceDaoEx); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java index df0f80bb926..6f44df6a809 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java @@ -101,13 +101,12 @@ public class ApplicationDAOImpl implements ApplicationDAO { stmt.setInt(8, tenantId); stmt.setObject(9,application.getAppProperties()); stmt.setString(10,application.getApplicationIdentifier()); - stmt.addBatch(); - } - stmt.executeBatch(); + stmt.executeUpdate(); - rs = stmt.getGeneratedKeys(); - while (rs.next()) { - applicationIds.add(rs.getInt(1)); + rs = stmt.getGeneratedKeys(); + if (rs.next()){ + applicationIds.add(rs.getInt(1)); + } } return applicationIds; } catch (SQLException e) { @@ -228,6 +227,7 @@ public class ApplicationDAOImpl implements ApplicationDAO { Application application = new Application(); try { + application.setId(rs.getInt("ID")); application.setName(rs.getString("NAME")); application.setType(rs.getString("TYPE")); 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 4a5ea72e16e..f0c769a56b6 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 @@ -108,11 +108,12 @@ public class ApplicationMappingDAOImpl implements ApplicationMappingDAO { Connection conn; ResultSet rs; int mappingId = -1; + PreparedStatement stmt = null; try { conn = this.getConnection(); String sql = "DELETE DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID = ? AND " + "APPLICATION_ID = ? AND TENANT_ID = ?"; - PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); + stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); for(Integer appId:appIdList){ stmt.setInt(1, deviceId); @@ -123,6 +124,8 @@ public class ApplicationMappingDAOImpl implements ApplicationMappingDAO { stmt.executeBatch(); } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while adding device application mapping", e); + }finally { + DeviceManagementDAOUtil.cleanupResources(stmt, null); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagementProviderServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagementProviderServiceTest.java index 3ca99f5928f..132f73cb176 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagementProviderServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagementProviderServiceTest.java @@ -62,10 +62,12 @@ public class ApplicationManagementProviderServiceTest { Application application1 = TestDataHolder.generateApplicationDummyData("org.wso2.app1"); Application application2 = TestDataHolder.generateApplicationDummyData("org.wso2.app2"); Application application3 = TestDataHolder.generateApplicationDummyData("org.wso2.app3"); + Application application4 = TestDataHolder.generateApplicationDummyData("org.wso2.app4"); applications.add(application1); applications.add(application2); applications.add(application3); + applications.add(application4); Device device = TestDataHolder.initialTestDevice; DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); @@ -83,15 +85,17 @@ public class ApplicationManagementProviderServiceTest { Assert.fail(msg, appMgtEx); } - Application application4 = TestDataHolder.generateApplicationDummyData("org.wso2.app3"); + Application application5 = TestDataHolder.generateApplicationDummyData("org.wso2.app5"); applications = new ArrayList(); applications.add(application4); applications.add(application3); + applications.add(application5); try { appMgtProvider.updateApplicationListInstalledInDevice(deviceIdentifier, applications); List installedApps = appMgtProvider.getApplicationListForDevice(deviceIdentifier); - Assert.assertEquals(installedApps.size(),2,"Num of installed applications should be two"); + log.info("Number of installed applications:"+installedApps.size()); + Assert.assertEquals(installedApps.size(),3,"Num of installed applications should be two"); } catch (ApplicationManagementException appMgtEx){ String msg = "Error occurred while updating app list '" + TestDataHolder.TEST_DEVICE_TYPE + "'"; log.error(msg, appMgtEx);