From 7296e386086ab60a2f5239bdb4c397d9f0263318 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Tue, 2 Jul 2019 23:50:22 +0530 Subject: [PATCH 1/3] Format APPM source --- .../device/application/mgt/common/Filter.java | 10 +- .../mgt/core/dao/ApplicationDAO.java | 8 - .../mgt/core/dao/ApplicationReleaseDAO.java | 10 +- .../GenericApplicationDAOImpl.java | 587 +++++++++--------- .../GenericApplicationReleaseDAOImpl.java | 407 ++++++------ .../mgt/core/impl/ApplicationManagerImpl.java | 22 +- .../mgt/core/impl/ReviewManagerImpl.java | 4 +- 7 files changed, 485 insertions(+), 563 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Filter.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Filter.java index a240bbbb3f..639d077ccc 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Filter.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Filter.java @@ -68,7 +68,7 @@ public class Filter { /** * Category list of the application */ - private List appCategories; + private List categories; /** * Tag list of the application @@ -155,12 +155,12 @@ public class Filter { this.appType = appType; } - public List getAppCategories() { - return appCategories; + public List getCategories() { + return categories; } - public void setAppCategories(List appCategories) { - this.appCategories = appCategories; + public void setCategories(List categories) { + this.categories = categories; } public List getTags() { return tags; } 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 d02301d77c..23066d2427 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 @@ -134,14 +134,6 @@ public interface ApplicationDAO { */ ApplicationDTO getApplication(String releaseUuid, int tenantId) throws ApplicationManagementDAOException; - /** - * To get the UUID of latest app release that satisfy the given criteria. - * - * @param appId application id - * @throws ApplicationManagementDAOException ApplicationDTO Management DAO Exception. - */ - String getUuidOfLatestRelease(int appId) throws ApplicationManagementDAOException; - /** * Verify whether application exist for given application name and device type. Because a name and device type is * unique for an 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 f36aafbeee..edc75ca569 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 @@ -66,7 +66,7 @@ public interface ApplicationReleaseDAO { * @param tenantId Tenant Id * @throws ApplicationManagementDAOException ApplicationDTO Management DAO Exception. */ - Rating getRating(String uuid, int tenantId) throws ApplicationManagementDAOException; + Rating getReleaseRating(String uuid, int tenantId) throws ApplicationManagementDAOException; List getReleaseRatings(String uuid, int tenantId) throws ApplicationManagementDAOException; @@ -101,14 +101,6 @@ public interface ApplicationReleaseDAO { */ String getPackageName(String releaseUuid, int tenantId) throws ApplicationManagementDAOException; - /** - * To verify whether application release exist or not for given application release uuid. - * - * @param uuid UUID of the application release. - * @param tenantId Tenant Id - * @throws ApplicationManagementDAOException Application Management DAO Exception. - */ - boolean isReleaseExist(String uuid, int tenantId) throws ApplicationManagementDAOException; String getReleaseHashValue(String uuid, 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/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 b86cb5c12d..f856de8313 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 @@ -236,41 +236,6 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } } - @Override - public String getUuidOfLatestRelease(int appId) throws ApplicationManagementDAOException { - if (log.isDebugEnabled()) { - log.debug("Getting UUID from the latest app release"); - } - - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - String sql = ""; - String uuId = null; - try { - conn = this.getDBConnection(); - sql += "SELECT APP_RELEASE.UUID AS UUID FROM AP_APP_RELEASE AS APP_RELEASE, AP_APP_LIFECYCLE_STATE " - + "AS LIFECYCLE WHERE APP_RELEASE.AP_APP_ID=? AND APP_RELEASE.ID = LIFECYCLE.AP_APP_RELEASE_ID " - + "AND LIFECYCLE.CURRENT_STATE = ? ORDER BY APP_RELEASE.ID DESC;"; - - stmt = conn.prepareStatement(sql); - stmt.setInt(1, appId); - stmt.setString(2, AppLifecycleState.PUBLISHED.toString()); - rs = stmt.executeQuery(); - if (rs.next()) { - uuId = rs.getString("UUID"); - } - return uuId; - } catch (SQLException e) { - throw new ApplicationManagementDAOException("Error occurred while getting uuid of latest app release", e); - } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection for " - + "getting app release id", e); - } finally { - DAOUtil.cleanupResources(stmt, rs); - } - } - @Override public int getApplicationCount(Filter filter,int deviceTypeId, int tenantId) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { @@ -372,75 +337,73 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } @Override - public ApplicationDTO getApplication(String releaseUuid, int tenantId) - throws ApplicationManagementDAOException { + public ApplicationDTO getApplication(String releaseUuid, int tenantId) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Getting application with the release UUID: " + releaseUuid + " from the database"); } - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; + String sql = "SELECT " + + "AP_APP.ID AS APP_ID, " + + "AP_APP.NAME AS APP_NAME, " + + "AP_APP.DESCRIPTION AS APP_DESCRIPTION, " + + "AP_APP.TYPE AS APP_TYPE, " + + "AP_APP.STATUS AS APP_STATUS, " + + "AP_APP.SUB_TYPE AS APP_SUB_TYPE, " + + "AP_APP.CURRENCY AS APP_CURRENCY, " + + "AP_APP.RATING AS APP_RATING, " + + "AP_APP.DEVICE_TYPE_ID AS APP_DEVICE_TYPE_ID, " + + "AP_APP_RELEASE.ID AS RELEASE_ID, " + + "AP_APP_RELEASE.DESCRIPTION AS RELEASE_DESCRIPTION, " + + "AP_APP_RELEASE.VERSION AS RELEASE_VERSION, " + + "AP_APP_RELEASE.UUID AS RELEASE_UUID, " + + "AP_APP_RELEASE.RELEASE_TYPE AS RELEASE_TYPE, " + + "AP_APP_RELEASE.INSTALLER_LOCATION AS AP_RELEASE_STORED_LOC, " + + "AP_APP_RELEASE.ICON_LOCATION AS AP_RELEASE_ICON_LOC, " + + "AP_APP_RELEASE.BANNER_LOCATION AS AP_RELEASE_BANNER_LOC, " + + "AP_APP_RELEASE.SC_1_LOCATION AS AP_RELEASE_SC1, " + + "AP_APP_RELEASE.SC_2_LOCATION AS AP_RELEASE_SC2, " + + "AP_APP_RELEASE.SC_3_LOCATION AS AP_RELEASE_SC3, " + + "AP_APP_RELEASE.APP_HASH_VALUE AS RELEASE_HASH_VALUE, " + + "AP_APP_RELEASE.APP_PRICE AS RELEASE_PRICE, " + + "AP_APP_RELEASE.APP_META_INFO AS RELEASE_META_INFO, " + + "AP_APP_RELEASE.PACKAGE_NAME AS PACKAGE_NAME, " + + "AP_APP_RELEASE.SUPPORTED_OS_VERSIONS AS RELEASE_SUP_OS_VERSIONS, " + + "AP_APP_RELEASE.RATING AS RELEASE_RATING, " + + "AP_APP_RELEASE.CURRENT_STATE AS RELEASE_CURRENT_STATE, " + + "AP_APP_RELEASE.RATED_USERS AS RATED_USER_COUNT " + + "FROM AP_APP " + + "INNER JOIN AP_APP_RELEASE ON " + + "AP_APP.ID = AP_APP_RELEASE.AP_APP_ID AND " + + "AP_APP.TENANT_ID = AP_APP_RELEASE.TENANT_ID " + + "WHERE " + + "AP_APP.ID = (SELECT AP_APP_RELEASE.AP_APP_ID FROM AP_APP_RELEASE WHERE AP_APP_RELEASE.UUID = ?) " + + "AND AP_APP.TENANT_ID = ?"; try { - conn = this.getDBConnection(); - String sql = "SELECT " - + "AP_APP.ID AS APP_ID, " - + "AP_APP.NAME AS APP_NAME, " - + "AP_APP.DESCRIPTION AS APP_DESCRIPTION, " - + "AP_APP.TYPE AS APP_TYPE, " - + "AP_APP.STATUS AS APP_STATUS, " - + "AP_APP.SUB_TYPE AS APP_SUB_TYPE, " - + "AP_APP.CURRENCY AS APP_CURRENCY, " - + "AP_APP.RATING AS APP_RATING, " - + "AP_APP.DEVICE_TYPE_ID AS APP_DEVICE_TYPE_ID, " - + "AP_APP_RELEASE.ID AS RELEASE_ID, " - + "AP_APP_RELEASE.DESCRIPTION AS RELEASE_DESCRIPTION, " - + "AP_APP_RELEASE.VERSION AS RELEASE_VERSION, " - + "AP_APP_RELEASE.UUID AS RELEASE_UUID, " - + "AP_APP_RELEASE.RELEASE_TYPE AS RELEASE_TYPE, " - + "AP_APP_RELEASE.INSTALLER_LOCATION AS AP_RELEASE_STORED_LOC, " - + "AP_APP_RELEASE.ICON_LOCATION AS AP_RELEASE_ICON_LOC, " - + "AP_APP_RELEASE.BANNER_LOCATION AS AP_RELEASE_BANNER_LOC, " - + "AP_APP_RELEASE.SC_1_LOCATION AS AP_RELEASE_SC1, " - + "AP_APP_RELEASE.SC_2_LOCATION AS AP_RELEASE_SC2, " - + "AP_APP_RELEASE.SC_3_LOCATION AS AP_RELEASE_SC3, " - + "AP_APP_RELEASE.APP_HASH_VALUE AS RELEASE_HASH_VALUE, " - + "AP_APP_RELEASE.APP_PRICE AS RELEASE_PRICE, " - + "AP_APP_RELEASE.APP_META_INFO AS RELEASE_META_INFO, " - + "AP_APP_RELEASE.PACKAGE_NAME AS PACKAGE_NAME, " - + "AP_APP_RELEASE.SUPPORTED_OS_VERSIONS AS RELEASE_SUP_OS_VERSIONS, " - + "AP_APP_RELEASE.RATING AS RELEASE_RATING, " - + "AP_APP_RELEASE.CURRENT_STATE AS RELEASE_CURRENT_STATE, " - + "AP_APP_RELEASE.RATED_USERS AS RATED_USER_COUNT " - + "FROM AP_APP " - + "INNER JOIN AP_APP_RELEASE ON " - + "AP_APP.ID = AP_APP_RELEASE.AP_APP_ID AND " - + "AP_APP.TENANT_ID = AP_APP_RELEASE.TENANT_ID " - + "WHERE " - + "AP_APP.ID = (SELECT AP_APP_RELEASE.AP_APP_ID FROM AP_APP_RELEASE WHERE AP_APP_RELEASE.UUID = ?) " - + "AND AP_APP.TENANT_ID = ?"; - - stmt = conn.prepareStatement(sql); - stmt.setString(1, releaseUuid); - stmt.setInt(2, tenantId); - rs = stmt.executeQuery(); - - if (log.isDebugEnabled()) { - log.debug("Successfully retrieved basic details of the application for the application release UUID: " - + releaseUuid); + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)){ + stmt.setString(1, releaseUuid); + stmt.setInt(2, tenantId); + try (ResultSet rs = stmt.executeQuery()) { + if (log.isDebugEnabled()) { + log.debug("Successfully retrieved basic details of the application for the application " + + "release UUID: " + releaseUuid); + } + return DAOUtil.loadApplication(rs); + } } - - return DAOUtil.loadApplication(rs); - - } catch (SQLException e) { - throw new ApplicationManagementDAOException( - "Error occurred while getting application details with app release uuid " + releaseUuid + - " while executing query.", e); } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); + String msg = "Error occurred while obtaining the DB connection to get application for application release " + + "UUID: " + releaseUuid; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while getting application details with app release uuid " + releaseUuid + + " while executing query. Executed query: " + sql; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } catch (UnexpectedServerErrorException e) { - throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); - } finally { - DAOUtil.cleanupResources(stmt, rs); + String msg = "Found more than one application for application release UUID: " + releaseUuid; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } } @@ -450,86 +413,84 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic if (log.isDebugEnabled()) { log.debug("Getting application with the id (" + applicationId + ") from the database"); } - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; + String sql = "SELECT " + + "AP_APP.ID AS APP_ID, " + + "AP_APP.NAME AS APP_NAME, " + + "AP_APP.DESCRIPTION AS APP_DESCRIPTION, " + + "AP_APP.TYPE AS APP_TYPE, " + + "AP_APP.STATUS AS APP_STATUS, " + + "AP_APP.SUB_TYPE AS APP_SUB_TYPE, " + + "AP_APP.CURRENCY AS APP_CURRENCY, " + + "AP_APP.RATING AS APP_RATING, " + + "AP_APP.DEVICE_TYPE_ID AS APP_DEVICE_TYPE_ID, " + + "AP_APP_RELEASE.ID AS RELEASE_ID, " + + "AP_APP_RELEASE.DESCRIPTION AS RELEASE_DESCRIPTION, " + + "AP_APP_RELEASE.VERSION AS RELEASE_VERSION, " + + "AP_APP_RELEASE.UUID AS RELEASE_UUID, " + + "AP_APP_RELEASE.RELEASE_TYPE AS RELEASE_TYPE, " + + "AP_APP_RELEASE.INSTALLER_LOCATION AS AP_RELEASE_STORED_LOC, " + + "AP_APP_RELEASE.ICON_LOCATION AS AP_RELEASE_ICON_LOC, " + + "AP_APP_RELEASE.BANNER_LOCATION AS AP_RELEASE_BANNER_LOC, " + + "AP_APP_RELEASE.SC_1_LOCATION AS AP_RELEASE_SC1, " + + "AP_APP_RELEASE.SC_2_LOCATION AS AP_RELEASE_SC2, " + + "AP_APP_RELEASE.SC_3_LOCATION AS AP_RELEASE_SC3, " + + "AP_APP_RELEASE.APP_HASH_VALUE AS RELEASE_HASH_VALUE, " + + "AP_APP_RELEASE.APP_PRICE AS RELEASE_PRICE, " + + "AP_APP_RELEASE.APP_META_INFO AS RELEASE_META_INFO, " + + "AP_APP_RELEASE.PACKAGE_NAME AS PACKAGE_NAME, " + + "AP_APP_RELEASE.SUPPORTED_OS_VERSIONS AS RELEASE_SUP_OS_VERSIONS, " + + "AP_APP_RELEASE.RATING AS RELEASE_RATING, " + + "AP_APP_RELEASE.CURRENT_STATE AS RELEASE_CURRENT_STATE, " + + "AP_APP_RELEASE.RATED_USERS AS RATED_USER_COUNT " + + "FROM AP_APP " + + "INNER JOIN AP_APP_RELEASE ON " + + "AP_APP.ID = AP_APP_RELEASE.AP_APP_ID AND " + + "AP_APP.TENANT_ID = AP_APP_RELEASE.TENANT_ID " + + "WHERE " + + "AP_APP.ID =? AND " + + "AP_APP.TENANT_ID = ?"; try { - conn = this.getDBConnection(); - String sql = "SELECT " - + "AP_APP.ID AS APP_ID, " - + "AP_APP.NAME AS APP_NAME, " - + "AP_APP.DESCRIPTION AS APP_DESCRIPTION, " - + "AP_APP.TYPE AS APP_TYPE, " - + "AP_APP.STATUS AS APP_STATUS, " - + "AP_APP.SUB_TYPE AS APP_SUB_TYPE, " - + "AP_APP.CURRENCY AS APP_CURRENCY, " - + "AP_APP.RATING AS APP_RATING, " - + "AP_APP.DEVICE_TYPE_ID AS APP_DEVICE_TYPE_ID, " - + "AP_APP_RELEASE.ID AS RELEASE_ID, " - + "AP_APP_RELEASE.DESCRIPTION AS RELEASE_DESCRIPTION, " - + "AP_APP_RELEASE.VERSION AS RELEASE_VERSION, " - + "AP_APP_RELEASE.UUID AS RELEASE_UUID, " - + "AP_APP_RELEASE.RELEASE_TYPE AS RELEASE_TYPE, " - + "AP_APP_RELEASE.INSTALLER_LOCATION AS AP_RELEASE_STORED_LOC, " - + "AP_APP_RELEASE.ICON_LOCATION AS AP_RELEASE_ICON_LOC, " - + "AP_APP_RELEASE.BANNER_LOCATION AS AP_RELEASE_BANNER_LOC, " - + "AP_APP_RELEASE.SC_1_LOCATION AS AP_RELEASE_SC1, " - + "AP_APP_RELEASE.SC_2_LOCATION AS AP_RELEASE_SC2, " - + "AP_APP_RELEASE.SC_3_LOCATION AS AP_RELEASE_SC3, " - + "AP_APP_RELEASE.APP_HASH_VALUE AS RELEASE_HASH_VALUE, " - + "AP_APP_RELEASE.APP_PRICE AS RELEASE_PRICE, " - + "AP_APP_RELEASE.APP_META_INFO AS RELEASE_META_INFO, " - + "AP_APP_RELEASE.PACKAGE_NAME AS PACKAGE_NAME, " - + "AP_APP_RELEASE.SUPPORTED_OS_VERSIONS AS RELEASE_SUP_OS_VERSIONS, " - + "AP_APP_RELEASE.RATING AS RELEASE_RATING, " - + "AP_APP_RELEASE.CURRENT_STATE AS RELEASE_CURRENT_STATE, " - + "AP_APP_RELEASE.RATED_USERS AS RATED_USER_COUNT " - + "FROM AP_APP " - + "INNER JOIN AP_APP_RELEASE ON " - + "AP_APP.ID = AP_APP_RELEASE.AP_APP_ID AND " - + "AP_APP.TENANT_ID = AP_APP_RELEASE.TENANT_ID " - + "WHERE " - + "AP_APP.ID =? AND " - + "AP_APP.TENANT_ID = ?"; - - stmt = conn.prepareStatement(sql); - stmt.setInt(1, applicationId); - stmt.setInt(2, tenantId); - rs = stmt.executeQuery(); - if (log.isDebugEnabled()) { - log.debug("Successfully retrieved basic details of the application with the id " - + applicationId); + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, applicationId); + stmt.setInt(2, tenantId); + try (ResultSet rs = stmt.executeQuery()) { + if (log.isDebugEnabled()) { + log.debug("Successfully retrieved basic details of the application with the id " + + applicationId); + } + return DAOUtil.loadApplication(rs); + } } - return DAOUtil.loadApplication(rs); - } catch (SQLException e) { - throw new ApplicationManagementDAOException( - "Error occurred while getting application details with app id " + applicationId + - " 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); + String msg = "Error occurred while obtaining the DB connection to get application for application ID: " + + applicationId; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred to get application details with app id " + applicationId + " while executing " + + "query. Query: " + sql; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } catch (UnexpectedServerErrorException e) { - throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); - } finally { - DAOUtil.cleanupResources(stmt, rs); + String msg = "Found more than one application for application ID: " + applicationId; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } } @Override public boolean updateApplication(ApplicationDTO applicationDTO, int tenantId) throws ApplicationManagementDAOException { - Connection conn; + String sql = "UPDATE AP_APP AP " + + "SET " + + "AP.NAME = ?, " + + "AP.DESCRIPTION = ?, " + + "AP.SUB_TYPE = ?, " + + "AP.CURRENCY = ? " + + "WHERE AP.ID = ? AND AP.TENANT_ID = ?"; try { - conn = this.getDBConnection(); - String sql = "UPDATE AP_APP AP " + - "SET " + - "AP.NAME = ?, " + - "AP.DESCRIPTION = ?, " + - "AP.SUB_TYPE = ?, " + - "AP.CURRENCY = ? " + - "WHERE AP.ID = ? AND AP.TENANT_ID = ?"; - + Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setString(1, applicationDTO.getName()); stmt.setString(2, applicationDTO.getDescription()); @@ -544,7 +505,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic log.error(msg); throw new ApplicationManagementDAOException(msg, e); } catch (SQLException e) { - String msg = "Error occurred when obtaining database connection for updating the application."; + String msg = "Error occurred when executing SQL to update an application. Executed query: " + sql; log.error(msg); throw new ApplicationManagementDAOException(msg, e); } @@ -553,16 +514,14 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic @Override public void updateApplicationRating(String uuid, double rating, int tenantId) throws ApplicationManagementDAOException { - Connection conn; + String sql = "UPDATE AP_APP AP " + + "SET " + + "AP.RATING = ? " + + "WHERE " + + "AP.ID = (SELECT AP_APP_ID FROM AP_APP_RELEASE WHERE UUID = ?) AND " + + "AP.TENANT_ID = ?"; try { - conn = this.getDBConnection(); - String sql = "UPDATE AP_APP AP " + - "SET " + - "AP.RATING = ? " + - "WHERE " + - "AP.ID = (SELECT AP_APP_ID FROM AP_APP_RELEASE WHERE UUID = ?) AND " + - "AP.TENANT_ID = ?"; - + Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setDouble(1, rating); stmt.setString(2, uuid); @@ -583,22 +542,27 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic @Override public void retireApplication(int appId) throws ApplicationManagementDAOException { - Connection conn; - PreparedStatement stmt = null; + String sql = "UPDATE " + + "AP_APP " + + "SET STATUS = ? " + + "WHERE ID = ? "; try { - conn = this.getDBConnection(); - String sql = "UPDATE AP_APP SET STATUS = ? WHERE ID = ? "; - stmt = conn.prepareStatement(sql); - stmt.setString(1, AppLifecycleState.RETIRED.toString()); - stmt.setInt(2, appId); - stmt.executeUpdate(); - + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)){ + stmt.setString(1, AppLifecycleState.RETIRED.toString()); + stmt.setInt(2, appId); + stmt.executeUpdate(); + } } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); + String msg = "Error occurred while obtaining the DB connection to retire application which has application " + + "ID: " + appId; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } catch (SQLException e) { - throw new ApplicationManagementDAOException("Error occurred while deleting the application: ", e); - } finally { - DAOUtil.cleanupResources(stmt, null); + String msg = "Error occurred while executing SQL to retire an application which has application ID " + + appId; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } } @@ -607,29 +571,28 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic if (log.isDebugEnabled()) { log.debug("Request received in DAO Layer to add tags"); } - Connection conn; - PreparedStatement stmt = null; String sql = "INSERT INTO AP_APP_TAG " - + "(TAG," + + "(TAG, " + " TENANT_ID) " + "VALUES (?, ?)"; try { - conn = this.getDBConnection(); - stmt = conn.prepareStatement(sql); - for (String tag : tags) { - stmt.setString(1, tag); - stmt.setInt(2, tenantId); - stmt.addBatch(); + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + for (String tag : tags) { + stmt.setString(1, tag); + stmt.setInt(2, tenantId); + stmt.addBatch(); + } + stmt.executeBatch(); } - stmt.executeBatch(); - } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException( - "Error occurred while obtaining the DB connection when adding tags", e); + String msg = "Error occurred while obtaining the DB connection when adding tags"; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } catch (SQLException e) { - throw new ApplicationManagementDAOException("Error occurred while adding tags", e); - } finally { - DAOUtil.cleanupResources(stmt, null); + String msg = "SQL Error occurred while adding tags. Executed Query: " + sql; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } } @@ -638,72 +601,70 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic if (log.isDebugEnabled()) { log.debug("Request received in DAO Layer to get all tags"); } - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; + String sql = "SELECT " + + "AP_APP_TAG.ID AS ID, " + + "AP_APP_TAG.TAG AS TAG " + + "FROM AP_APP_TAG " + + "WHERE TENANT_ID = ?"; try { List tagEntities = new ArrayList<>(); - String sql = "SELECT " - + "AP_APP_TAG.ID AS ID, " - + "AP_APP_TAG.TAG AS TAG " - + "FROM AP_APP_TAG " - + "WHERE TENANT_ID = ?"; - conn = this.getDBConnection(); - stmt = conn.prepareStatement(sql); - stmt.setInt(1, tenantId); - rs = stmt.executeQuery(); - - while(rs.next()){ - TagDTO tagDTO = new TagDTO(); - tagDTO.setId(rs.getInt("ID")); - tagDTO.setTagName(rs.getString("TAG")); - tagEntities.add(tagDTO); + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + try (ResultSet rs = stmt.executeQuery()) { + while (rs.next()) { + TagDTO tagDTO = new TagDTO(); + tagDTO.setId(rs.getInt("ID")); + tagDTO.setTagName(rs.getString("TAG")); + tagEntities.add(tagDTO); + } + return tagEntities; + } } - return tagEntities; } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException( - "Error occurred while obtaining the DB connection when adding tags", e); + String msg = "Error occurred while obtaining the DB connection when getting all tags"; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } catch (SQLException e) { - throw new ApplicationManagementDAOException("Error occurred while adding tags", e); - } finally { - DAOUtil.cleanupResources(stmt, rs); + String msg = "SQL Error occurred while getting all tags"; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } } @Override public List getAllCategories(int tenantId) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { - log.debug("Request received in DAO Layer to get all tags"); + log.debug("Request received in DAO Layer to get all categories."); } - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; + String sql = "SELECT " + + "AP_APP_CATEGORY.ID AS ID, " + + "AP_APP_CATEGORY.CATEGORY AS CATEGORY " + + "FROM AP_APP_CATEGORY " + + "WHERE TENANT_ID = ?"; try { List categories = new ArrayList<>(); - String sql = "SELECT " - + "AP_APP_CATEGORY.ID AS ID, " - + "AP_APP_CATEGORY.CATEGORY AS CATEGORY " - + "FROM AP_APP_CATEGORY " - + "WHERE TENANT_ID = ?"; - conn = this.getDBConnection(); - stmt = conn.prepareStatement(sql); - stmt.setInt(1, tenantId); - rs = stmt.executeQuery(); - - while(rs.next()){ - CategoryDTO category = new CategoryDTO(); - category.setId(rs.getInt("ID")); - category.setCategoryName(rs.getString("CATEGORY")); - categories.add(category); + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + try (ResultSet rs = stmt.executeQuery()) { + while (rs.next()) { + CategoryDTO category = new CategoryDTO(); + category.setId(rs.getInt("ID")); + category.setCategoryName(rs.getString("CATEGORY")); + categories.add(category); + } + return categories; + } } - return categories; } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException( - "Error occurred while obtaining the DB connection when getting categories", e); + String msg = "Error occurred while obtaining the DB connection when getting all categories."; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } catch (SQLException e) { - throw new ApplicationManagementDAOException("Error occurred while getting categories", e); - } finally { - DAOUtil.cleanupResources(stmt, rs); + String msg = "SQL Error occurred while getting all categories. Executed query: " + sql; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } } @@ -711,14 +672,15 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic public List getCategoryIdsForCategoryNames(List categoryNames, int tenantId) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { - log.debug("Request received in DAO Layer to get tag ids for given tag names"); + log.debug("Request received in DAO Layer to get category ids for given category names"); } try { Connection conn = this.getDBConnection(); int index = 1; List tagIds = new ArrayList<>(); StringJoiner joiner = new StringJoiner(",", - "SELECT AP_APP_CATEGORY.ID AS ID FROM AP_APP_CATEGORY WHERE AP_APP_CATEGORY.CATEGORY IN (", ") AND TENANT_ID = ?"); + "SELECT AP_APP_CATEGORY.ID AS ID FROM AP_APP_CATEGORY WHERE AP_APP_CATEGORY.CATEGORY IN (", + ") AND TENANT_ID = ?"); categoryNames.stream().map(ignored -> "?").forEach(joiner::add); String query = joiner.toString(); try (PreparedStatement ps = conn.prepareStatement(query)) { @@ -734,17 +696,21 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } return tagIds; } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException( - "Error occurred while obtaining the DB connection when getting categories", e); + String msg = "Error occurred while obtaining the DB connection when getting category ids for given " + + "category names"; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } catch (SQLException e) { - throw new ApplicationManagementDAOException("Error occurred while getting categories", e); + String msg = "SQL Error occurred while getting all categories."; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } } @Override public List getDistinctCategoryIdsInCategoryMapping() throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { - log.debug("Request received in DAO Layer to get distinct category ids for given tag names"); + log.debug("Request received in DAO Layer to get distinct category ids in category mapping."); } try { Connection conn = this.getDBConnection(); @@ -759,12 +725,14 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } return distinctCategoryIds; } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException( - "Error occurred while obtaining the DB connection when getting distinct category ids in tag mapping", - e); + String msg = "Error occurred while obtaining the DB connection when getting distinct category ids in " + + "category mapping"; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } catch (SQLException e) { - throw new ApplicationManagementDAOException( - "Error occurred while getting distinct category ids in tag mapping", e); + String msg = "SQL Error occurred while getting distinct category ids in category mapping."; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } } @@ -773,12 +741,12 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic if (log.isDebugEnabled()) { log.debug("Request received in DAO Layer to get category for given category name."); } + String sql = "SELECT AP_APP_CATEGORY.ID AS ID " + + "FROM AP_APP_CATEGORY " + + "WHERE AP_APP_CATEGORY.CATEGORY = ? AND " + + "AP_APP_CATEGORY.TENANT_ID = ?"; try { Connection conn = this.getDBConnection(); - String sql = "SELECT AP_APP_CATEGORY.ID AS ID" - + " FROM AP_APP_CATEGORY " - + "WHERE AP_APP_CATEGORY.CATEGORY = ? AND " - + "AP_APP_CATEGORY.TENANT_ID = ?"; try (PreparedStatement ps = conn.prepareStatement(sql)) { ps.setString(1, categoryName); ps.setInt(2, tenantId); @@ -789,80 +757,81 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic categoryDTO.setCategoryName(categoryName); return categoryDTO; } + return null; } } - return null; } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException( - "Error occurred while obtaining the DB connection when getting category Id for given category name", - e); + String msg = "Error occurred while obtaining the DB connection when getting category for given category " + + "name."; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } catch (SQLException e) { - throw new ApplicationManagementDAOException( - "SQL Error occurred while getting category Id for category name.", e); + String msg = "SQL Error occurred while getting category for category name. Executed query " + sql; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } } @Override public void addCategories(List categories, int tenantId) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { - log.debug("Request received in DAO Layer to add tags"); + log.debug("DAO Request received in DAO Layer to add categories."); } - Connection conn; - PreparedStatement stmt = null; String sql = "INSERT INTO AP_APP_CATEGORY " + "(CATEGORY," + " TENANT_ID) " + "VALUES (?, ?)"; try { - conn = this.getDBConnection(); - stmt = conn.prepareStatement(sql); - for (String category : categories) { - stmt.setString(1, category); - stmt.setInt(2, tenantId); - stmt.addBatch(); + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + for (String category : categories) { + stmt.setString(1, category); + stmt.setInt(2, tenantId); + stmt.addBatch(); + } + stmt.executeBatch(); } - stmt.executeBatch(); - } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException( - "Error occurred while obtaining the DB connection when adding categories.", e); + String msg = "Error occurred while obtaining the DB connection when adding categories."; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } catch (SQLException e) { - throw new ApplicationManagementDAOException("Error occurred while adding categories.", e); - } finally { - DAOUtil.cleanupResources(stmt, null); + String msg = "SQL Error occurred while adding categories. Executed query " + sql; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } } @Override - public void addCategoryMapping (List categoryIds, int applicationId, int tenantId) throws ApplicationManagementDAOException { + public void addCategoryMapping(List categoryIds, int applicationId, int tenantId) + throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { - log.debug("Request received in DAO Layer to add categories"); + log.debug("Request received in DAO Layer to add category mappings"); } - Connection conn; - PreparedStatement stmt = null; String sql = "INSERT INTO AP_APP_CATEGORY_MAPPING " + "(AP_APP_CATEGORY_ID, " + "AP_APP_ID, " + " TENANT_ID) " + "VALUES (?, ?, ?)"; try { - conn = this.getDBConnection(); - stmt = conn.prepareStatement(sql); - for (Integer categoryId : categoryIds) { - stmt.setInt(1, categoryId); - stmt.setInt(2, applicationId); - stmt.setInt(3, tenantId); - stmt.addBatch(); + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + for (Integer categoryId : categoryIds) { + stmt.setInt(1, categoryId); + stmt.setInt(2, applicationId); + stmt.setInt(3, tenantId); + stmt.addBatch(); + } + stmt.executeBatch(); } - stmt.executeBatch(); - } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException( - "Error occurred while obtaining the DB connection when adding data into category mapping.", e); + String msg = "Error occurred while obtaining the DB connection when adding data into category mapping."; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } catch (SQLException e) { - throw new ApplicationManagementDAOException("Error occurred while adding data into category mapping.", e); - } finally { - DAOUtil.cleanupResources(stmt, null); + String msg = "SQL Error occurred while adding data into category mapping."; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } } 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 ad9efe14b7..fee06cdb52 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 @@ -191,52 +191,51 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements } @Override - public Rating getRating(String uuid, int tenantId) throws ApplicationManagementDAOException { - Connection connection; - PreparedStatement statement = null; - ResultSet resultSet = null; - Rating rating = null; + public Rating getReleaseRating(String uuid, int tenantId) throws ApplicationManagementDAOException { String sql = "SELECT " + "RATING, " + "RATED_USERS " + "FROM AP_APP_RELEASE " + "WHERE UUID = ? AND TENANT_ID = ?"; try { - connection = this.getDBConnection(); - statement = connection.prepareStatement(sql); - statement.setString(1, uuid); - statement.setInt(2, tenantId); - resultSet = statement.executeQuery(); - - if (resultSet.next()) { - rating = new Rating(); - rating.setRatingValue(resultSet.getDouble("RATING")); - rating.setNoOfUsers(resultSet.getInt("RATED_USERS")); + Connection connection = this.getDBConnection(); + try (PreparedStatement statement = connection.prepareStatement(sql)) { + statement.setString(1, uuid); + statement.setInt(2, tenantId); + try (ResultSet resultSet = statement.executeQuery()) { + if (resultSet.next()) { + Rating rating = new Rating(); + rating.setRatingValue(resultSet.getDouble("RATING")); + rating.setNoOfUsers(resultSet.getInt("RATED_USERS")); + return rating; + } + } } - return rating; + return null; } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException( - "Database connection exception while trying to update the application release", e); + String msg = "Database connection error occured when try to get application release rating which has " + + "application release UUID: " + uuid; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } catch (SQLException e) { - throw new ApplicationManagementDAOException( - "SQL exception while updating the release ,while executing the query " + sql, e); - } finally { - DAOUtil.cleanupResources(statement, resultSet); + String msg = "SQL exception occured whn processing query: " + sql + " to get application release rating " + + "which has application release uuid: " + uuid; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } } @Override public List getReleaseRatings(String uuid, int tenantId) throws ApplicationManagementDAOException { - Connection connection; List ratingValues = new ArrayList<>(); String sql = "SELECT " + "RATING " + "FROM AP_APP_RELEASE " + "WHERE " - + "AP_APP_ID = (SELECT AP_APP_ID FROM AP_APP_RELEASE WHERE UUID = ?) AND " + + "AP_APP_ID = (SELECT AP_APP_ID FROM AP_APP_RELEASE WHERE UUID = ?) AND " + "TENANT_ID = ?"; try { - connection = this.getDBConnection(); + Connection connection = this.getDBConnection(); try (PreparedStatement statement = connection.prepareStatement(sql)) { statement.setString(1, uuid); statement.setInt(2, tenantId); @@ -247,26 +246,21 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements } } } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException( - "Database connection exception while trying to update the application release", e); + String msg = "Database connection exception occurred when getting all release rating values for a " + + "particular application."; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } catch (SQLException e) { - throw new ApplicationManagementDAOException( - "SQL exception while updating the release ,while executing the query " + sql, e); + String msg = "SQL exception occurred while getting all release rating values for a particular application. " + + "Executed query is" + sql; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } } - - /** - * To insert the application release properties. - * - * @param applicationReleaseDTO ApplicationDTO Release the properties of which that need to be inserted. - * @throws ApplicationManagementDAOException ApplicationDTO Management DAO Exception. - */ @Override public ApplicationReleaseDTO updateRelease(ApplicationReleaseDTO applicationReleaseDTO, int tenantId) throws ApplicationManagementDAOException { - Connection connection; - PreparedStatement statement = null; String sql = "UPDATE AP_APP_RELEASE " + "SET " + "DESCRIPTION = ?, " @@ -286,89 +280,95 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements + "APP_META_INFO = ?, " + "SUPPORTED_OS_VERSIONS = ?, " + "CURRENT_STATE = ? " - + "WHERE ID = ? AND TENANT_ID = ? "; + + "WHERE ID = ? AND TENANT_ID = ? "; try { - connection = this.getDBConnection(); - statement = connection.prepareStatement(sql); - statement.setString(1, applicationReleaseDTO.getDescription()); - statement.setString(2, applicationReleaseDTO.getVersion()); - statement.setString(3, applicationReleaseDTO.getUuid()); - statement.setString(4, applicationReleaseDTO.getReleaseType()); - statement.setString(5, applicationReleaseDTO.getPackageName()); - statement.setDouble(6, applicationReleaseDTO.getPrice()); - statement.setString(7, applicationReleaseDTO.getInstallerName()); - statement.setString(8, applicationReleaseDTO.getBannerName()); - statement.setString(9, applicationReleaseDTO.getIconName()); - statement.setString(10, applicationReleaseDTO.getScreenshotName1()); - statement.setString(11, applicationReleaseDTO.getScreenshotName2()); - statement.setString(12, applicationReleaseDTO.getScreenshotName3()); - statement.setString(13, applicationReleaseDTO.getAppHashValue()); - statement.setBoolean(14, applicationReleaseDTO.getIsSharedWithAllTenants()); - statement.setString(15, applicationReleaseDTO.getMetaData()); - statement.setString(16, applicationReleaseDTO.getSupportedOsVersions()); - statement.setString(17, applicationReleaseDTO.getCurrentState().toUpperCase()); - statement.setInt(18, applicationReleaseDTO.getId()); - statement.setInt(19, tenantId); - if (statement.executeUpdate() == 0) { - return null; + Connection connection = this.getDBConnection(); + try (PreparedStatement statement = connection.prepareStatement(sql)) { + statement.setString(1, applicationReleaseDTO.getDescription()); + statement.setString(2, applicationReleaseDTO.getVersion()); + statement.setString(3, applicationReleaseDTO.getUuid()); + statement.setString(4, applicationReleaseDTO.getReleaseType()); + statement.setString(5, applicationReleaseDTO.getPackageName()); + statement.setDouble(6, applicationReleaseDTO.getPrice()); + statement.setString(7, applicationReleaseDTO.getInstallerName()); + statement.setString(8, applicationReleaseDTO.getBannerName()); + statement.setString(9, applicationReleaseDTO.getIconName()); + statement.setString(10, applicationReleaseDTO.getScreenshotName1()); + statement.setString(11, applicationReleaseDTO.getScreenshotName2()); + statement.setString(12, applicationReleaseDTO.getScreenshotName3()); + statement.setString(13, applicationReleaseDTO.getAppHashValue()); + statement.setBoolean(14, applicationReleaseDTO.getIsSharedWithAllTenants()); + statement.setString(15, applicationReleaseDTO.getMetaData()); + statement.setString(16, applicationReleaseDTO.getSupportedOsVersions()); + statement.setString(17, applicationReleaseDTO.getCurrentState().toUpperCase()); + statement.setInt(18, applicationReleaseDTO.getId()); + statement.setInt(19, tenantId); + if (statement.executeUpdate() == 0) { + return null; + } } } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException( - "Database connection exception while trying to update the application release", e); + String msg = "Database connection exception occured while trying to update the application release which " + + "has application release ID: " + applicationReleaseDTO.getId(); + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } catch (SQLException e) { - throw new ApplicationManagementDAOException( - "SQL exception while updating the release ,while executing the query " + sql, e); - } finally { - DAOUtil.cleanupResources(statement, null); + String msg = "SQL exception occured while updating the application release which has release ID: " + + applicationReleaseDTO.getId() + ". Executed query is " + sql; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } return applicationReleaseDTO; } @Override public void deleteRelease(int id) throws ApplicationManagementDAOException { - Connection connection; - PreparedStatement statement = null; String sql = "DELETE " + "FROM AP_APP_RELEASE " + "WHERE ID = ?"; try { - connection = this.getDBConnection(); - statement = connection.prepareStatement(sql); - statement.setInt(1, id); - statement.executeUpdate(); + Connection connection = this.getDBConnection(); + try (PreparedStatement statement = connection.prepareStatement(sql)) { + statement.setInt(1, id); + statement.executeUpdate(); + } } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException( - "Database connection exception while trying to delete the release fore release ID: " + id, e); + String msg = "Database connection exception occurred while trying to delete the application release which " + + "has ID: " + id; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } catch (SQLException e) { - throw new ApplicationManagementDAOException( - "SQL exception while deleting the release for release ID: " + id + ",while executing the query sql" - , e); - } finally { - DAOUtil.cleanupResources(statement, null); + String msg = "SQL exception occurred while deleting the release for release ID: " + id + ",while executing" + + " the query sql " + sql; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } } @Override public void deleteReleases(List applicationReleaseIds) throws ApplicationManagementDAOException{ - Connection connection; String sql = "DELETE " + "FROM AP_APP_RELEASE " + "WHERE ID = ?"; try { - connection = this.getDBConnection(); - try (PreparedStatement statement = connection.prepareStatement(sql)){ - for (Integer releaseId : applicationReleaseIds){ + Connection connection = this.getDBConnection(); + try (PreparedStatement statement = connection.prepareStatement(sql)) { + for (Integer releaseId : applicationReleaseIds) { statement.setInt(1, releaseId); statement.addBatch(); } statement.executeBatch(); } } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException( - "Database connection exception occurred while trying to delete given application release", e); + String msg = "Database connection exception occurred while trying to delete application releases for given " + + "application release ids"; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } catch (SQLException e) { - throw new ApplicationManagementDAOException( - "SQL exception occurred while execute delete query for deleting given application releases.", e); + String msg = "SQL exception occurred while execute delete query for deleting given application releases. " + + "Executed query: " + sql; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } } @@ -378,27 +378,30 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements if (log.isDebugEnabled()) { log.debug("Verifying application release existence by application hash value: " + hashVal); } - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; + String sql = "SELECT " + + "AR.ID AS RELEASE_ID " + + "FROM AP_APP_RELEASE AS AR " + + "WHERE AR.APP_HASH_VALUE = ? AND " + + "AR.TENANT_ID = ?"; try { - conn = this.getDBConnection(); - String sql = "SELECT AR.ID AS RELEASE_ID FROM AP_APP_RELEASE AS AR WHERE AR.APP_HASH_VALUE = ? AND " - + "AR.TENANT_ID = ?;"; - - stmt = conn.prepareStatement(sql); - stmt.setString(1, hashVal); - stmt.setInt(2, tenantId); - rs = stmt.executeQuery(); - return rs.next(); - } catch (SQLException e) { - throw new ApplicationManagementDAOException( - "Error occurred while getting application release details for application release hash value: " - + hashVal + " While executing query ", e); + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1, hashVal); + stmt.setInt(2, tenantId); + try (ResultSet rs = stmt.executeQuery()) { + return rs.next(); + } + } } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); - } finally { - DAOUtil.cleanupResources(stmt, rs); + String msg = "Database connection error occurred while verifying release existence for app release hash " + + "value. Hash value: " + hashVal; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while getting application release details for application release hash value: " + + hashVal + " While executing query "; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } } @@ -407,69 +410,37 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements if (log.isDebugEnabled()) { log.debug("Getting package name of the application release by application id:" + releaseUuid); } - Connection conn; + String sql = "SELECT " + + "AR.PACKAGE_NAME AS PACKAGE_NAME " + + "FROM AP_APP_RELEASE AS AR " + + "WHERE AR.UUID = ? " + + "AND AR.TENANT_ID = ?"; try { - conn = this.getDBConnection(); - String sql = "SELECT " - + "AR.PACKAGE_NAME AS PACKAGE_NAME " - + "FROM AP_APP_RELEASE AS AR " - + "WHERE AR.UUID = ? " - + "AND AR.TENANT_ID = ?"; - - try (PreparedStatement stmt = conn.prepareStatement(sql)){ + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setString(1, releaseUuid); stmt.setInt(2, tenantId); - try (ResultSet rs = stmt.executeQuery()){ + try (ResultSet rs = stmt.executeQuery()) { if (log.isDebugEnabled()) { log.debug("Successfully retrieved package name of the application release with the UUID: " + releaseUuid); } - if (rs.next()){ + if (rs.next()) { return rs.getString("PACKAGE_NAME"); } return null; } } - } catch (SQLException e) { - throw new ApplicationManagementDAOException( - "Error occurred while getting package name of the application release with app ID: " + releaseUuid, e); } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException( - "Error occurred while obtaining the DB connection to get application release package name.", e); - } - } - - @Override - public boolean isReleaseExist(String uuid, int tenantId) throws ApplicationManagementDAOException { - if (log.isDebugEnabled()) { - log.debug("Verifying application release existence by application release uuid: " + uuid); - } - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - try { - conn = this.getDBConnection(); - String sql = - "SELECT AR.ID AS RELEASE_ID FROM AP_APP_RELEASE AS AR WHERE AR.UUID = ? AND AR.TENANT_ID = ?;"; - - stmt = conn.prepareStatement(sql); - stmt.setString(1, uuid); - stmt.setInt(2, tenantId); - rs = stmt.executeQuery(); - - if (log.isDebugEnabled()) { - log.debug("Successfully retrieved basic details of the application release with the application UUID: " - + uuid); - } - return rs.next(); + String msg = "Error occurred while obtaining the DB connection to get application release package name " + + "which has application release UUID: " + releaseUuid; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } catch (SQLException e) { - throw new ApplicationManagementDAOException( - "Error occurred when executing query to get application release details for App release uuid: " - + uuid, e); - } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); - } finally { - DAOUtil.cleanupResources(stmt, rs); + String msg = "Error occurred while getting package name of the application release with app UUID: " + + releaseUuid; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } } @@ -478,45 +449,36 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements if (log.isDebugEnabled()) { log.debug("Getting application release artifact stored location paths for: " + uuid); } - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - String releaseHashValue = null; + String sql = "SELECT " + + "AR.APP_HASH_VALUE AS HASH_VALUE " + + "FROM AP_APP_RELEASE AR " + + "WHERE AR.UUID = ? AND AR.TENANT_ID = ?"; try { - conn = this.getDBConnection(); - String sql = "SELECT " - + "AR.APP_HASH_VALUE AS HASH_VALUE " - + "FROM AP_APP_RELEASE AR " - + "WHERE AR.UUID = ? AND AR.TENANT_ID = ?;"; - - stmt = conn.prepareStatement(sql); - stmt.setString(1, uuid); - stmt.setInt(2, tenantId); - rs = stmt.executeQuery(); - - if (log.isDebugEnabled()) { - log.debug( - "Successfully retrieved application release artifact details of the application release with the application UUID: " - + uuid); - } - - if (rs.getFetchSize() >1){ - String msg = "Found more than one application release for UUID: " + uuid; - log.error(msg); - throw new ApplicationManagementDAOException(msg); - } - while(rs.next()){ - releaseHashValue = rs.getString("HASH_VALUE"); + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)){ + stmt.setString(1, uuid); + stmt.setInt(2, tenantId); + try (ResultSet rs = stmt.executeQuery()){ + if (log.isDebugEnabled()) { + log.debug("Successfully retrieved application release hash value for application release " + + "which has release UUID: " + uuid); + } + if(rs.next()){ + return rs.getString("HASH_VALUE"); + } + return null; + } } - return releaseHashValue; - } catch (SQLException e) { - throw new ApplicationManagementDAOException( - "Error occurred when executing query to get application release artifact paths for App release uuid: " - + uuid, e); } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); - } finally { - DAOUtil.cleanupResources(stmt, rs); + String msg = "Error occurred while obtaining the DB connection to get hash value for application release " + + "which has application release UUID: " + uuid; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred when executing query to get application release hash value which has " + + "application release uuid: " + uuid + ". Executed query: " + sql; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } } @@ -526,13 +488,13 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements if (log.isDebugEnabled()) { log.debug("Verifying application release existence for package name:" + packageName); } - Connection conn; + String sql = "SELECT AR.ID AS RELEASE_ID " + + "FROM AP_APP_RELEASE AS AR " + + "WHERE AR.PACKAGE_NAME = ? AND " + + "AR.CURRENT_STATE != ? AND " + + "AR.TENANT_ID = ? LIMIT 1"; try { - conn = this.getDBConnection(); - String sql = "SELECT AR.ID AS RELEASE_ID " - + "FROM AP_APP_RELEASE AS AR " - + "WHERE AR.PACKAGE_NAME = ? AND AR.CURRENT_STATE != ? AND AR.TENANT_ID = ? LIMIT 1"; - + Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setString(1, packageName); stmt.setString(2, inactiveState); @@ -541,11 +503,16 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements return rs.next(); } } - } catch (SQLException e) { - throw new ApplicationManagementDAOException( - "Error occurred while getting application release details for package name: " + packageName, e); } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); + String msg = "Error occurred while obtaining the DB connection to verify the existence of package name for " + + "active application release. Package name: " + packageName; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "SQL error occurred while verifying the existence of package name for active application " + + "release. package name: " + packageName; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } } @@ -555,15 +522,13 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements if (log.isDebugEnabled()) { log.debug("Verifying application release existence in the installable state: :" + installableStateName); } - Connection conn; + String sql = "SELECT AR.ID AS RELEASE_ID " + + "FROM AP_APP_RELEASE AS AR " + + "WHERE AR.CURRENT_STATE = ? AND " + + "AR.AP_APP_ID = (SELECT AP_APP_ID FROM AP_APP_RELEASE WHERE UUID = ?) AND " + + "AR.TENANT_ID = ?"; try { - conn = this.getDBConnection(); - String sql = "SELECT AR.ID AS RELEASE_ID " - + "FROM AP_APP_RELEASE AS AR " - + "WHERE AR.CURRENT_STATE = ? AND " - + "AR.AP_APP_ID = (SELECT AP_APP_ID FROM AP_APP_RELEASE WHERE UUID = ?) AND " - + "AR.TENANT_ID = ?"; - + Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setString(1, installableStateName); stmt.setString(2, releaseUuid); @@ -572,15 +537,17 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements return rs.next(); } } - } catch (SQLException e) { - throw new ApplicationManagementDAOException( - "Error occurred while getting application release details in installable state: " - + installableStateName, e); } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException( - "Error occurred while obtaining the DB connection to get application release data in installable " - + "state.", e); + String msg = "Error occurred while obtaining the DB connection to verify the existence of app release for " + + "application release uuid ;" + releaseUuid + " and application release state " + + installableStateName; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while executing SQL to verify the existence of app release for application " + + "release uuid ;" + releaseUuid + " and application release state " + installableStateName; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } } - -} \ 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 94fcde264f..f3eded5f3b 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 @@ -524,7 +524,7 @@ public class ApplicationManagerImpl implements ApplicationManager { for (ApplicationDTO applicationDTO : appDTOs) { boolean isSearchingApp = true; List filteringTags = filter.getTags(); - List filteringCategories = filter.getAppCategories(); + List filteringCategories = filter.getCategories(); List filteringUnrestrictedRoles = filter.getUnrestrictedRoles(); if (!lifecycleStateManager.getEndState().equals(applicationDTO.getStatus())) { @@ -2511,15 +2511,17 @@ public class ApplicationManagerImpl implements ApplicationManager { + "price is " + price + " for " + applicationSubType + " application."); } - DeviceType deviceTypeObj = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId()); - String supportedOSVersions = applicationReleaseDTO.getSupportedOsVersions(); - if (!ApplicationType.WEB_CLIP.toString().equals(appType) && !ApplicationType.WEB_APP.toString().equals(appType) - && !StringUtils.isEmpty(supportedOSVersions) && !isValidOsVersions( - supportedOSVersions, deviceTypeObj.getName())) { - String msg = "You are trying to update application release which has invalid or unsupported OS " - + "versions in the supportedOsVersions section. Hence, please re-evaluate the request payload."; - log.error(msg); - throw new BadRequestException(msg); + if (!ApplicationType.WEB_CLIP.toString().equals(appType) && !ApplicationType.WEB_APP.toString() + .equals(appType)) { + DeviceType deviceTypeObj = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId()); + String supportedOSVersions = applicationReleaseDTO.getSupportedOsVersions(); + if (!StringUtils.isEmpty(supportedOSVersions) && !isValidOsVersions(supportedOSVersions, + deviceTypeObj.getName())) { + String msg = "You are trying to update application release which has invalid or unsupported OS " + + "versions in the supportedOsVersions section. Hence, please re-evaluate the request payload."; + log.error(msg); + throw new BadRequestException(msg); + } } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ReviewManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ReviewManagerImpl.java index 7b9c6d56c1..8336f34bec 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ReviewManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ReviewManagerImpl.java @@ -626,7 +626,7 @@ public class ReviewManagerImpl implements ReviewManager { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); try { ConnectionManagerUtil.openDBConnection(); - Rating rating = this.applicationReleaseDAO.getRating(appReleaseUuid, tenantId); + Rating rating = this.applicationReleaseDAO.getReleaseRating(appReleaseUuid, tenantId); if (rating == null) { throw new NotFoundException( "Couldn't find rating for application release UUID: " + appReleaseUuid @@ -710,7 +710,7 @@ public class ReviewManagerImpl implements ReviewManager { private void calculateRating(int newRatingVal, int oldRatingVal, String uuid, int tenantId) { try { ConnectionManagerUtil.beginDBTransaction(); - Rating rating = this.applicationReleaseDAO.getRating(uuid, tenantId); + Rating rating = this.applicationReleaseDAO.getReleaseRating(uuid, tenantId); if (rating == null) { log.error("Couldn't find rating for application release uuid: " + uuid); } else { From a5d39e76b59d83c19bf8d9a9db273ff7c52daf01 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Thu, 4 Jul 2019 21:17:15 +0530 Subject: [PATCH 2/3] Improve review update logic --- .../mgt/common/services/ReviewManager.java | 2 +- .../application/mgt/core/dao/ReviewDAO.java | 2 +- .../core/dao/impl/review/ReviewDAOImpl.java | 8 +++- .../mgt/core/impl/ReviewManagerImpl.java | 35 +++++++++----- .../admin/ReviewManagementAdminAPI.java | 48 ------------------- .../admin/ReviewManagementAdminAPIImpl.java | 33 ------------- .../impl/ReviewManagementAPIImpl.java | 6 +-- 7 files changed, 33 insertions(+), 101 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/ReviewManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ReviewManager.java index 779f718466..7a063b36bd 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ReviewManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ReviewManager.java @@ -78,7 +78,7 @@ public interface ReviewManager { * @return {@link Review}updated review * @throws ReviewManagementException Exceptions of the reviewTmp management */ - boolean updateReview(ReviewWrapper updatingReview, int reviewId, String uuid, boolean isPrivilegedUser) + Review updateReview(ReviewWrapper updatingReview, int reviewId, String uuid, boolean isPrivilegedUser) throws ReviewManagementException, ApplicationManagementException; /** diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ReviewDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ReviewDAO.java index 904c5dd791..19d8e727ef 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ReviewDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ReviewDAO.java @@ -62,7 +62,7 @@ import java.util.List; * @return row count if updating is succeed otherwise 0 * @throws ReviewManagementDAOException Exceptions of the reviewTmp management. */ - int updateReview(ReviewDTO reviewDTO, int reviewId, boolean isActiveReview, int tenantId) + ReviewDTO updateReview(ReviewDTO reviewDTO, int reviewId, boolean isActiveReview, int tenantId) throws ReviewManagementDAOException; 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/review/ReviewDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/review/ReviewDAOImpl.java index 14dec383b8..5c30e9ce55 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/review/ReviewDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/review/ReviewDAOImpl.java @@ -142,7 +142,7 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO { } @Override - public int updateReview(ReviewDTO reviewDTO, int reviewId, boolean isActiveReview, int tenantId) + public ReviewDTO updateReview(ReviewDTO reviewDTO, int reviewId, boolean isActiveReview, int tenantId) throws ReviewManagementDAOException { if (log.isDebugEnabled()) { log.debug("Request received to DAO Layer to update the Review which has ID " + reviewId); @@ -167,7 +167,11 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO { statement.setBoolean(4, isActiveReview); statement.setInt(5, reviewId); statement.setInt(6, tenantId); - return statement.executeUpdate(); + if (statement.executeUpdate() == 1) { + reviewDTO.setModifiedAt(timestamp); + return reviewDTO; + } + return null; } } catch (DBConnectionException e) { String msg = "Error occured while getting the db connection to update review for review ID: " + reviewId; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ReviewManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ReviewManagerImpl.java index 8336f34bec..79664eb409 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ReviewManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ReviewManagerImpl.java @@ -272,7 +272,7 @@ public class ReviewManagerImpl implements ReviewManager { } @Override - public boolean updateReview(ReviewWrapper updatingReview, int reviewId, String uuid, + public Review updateReview(ReviewWrapper updatingReview, int reviewId, String uuid, boolean isPrivilegedUser) throws ReviewManagementException, ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); @@ -299,6 +299,8 @@ public class ReviewManagerImpl implements ReviewManager { .calculateRating(updatingReview.getRating(), reviewDTO.getRating(), uuid, tenantId); new Thread(task).start(); reviewDTO.setRating(updatingReview.getRating()); + } + if (!reviewDTO.getContent().equals(updatingReview.getContent())) { reviewDTO.setContent(updatingReview.getContent()); } } else { @@ -310,22 +312,28 @@ public class ReviewManagerImpl implements ReviewManager { reviewDTO.setContent(updatingReview.getContent()); } - if (updateReviewInDB(reviewDTO, reviewId, isActiveReview, tenantId)) { + ReviewDTO updatedReviewDTO = updateReviewInDB(reviewDTO, reviewId, isActiveReview, tenantId); + if (updatedReviewDTO != null) { if (!isActiveReview) { - if (addReview(updatingReview, uuid, true) != null) { - return true; + Review newReview = addReview(updatingReview, uuid, true); + if (newReview != null) { + return newReview; } else { - if (updateReviewInDB(reviewDTO, reviewId, true, tenantId)) { - return false; + ReviewDTO restoringReviewDTO = updateReviewInDB(reviewDTO, reviewId, true, tenantId); + if (restoringReviewDTO != null) { + String msg = "Review Updating Status: Adding new Review for application release which has" + + " UUID: " + uuid + " is failed and the old review is restored."; + log.error(msg); + throw new ApplicationManagementException(msg); } else { - String msg = "Review Updating Status: Adding new Review for application release which has UUID: " - + "" + uuid + " is failed and the old review restoring is also failed."; + String msg = "Review Updating Status: Adding new Review for application release which has " + + "UUID: " + uuid + " is failed and the old review restoring is also failed."; log.error(msg); throw new ApplicationManagementException(msg); } } } - return true; + return reviewDTOToReview(updatedReviewDTO); } else { String msg = "Review Updating is failed. Hence please contact the administrator."; log.error(msg); @@ -333,16 +341,17 @@ public class ReviewManagerImpl implements ReviewManager { } } - private boolean updateReviewInDB(ReviewDTO reviewDTO, int reviewId, boolean isActiveReview, + private ReviewDTO updateReviewInDB(ReviewDTO reviewDTO, int reviewId, boolean isActiveReview, int tenantId) throws ReviewManagementException, ApplicationManagementException { try { ConnectionManagerUtil.beginDBTransaction(); - if (this.reviewDAO.updateReview(reviewDTO, reviewId, isActiveReview, tenantId) == 1) { + ReviewDTO updatedReviewDTO = this.reviewDAO.updateReview(reviewDTO, reviewId, isActiveReview, tenantId); + if (updatedReviewDTO != null) { ConnectionManagerUtil.commitDBTransaction(); - return true; + return updatedReviewDTO; } ConnectionManagerUtil.rollbackDBTransaction(); - return false; + return null; } catch (ReviewManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); String msg = "Error occured while getting reviewTmp with reviewTmp id " + reviewId + "."; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/admin/ReviewManagementAdminAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/admin/ReviewManagementAdminAPI.java index 2ed8047ef7..e8f8a4c783 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/admin/ReviewManagementAdminAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/admin/ReviewManagementAdminAPI.java @@ -88,54 +88,6 @@ scopes = { public interface ReviewManagementAdminAPI { String SCOPE = "scope"; - @PUT - @Path("/{uuid}/{reviewId}") - @Produces(MediaType.APPLICATION_JSON) - @Consumes(MediaType.APPLICATION_JSON) - @ApiOperation( - consumes = MediaType.APPLICATION_JSON, - produces = MediaType.APPLICATION_JSON, - httpMethod = "PUT", - value = "Edit a review", - notes = "This will edit the review", - tags = "Review Management", - extensions = { - @Extension(properties = { - @ExtensionProperty(name = SCOPE, value = "perm:admin:app:review:update") - }) - } - ) - @ApiResponses( - value = { - @ApiResponse( - code = 200, - message = "OK. \n Successfully updated reviewTmp.", - response = Review.class), - @ApiResponse( - code = 400, - message = "Bad Request. \n Invalid request or validation error."), - @ApiResponse( - code = 500, - message = "Internal Server Error. \n Error occurred while updating the new reviewTmp.", - response = ErrorResponse.class) - }) - Response updateReview( - @ApiParam( - name = "reviewTmp", - value = "The review that need to be updated.", - required = true) - @Valid ReviewWrapper updatingReview, - @ApiParam( - name = "uuid", - value = "uuid of the application release", - required = true) - @PathParam("uuid") String uuid, - @ApiParam( - name = "reviewId", - value = "review id of the updating reviewTmp.", - required = true) - @PathParam("reviewId") int reviewId); - @DELETE @Path("/{uuid}/{reviewId}") @Produces(MediaType.APPLICATION_JSON) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/admin/ReviewManagementAdminAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/admin/ReviewManagementAdminAPIImpl.java index b0718a0f7c..fbda07e392 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/admin/ReviewManagementAdminAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/admin/ReviewManagementAdminAPIImpl.java @@ -48,38 +48,6 @@ public class ReviewManagementAdminAPIImpl implements ReviewManagementAdminAPI { private static Log log = LogFactory.getLog(ReviewManagementAdminAPIImpl.class); - //todo remove this API - @Override - @PUT - @Consumes("application/json") - @Path("/{uuid}/{reviewId}") - public Response updateReview( - @ApiParam ReviewWrapper updatingReview, - @PathParam("uuid") String uuid, - @PathParam("reviewId") int reviewId) { - ReviewManager reviewManager = APIUtil.getReviewManager(); - try { - if (reviewManager.updateReview(updatingReview, reviewId, uuid, true)) { - return Response.status(Response.Status.OK).entity(updatingReview).build(); - } else { - String msg = "Review updating failed. Please contact the administrator"; - log.error(msg); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } - } catch (ReviewManagementException e) { - String msg = "Error occurred while retrieving comments."; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } catch (NotFoundException e) { - String msg = "Couldn't found application release data for UUID " + uuid + " or Review for review ID: " + reviewId; - log.error(msg, e); - return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); - } catch (ApplicationManagementException e) { - String msg = "Error occurred when getting application release data for application release UUID:." + uuid; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } - } - @Override @DELETE @Path("/{uuid}/{reviewId}") @@ -91,7 +59,6 @@ public class ReviewManagementAdminAPIImpl implements ReviewManagementAdminAPI { try { reviewManager.deleteReview(uuid, reviewId, true); return Response.status(Response.Status.OK).entity("Review is deleted successfully.").build(); - } catch (NotFoundException e) { String msg = "Couldn't found an application review to delete which match with the request."; log.error(msg, e); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/ReviewManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/ReviewManagementAPIImpl.java index fece4a992d..c8859622d8 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/ReviewManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/ReviewManagementAPIImpl.java @@ -199,8 +199,9 @@ public class ReviewManagementAPIImpl implements ReviewManagementAPI { @PathParam("reviewId") int reviewId) { ReviewManager reviewManager = APIUtil.getReviewManager(); try { - if (reviewManager.updateReview(updatingReview, reviewId, uuid, false)) { - return Response.status(Response.Status.OK).entity(updatingReview).build(); + Review updatedReview = reviewManager.updateReview(updatingReview, reviewId, uuid, false); + if (updatedReview != null) { + return Response.status(Response.Status.OK).entity(updatedReview).build(); } else { String msg = "Review updating failed. Please contact the administrator"; log.error(msg); @@ -230,7 +231,6 @@ public class ReviewManagementAPIImpl implements ReviewManagementAPI { public Response deleteReview( @PathParam("uuid") String uuid, @PathParam("reviewId") int reviewId) { - ReviewManager reviewManager = APIUtil.getReviewManager(); try { reviewManager.deleteReview(uuid, reviewId, false); From 30a6b3ee2893ffdac039a7875aa1e9e1a2cd9956 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Thu, 4 Jul 2019 21:37:15 +0530 Subject: [PATCH 3/3] Format APPM source --- .../GenericApplicationDAOImpl.java | 88 +++++++++++-------- .../impl/ReviewManagementAPIImpl.java | 1 - 2 files changed, 51 insertions(+), 38 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/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 f856de8313..37408748c3 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 @@ -840,26 +840,28 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic if (log.isDebugEnabled()) { log.debug("Request received in DAO Layer to delete Category mappings."); } - Connection conn; String sql = "DELETE FROM " + "AP_APP_CATEGORY_MAPPING " + "WHERE " + "AP_APP_ID = ? AND " + "TENANT_ID = ?"; try { - conn = this.getDBConnection(); - try (PreparedStatement stmt = conn.prepareStatement(sql)){ + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, applicationId); stmt.setInt(2, tenantId); stmt.executeUpdate(); } } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException( - "Error occurred while obtaining the DB connection when deleting category mapping of application ID: " - + applicationId , e); + String msg = "Error occurred while obtaining the DB connection when deleting category mapping of " + + "application ID: " + applicationId; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } catch (SQLException e) { - throw new ApplicationManagementDAOException("Error occurred when deleting category mapping of application ID: " - + applicationId, e); + String msg = "SQL Error occurred when deleting category mapping of application ID: " + applicationId + + " Executed query: " + sql; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } } @@ -867,16 +869,15 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic public void deleteAppCategories(List categoryIds, int applicationId, int tenantId) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { - log.debug("Request received in DAO Layer to delete Tag mappings."); + log.debug("Request received in DAO Layer to delete application category."); } - Connection conn; String sql = "DELETE FROM " + "AP_APP_CATEGORY_MAPPING WHERE " + "AP_APP_CATEGORY_ID = ? AND " + "AP_APP_ID = ? AND " + "TENANT_ID = ?"; try { - conn = this.getDBConnection(); + Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)){ for (Integer categoryId : categoryIds){ stmt.setInt(1, categoryId); @@ -887,10 +888,13 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic stmt.executeBatch(); } } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException( - "Error occurred while obtaining the DB connection when deleting category mapping", e); + String msg = "Error occurred while obtaining the DB connection when deleting category mapping."; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } catch (SQLException e) { - throw new ApplicationManagementDAOException("Error occurred when deleting category mapping", e); + String msg = "SQL Error occurred when deleting category mapping. Executed query: " + sql; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } } @@ -899,26 +903,28 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic if (log.isDebugEnabled()) { log.debug("Request received in DAO Layer to delete category."); } - Connection conn; String sql = "DELETE FROM " + "AP_APP_CATEGORY " + "WHERE " + "ID = ? AND " + "TENANT_ID = ?"; try { - conn = this.getDBConnection(); + Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, categoryId); stmt.setInt(2, tenantId); stmt.executeUpdate(); } } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException( - "Error occurred while obtaining the DB connection when deleting category which has ID: " - + categoryId, e); + String msg = "Error occurred while obtaining the DB connection when deleting category which has ID: " + + categoryId; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } catch (SQLException e) { - throw new ApplicationManagementDAOException( - "Error occurred when deleting category which has ID: " + categoryId, e); + String msg = "SQL Error occurred when deleting category which has ID: " + categoryId + ". Executed query: " + + sql; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } } @@ -927,7 +933,6 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic if (log.isDebugEnabled()) { log.debug("Request received in DAO Layer to update a category."); } - Connection conn; String sql = "UPDATE " + "AP_APP_CATEGORY cat " + "SET cat.CATEGORY = ? " + @@ -935,7 +940,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic "cat.ID = ? AND " + "cat.TENANT_ID = ?"; try { - conn = this.getDBConnection(); + Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setString(1, categoryDTO.getCategoryName()); stmt.setInt(2, categoryDTO.getId()); @@ -943,12 +948,15 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic stmt.executeUpdate(); } } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException( - "Error occurred while obtaining the DB connection when updating category which has ID: " - + categoryDTO.getId(), e); + String msg = "Error occurred while obtaining the DB connection when updating category which has ID: " + + categoryDTO.getId(); + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } catch (SQLException e) { - throw new ApplicationManagementDAOException( - "Error occurred when updating category which has ID: " + categoryDTO.getId(), e); + String msg = "Error occurred when updating category which has ID: " + categoryDTO.getId() + ". Executed " + + "query: " + sql; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } } @@ -963,7 +971,8 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic int index = 1; List tagIds = new ArrayList<>(); StringJoiner joiner = new StringJoiner(",", - "SELECT AP_APP_TAG.ID AS ID FROM AP_APP_TAG WHERE AP_APP_TAG.TAG IN (", ") AND TENANT_ID = ?"); + "SELECT AP_APP_TAG.ID AS ID FROM AP_APP_TAG WHERE AP_APP_TAG.TAG IN (", + ") AND TENANT_ID = ?"); tagNames.stream().map(ignored -> "?").forEach(joiner::add); String query = joiner.toString(); try (PreparedStatement ps = conn.prepareStatement(query)) { @@ -979,17 +988,20 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } return tagIds; } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException( - "Error occurred while obtaining the DB connection when adding tags", e); + String msg = "Error occurred while obtaining the DB connection when getting tag IDs for given tag names."; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } catch (SQLException e) { - throw new ApplicationManagementDAOException("Error occurred while adding tags", e); + String msg = "SQL Error occurred while getting tag IDs for given tag names"; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } } @Override public TagDTO getTagForTagName(String tagName, int tenantId) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { - log.debug("Request received in DAO Layer to get tag id for given tag name."); + log.debug("Request received in DAO Layer to get tag for given tag name."); } try { Connection conn = this.getDBConnection(); @@ -1011,17 +1023,19 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } return null; } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException( - "Error occurred while obtaining the DB connection when getting tag Id for given tag name", e); + String msg = "Error occurred while obtaining the DB connection when getting tag for given tag name"; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } catch (SQLException e) { - throw new ApplicationManagementDAOException("SQL Error occurred while getting tag Id for tag name.", e); + String msg = "SQL Error occurred while getting tag for tag name."; + throw new ApplicationManagementDAOException(msg, e); } } @Override public List getDistinctTagIdsInTagMapping() throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { - log.debug("Request received in DAO Layer to get distinct tag ids for given tag names"); + log.debug("Request received in DAO Layer to get distinct tag ids in tag mapping."); } try { Connection conn = this.getDBConnection(); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/ReviewManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/ReviewManagementAPIImpl.java index c8859622d8..ef5758a20d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/ReviewManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/ReviewManagementAPIImpl.java @@ -235,7 +235,6 @@ public class ReviewManagementAPIImpl implements ReviewManagementAPI { try { reviewManager.deleteReview(uuid, reviewId, false); return Response.status(Response.Status.OK).entity("Review is deleted successfully.").build(); - } catch (NotFoundException e) { String msg = "Couldn't found an application review to delete which match with the request."; log.error(msg, e);