From a3b8cef1a2a58233fea534cee15e5dc0af287093 Mon Sep 17 00:00:00 2001 From: inoshperera Date: Sat, 29 Sep 2018 18:15:58 +0530 Subject: [PATCH] Fixing connection issue, dao layer fixes --- .../common/services/ApplicationManager.java | 8 +- .../GenericApplicationReleaseDAOImpl.java | 36 +++++---- .../mgt/core/impl/ApplicationManagerImpl.java | 73 +++++++++++-------- .../impl/ApplicationManagementAPIImpl.java | 4 +- .../dbscripts/cdm/application-mgt/h2.sql | 3 +- 5 files changed, 71 insertions(+), 53 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 a0a231d772d..33a05562aeb 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 @@ -68,9 +68,11 @@ public interface ApplicationManager { * * @param applicationId ID of tha application * @param releaseUuid UUID of tha application release + * @param handleConnections Whether it is necessary handle DB connections. * @throws ApplicationManagementException Application Management Exception */ - String deleteApplicationRelease(int applicationId, String releaseUuid) throws ApplicationManagementException; + String deleteApplicationRelease(int applicationId, String releaseUuid, boolean handleConnections) throws + ApplicationManagementException; /** * To get the applications based on the search filter. @@ -156,9 +158,11 @@ public interface ApplicationManager { * @param releaseUuid UUID of the Application Release. * @param state Lifecycle state to change the app * @param checkExist whether it is needed to check if the app and release already exist in the database + * @param handleDBConnections Whether it is necessary to open connections * @throws ApplicationManagementException Application Management Exception. */ - void changeLifecycleState(int applicationId, String releaseUuid, LifecycleState state, Boolean checkExist) + void changeLifecycleState(int applicationId, String releaseUuid, LifecycleState state, Boolean checkExist, + Boolean handleDBConnections) throws 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/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 319a7dd0495..50a2e8e4a07 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 @@ -57,9 +57,10 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements PreparedStatement statement = null; ResultSet resultSet = null; - String sql = "INSERT INTO AP_APP_RELEASE (VERSION,TENANT_ID,UUID,RELEASE_TYPE, PACKAGE_NAME, APP_PRICE," - + "STORED_LOCATION, BANNER_LOCATION, SC_1_LOCATION,SC_2_LOCATION,SC_3_LOCATION, APP_HASH_VALUE," - + "SHARED_WITH_ALL_TENANTS, APP_META_INFO,AP_APP_ID) " + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);"; + String sql = "INSERT INTO AP_APP_RELEASE (VERSION,TENANT_ID,UUID,RELEASE_TYPE, PACKAGE_NAME, APP_PRICE, " + + "STORED_LOCATION, ICON_LOCATION, BANNER_LOCATION, SC_1_LOCATION,SC_2_LOCATION,SC_3_LOCATION," + + "APP_HASH_VALUE, SHARED_WITH_ALL_TENANTS, APP_META_INFO,AP_APP_ID) " + + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);"; int index = 0; String generatedColumns[] = { "ID" }; @@ -117,7 +118,8 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements ResultSet resultSet = null; String sql = "SELECT AR.ID AS RELEASE_ID, AR.VERSION AS RELEASE_VERSION, AR.UUID AS UUID, AR.RELEASE_TYPE AS " + "RELEASE_TYPE, AR.PACKAGE_NAME AS PACKAGE_NAME, AR.APP_PRICE AS APP_PRICE, AR.STORED_LOCATION AS " - + "STORED_LOCATION, AR.BANNER_LOCATION AS BANNER_LOCATION, AR.SC_1_LOCATION AS SCREEN_SHOT_1, " + + "STORED_LOCATION, AR.BANNER_LOCATION AS BANNER_LOCATION, ICON_LOCATION, AR.SC_1_LOCATION AS " + + "SCREEN_SHOT_1, " + "AR.SC_2_LOCATION AS SCREEN_SHOT_2, AR.SC_3_LOCATION AS SCREEN_SHOT_3, AR.APP_HASH_VALUE AS " + "HASH_VALUE, AR.SHARED_WITH_ALL_TENANTS AS SHARED, AR.APP_META_INFO AS APP_META_INFO , " + "AR.RATING AS RATING, AL.CURRENT_STATE, AL.PREVIOUS_STATE, AL.UPDATED_BY, AL.UPDATED_AT FROM " @@ -166,7 +168,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements ResultSet resultSet = null; String sql = "SELECT AR.ID AS RELEASE_ID, AR.VERSION AS RELEASE_VERSION, AR.UUID, AR.RELEASE_TYPE, AR.APP_PRICE, " - + "AR.STORED_LOCATION, AR.BANNER_LOCATION, AR.SC_1_LOCATION AS SCREEN_SHOT_1, " + + "AR.STORED_LOCATION, AR.BANNER_LOCATION, AR.SC_1_LOCATION AS SCREEN_SHOT_1, ICON_LOCATION," + "AR.SC_2_LOCATION AS SCREEN_SHOT_2, AR.SC_3_LOCATION AS SCREEN_SHOT_3, AR.PACKAGE_NAME AS " + "PACKAGE_NAME, AR.APP_HASH_VALUE AS HASH_VALUE, AR.SHARED_WITH_ALL_TENANTS AS SHARED, " + "AR.APP_META_INFO AS APP_META_INFO, AR.RATING AS RATING, AL.CURRENT_STATE, AL.PREVIOUS_STATE, " @@ -213,7 +215,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements ResultSet resultSet = null; List applicationReleases = new ArrayList<>(); String sql = "SELECT AR.ID AS RELEASE_ID, AR.VERSION AS RELEASE_VERSION, AR.UUID, AR.RELEASE_TYPE " - + "AS RELEASE_TYPE, AR.PACKAGE_NAME AS PACKAGE_NAME, AR.APP_PRICE, AR.STORED_LOCATION, " + + "AS RELEASE_TYPE, AR.PACKAGE_NAME AS PACKAGE_NAME, AR.APP_PRICE, AR.STORED_LOCATION, ICON_LOCATION" + "AR.BANNER_LOCATION, AR.SC_1_LOCATION AS SCREEN_SHOT_1, AR.SC_2_LOCATION AS SCREEN_SHOT_2, " + "AR.SC_3_LOCATION AS SCREEN_SHOT_3, AR.APP_HASH_VALUE AS HASH_VALUE, " + "AR.SHARED_WITH_ALL_TENANTS AS SHARED, AR.APP_META_INFO AS APP_META_INFO, " @@ -361,7 +363,8 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements Connection connection; PreparedStatement statement = null; String sql = "UPDATE AP_APP_RELEASE SET VERSION = ?, UUID = ?, RELEASE_TYPE = ?, PACKAGE_NAME = ?," - + " APP_PRICE = ?, STORED_LOCATION = ?, BANNER_LOCATION = ?, SC_1_LOCATION = ?, SC_2_LOCATION = ?," + + " APP_PRICE = ?, STORED_LOCATION = ?, BANNER_LOCATION = ?, ICON_LOCATION =?, SC_1_LOCATION = ?, " + + "SC_2_LOCATION = ?," + " SC_3_LOCATION = ?, APP_HASH_VALUE = ?, SHARED_WITH_ALL_TENANTS = ?, APP_META_INFO = ? " + "WHERE AP_APP_ID = ? AND TENANT_ID = ? AND ID = ?;"; try { @@ -374,15 +377,16 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements statement.setDouble(5, applicationRelease.getPrice()); statement.setString(6, applicationRelease.getAppStoredLoc()); statement.setString(7, applicationRelease.getBannerLoc()); - statement.setString(8, applicationRelease.getScreenshotLoc1()); - statement.setString(9, applicationRelease.getScreenshotLoc2()); - statement.setString(10, applicationRelease.getScreenshotLoc3()); - statement.setString(11, applicationRelease.getAppHashValue()); - statement.setInt(12, applicationRelease.getIsSharedWithAllTenants()); - statement.setString(13, applicationRelease.getMetaData()); - statement.setInt(14, applicationId); - statement.setInt(15, tenantId); - statement.setInt(16, applicationRelease.getId()); + statement.setString(8, applicationRelease.getIconLoc()); + statement.setString(9, applicationRelease.getScreenshotLoc1()); + statement.setString(10, applicationRelease.getScreenshotLoc2()); + statement.setString(11, applicationRelease.getScreenshotLoc3()); + statement.setString(12, applicationRelease.getAppHashValue()); + statement.setInt(13, applicationRelease.getIsSharedWithAllTenants()); + statement.setString(14, applicationRelease.getMetaData()); + statement.setInt(15, applicationId); + statement.setInt(16, tenantId); + statement.setInt(17, applicationRelease.getId()); statement.executeUpdate(); } catch (DBConnectionException e) { throw new ApplicationManagementDAOException( 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 7c20c984ebe..87334027851 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 @@ -166,7 +166,7 @@ public class ApplicationManagerImpl implements ApplicationManager { LifecycleState lifecycleState = new LifecycleState(); lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString()); lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString()); - changeLifecycleState(appId, applicationRelease.getUuid(), lifecycleState, false); + changeLifecycleState(appId, applicationRelease.getUuid(), lifecycleState, false, false); applicationRelease.setLifecycleState(lifecycleState); applicationReleases.add(applicationRelease); @@ -244,18 +244,16 @@ public class ApplicationManagerImpl implements ApplicationManager { log.debug("Application release request is received for the application " + application.toString()); } try { - ConnectionManagerUtil.beginDBTransaction(); + ConnectionManagerUtil.getDBConnection(); applicationRelease = this.applicationReleaseDAO .createRelease(applicationRelease, application.getId(), tenantId); LifecycleState lifecycleState = new LifecycleState(); lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString()); lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString()); - changeLifecycleState(application.getId(), applicationRelease.getUuid(), lifecycleState, true); - - ConnectionManagerUtil.commitDBTransaction(); + changeLifecycleState(application.getId(), applicationRelease.getUuid(), lifecycleState, true, + false); return applicationRelease; } catch (ApplicationManagementDAOException e) { - ConnectionManagerUtil.rollbackDBTransaction(); throw new ApplicationManagementException( "Error occurred while adding application release into IoTS app management Application id of the " + "application release: " + applicationId, e); @@ -502,7 +500,7 @@ public class ApplicationManagerImpl implements ApplicationManager { "You don't have permission to delete this application. In order to delete an application you " + "need to have admin permission"); } - + ConnectionManagerUtil.openDBConnection(); application = getApplicationIfAccessible(applicationId); if ( application == null) { throw new ApplicationManagementException("Invalid Application"); @@ -512,16 +510,17 @@ public class ApplicationManagerImpl implements ApplicationManager { log.debug("Request is received to delete applications which are related with the application id " + applicationId); } + ConnectionManagerUtil.beginDBTransaction(); for (ApplicationRelease applicationRelease : applicationReleases) { LifecycleState appLifecycleState = getLifecycleState(applicationId, applicationRelease.getUuid()); LifecycleState newAppLifecycleState = new LifecycleState(); newAppLifecycleState.setPreviousState(appLifecycleState.getCurrentState()); newAppLifecycleState.setCurrentState(AppLifecycleState.REMOVED.toString()); - changeLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState, true); + changeLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState, true, false); storedLocations.add(applicationRelease.getAppHashValue()); } - ConnectionManagerUtil.openDBConnection(); this.applicationDAO.deleteApplication(applicationId); + ConnectionManagerUtil.commitDBTransaction(); } catch (UserStoreException e) { String msg = "Error occured while check whether current user has the permission to delete an application"; log.error(msg); @@ -533,28 +532,37 @@ public class ApplicationManagerImpl implements ApplicationManager { } @Override - public String deleteApplicationRelease(int applicationId, String releaseUuid) + public String deleteApplicationRelease(int applicationId, String releaseUuid, boolean handleConnections) throws ApplicationManagementException { Application application = getApplicationIfAccessible(applicationId); if (application == null) { throw new ApplicationManagementException("Invalid Application ID is received"); } - ApplicationRelease applicationRelease = getAppReleaseIfExists(applicationId, releaseUuid); - LifecycleState appLifecycleState = getLifecycleState(applicationId, applicationRelease.getUuid()); - String currentState = appLifecycleState.getCurrentState(); - if (AppLifecycleState.DEPRECATED.toString().equals(currentState) || AppLifecycleState - .REJECTED.toString().equals(currentState) || AppLifecycleState.UNPUBLISHED.toString().equals - (currentState) ) { - LifecycleState newAppLifecycleState = new LifecycleState(); - newAppLifecycleState.setPreviousState(appLifecycleState.getCurrentState()); - newAppLifecycleState.setCurrentState(AppLifecycleState.REMOVED.toString()); - changeLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState, true); - }else{ - throw new ApplicationManagementException("Can't delete the application release, You have to move the " + - "lifecycle state from "+ currentState + " to acceptable " + - "state") ; + if (handleConnections) { + ConnectionManagerUtil.openDBConnection(); + } + try { + ApplicationRelease applicationRelease = getAppReleaseIfExists(applicationId, releaseUuid); + LifecycleState appLifecycleState = getLifecycleState(applicationId, applicationRelease.getUuid()); + String currentState = appLifecycleState.getCurrentState(); + if (AppLifecycleState.DEPRECATED.toString().equals(currentState) || AppLifecycleState + .REJECTED.toString().equals(currentState) || AppLifecycleState.UNPUBLISHED.toString().equals + (currentState)) { + LifecycleState newAppLifecycleState = new LifecycleState(); + newAppLifecycleState.setPreviousState(appLifecycleState.getCurrentState()); + newAppLifecycleState.setCurrentState(AppLifecycleState.REMOVED.toString()); + changeLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState, true, false); + } else { + throw new ApplicationManagementException("Can't delete the application release, You have to move the " + + "lifecycle state from " + currentState + " to acceptable " + + "state"); + } + return applicationRelease.getAppHashValue(); + } finally { + if (handleConnections) { + ConnectionManagerUtil.closeDBConnection(); + } } - return applicationRelease.getAppHashValue(); } /** @@ -856,12 +864,14 @@ public class ApplicationManagerImpl implements ApplicationManager { } @Override - public void changeLifecycleState(int applicationId, String releaseUuid, LifecycleState state, Boolean checkExist) - throws ApplicationManagementException { + public void changeLifecycleState(int applicationId, String releaseUuid, LifecycleState state, Boolean checkExist, + Boolean handleDBConnections) throws ApplicationManagementException { try { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); if (checkExist) { - ConnectionManagerUtil.openDBConnection(); + if (handleDBConnections) { + ConnectionManagerUtil.openDBConnection(); + } if (!this.applicationDAO.verifyApplicationExistenceById(applicationId, tenantId)){ throw new NotFoundException( "Couldn't found application for the application Id: " + applicationId); @@ -879,11 +889,9 @@ public class ApplicationManagerImpl implements ApplicationManager { if (state.getCurrentState() != null && state.getPreviousState() != null) { if (getLifecycleManagementService() .isValidStateChange(state.getPreviousState(), state.getCurrentState())) { - ConnectionManagerUtil.beginDBTransaction(); //todo if current state of the adding lifecycle state is PUBLISHED, need to check whether is there //todo any other application release in PUBLISHED state for the application( i.e for the appid) this.lifecycleStateDAO.addLifecycleState(state, applicationId, releaseUuid, tenantId); - ConnectionManagerUtil.commitDBTransaction(); } else { log.error("Invalid lifecycle state transition from '" + state.getPreviousState() + "'" + " to '" + state.getCurrentState() + "'"); @@ -892,12 +900,13 @@ public class ApplicationManagerImpl implements ApplicationManager { } } } catch (LifeCycleManagementDAOException e) { - ConnectionManagerUtil.rollbackDBTransaction(); throw new ApplicationManagementException( "Failed to add lifecycle state. Application Id: " + applicationId + " Application release UUID: " + releaseUuid, e); } finally { - ConnectionManagerUtil.closeDBConnection(); + if (handleDBConnections) { + ConnectionManagerUtil.closeDBConnection(); + } } } 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/ApplicationManagementAPIImpl.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/ApplicationManagementAPIImpl.java index 06a7784d685..1751b2a11ae 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/ApplicationManagementAPIImpl.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/ApplicationManagementAPIImpl.java @@ -437,7 +437,7 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { ApplicationManager applicationManager = APIUtil.getApplicationManager(); ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); try { - String storedLocation = applicationManager.deleteApplicationRelease(applicationId, releaseUuid); + String storedLocation = applicationManager.deleteApplicationRelease(applicationId, releaseUuid, true); applicationStorageManager.deleteApplicationReleaseArtifacts(storedLocation); String responseMsg = "Successfully deleted the application release of: " + applicationId + ""; return Response.status(Response.Status.OK).entity(responseMsg).build(); @@ -483,7 +483,7 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { LifecycleState state) { ApplicationManager applicationManager = APIUtil.getApplicationManager(); try { - applicationManager.changeLifecycleState(applicationId, applicationUuid, state, true); + applicationManager.changeLifecycleState(applicationId, applicationUuid, state, true, true); } catch (NotFoundException e) { String msg = "Could,t find application release for application id: " + applicationId + " and application release uuid: " + applicationUuid; diff --git a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/h2.sql b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/h2.sql index 6bada31a2a2..a3c9512ab9e 100644 --- a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/h2.sql +++ b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/h2.sql @@ -28,6 +28,7 @@ CREATE TABLE IF NOT EXISTS AP_APP_RELEASE ( APP_PRICE DECIMAL(6,2) NULL DEFAULT NULL, STORED_LOCATION VARCHAR(100) NOT NULL, BANNER_LOCATION VARCHAR(100) NOT NULL, + ICON_LOCATION VARCHAR(100) NOT NULL, SC_1_LOCATION VARCHAR(100) NOT NULL, SC_2_LOCATION VARCHAR(100) NULL DEFAULT NULL, SC_3_LOCATION VARCHAR(100) NULL DEFAULT NULL, @@ -73,7 +74,7 @@ CREATE INDEX fk_AP_APP_COMMENT_AP_APP_RELEASE1_idx ON AP_APP_REVIEW (AP_APP_RELE CREATE TABLE IF NOT EXISTS AP_APP_LIFECYCLE_STATE ( ID INT(11) NOT NULL AUTO_INCREMENT, CURRENT_STATE VARCHAR(45) NOT NULL, - PREVIOUSE_STATE VARCHAR(45) NOT NULL, + PREVIOUS_STATE VARCHAR(45) NOT NULL, TENANT_ID VARCHAR(45) NOT NULL, UPDATED_BY VARCHAR(100) NOT NULL, UPDATED_AT TIMESTAMP NOT NULL,