From abf9c05f0c909eea95414e63fffe2f8fdb5bc6ad Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Thu, 27 Sep 2018 08:15:19 +0530 Subject: [PATCH] Improve applications retreving method --- .../common/services/ApplicationManager.java | 17 +++-- .../mgt/core/dao/ApplicationReleaseDAO.java | 5 +- .../GenericApplicationReleaseDAOImpl.java | 24 +++--- .../mgt/core/impl/ApplicationManagerImpl.java | 73 +++++++++++++++---- 4 files changed, 78 insertions(+), 41 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java index 7d5af46875..44733a2f37 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java @@ -121,14 +121,15 @@ public interface ApplicationManager { */ Boolean isUserAllowable(List unrestrictedRoles, String userName) throws ApplicationManagementException; - /** - * To get all the releases of a particular Application. - * - * @param applicationId ID of the Application to get all the releases. - * @return the List of the Application releases related with the particular Application. - * @throws ApplicationManagementException Application Management Exception. - */ - List getReleases(int applicationId) throws ApplicationManagementException; +// todo +// /** +// * To get all the releases of a particular Application. +// * +// * @param applicationId ID of the Application to get all the releases. +// * @return the List of the Application releases related with the particular Application. +// * @throws ApplicationManagementException Application Management Exception. +// */ +// List getinstallableReleases(int applicationId) throws ApplicationManagementException; /** * To get all the releases of a particular Application. diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java index 1add29c835..a62053a377 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java @@ -58,13 +58,12 @@ public interface ApplicationReleaseDAO { /** * To get all the releases of a particular application. * - * @param applicationName Name of the Application - * @param applicationType Type of the Application + * @param applicationId Id of the Application * @param tenantId tenant id of the application * @return list of the application releases * @throws ApplicationManagementDAOException Application Management DAO Exception. */ - List getReleases(String applicationName, String applicationType, int tenantId) throws + List getReleases(int applicationId, int tenantId) throws ApplicationManagementDAOException; /** diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java index 085c6e3eb2..4445bcf717 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java @@ -118,7 +118,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements Connection connection; PreparedStatement statement = null; ResultSet resultSet = null; - String sql = "SELECT AR.ID AS RELESE_ID, AR.VERSION AS RELEASE_VERSION, AR.UUID AS UUID, AR.RELEASE_TYPE AS " + String sql = "SELECT AR.ID AS RELEASE_ID, AR.VERSION AS RELEASE_VERSION, AR.UUID AS UUID, AR.RELEASE_TYPE AS " + "RELEASE_TYPE, AR.PACKAGE_NAME AS PACKAGE_NAME, AR.APP_PRICE AS APP_PRICE, AR.STORED_LOCATION AS " + "STORED_LOCATION, AR.BANNER_LOCATION AS BANNER_LOCATION, AR.SC_1_LOCATION AS SCREEN_SHOT_1, " + "AR.SC_2_LOCATION AS SCREEN_SHOT_2, AR.SC_3_LOCATION AS SCREEN_SHOT_3, AR.APP_HASH_VALUE AS " @@ -169,7 +169,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements Connection connection; PreparedStatement statement = null; ResultSet resultSet = null; - String sql = "SELECT AR.ID AS RELESE_ID, AR.VERSION AS RELEASE_VERSION, AR.UUID, AR.RELEASE_TYPE, AR.APP_PRICE," + String sql = "SELECT AR.ID AS RELEASE_ID, AR.VERSION AS RELEASE_VERSION, AR.UUID, AR.RELEASE_TYPE, AR.APP_PRICE," + " AR.STORED_LOCATION, AR.BANNER_LOCATION, AR.SC_1_LOCATION AS SCREEN_SHOT_1, " + "AR.SC_2_LOCATION AS SCREEN_SHOT_2, AR.SC_3_LOCATION AS SCREEN_SHOT_3, AR.APP_HASH_VALUE AS " + "HASH_VALUE, AR.SHARED_WITH_ALL_TENANTS AS SHARED, AR.APP_META_INFO, AR.CREATED_BY, AR.CREATED_AT, AR" + @@ -208,33 +208,29 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements /** * To insert the application release properties. * - * @param applicationName Name of the application. - * @param applicationType Type of the application. + * @param applicationId Id of the application. * @param tenantId Tenant Id * @throws ApplicationManagementDAOException Application Management DAO Exception. */ @Override - public List getReleases(String applicationName, String applicationType, int tenantId) + public List getReleases(int applicationId, int tenantId) throws ApplicationManagementDAOException { Connection connection; PreparedStatement statement = null; ResultSet resultSet = null; List applicationReleases = new ArrayList<>(); - String sql = "SELECT AR.ID AS RELESE_ID, AR.VERSION AS RELEASE_VERSION, AR.UUID, AR.RELEASE_TYPE " + String sql = "SELECT AR.ID AS RELEASE_ID, AR.VERSION AS RELEASE_VERSION, AR.UUID, AR.RELEASE_TYPE " + "AS RELEASE_TYPE, AR.PACKAGE_NAME AS PACKAGE_NAME, AR.APP_PRICE, AR.STORED_LOCATION, " + "AR.BANNER_LOCATION, AR.SC_1_LOCATION AS SCREEN_SHOT_1, AR.SC_2_LOCATION AS SCREEN_SHOT_2, " + "AR.SC_3_LOCATION AS SCREEN_SHOT_3, AR.APP_HASH_VALUE AS HASH_VALUE, " + "AR.SHARED_WITH_ALL_TENANTS AS SHARED, AR.APP_META_INFO AS APP_META_INFO, " - + "AR.RATING AS RATING FROM AP_APP_RELEASE AS AR where AR.AP_APP_ID=(SELECT ID FROM AP_APP " - + "WHERE NAME = ? AND TYPE = ? AND TENANT_ID = ?) AND AR.TENANT_ID = ?;"; + + "AR.RATING AS RATING FROM AP_APP_RELEASE AS AR where AR.AP_APP_ID=? AND AR.TENANT_ID = ?;"; try { connection = this.getDBConnection(); statement = connection.prepareStatement(sql); - statement.setString(1, applicationName); - statement.setString(2, applicationType); - statement.setInt(3, tenantId); - statement.setInt(4, tenantId); + statement.setInt(1, applicationId); + statement.setInt(2, tenantId); resultSet = statement.executeQuery(); while (resultSet.next()) { @@ -245,10 +241,10 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements return applicationReleases; } catch (DBConnectionException e) { throw new ApplicationManagementDAOException("Database connection exception while trying to get the " - + "release details of the application with Name " + applicationName, e); + + "release details of the application with app ID: " + applicationId, e); } catch (SQLException e) { throw new ApplicationManagementDAOException( - "Error while getting all the release details of the " + applicationName + " application" + "Error while getting all the release details of the app ID: " + applicationId + ", while executing the query " + sql, e); } finally { Util.cleanupResources(statement, resultSet); 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 52073bdbae..adf98a7c18 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 @@ -31,7 +31,6 @@ import org.wso2.carbon.device.application.mgt.common.ApplicationSubscriptionType import org.wso2.carbon.device.application.mgt.common.ApplicationType; import org.wso2.carbon.device.application.mgt.common.Filter; import org.wso2.carbon.device.application.mgt.common.LifecycleState; -import org.wso2.carbon.device.application.mgt.common.SortingOrder; import org.wso2.carbon.device.application.mgt.common.Tag; import org.wso2.carbon.device.application.mgt.common.UnrestrictedRole; import org.wso2.carbon.device.application.mgt.common.User; @@ -51,7 +50,6 @@ import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManger; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO; import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.user.api.UserRealm; @@ -214,13 +212,13 @@ public class ApplicationManagerImpl implements ApplicationManager { try { ConnectionManagerUtil.getDBConnection(); applicationList = applicationDAO.getApplications(filter, tenantId); - if(applicationList != null && applicationList.getApplications() != null && applicationList + if(applicationList != null && applicationList.getApplications() != null && !applicationList .getApplications().isEmpty()) { if (!isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { applicationList = getRoleRestrictedApplicationList(applicationList, userName); } for (Application application : applicationList.getApplications()) { - applicationReleases = getReleases(application.getId()); + applicationReleases = getReleases(application, false); application.setApplicationReleases(applicationReleases); } } @@ -288,7 +286,7 @@ public class ApplicationManagerImpl implements ApplicationManager { application = ApplicationManagementDAOFactory.getApplicationDAO() .getApplicationById(id, tenantId); if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { - applicationReleases = getReleases(application.getId()); + applicationReleases = getReleases(application, false); application.setApplicationReleases(applicationReleases); return application; } @@ -305,7 +303,7 @@ public class ApplicationManagerImpl implements ApplicationManager { return null; } - applicationReleases = getReleases(application.getId()); + applicationReleases = getReleases(application, false); application.setApplicationReleases(applicationReleases); return application; } catch (UserStoreException e) { @@ -352,7 +350,7 @@ public class ApplicationManagerImpl implements ApplicationManager { application = ApplicationManagementDAOFactory.getApplicationDAO() .getApplication(appName, appType, tenantId); if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { - applicationReleases = getReleases(application.getId()); + applicationReleases = getReleases(application, false); application.setApplicationReleases(applicationReleases); return application; } @@ -369,7 +367,7 @@ public class ApplicationManagerImpl implements ApplicationManager { return null; } - applicationReleases = getReleases(application.getId()); + applicationReleases = getReleases(application, false); application.setApplicationReleases(applicationReleases); return application; } catch (UserStoreException e) { @@ -424,11 +422,38 @@ public class ApplicationManagerImpl implements ApplicationManager { } } - @Override - public List getReleases(int applicationId) throws ApplicationManagementException { +// todo +// public List getinstallableReleases(int applicationId) throws ApplicationManagementException { +// int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); +// +// Application application = getApplicationIfAccessible(applicationId); +// List applicationReleases; +// List filteredApplicationReleases = new ArrayList<>(); +// if (log.isDebugEnabled()) { +// log.debug("Request is received to retrieve all the releases related with the application " + application +// .toString()); +// } +// ConnectionManagerUtil.getDBConnection(); +// applicationReleases = this.applicationReleaseDAO.getReleases(application.getName(), application.getType(), tenantId); +// for (ApplicationRelease applicationRelease : applicationReleases) { +// LifecycleState lifecycleState = ApplicationManagementDAOFactory.getLifecycleStateDAO(). +// getLatestLifeCycleStateByReleaseID(applicationRelease.getId()); +// if (lifecycleState != null) { +// applicationRelease.setLifecycleState(lifecycleState); +// +// if (!AppLifecycleState.REMOVED.toString() +// .equals(applicationRelease.getLifecycleState().getCurrentState())) { +// filteredApplicationReleases.add(applicationRelease); +// } +// } +// } +// return filteredApplicationReleases; +// +// } + + private List getReleases(Application application, boolean requirePublishedRelease) + throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - - Application application = getApplicationIfAccessible(applicationId); List applicationReleases; List filteredApplicationReleases = new ArrayList<>(); if (log.isDebugEnabled()) { @@ -436,7 +461,7 @@ public class ApplicationManagerImpl implements ApplicationManager { .toString()); } ConnectionManagerUtil.getDBConnection(); - applicationReleases = this.applicationReleaseDAO.getReleases(application.getName(), application.getType(), tenantId); + applicationReleases = this.applicationReleaseDAO.getReleases(application.getId(), tenantId); for (ApplicationRelease applicationRelease : applicationReleases) { LifecycleState lifecycleState = ApplicationManagementDAOFactory.getLifecycleStateDAO(). getLatestLifeCycleStateByReleaseID(applicationRelease.getId()); @@ -445,19 +470,34 @@ public class ApplicationManagerImpl implements ApplicationManager { if (!AppLifecycleState.REMOVED.toString() .equals(applicationRelease.getLifecycleState().getCurrentState())) { - filteredApplicationReleases.add(applicationRelease); + if (requirePublishedRelease){ + if (AppLifecycleState.PUBLISHED.toString() + .equals(applicationRelease.getLifecycleState().getCurrentState())){ + filteredApplicationReleases.add(applicationRelease); + } + }else{ + filteredApplicationReleases.add(applicationRelease); + } } } } + + if (requirePublishedRelease && filteredApplicationReleases.size() > 1) { + log.error("There are more than one published application releases for application ID: " + application + .getId()); + } return filteredApplicationReleases; } + + @Override public List deleteApplication(int applicationId) throws ApplicationManagementException { String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); List storedLocations = new ArrayList<>(); + Application application; try { if (!isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { @@ -466,10 +506,11 @@ public class ApplicationManagerImpl implements ApplicationManager { "need to have admin permission"); } - if (getApplicationIfAccessible(applicationId) == null) { + application = getApplicationIfAccessible(applicationId); + if ( application == null) { throw new ApplicationManagementException("Invalid Application"); } - List applicationReleases = getReleases(applicationId); + List applicationReleases = getReleases(application, false); if (log.isDebugEnabled()) { log.debug("Request is received to delete applications which are related with the application id " + applicationId);