From f66ff60adad2cd7dad6ee2914e919d841cee8f62 Mon Sep 17 00:00:00 2001 From: Madawa Soysa Date: Thu, 15 Feb 2018 16:42:53 +0530 Subject: [PATCH] Adding a method to retrieve Application by UUID Application install request is made for a UUID which is associated with an application release. In order to get the details of the application to build the payload of the install application operation, it is required to get the application from the database related to the UUID. --- .../common/services/ApplicationManager.java | 8 ++ .../mgt/core/dao/ApplicationDAO.java | 10 +++ .../application/mgt/core/dao/common/Util.java | 33 +++++++ .../GenericApplicationDAOImpl.java | 86 ++++++++++++++++++- .../mgt/core/impl/ApplicationManagerImpl.java | 23 +++++ 5 files changed, 157 insertions(+), 3 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 1dc480a883..3eb431e12c 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 @@ -128,6 +128,14 @@ public interface ApplicationManager { */ Application getApplicationById(int applicationId) throws ApplicationManagementException; + /** + * To get an application associated with the release. + * + * @param appReleaseUUID UUID of the app release + * @return {@link Application} associated with the release + * @throws ApplicationManagementException If unable to retrieve {@link Application} associated with the given UUID + */ + Application getApplicationByRelease(String appReleaseUUID) throws ApplicationManagementException; /** * To get Application with the given UUID. diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java index b34d178dea..e376d038f1 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java @@ -150,5 +150,15 @@ public interface ApplicationDAO { * @throws ApplicationManagementDAOException Application Management DAO Exception. */ void deleteTags(int applicationId) throws ApplicationManagementDAOException; + + /** + * To get an {@link Application} associated with the given release + * + * @param appReleaseUUID UUID of the {@link ApplicationRelease} + * @param tenantId ID of the tenant + * @return {@link Application} associated with the given release UUID + * @throws ApplicationManagementDAOException if unable to fetch the Application from the data store. + */ + Application getApplicationByRelease(String appReleaseUUID, 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/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 f5c5bd33d2..5c48f38486 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 @@ -146,6 +146,39 @@ public class Util { return application; } + + /** + * Populates {@link ApplicationRelease} object with the result obtained from the database. + * + * @param rs {@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(); + + appRelease.setId(rs.getInt("RELEASE_ID")); + appRelease.setVersion(rs.getString("VERSION")); + appRelease.setTenantId(rs.getString("TENANT_ID")); + 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.setApplicationCreator(rs.getString("CREATED_BY")); + appRelease.setCreatedAt(rs.getTimestamp("CREATED_AT")); + appRelease.setPublishedBy(rs.getString("PUBLISHED_BY")); + appRelease.setPublishedAt(rs.getTimestamp("PUBLISHED_AT")); + appRelease.setStars(rs.getInt("STARS")); + 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")); + + return appRelease; + } /** * Cleans up the statement and resultset after executing the query * 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 e3c6bf748a..ca4a69fbf3 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 @@ -21,7 +21,13 @@ package org.wso2.carbon.device.application.mgt.core.dao.impl.application; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.json.JSONException; -import org.wso2.carbon.device.application.mgt.common.*; +import org.wso2.carbon.device.application.mgt.common.Application; +import org.wso2.carbon.device.application.mgt.common.ApplicationList; +import org.wso2.carbon.device.application.mgt.common.ApplicationRelease; +import org.wso2.carbon.device.application.mgt.common.Filter; +import org.wso2.carbon.device.application.mgt.common.Pagination; +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.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; import org.wso2.carbon.device.application.mgt.core.dao.ApplicationDAO; @@ -29,7 +35,12 @@ import org.wso2.carbon.device.application.mgt.core.dao.common.Util; import org.wso2.carbon.device.application.mgt.core.dao.impl.AbstractDAOImpl; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; -import java.sql.*; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; import java.util.List; /** @@ -122,7 +133,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic ResultSet rs = null; int isExist = 0; int index = 0; - String sql = "SELECT * FROM AP_APP WHERE NAME = ? AND TYPE = ? TENANT_ID = ?"; + String sql = "SELECT * FROM AP_APP WHERE NAME = ? AND TYPE = ? AND TENANT_ID = ?"; try{ conn = this.getDBConnection(); conn.setAutoCommit(false); @@ -525,6 +536,75 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } } + @Override + public Application getApplicationByRelease(String appReleaseUUID, int tenantId) + throws ApplicationManagementDAOException { + if (log.isDebugEnabled()){ + log.debug("Getting application with the UUID (" + appReleaseUUID + ") from the database"); + } + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + conn = this.getDBConnection(); + String sql = "SELECT AP_APP_RELEASE.ID AS RELEASE_ID, AP_APP_RELEASE.VERSION, AP_APP_RELEASE.TENANT_ID," + + "AP_APP_RELEASE.UUID, AP_APP_RELEASE.RELEASE_TYPE, AP_APP_RELEASE.APP_PRICE, " + + "AP_APP_RELEASE.STORED_LOCATION, AP_APP_RELEASE.BANNER_LOCATION, AP_APP_RELEASE.SC_1_LOCATION," + + "AP_APP_RELEASE.SC_2_LOCATION, AP_APP_RELEASE.SC_3_LOCATION, AP_APP_RELEASE.APP_HASH_VALUE," + + "AP_APP_RELEASE.SHARED_WITH_ALL_TENANTS, AP_APP_RELEASE.APP_META_INFO, AP_APP_RELEASE.CREATED_BY," + + "AP_APP_RELEASE.CREATED_AT, AP_APP_RELEASE.PUBLISHED_BY, AP_APP_RELEASE.PUBLISHED_AT, " + + "AP_APP_RELEASE.STARS," + + "AP_APP.ID AS APP_ID, AP_APP.NAME AS APP_NAME, AP_APP.TYPE AS APP_TYPE, " + + "AP_APP.APP_CATEGORY AS APP_CATEGORY, AP_APP.IS_FREE, AP_UNRESTRICTED_ROLES.ROLE AS ROLE " + + "FROM AP_APP, AP_UNRESTRICTED_ROLES, AP_APP_RELEASE " + + "WHERE AP_APP_RELEASE.UUID=? AND AP_APP.TENANT_ID=?;"; + + stmt = conn.prepareStatement(sql); + stmt.setString(1, appReleaseUUID); + stmt.setInt(2, tenantId); + rs = stmt.executeQuery(); + + if (log.isDebugEnabled()) { + log.debug("Successfully retrieved details of the application with the UUID " + appReleaseUUID); + } + + Application application = null; + while(rs.next()) { + ApplicationRelease appRelease = Util.readApplicationRelease(rs); + application = new Application(); + + application.setId(rs.getInt("APP_ID")); + application.setName(rs.getString("APP_NAME")); + application.setType(rs.getString("APP_TYPE")); + application.setAppCategory(rs.getString("APP_CATEGORY")); + application.setIsFree(rs.getInt("IS_FREE")); + application.setIsRestricted(rs.getInt("RESTRICTED")); + + UnrestrictedRole unrestrictedRole = new UnrestrictedRole(); + unrestrictedRole.setRole(rs.getString("ROLE")); + List unrestrictedRoleList = new ArrayList<>(); + unrestrictedRoleList.add(unrestrictedRole); + + application.setUnrestrictedRoles(unrestrictedRoleList); + + List applicationReleaseList = new ArrayList<>(); + applicationReleaseList.add(appRelease); + + application.setApplicationReleases(applicationReleaseList); + } + return application; + } catch (SQLException e) { + throw new ApplicationManagementDAOException("Error occurred while getting application details with UUID " + + appReleaseUUID + " While executing query ", e); + } catch (JSONException e) { + throw new ApplicationManagementDAOException("Error occurred while parsing JSON", e); + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); + } finally { + Util.cleanupResources(stmt, rs); + } + } + @Override public int getApplicationId(String appName, String appType, int tenantId) throws ApplicationManagementDAOException { Connection conn; 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 0187525a0d..6375e6ee69 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 @@ -282,6 +282,29 @@ public class ApplicationManagerImpl implements ApplicationManager { } } + @Override + public Application getApplicationByRelease(String appReleaseUUID) throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + Application application; + try { + ConnectionManagerUtil.openDBConnection(); + application = ApplicationManagementDAOFactory.getApplicationDAO() + .getApplicationByRelease(appReleaseUUID, tenantId); + + if (application.getUnrestrictedRoles().isEmpty() || isRoleExists(application.getUnrestrictedRoles(), + userName)) { + return application; + } + return null; + } catch (UserStoreException e) { + throw new ApplicationManagementException( + "User-store exception while getting application with the application UUID " + appReleaseUUID); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + public Boolean verifyApplicationExistenceById(int appId) throws ApplicationManagementException { try { Boolean isAppExist;