From 74be4d8a821f1d8b28c98c0018788fa262837825 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Mon, 1 Oct 2018 18:24:12 +0530 Subject: [PATCH] Improve application releases getting logic --- .../common/services/ApplicationManager.java | 9 --- .../application/mgt/core/dao/common/Util.java | 52 ++++++++------- .../GenericApplicationDAOImpl.java | 2 +- .../GenericApplicationReleaseDAOImpl.java | 63 ++++++------------- .../mgt/core/impl/ApplicationManagerImpl.java | 48 +++++++------- 5 files changed, 76 insertions(+), 98 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 33a05562aeb..81c5af08985 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 @@ -131,15 +131,6 @@ public interface ApplicationManager { */ Boolean isUserAllowable(List unrestrictedRoles, String userName) throws ApplicationManagementException; - /** - * To get the release of a particular Application. - * - * @param applicationId ID of the Application. - * @param state state of the Application. - * @return the List of the Application releases related with the particular Application. - * @throws ApplicationManagementException Application Management Exception. - */ - List getReleaseInState(int applicationId, String state) 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/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 1722a420703..32a4cd36e7d 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 @@ -59,17 +59,17 @@ public class Util { List applications = new ArrayList<>(); Application application = null; - int applicatioId = -1; + int applicationId = -1; while (rs.next()) { - if (applicatioId != rs.getInt("APP_ID")) { + if (applicationId != rs.getInt("APP_ID")) { if (application != null) { applications.add(application); } - applicatioId = rs.getInt("APP_ID"); + applicationId = rs.getInt("APP_ID"); application = new Application(); - application.setId(applicatioId); + application.setId(applicationId); application.setName(rs.getString("APP_NAME")); application.setType(rs.getString("APP_TYPE")); application.setAppCategory(rs.getString("APP_CATEGORY")); @@ -160,29 +160,37 @@ public class Util { /** * Populates {@link ApplicationRelease} object with the result obtained from the database. * - * @param rs {@link ResultSet} from obtained from the database + * @param resultSet {@link ResultSet} from obtained from the database * @return {@link ApplicationRelease} object populated with the data * @throws SQLException If unable to populate {@link ApplicationRelease} object with the data */ - public static ApplicationRelease readApplicationRelease(ResultSet rs) throws SQLException { - ApplicationRelease appRelease = new ApplicationRelease(); + public static ApplicationRelease loadApplicationRelease(ResultSet resultSet) throws SQLException { + ApplicationRelease applicationRelease = new ApplicationRelease(); + applicationRelease.setId(resultSet.getInt("RELEASE_ID")); + applicationRelease.setVersion(resultSet.getString("RELEASE_VERSION")); + applicationRelease.setUuid(resultSet.getString("UUID")); + applicationRelease.setReleaseType(resultSet.getString("RELEASE_TYPE")); + applicationRelease.setPackageName(resultSet.getString("PACKAGE_NAME")); + applicationRelease.setPrice(resultSet.getDouble("APP_PRICE")); + applicationRelease.setAppStoredLoc(resultSet.getString("STORED_LOCATION")); + applicationRelease.setBannerLoc(resultSet.getString("BANNER_LOCATION")); + applicationRelease.setIconLoc(resultSet.getString("ICON_LOCATION")); + applicationRelease.setScreenshotLoc1(resultSet.getString("SCREEN_SHOT_1")); + applicationRelease.setScreenshotLoc2(resultSet.getString("SCREEN_SHOT_2")); + applicationRelease.setScreenshotLoc3(resultSet.getString("SCREEN_SHOT_3")); + applicationRelease.setAppHashValue(resultSet.getString("HASH_VALUE")); + applicationRelease.setIsSharedWithAllTenants(resultSet.getInt("SHARED")); + applicationRelease.setMetaData(resultSet.getString("APP_META_INFO")); + applicationRelease.setRating(resultSet.getDouble("RATING")); - appRelease.setId(rs.getInt("RELEASE_ID")); - appRelease.setVersion(rs.getString("VERSION")); - appRelease.setUuid(rs.getString("UUID")); - appRelease.setReleaseType(rs.getString("RELEASE_TYPE")); - appRelease.setPrice(rs.getDouble("APP_PRICE")); - appRelease.setAppHashValue(rs.getString("APP_HASH_VALUE")); - appRelease.setAppStoredLoc(rs.getString("STORED_LOCATION")); - appRelease.setBannerLoc(rs.getString("BANNER_LOCATION")); - appRelease.setRating(rs.getDouble("RATING")); - appRelease.setIsSharedWithAllTenants(rs.getInt("SHARED_WITH_ALL_TENANTS")); - appRelease.setMetaData(rs.getString("APP_META_INFO")); - appRelease.setScreenshotLoc1(rs.getString("SC_1_LOCATION")); - appRelease.setScreenshotLoc2(rs.getString("SC_2_LOCATION")); - appRelease.setScreenshotLoc3(rs.getString("SC_3_LOCATION")); + LifecycleState lifecycleState = new LifecycleState(); + lifecycleState.setCurrentState(resultSet.getString("CURRENT_STATE")); + lifecycleState.setPreviousState(resultSet.getString("PREVIOUS_STATE")); + lifecycleState.setUpdatedBy(resultSet.getString("UPDATED_BY")); + lifecycleState.setUpdatedAt(resultSet.getTimestamp("UPDATED_AT")); - return appRelease; + applicationRelease.setLifecycleState(lifecycleState); + return applicationRelease; } /** 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/GenericApplicationDAOImpl.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/GenericApplicationDAOImpl.java index 1f365f86ff0..8da27347750 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/GenericApplicationDAOImpl.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/GenericApplicationDAOImpl.java @@ -644,7 +644,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic Application application = null; while (rs.next()) { - ApplicationRelease appRelease = Util.readApplicationRelease(rs); + ApplicationRelease appRelease = Util.loadApplicationRelease(rs); application = new Application(); application.setId(rs.getInt("APP_ID")); 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 a6ae8c2f714..81acab24549 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 @@ -138,7 +138,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements resultSet = statement.executeQuery(); if (resultSet.next()) { - return constructApplicationRelease(resultSet); + return Util.loadApplicationRelease(resultSet); } return null; } catch (DBConnectionException e) { @@ -185,7 +185,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements resultSet = statement.executeQuery(); if (resultSet.next()) { - return constructApplicationRelease(resultSet); + return Util.loadApplicationRelease(resultSet); } return null; } catch (DBConnectionException e) { @@ -219,7 +219,10 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements + "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=? AND AR.TENANT_ID = ?;"; + + "AR.RATING AS RATING AL.CURRENT_STATE AS CURRENT_STATE, AL.PREVIOUS_STATE AS PREVIOUSE_STATE, " + + "AL.UPDATED_BY AS UPDATED_BY, AL.UPDATED_AT AS UPDATED_AT FROM AP_APP_RELEASE " + + "AS AR, AP_APP_LIFECYCLE_STATE AS AL WHERE AR.AP_APP_ID=? AND AL.AP_APP_RELEASE_ID=AR.ID AND " + + "AR.TENANT_ID=? AND AL.TENANT_ID=AR.TENANT_ID ORDER BY AL.UPDATED_AT DESC LIMIT 1;"; try { connection = this.getDBConnection(); @@ -229,9 +232,8 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements resultSet = statement.executeQuery(); while (resultSet.next()) { - ApplicationRelease applicationRelease = constructApplicationRelease(resultSet); + ApplicationRelease applicationRelease = Util.loadApplicationRelease(resultSet); applicationReleases.add(applicationRelease); - } return applicationReleases; } catch (DBConnectionException e) { @@ -253,24 +255,26 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements PreparedStatement statement = null; ResultSet resultSet = null; List applicationReleases = new ArrayList<>(); - 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.ICON_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.RATING FROM AP_APP_RELEASE AS " - + "AR where AR.TENANT_ID = ? AND AR.AP_APP_ID=(SELECT AP_APP_ID" + - " FROM AP_APP_LIFECYCLE_STATE WHERE AP_APP_ID = ? AND CURRENT_STATE = ? AND TENANT_ID = ?);"; + 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, ICON_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, AL.CURRENT_STATE, AL.PREVIOUS_STATE, AL.UPDATED_BY, " + + "AL.UPDATED_AT FROM AP_APP_RELEASE AS AR, AP_APP_LIFECYCLE_STATE AS AL " + + "WHERE AR.AP_APP_ID=? AND AL.AP_APP_RELEASE_ID=AR.ID AND AL.CURRENT_STATE=? AND AR.TENANT_ID=? AND " + + "AL.TENANT_ID=AR.TENANT_ID ORDER BY AL.UPDATED_AT DESC;"; try { connection = this.getDBConnection(); statement = connection.prepareStatement(sql); - statement.setInt(1, tenantId); - statement.setInt(2, appId); - statement.setString(3, state); - statement.setInt(4, tenantId); + statement.setInt(1, appId); + statement.setString(2, state); + statement.setInt(3, tenantId); resultSet = statement.executeQuery(); while (resultSet.next()) { - ApplicationRelease appRelease = constructApplicationRelease(resultSet); + ApplicationRelease appRelease = Util.loadApplicationRelease(resultSet); applicationReleases.add(appRelease); } return applicationReleases; @@ -499,31 +503,4 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements Util.cleanupResources(stmt, rs); } } - - /** - * This method is capable to construct {@link ApplicationRelease} and return the object - * - * @param resultSet result set obtained from the query executing. - * @throws SQLException SQL exception while accessing result set data. - */ - private ApplicationRelease constructApplicationRelease(ResultSet resultSet) throws SQLException { - ApplicationRelease applicationRelease = new ApplicationRelease(); - applicationRelease.setId(resultSet.getInt("RELEASE_ID")); - applicationRelease.setVersion(resultSet.getString("RELEASE_VERSION")); - applicationRelease.setUuid(resultSet.getString("UUID")); - applicationRelease.setReleaseType(resultSet.getString("RELEASE_TYPE")); - applicationRelease.setPackageName(resultSet.getString("PACKAGE_NAME")); - applicationRelease.setPrice(resultSet.getDouble("APP_PRICE")); - applicationRelease.setAppStoredLoc(resultSet.getString("STORED_LOCATION")); - applicationRelease.setBannerLoc(resultSet.getString("BANNER_LOCATION")); - applicationRelease.setIconLoc(resultSet.getString("ICON_LOCATION")); - applicationRelease.setScreenshotLoc1(resultSet.getString("SCREEN_SHOT_1")); - applicationRelease.setScreenshotLoc2(resultSet.getString("SCREEN_SHOT_2")); - applicationRelease.setScreenshotLoc3(resultSet.getString("SCREEN_SHOT_3")); - applicationRelease.setAppHashValue(resultSet.getString("HASH_VALUE")); - applicationRelease.setIsSharedWithAllTenants(resultSet.getInt("SHARED")); - applicationRelease.setMetaData(resultSet.getString("APP_META_INFO")); - applicationRelease.setRating(resultSet.getDouble("RATING")); - return applicationRelease; - } } \ No newline at end of file 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 87334027851..9e2082dca0c 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 @@ -117,6 +117,11 @@ public class ApplicationManagerImpl implements ApplicationManager { ConnectionManagerUtil.rollbackDBTransaction(); return null; } + if (!application.getUnrestrictedRoles().isEmpty()) { + application.setIsRestricted(true); + } else { + application.setIsRestricted(false); + } // Insert to application table int appId = this.applicationDAO.createApplication(application, deviceType.getId()); @@ -134,18 +139,12 @@ public class ApplicationManagerImpl implements ApplicationManager { log.debug("New tags entry added to AP_APP_TAG table. App Id:" + appId); } } - if (!application.getUnrestrictedRoles().isEmpty()) { - application.setIsRestricted(true); + if (application.getIsRestricted()) { this.visibilityDAO.addUnrestrictedRoles(application.getUnrestrictedRoles(), appId, tenantId); if(log.isDebugEnabled()){ log.debug("New restricted roles to app ID mapping added to AP_UNRESTRICTED_ROLE table." + " App Id:" + appId); } - } else { - if(log.isDebugEnabled()){ - log.debug("App is not restricted to role. App Id:" + appId); - } - application.setIsRestricted(false); } if (application.getApplicationReleases().size() > 1 ){ throw new ApplicationManagementException( @@ -284,10 +283,9 @@ public class ApplicationManagerImpl implements ApplicationManager { if (handleConnections) { ConnectionManagerUtil.openDBConnection(); } - application = ApplicationManagementDAOFactory.getApplicationDAO() - .getApplicationById(appId, tenantId); + application = this.applicationDAO.getApplicationById(appId, tenantId); if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { - applicationReleases = getReleaseInState(appId, state); + applicationReleases = getReleaseInState(appId, state, tenantId); application.setApplicationReleases(applicationReleases); return application; } @@ -303,9 +301,7 @@ public class ApplicationManagerImpl implements ApplicationManager { if (!isAppAllowed) { return null; } - if (state != null) { - applicationReleases = getReleaseInState(appId, state); - } + applicationReleases = getReleaseInState(appId, state, tenantId); application.setApplicationReleases(applicationReleases); return application; } catch (UserStoreException e) { @@ -439,8 +435,7 @@ public class ApplicationManagerImpl implements ApplicationManager { for (ApplicationRelease applicationRelease : applicationReleases) { LifecycleState lifecycleState = null; try { - lifecycleState = ApplicationManagementDAOFactory.getLifecycleStateDAO(). - getLatestLifeCycleStateByReleaseID(applicationRelease.getId()); + lifecycleState = this.lifecycleStateDAO.getLatestLifeCycleStateByReleaseID(applicationRelease.getId()); } catch (LifeCycleManagementDAOException e) { throw new ApplicationManagementException( "Error occurred while getting the latest lifecycle state for the application release UUID: " @@ -471,20 +466,27 @@ public class ApplicationManagerImpl implements ApplicationManager { } + private List getReleaseInState(int applicationId, String state, int tenantId) + throws ApplicationManagementException { + List applicationReleases; + List filteredReleases = new ArrayList<>(); - - @Override - public List getReleaseInState(int applicationId, String state) throws - ApplicationManagementException { - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - + // todo check whether this rquired or not Application application = getApplicationIfAccessible(applicationId); if (log.isDebugEnabled()) { log.debug("Request is received to retrieve all the releases related with the application " + application .toString()); } - ConnectionManagerUtil.getDBConnection(); - return this.applicationReleaseDAO.getReleaseByState(applicationId, tenantId, state); + applicationReleases = this.applicationReleaseDAO.getReleases(applicationId, tenantId); + if (state != null && !state.isEmpty()) { + for (ApplicationRelease applicationRelease : applicationReleases) { + if (state.equals(applicationRelease.getLifecycleState().getCurrentState())) { + filteredReleases.add(applicationRelease); + } + } + return filteredReleases; + } + return applicationReleases; } @Override