From e2acb5c4c89737ccba624f21f724d2a812e6c7de Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Mon, 4 Mar 2019 02:57:08 +0530 Subject: [PATCH] Fix application installing issue --- .../application/mgt/core/dao/common/Util.java | 42 ++++++++++--------- .../mgt/core/impl/ApplicationManagerImpl.java | 34 +++++++++------ 2 files changed, 44 insertions(+), 32 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java index fdfd431925..ed27d1a24d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java @@ -63,12 +63,13 @@ public class Util { while (rs.next()) { if (applicationId != rs.getInt("APP_ID")) { - if (application != null) { applications.add(application); } applicationId = rs.getInt("APP_ID"); application = new Application(); + application.setTags(new ArrayList<>()); + application.setUnrestrictedRoles(new ArrayList<>()); application.setId(applicationId); application.setName(rs.getString("APP_NAME")); application.setType(rs.getString("APP_TYPE")); @@ -76,23 +77,25 @@ public class Util { application.setSubType(rs.getString("SUB_TYPE")); application.setPaymentCurrency(rs.getString("CURRENCY")); application.setIsRestricted(rs.getBoolean("RESTRICTED")); - - List tags = new ArrayList<>(); - tags.add(rs.getString("APP_TAG").toLowerCase()); - application.setTags(tags); - - List unrestrictedRoles = new ArrayList<>(); - unrestrictedRoles.add(rs.getString("ROLE").toLowerCase()); - application.setUnrestrictedRoles(unrestrictedRoles); - } else { - String tag = rs.getString("APP_TAG").toLowerCase(); - String unrestrictedRole = rs.getString("ROLE").toLowerCase(); - if (application != null && !application.getTags().contains(tag)) { + String tag = rs.getString("APP_TAG"); + String unrestrictedRole = rs.getString("ROLE"); + if (tag != null) { application.getTags().add(tag); } - if (application != null && !application.getUnrestrictedRoles().contains(unrestrictedRole)) { + if (unrestrictedRole != null) { application.getUnrestrictedRoles().add(unrestrictedRole); } + } else { + String tag = rs.getString("APP_TAG"); + String unrestrictedRole = rs.getString("ROLE"); + if (application != null) { + if (tag != null && !application.getTags().contains(tag)) { + application.getTags().add(tag); + } + if (unrestrictedRole != null && !application.getUnrestrictedRoles().contains(unrestrictedRole)) { + application.getUnrestrictedRoles().add(unrestrictedRole); + } + } } if (rs.last()) { applications.add(application); @@ -119,6 +122,8 @@ public class Util { while (rs.next()) { if (iteration == 0) { application = new Application(); + application.setTags(new ArrayList<>()); + application.setUnrestrictedRoles(new ArrayList<>()); applicatioId = rs.getInt("APP_ID"); application.setId(applicatioId); application.setName(rs.getString("APP_NAME")); @@ -130,13 +135,12 @@ public class Util { application.setDeviceTypeId(rs.getInt("DEVICE_TYPE_ID")); } - String tag = rs.getString("APP_TAG").toLowerCase(); - String unrestrictedRole = rs.getString("ROLE").toLowerCase(); - if (application.getTags() != null && application.getTags().contains(tag)) { + String tag = rs.getString("APP_TAG"); + String unrestrictedRole = rs.getString("ROLE"); + if (tag != null && !application.getTags().contains(tag)) { application.getTags().add(tag); } - if (application.getUnrestrictedRoles() != null && application.getUnrestrictedRoles() - .contains(unrestrictedRole)) { + if (unrestrictedRole != null && !application.getUnrestrictedRoles().contains(unrestrictedRole)) { application.getUnrestrictedRoles().add(unrestrictedRole); } iteration++; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java index cc95db446f..c3718c1df9 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java @@ -100,17 +100,15 @@ public class ApplicationManagerImpl implements ApplicationManager { if (log.isDebugEnabled()) { log.debug("Create Application received for the tenant : " + tenantId + " From" + " the user : " + userName); } - - ConnectionManagerUtil.openDBConnection(); validateAppCreatingRequest(application, tenantId); validateAppReleasePayload(application.getApplicationReleases().get(0)); DeviceType deviceType; ApplicationRelease applicationRelease; List applicationReleases = new ArrayList<>(); try { - ConnectionManagerUtil.beginDBTransaction(); // Getting the device type details to get device type ID for internal mappings deviceType = Util.getDeviceManagementService().getDeviceType(application.getDeviceType()); + ConnectionManagerUtil.beginDBTransaction(); if (deviceType == null) { log.error("Device type is not matched with application type"); @@ -165,7 +163,8 @@ public class ApplicationManagerImpl implements ApplicationManager { LifecycleState lifecycleState = new LifecycleState(); lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString()); lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString()); - changeLifecycleState(appId, applicationRelease.getUuid(), lifecycleState, false); + lifecycleState.setUpdatedBy(userName); + this.lifecycleStateDAO.addLifecycleState(lifecycleState, appId, applicationRelease.getUuid(), tenantId); applicationRelease.setLifecycleState(lifecycleState); applicationReleases.add(applicationRelease); @@ -192,8 +191,6 @@ public class ApplicationManagerImpl implements ApplicationManager { log.error(msg, e); ConnectionManagerUtil.rollbackDBTransaction(); throw new ApplicationManagementException(msg, e); - } finally { - ConnectionManagerUtil.closeDBConnection(); } } @@ -693,19 +690,30 @@ public class ApplicationManagerImpl implements ApplicationManager { * @param application Application that need to be validated * @throws ValidationException Validation Exception */ - private void validateApplicationExistence(Application application, int tenantId) throws ApplicationManagementException { + private void validateApplicationExistence(Application application, int tenantId) + throws ApplicationManagementException { Filter filter = new Filter(); filter.setFullMatch(true); filter.setAppName(application.getName().trim()); filter.setOffset(0); filter.setLimit(1); + try { + ConnectionManagerUtil.openDBConnection(); + ApplicationList applicationList = applicationDAO.getApplications(filter, tenantId); + if (applicationList != null && applicationList.getApplications() != null && !applicationList + .getApplications().isEmpty()) { + throw new ApplicationManagementException( + "Already an application registered with same name - " + applicationList.getApplications().get(0) + .getName()); + } + } catch (DBConnectionException e) { + throw new ApplicationManagementException("test 1"); - ApplicationList applicationList = applicationDAO.getApplications(filter, tenantId); - if (applicationList != null && applicationList.getApplications() != null && !applicationList.getApplications() - .isEmpty()) { - throw new ApplicationManagementException( - "Already an application registered with same name - " + applicationList.getApplications().get(0) - .getName()); + } catch (ApplicationManagementDAOException e) { + throw new ApplicationManagementException("test 2"); + + } finally { + ConnectionManagerUtil.closeDBConnection(); } }