From 75008e38b83ebaf6175dc0d1c9a15437add4b58b Mon Sep 17 00:00:00 2001 From: inoshperera Date: Sun, 23 Sep 2018 16:14:13 +0530 Subject: [PATCH 1/9] Fixes to get create app working --- .../application/mgt/core/dao/common/Util.java | 5 +- .../GenericApplicationReleaseDAOImpl.java | 4 +- .../mgt/core/impl/ApplicationManagerImpl.java | 133 +++++++++++------- 3 files changed, 87 insertions(+), 55 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/common/Util.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java index 7c0d414a5ef..b3c0e401204 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java @@ -137,10 +137,11 @@ public class Util { tag.setTagName(rs.getString("APP_TAG")); UnrestrictedRole unrestrictedRole = new UnrestrictedRole(); unrestrictedRole.setRole(rs.getString("ROLE")); - if (application.getTags().contains(tag)) { + if (application.getTags() != null && application.getTags().contains(tag)) { application.getTags().add(tag); } - if (application.getUnrestrictedRoles().contains(unrestrictedRole)) { + if (application.getUnrestrictedRoles() != null && application.getUnrestrictedRoles() + .contains(unrestrictedRole)) { application.getUnrestrictedRoles().add(unrestrictedRole); } iteration++; 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 ecd793fb2d5..b26acb89326 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 @@ -66,7 +66,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements 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,CREATED_BY,AP_APP_ID) " - + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);"; + + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, ?);"; int index = 0; String generatedColumns[] = {"ID"}; @@ -230,7 +230,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements + " AR.STORED_LOCATION, AR.BANNER_LOCATION, AR.SC_1_LOCATION AS SCREEN_SHOT_1, AR.SC_2_LOCATION AS " + "SCREEN_SHOT_2, AR.SC_3_LOCATION AS SCREEN_SHOT_3, AR.APP_HASH_VALUE AS HASH_VALUE, " + "AR.SHARED_WITH_ALL_TENANTS AS SHARED, AR.APP_META_INFO, AR.CREATED_BY, AR.CREATED_AT, " - + "AR.PUBLISHED_BY, AR.PUBLISHED_AT, AR.STARS FROM AP_APP_RELEASE AS " + + "AR.PUBLISHED_BY, AR.PUBLISHED_AT, AR.STARS, AR.RATING FROM AP_APP_RELEASE AS " + "AR where AR.AP_APP_ID=(SELECT ID FROM AP_APP WHERE NAME = ? AND TYPE = ? " + "AND TENANT_ID = ?) AND AR.TENANT_ID = ? ;"; 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 ce7b68229d1..ec236c453d0 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 @@ -106,9 +106,10 @@ public class ApplicationManagerImpl implements ApplicationManager { ApplicationRelease applicationRelease; List applicationReleases = new ArrayList<>(); try { - ConnectionManagerUtil.openDBConnection(); + ConnectionManagerUtil.getDBConnection(); ConnectionManagerUtil.beginDBTransaction(); MAMDeviceConnectorImpl mamDeviceConnector = new MAMDeviceConnectorImpl(); + // Getting the device type details to get device type ID for internal mappings deviceType = mamDeviceConnector.getDeviceManagementService().getDeviceType(application.getDeviceType()); if (deviceType == null) { @@ -116,6 +117,7 @@ public class ApplicationManagerImpl implements ApplicationManager { ConnectionManagerUtil.rollbackDBTransaction(); return null; } + // Insert to application table int appId = this.applicationDAO.createApplication(application, deviceType.getId()); if (appId == -1) { @@ -123,13 +125,26 @@ public class ApplicationManagerImpl implements ApplicationManager { ConnectionManagerUtil.rollbackDBTransaction(); return null; } else { + if(log.isDebugEnabled()){ + log.debug("New Application entry added to AP_APP table. App Id:" + appId); + } if (!application.getTags().isEmpty()) { this.applicationDAO.addTags(application.getTags(), appId, tenantId); + if(log.isDebugEnabled()){ + log.debug("New tags entry added to AP_APP_TAG table. App Id:" + appId); + } } if (!application.getUnrestrictedRoles().isEmpty()) { application.setIsRestricted(true); this.visibilityDAO.addUnrestrictedRoles(application.getUnrestrictedRoles(), appId, tenantId); + if(log.isDebugEnabled()){ + log.debug("New restricted roles to app ID mapping added to AP_UNRESTRICTED_ROLE table." + + " App Id:" + appId); + } } else { + if(log.isDebugEnabled()){ + log.debug("App is not restricted to role. App Id:" + appId); + } application.setIsRestricted(false); } if (application.getApplicationReleases().size() > 1 ){ @@ -137,14 +152,22 @@ public class ApplicationManagerImpl implements ApplicationManager { "Invalid payload. Application creating payload should contains one application release, but " + "the payload contains more than one"); } + + if(log.isDebugEnabled()){ + log.debug("Creating a new release. App Id:" + appId); + } applicationRelease = application.getApplicationReleases().get(0); applicationRelease = this.applicationReleaseDAO .createRelease(applicationRelease, appId, tenantId); + if(log.isDebugEnabled()){ + log.debug("Changing lifecycle state. App Id:" + appId); + } LifecycleState lifecycleState = new LifecycleState(); lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString()); lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString()); - changeLifecycleState(appId, applicationRelease.getUuid(), lifecycleState); + changeLifecycleState(appId, applicationRelease.getUuid(), lifecycleState, false, + applicationRelease.getId()); applicationRelease.setLifecycleState(lifecycleState); applicationReleases.add(applicationRelease); @@ -161,11 +184,18 @@ public class ApplicationManagerImpl implements ApplicationManager { ConnectionManagerUtil.rollbackDBTransaction(); throw new ApplicationManagementException(msg, e); } catch (DeviceManagementException e) { - e.printStackTrace(); + String msg = "Error occurred while getting device type details"; + log.error(msg, e); + ConnectionManagerUtil.rollbackDBTransaction(); + throw new ApplicationManagementException(msg, e); + } catch (Exception e) { + String msg = "Unknown exception while creating application."; + log.error(msg, e); + ConnectionManagerUtil.rollbackDBTransaction(); + throw new ApplicationManagementException(msg, e); } finally { - //ConnectionManagerUtil.closeDBConnection(); //todo: check this again + ConnectionManagerUtil.closeDBConnection(); } - return null; } @Override @@ -181,14 +211,17 @@ public class ApplicationManagerImpl implements ApplicationManager { } try { - ConnectionManagerUtil.openDBConnection(); + ConnectionManagerUtil.getDBConnection(); applicationList = applicationDAO.getApplications(filter, tenantId); - if (!isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { - applicationList = getRoleRestrictedApplicationList(applicationList, userName); - } - for (Application application : applicationList.getApplications()) { - applicationReleases = getReleases(application.getId()); - application.setApplicationReleases(applicationReleases); + if(applicationList != null && applicationList.getApplications() != null && applicationList + .getApplications().size() > 0) { + if (!isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { + applicationList = getRoleRestrictedApplicationList(applicationList, userName); + } + for (Application application : applicationList.getApplications()) { + applicationReleases = getReleases(application.getId()); + application.setApplicationReleases(applicationReleases); + } } return applicationList; } catch (UserStoreException e) { @@ -198,8 +231,6 @@ public class ApplicationManagerImpl implements ApplicationManager { } catch (ApplicationManagementDAOException e) { throw new ApplicationManagementException( "DAO exception while getting applications for the user " + userName + " of tenant " + tenantId, e); - } finally { - ConnectionManagerUtil.closeDBConnection(); } } @@ -219,7 +250,7 @@ public class ApplicationManagerImpl implements ApplicationManager { LifecycleState lifecycleState = new LifecycleState(); lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString()); lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString()); - changeLifecycleState(application.getId(), applicationRelease.getUuid(), lifecycleState); + changeLifecycleState(application.getId(), applicationRelease.getUuid(), lifecycleState, true, 0); ConnectionManagerUtil.commitDBTransaction(); return applicationRelease; @@ -364,12 +395,12 @@ public class ApplicationManagerImpl implements ApplicationManager { log.debug("Request is received to retrieve all the releases related with the application " + application .toString()); } - try { - ConnectionManagerUtil.openDBConnection(); - applicationReleases = this.applicationReleaseDAO.getReleases(application.getName(), application.getType(), tenantId); - for (ApplicationRelease applicationRelease : applicationReleases) { - LifecycleState lifecycleState = ApplicationManagementDAOFactory.getLifecycleStateDAO(). - getLatestLifeCycleStateByReleaseID(applicationRelease.getId()); + ConnectionManagerUtil.getDBConnection(); + applicationReleases = this.applicationReleaseDAO.getReleases(application.getName(), application.getType(), tenantId); + for (ApplicationRelease applicationRelease : applicationReleases) { + LifecycleState lifecycleState = ApplicationManagementDAOFactory.getLifecycleStateDAO(). + getLatestLifeCycleStateByReleaseID(applicationRelease.getId()); + if (lifecycleState != null) { applicationRelease.setLifecycleState(lifecycleState); if (!AppLifecycleState.REMOVED.toString() @@ -377,10 +408,9 @@ public class ApplicationManagerImpl implements ApplicationManager { filteredApplicationReleases.add(applicationRelease); } } - return filteredApplicationReleases; - } finally { - ConnectionManagerUtil.closeDBConnection(); } + return filteredApplicationReleases; + } @Override @@ -409,7 +439,7 @@ public class ApplicationManagerImpl implements ApplicationManager { LifecycleState newAppLifecycleState = new LifecycleState(); newAppLifecycleState.setPreviousState(appLifecycleState.getCurrentState()); newAppLifecycleState.setCurrentState(AppLifecycleState.REMOVED.toString()); - changeLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState); + changeLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState, true, 0); storedLocations.add(applicationRelease.getAppHashValue()); } ConnectionManagerUtil.openDBConnection(); @@ -440,7 +470,7 @@ public class ApplicationManagerImpl implements ApplicationManager { LifecycleState newAppLifecycleState = new LifecycleState(); newAppLifecycleState.setPreviousState(appLifecycleState.getCurrentState()); newAppLifecycleState.setCurrentState(AppLifecycleState.REMOVED.toString()); - changeLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState); + changeLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState, true, 0); }else{ throw new ApplicationManagementException("Can't delete the application release, You have to move the " + "lifecycle state from "+ currentState + " to acceptable " + @@ -582,27 +612,24 @@ public class ApplicationManagerImpl implements ApplicationManager { ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); ApplicationRelease applicationRelease; - try { - if (applicationId <= 0) { - throw new ApplicationManagementException( - "Application id could,t be a negative integer. Hence please add " + - "valid application id."); - } - if (applicationUuid == null) { - throw new ApplicationManagementException("Application UUID is null. Application UUID is a required " - + "parameter to get the relevant application."); - } - ConnectionManagerUtil.openDBConnection(); - applicationRelease = this.applicationReleaseDAO.getReleaseByIds(applicationId, applicationUuid, tenantId); - if (applicationRelease == null) { - throw new ApplicationManagementException("Doesn't exist a application release for application ID: " + - applicationId + "and application UUID: " + - applicationUuid); - } - return applicationRelease; - } finally { - ConnectionManagerUtil.closeDBConnection(); + + if (applicationId <= 0) { + throw new ApplicationManagementException( + "Application id could,t be a negative integer. Hence please add " + + "valid application id."); } + if (applicationUuid == null) { + throw new ApplicationManagementException("Application UUID is null. Application UUID is a required " + + "parameter to get the relevant application."); + } + ConnectionManagerUtil.getDBConnection(); + applicationRelease = this.applicationReleaseDAO.getReleaseByIds(applicationId, applicationUuid, tenantId); + if (applicationRelease == null) { + throw new ApplicationManagementException("Doesn't exist a application release for application ID: " + + applicationId + "and application UUID: " + + applicationUuid); + } + return applicationRelease; } @@ -702,11 +729,15 @@ public class ApplicationManagerImpl implements ApplicationManager { } @Override - public void changeLifecycleState(int applicationId, String applicationUuid, LifecycleState state) - throws ApplicationManagementException { + public void changeLifecycleState(int applicationId, String applicationUuid, LifecycleState state, Boolean + checkExist, int releaseId) throws ApplicationManagementException { try { - Application application = getApplicationIfAccessible(applicationId); - ApplicationRelease applicationRelease = getAppReleaseIfExists(applicationId, applicationUuid); + if (checkExist) { + getApplicationIfAccessible(applicationId); + } + if (releaseId < 1) { + releaseId = getAppReleaseIfExists(applicationId, applicationUuid).getId(); + } int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); state.setUpdatedBy(userName); @@ -714,7 +745,7 @@ public class ApplicationManagerImpl implements ApplicationManager { if (state.getCurrentState() != null && state.getPreviousState() != null) { if (lifecycleStateManger.isValidStateChange(state.getPreviousState(), state.getCurrentState())) { this.lifecycleStateDAO - .addLifecycleState(state, application.getId(), applicationRelease.getId(), tenantId); + .addLifecycleState(state, applicationId, releaseId, tenantId); } else { log.error("Invalid lifecycle state transition from '" + state.getPreviousState() + "'" + " to '" + state.getCurrentState() + "'"); From 27e4dc14d3a53546228c52d3b50f8cf187690acc Mon Sep 17 00:00:00 2001 From: inoshperera Date: Sun, 23 Sep 2018 16:20:48 +0530 Subject: [PATCH 2/9] add created related fixes --- .../mgt/common/services/ApplicationManager.java | 6 +++++- .../api/services/impl/ApplicationManagementAPIImpl.java | 8 +++++++- 2 files changed, 12 insertions(+), 2 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 4df7b50d33a..549475a2bbc 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 @@ -148,9 +148,13 @@ public interface ApplicationManager { * * @param applicationId ID of the Application. * @param applicationUuid 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 releaseId The release ID of the application(optional) * @throws ApplicationManagementException Application Management Exception. */ - void changeLifecycleState(int applicationId, String applicationUuid, LifecycleState state) throws ApplicationManagementException; + void changeLifecycleState(int applicationId, String applicationUuid, LifecycleState state, Boolean checkExist, + int releaseId) throws ApplicationManagementException; /** * Get the application if application is an accessible one. 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 2b4c758e193..9e27fa77a62 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 @@ -136,6 +136,8 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { if (!isValidAppCreatingRequest(binaryFile, iconFile, bannerFile, attachmentList, application)) { return Response.status(Response.Status.BAD_REQUEST).build(); } + + // The application executable artifacts such as apks are uploaded. if (!ApplicationType.ENTERPRISE.toString().equals(application.getType())) { applicationRelease = application.getApplicationReleases().get(0); applicationRelease = applicationStorageManager @@ -157,11 +159,15 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { for (Attachment screenshot : attachmentList) { attachments.add(screenshot.getDataHandler().getInputStream()); } + + // Upload images applicationRelease = applicationStorageManager.uploadImageArtifacts(applicationRelease, iconFileStream, bannerFileStream, attachments); applicationRelease.setUuid(UUID.randomUUID().toString()); applicationReleases.add(applicationRelease); application.setApplicationReleases(applicationReleases); + + // Created new application entry Application createdApplication = applicationManager.createApplication(application); if (createdApplication != null) { return Response.status(Response.Status.CREATED).entity(createdApplication).build(); @@ -460,7 +466,7 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { LifecycleState state) { ApplicationManager applicationManager = APIUtil.getApplicationManager(); try { - applicationManager.changeLifecycleState(applicationId, applicationUuid, state); + applicationManager.changeLifecycleState(applicationId, applicationUuid, state, true, 0); } catch (ApplicationManagementException e) { String msg = "Error occurred while adding lifecycle state."; log.error(msg, e); From 4512a4e2d7965aeefc6f74b2260443d0b2e0ac5e Mon Sep 17 00:00:00 2001 From: inoshperera Date: Sun, 23 Sep 2018 21:17:24 +0530 Subject: [PATCH 3/9] working state on happy path app create --- .../mgt/core/impl/ApplicationManagerImpl.java | 20 +++++++++++-- ...ApplicationManagementServiceComponent.java | 1 + .../core/lifecycle/LifecycleStateManger.java | 30 ++++++++++++++++++- 3 files changed, 47 insertions(+), 4 deletions(-) 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 ec236c453d0..d92c4feda51 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 @@ -100,13 +100,14 @@ public class ApplicationManagerImpl implements ApplicationManager { + " the user : " + application.getUser().getUserName()); } + + ConnectionManagerUtil.openDBConnection(); validateAppCreatingRequest(application); validateAppReleasePayload(application.getApplicationReleases().get(0)); DeviceType deviceType; ApplicationRelease applicationRelease; List applicationReleases = new ArrayList<>(); try { - ConnectionManagerUtil.getDBConnection(); ConnectionManagerUtil.beginDBTransaction(); MAMDeviceConnectorImpl mamDeviceConnector = new MAMDeviceConnectorImpl(); // Getting the device type details to get device type ID for internal mappings @@ -188,7 +189,7 @@ public class ApplicationManagerImpl implements ApplicationManager { log.error(msg, e); ConnectionManagerUtil.rollbackDBTransaction(); throw new ApplicationManagementException(msg, e); - } catch (Exception e) { + } catch (Exception e) { String msg = "Unknown exception while creating application."; log.error(msg, e); ConnectionManagerUtil.rollbackDBTransaction(); @@ -743,7 +744,8 @@ public class ApplicationManagerImpl implements ApplicationManager { state.setUpdatedBy(userName); if (state.getCurrentState() != null && state.getPreviousState() != null) { - if (lifecycleStateManger.isValidStateChange(state.getPreviousState(), state.getCurrentState())) { + + if (getLifecycleManagementService().isValidStateChange(state.getPreviousState(), state.getCurrentState())) { this.lifecycleStateDAO .addLifecycleState(state, applicationId, releaseId, tenantId); } else { @@ -859,4 +861,16 @@ public class ApplicationManagerImpl implements ApplicationManager { } return list; } + + public LifecycleStateManger getLifecycleManagementService() { + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + LifecycleStateManger deviceManagementProviderService = + (LifecycleStateManger) ctx.getOSGiService(LifecycleStateManger.class, null); + if (deviceManagementProviderService == null) { + String msg = "DeviceImpl Management provider service has not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } + return deviceManagementProviderService; + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/ApplicationManagementServiceComponent.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/ApplicationManagementServiceComponent.java index cf891d5d1c7..f6e8ec9279e 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/ApplicationManagementServiceComponent.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/ApplicationManagementServiceComponent.java @@ -103,6 +103,7 @@ public class ApplicationManagementServiceComponent { getConfiguration().getLifecycleStates(); LifecycleStateManger lifecycleStateManger = new LifecycleStateManger(lifecycleStates); DataHolder.getInstance().setLifecycleStateManger(lifecycleStateManger); + bundleContext.registerService(LifecycleStateManger.class.getName(), lifecycleStateManger, null); log.info("ApplicationManagement core bundle has been successfully initialized"); } catch (Throwable e) { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManger.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManger.java index 1a0218b20b9..ea367351002 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManger.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManger.java @@ -3,6 +3,7 @@ package org.wso2.carbon.device.application.mgt.core.lifecycle; import org.wso2.carbon.device.application.mgt.core.lifecycle.config.LifecycleState; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; @@ -26,9 +27,36 @@ public class LifecycleStateManger { } public boolean isValidStateChange(String currentState, String nextState) { - if (lifecycleStates.get(currentState).getProceedingStates().contains(nextState)) { + if (currentState.equalsIgnoreCase(nextState)) { return true; } + + State state = getMatchingState(currentState); + if (state != null) { + return getMatchingNextState(state.getProceedingStates(), nextState); + } + return false; + } + + private State getMatchingState(String currentState) { + Iterator it = lifecycleStates.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry pair = (Map.Entry)it.next(); + if(pair.getKey().toString().equalsIgnoreCase(currentState)) { + return lifecycleStates.get(pair.getKey().toString()); + } + it.remove(); + } + return null; + } + + private boolean getMatchingNextState(Set proceedingStates, String nextState) { + + for (String state: proceedingStates) { + if (state.equalsIgnoreCase(nextState)) { + return true; + } + } return false; } } From f1e0b1a4fa16c7da67aa084c940206a5ba4152dd Mon Sep 17 00:00:00 2001 From: inoshperera Date: Sun, 23 Sep 2018 23:08:45 +0530 Subject: [PATCH 4/9] adding get app by ID --- .../common/services/ApplicationManager.java | 7 ++- .../mgt/core/dao/ApplicationDAO.java | 11 +++++ .../GenericApplicationDAOImpl.java | 44 ++++++++++++++++++- .../mgt/core/impl/ApplicationManagerImpl.java | 43 +++++++++++++++++- .../services/ApplicationManagementAPI.java | 11 ++--- .../impl/ApplicationManagementAPIImpl.java | 12 ++--- 6 files changed, 107 insertions(+), 21 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 28203e646e0..7d5af468758 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 @@ -87,14 +87,13 @@ public interface ApplicationManager { String getUuidOfLatestRelease(int appId) throws ApplicationManagementException; /** - * To get Application with the given UUID. + * To get Application with the given Id. * - * @param appType type of the Application - * @param appName name of the Application + * @param id id of the Application * @return the Application identified by the UUID * @throws ApplicationManagementException Application Management Exception. */ - Application getApplication(String appType, String appName) throws ApplicationManagementException; + Application getApplicationById(int id) throws ApplicationManagementException; /** * To get an application associated with the release. 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 b65a266c32d..2556238e616 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 @@ -84,6 +84,17 @@ public interface ApplicationDAO { */ Application getApplication(String appName, String appType, int tenantId) throws ApplicationManagementDAOException; + /** + * To get the application with the given id + * + * @param id ID of the application. + * @param tenantId ID of the tenant. + * @return the application + * @throws ApplicationManagementDAOException Application Management DAO Exception. + */ + Application getApplicationById(String id, int tenantId) throws + ApplicationManagementDAOException; + /** * To get the application with the given uuid * diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/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 70248a15ad6..ab294dc15ff 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 @@ -337,6 +337,48 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } } + @Override + public Application getApplicationById(String id, int tenantId) throws + ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Getting application with the id:" + id); + } + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + conn = this.getDBConnection(); + String sql = + "SELECT AP_APP.ID AS APP_ID, AP_APP.NAME AS APP_NAME, AP_APP.TYPE AS APP_TYPE, AP_APP.APP_CATEGORY " + + "AS APP_CATEGORY, AP_APP.SUB_TYPE AS SUB_TYPE ,AP_APP.CURRENCY AS CURRENCY," + + " AP_APP.RESTRICTED AS RESTRICTED, AP_APP_TAG.TAG AS APP_TAG, AP_UNRESTRICTED_ROLE.ROLE " + + "AS ROLE FROM AP_APP, AP_APP_TAG, AP_UNRESTRICTED_ROLE WHERE AP_APP.NAME=? AND " + + "AP_APP.APP_ID= ? AND AP_APP.TENANT_ID=?;"; + + stmt = conn.prepareStatement(sql); + stmt.setString(1, id); + stmt.setInt(2, tenantId); + rs = stmt.executeQuery(); + + if (log.isDebugEnabled()) { + log.debug("Successfully retrieved basic details of the application with the id:" + id); + } + + return Util.loadApplication(rs); + + } catch (SQLException e) { + throw new ApplicationManagementDAOException( + "Error occurred while getting application details with app id " + id + + " While executing query ", e); + } catch (JSONException e) { + throw new ApplicationManagementDAOException("Error occurred while parsing JSON", e); + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); + } finally { + Util.cleanupResources(stmt, rs); + } + } + @Override public Application getApplicationById(int applicationId, int tenantId) throws ApplicationManagementDAOException { @@ -420,7 +462,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic int paramIndex = 1; Connection conn; PreparedStatement stmt = null; - Application existingApplication = this.getApplication(application.getName(), application.getType(), tenantId); + Application existingApplication = this.getApplicationById(application.getId(), tenantId); if (existingApplication == null) { throw new ApplicationManagementException("There doesn't have an application for updating"); 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 58099ba0717..d8c7706972b 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 @@ -276,6 +276,46 @@ public class ApplicationManagerImpl implements ApplicationManager { } + @Override + public Application getApplicationById(int id) throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + Application application; + boolean isAppAllowed = false; + List applicationReleases; + try { + ConnectionManagerUtil.openDBConnection(); + application = ApplicationManagementDAOFactory.getApplicationDAO() + .getApplicationById(id, tenantId); + if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { + applicationReleases = getReleases(application.getId()); + application.setApplicationReleases(applicationReleases); + return application; + } + + if (!application.getUnrestrictedRoles().isEmpty()) { + if (isRoleExists(application.getUnrestrictedRoles(), userName)) { + isAppAllowed = true; + } + } else { + isAppAllowed = true; + } + + if (!isAppAllowed) { + return null; + } + + applicationReleases = getReleases(application.getId()); + application.setApplicationReleases(applicationReleases); + return application; + } catch (UserStoreException e) { + throw new ApplicationManagementException( + "User-store exception while getting application with the application id " + id); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + private boolean isRoleExists(Collection unrestrictedRoleList, String userName) throws UserStoreException { String[] roleList; @@ -301,7 +341,6 @@ public class ApplicationManagerImpl implements ApplicationManager { return roleList; } - @Override public Application getApplication(String appType, String appName) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); @@ -311,7 +350,7 @@ public class ApplicationManagerImpl implements ApplicationManager { try { ConnectionManagerUtil.openDBConnection(); application = ApplicationManagementDAOFactory.getApplicationDAO() - .getApplication(appType, appName, tenantId); + .getApplication(appName, appType, tenantId); if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { applicationReleases = getReleases(application.getId()); application.setApplicationReleases(applicationReleases); 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/ApplicationManagementAPI.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/ApplicationManagementAPI.java index 1dec91b98b8..35b70eeba57 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/ApplicationManagementAPI.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/ApplicationManagementAPI.java @@ -174,15 +174,10 @@ public interface ApplicationManagementAPI { }) Response getApplication( @ApiParam( - name = "appType", - value = "Type of the application", + name = "appId", + value = "application Id", required = true) - @PathParam("appType") String appType, - @ApiParam( - name = "appName", - value = "Application name", - required = true) - @QueryParam("isWithImages") String appName + @PathParam("appId") int appId ); @PUT 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 9e27fa77a62..6f16e1dc0fe 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 @@ -85,23 +85,23 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { } } + @GET @Consumes("application/json") - @Path("/{appType}") + @Path("/{appId}") public Response getApplication( - @PathParam("appType") String appType, - @QueryParam("appName") String appName) { + @PathParam("appId") int appId) { ApplicationManager applicationManager = APIUtil.getApplicationManager(); try { - Application application = applicationManager.getApplication(appType, appName); + Application application = applicationManager.getApplicationById(appId); if (application == null) { return Response.status(Response.Status.NOT_FOUND).entity - ("Application with application type: " + appType + " not found").build(); + ("Application with application id: " + appId + " not found").build(); } return Response.status(Response.Status.OK).entity(application).build(); } catch (ApplicationManagementException e) { - log.error("Error occurred while getting application with the uuid " + appType, e); + log.error("Error occurred while getting application with the id " + appId, e); return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); } } From 4e7f1e40478681cadf847e53537e430a872966bc Mon Sep 17 00:00:00 2001 From: inoshperera Date: Sat, 29 Sep 2018 09:39:53 +0530 Subject: [PATCH 5/9] partial fix on app update apis --- .../mgt/core/dao/ApplicationDAO.java | 11 -- .../mgt/core/dao/ApplicationReleaseDAO.java | 12 ++ .../application/mgt/core/dao/common/Util.java | 108 ++++++++++++++++-- .../GenericApplicationDAOImpl.java | 53 +-------- .../GenericApplicationReleaseDAOImpl.java | 105 +++++++++++++---- .../core/exception/BadRequestException.java | 37 ++++++ .../core/exception/ForbiddenException.java | 38 ++++++ .../UnexpectedServerErrorException.java | 34 ++++++ .../mgt/core/impl/ApplicationManagerImpl.java | 108 ++++++++++++++---- .../impl/ApplicationStorageManagerImpl.java | 11 +- 10 files changed, 399 insertions(+), 118 deletions(-) create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/BadRequestException.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/ForbiddenException.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/UnexpectedServerErrorException.java 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 2556238e616..b65a266c32d 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 @@ -84,17 +84,6 @@ public interface ApplicationDAO { */ Application getApplication(String appName, String appType, int tenantId) throws ApplicationManagementDAOException; - /** - * To get the application with the given id - * - * @param id ID of the application. - * @param tenantId ID of the tenant. - * @return the application - * @throws ApplicationManagementDAOException Application Management DAO Exception. - */ - Application getApplicationById(String id, int tenantId) throws - ApplicationManagementDAOException; - /** * To get the application with the given uuid * diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/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 1add29c8355..b3ffce7e7bd 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 @@ -67,6 +67,18 @@ public interface ApplicationReleaseDAO { List getReleases(String applicationName, String applicationType, int tenantId) throws ApplicationManagementDAOException; + /** + * To get the release by state. + * + * @param appId Id of the Application + * @param tenantId tenant id of the application + * @param state state of the application + * @return list of the application releases + * @throws ApplicationManagementDAOException Application Management DAO Exception. + */ + List getReleaseByState(int appId, int tenantId, String state) + throws ApplicationManagementDAOException; + /** * To update an Application release. * diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java index b3c0e401204..736ca723230 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java @@ -21,13 +21,18 @@ package org.wso2.carbon.device.application.mgt.core.dao.common; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.json.JSONException; +import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.application.mgt.common.*; import org.wso2.carbon.device.application.mgt.common.Application; import org.wso2.carbon.device.application.mgt.common.PaginationRequest; import org.wso2.carbon.device.application.mgt.common.exception.ReviewManagementException; +import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; +import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager; +import org.wso2.carbon.device.application.mgt.common.services.SubscriptionManager; import org.wso2.carbon.device.application.mgt.core.config.Configuration; import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager; +import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -131,19 +136,20 @@ public class Util { application.setSubType(rs.getString("SUB_TYPE")); application.setPaymentCurrency(rs.getString("CURRENCY")); application.setIsRestricted(rs.getBoolean("RESTRICTED")); + application.setDeviceTypeId(rs.getInt("DEVICE_TYPE_ID")); } - Tag tag = new Tag(); - tag.setTagName(rs.getString("APP_TAG")); - UnrestrictedRole unrestrictedRole = new UnrestrictedRole(); - unrestrictedRole.setRole(rs.getString("ROLE")); - if (application.getTags() != null && application.getTags().contains(tag)) { - application.getTags().add(tag); - } - if (application.getUnrestrictedRoles() != null && application.getUnrestrictedRoles() - .contains(unrestrictedRole)) { - application.getUnrestrictedRoles().add(unrestrictedRole); - } +// Tag tag = new Tag(); +// tag.setTagName(rs.getString("APP_TAG")); +// UnrestrictedRole unrestrictedRole = new UnrestrictedRole(); +// unrestrictedRole.setRole(rs.getString("ROLE")); +// if (application.getTags() != null && application.getTags().contains(tag)) { +// application.getTags().add(tag); +// } +// if (application.getUnrestrictedRoles() != null && application.getUnrestrictedRoles() +// .contains(unrestrictedRole)) { +// application.getUnrestrictedRoles().add(unrestrictedRole); +// } iteration++; } } @@ -217,4 +223,84 @@ public class Util { } return paginationRequest; } + + private static ApplicationManager applicationManager; + private static ApplicationStorageManager applicationStorageManager; + private static SubscriptionManager subscriptionManager; + + public static ApplicationManager getApplicationManager() { + if (applicationManager == null) { + synchronized (Util.class) { + if (applicationManager == null) { + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + applicationManager = + (ApplicationManager) ctx.getOSGiService(ApplicationManager.class, null); + if (applicationManager == null) { + String msg = "Application Manager service has not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } + } + } + } + return applicationManager; + } + + /** + * To get the Application Storage Manager from the osgi context. + * @return ApplicationStoreManager instance in the current osgi context. + */ + public static ApplicationStorageManager getApplicationStorageManager() { + if (applicationStorageManager == null) { + synchronized (Util.class) { + if (applicationStorageManager == null) { + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + applicationStorageManager = (ApplicationStorageManager) ctx + .getOSGiService(ApplicationStorageManager.class, null); + if (applicationStorageManager == null) { + String msg = "Application Storage Manager service has not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } + } + } + } + return applicationStorageManager; + } + + + /** + * To get the Subscription Manager from the osgi context. + * @return SubscriptionManager instance in the current osgi context. + */ + public static SubscriptionManager getSubscriptionManager() { + if (subscriptionManager == null) { + synchronized (Util.class) { + if (subscriptionManager == null) { + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + subscriptionManager = + (SubscriptionManager) ctx.getOSGiService(SubscriptionManager.class, null); + if (subscriptionManager == null) { + String msg = "Subscription Manager service has not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } + } + } + } + + return subscriptionManager; + } + + public static DeviceManagementProviderService getDeviceManagementService() { + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + DeviceManagementProviderService deviceManagementProviderService = + (DeviceManagementProviderService) ctx.getOSGiService(DeviceManagementProviderService.class, null); + if (deviceManagementProviderService == null) { + String msg = "DeviceImpl Management provider service has not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } + return deviceManagementProviderService; + } } 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 ab294dc15ff..87e9e687b8c 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 @@ -337,48 +337,6 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } } - @Override - public Application getApplicationById(String id, int tenantId) throws - ApplicationManagementDAOException { - if (log.isDebugEnabled()) { - log.debug("Getting application with the id:" + id); - } - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - try { - conn = this.getDBConnection(); - String sql = - "SELECT AP_APP.ID AS APP_ID, AP_APP.NAME AS APP_NAME, AP_APP.TYPE AS APP_TYPE, AP_APP.APP_CATEGORY " - + "AS APP_CATEGORY, AP_APP.SUB_TYPE AS SUB_TYPE ,AP_APP.CURRENCY AS CURRENCY," - + " AP_APP.RESTRICTED AS RESTRICTED, AP_APP_TAG.TAG AS APP_TAG, AP_UNRESTRICTED_ROLE.ROLE " - + "AS ROLE FROM AP_APP, AP_APP_TAG, AP_UNRESTRICTED_ROLE WHERE AP_APP.NAME=? AND " - + "AP_APP.APP_ID= ? AND AP_APP.TENANT_ID=?;"; - - stmt = conn.prepareStatement(sql); - stmt.setString(1, id); - stmt.setInt(2, tenantId); - rs = stmt.executeQuery(); - - if (log.isDebugEnabled()) { - log.debug("Successfully retrieved basic details of the application with the id:" + id); - } - - return Util.loadApplication(rs); - - } catch (SQLException e) { - throw new ApplicationManagementDAOException( - "Error occurred while getting application details with app id " + id + - " While executing query ", e); - } catch (JSONException e) { - throw new ApplicationManagementDAOException("Error occurred while parsing JSON", e); - } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); - } finally { - Util.cleanupResources(stmt, rs); - } - } - @Override public Application getApplicationById(int applicationId, int tenantId) throws ApplicationManagementDAOException { @@ -391,11 +349,12 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic try { conn = this.getDBConnection(); String sql = - "SELECT AP_APP.ID AS APP_ID, AP_APP.NAME AS APP_NAME, AP_APP.TYPE AS APP_TYPE, AP_APP.APP_CATEGORY " - + "AS APP_CATEGORY, AP_APP.SUB_TYPE AS SUB_TYPE ,AP_APP.CURRENCY AS CURRENCY, " - + "AP_APP.RESTRICTED AS RESTRICTED, AP_APP_TAG.TAG AS APP_TAG, AP_UNRESTRICTED_ROLE.ROLE " - + "AS ROLE FROM AP_APP, AP_APP_TAG, AP_UNRESTRICTED_ROLE WHERE AP_APP.ID=? AND " - + "AP_APP.TENANT_ID=?;"; + "SELECT AP_APP.ID AS APP_ID, AP_APP.NAME AS APP_NAME, AP_APP.TYPE AS APP_TYPE, " + + "AP_APP.APP_CATEGORY AS APP_CATEGORY, AP_APP.SUB_TYPE AS SUB_TYPE ," + + "AP_APP.CURRENCY AS CURRENCY, AP_APP.RESTRICTED AS RESTRICTED, " + + "DM_DEVICE_TYPE_ID AS DEVICE_TYPE_ID " + + "FROM AP_APP " + + "WHERE AP_APP.ID=? AND AP_APP.TENANT_ID=?;"; stmt = conn.prepareStatement(sql); stmt.setInt(1, applicationId); 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 f01753a53b7..09ddddf5bea 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 @@ -64,9 +64,10 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements 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," + + "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,CREATED_BY,AP_APP_ID) " - + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, ?);"; + + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, ?);"; int index = 0; String generatedColumns[] = {"ID"}; @@ -80,6 +81,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements statement.setString(++index, String.valueOf(applicationRelease.getPackageName())); statement.setDouble(++index, applicationRelease.getPrice()); statement.setString(++index, applicationRelease.getAppStoredLoc()); + statement.setString(++index, applicationRelease.getIconLoc()); statement.setString(++index, applicationRelease.getBannerLoc()); statement.setString(++index, applicationRelease.getScreenshotLoc1()); statement.setString(++index, applicationRelease.getScreenshotLoc2()); @@ -123,7 +125,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements Connection connection; PreparedStatement statement = null; ResultSet resultSet = null; - String sql = "SELECT AR.ID AS RELESE_ID, AR.VERSION AS RELEASE_VERSION, AR.UUID, AR.RELEASE_TYPE, " + String sql = "SELECT AR.ID AS RELEASE_ID, AR.VERSION AS RELEASE_VERSION, AR.UUID, AR.RELEASE_TYPE, " + "AR.PACKAGE_NAME AS PACKAGE_NAME, AR.APP_PRICE, AR.STORED_LOCATION, AR.BANNER_LOCATION, " + "AR.SC_1_LOCATION AS SCREEN_SHOT_1, AR.SC_2_LOCATION AS SCREEN_SHOT_2, AR.SC_3_LOCATION AS " + "SCREEN_SHOT_3, AR.APP_HASH_VALUE AS HASH_VALUE, AR.SHARED_WITH_ALL_TENANTS AS SHARED, " @@ -175,9 +177,9 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements Connection connection; PreparedStatement statement = null; ResultSet resultSet = null; - String sql = "SELECT AR.ID AS RELESE_ID, AR.VERSION AS RELEASE_VERSION, AR.UUID, AR.RELEASE_TYPE, AR.APP_PRICE," - + " AR.STORED_LOCATION, AR.BANNER_LOCATION, AR.SC_1_LOCATION AS SCREEN_SHOT_1, " - + "AR.SC_2_LOCATION AS SCREEN_SHOT_2, AR.SC_3_LOCATION AS SCREEN_SHOT_3, AR.APP_HASH_VALUE AS " + + String sql = "SELECT AR.ID AS RELEASE_ID, AR.PACKAGE_NAME, AR.VERSION AS RELEASE_VERSION, AR.UUID, AR" + + ".RELEASE_TYPE, AR.APP_PRICE, AR.STORED_LOCATION, AR.ICON_LOCATION, AR.BANNER_LOCATION, AR" + + ".SC_1_LOCATION AS SCREEN_SHOT_1, AR.SC_2_LOCATION AS SCREEN_SHOT_2, AR.SC_3_LOCATION AS SCREEN_SHOT_3, AR.APP_HASH_VALUE AS " + "HASH_VALUE, AR.SHARED_WITH_ALL_TENANTS AS SHARED, AR.APP_META_INFO, AR.CREATED_BY, AR.CREATED_AT, AR" + ".PUBLISHED_BY, AR.PUBLISHED_AT, AR.STARS, AL.CURRENT_STATE, AL.PREVIOUSE_STATE, AL.UPDATED_BY, " + "AL.UPDATED_AT FROM AP_APP_RELEASE AS AR, AP_APP_LIFECYCLE_STATE AS AL WHERE " + @@ -276,6 +278,63 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements } } + @Override + public List getReleaseByState(int appId, int tenantId, String state) throws + ApplicationManagementDAOException { + Connection connection; + PreparedStatement statement = null; + ResultSet resultSet = null; + List applicationReleases = new ArrayList<>(); + String sql = "SELECT AR.ID AS RELEASE_ID, AR.VERSION AS RELEASE_VERSION, AR.UUID, AR.RELEASE_TYPE, AR.APP_PRICE," + + " AR.STORED_LOCATION, AR.ICON_LOCATION, AR.BANNER_LOCATION, AR.SC_1_LOCATION AS SCREEN_SHOT_1, AR" + + ".SC_2_LOCATION AS SCREEN_SHOT_2, AR.SC_3_LOCATION AS SCREEN_SHOT_3, AR.APP_HASH_VALUE AS HASH_VALUE, " + + "AR.SHARED_WITH_ALL_TENANTS AS SHARED, AR.APP_META_INFO, AR.CREATED_BY, AR.CREATED_AT, " + + "AR.PUBLISHED_BY, AR.PUBLISHED_AT, AR.STARS, AR.RATING FROM AP_APP_RELEASE AS " + + "AR where AR.TENANT_ID = ? AND AR.AP_APP_ID=(SELECT AP_APP_ID" + + " FROM AP_APP_LIFECYCLE_STATE WHERE AP_APP_ID = ? AND CURRENT_STATE = ? AND TENANT_ID = ?);"; + + try { + connection = this.getDBConnection(); + statement = connection.prepareStatement(sql); + statement.setInt(1, tenantId); + statement.setInt(2, appId); + statement.setString(3, state); + statement.setInt(4, tenantId); + resultSet = statement.executeQuery(); + + while (resultSet.next()) { + ApplicationRelease applicationRelease = new ApplicationRelease(); + applicationRelease.setId(resultSet.getInt("RELEASE_ID")); + applicationRelease.setVersion(resultSet.getString("RELEASE_VERSION")); + applicationRelease.setUuid(resultSet.getString("UUID")); + applicationRelease.setReleaseType(resultSet.getString("RELEASE_TYPE")); + applicationRelease.setPrice(resultSet.getDouble("APP_PRICE")); + applicationRelease.setAppStoredLoc(resultSet.getString("STORED_LOCATION")); + applicationRelease.setIconLoc(resultSet.getString("ICON_LOCATION")); + applicationRelease.setBannerLoc(resultSet.getString("BANNER_LOCATION")); + applicationRelease.setScreenshotLoc1(resultSet.getString("SCREEN_SHOT_1")); + applicationRelease.setScreenshotLoc2(resultSet.getString("SCREEN_SHOT_2")); + applicationRelease.setScreenshotLoc3(resultSet.getString("SCREEN_SHOT_3")); + applicationRelease.setAppHashValue(resultSet.getString("HASH_VALUE")); + applicationRelease.setIsSharedWithAllTenants(resultSet.getInt("SHARED")); + applicationRelease.setMetaData(resultSet.getString("APP_META_INFO")); + applicationRelease.setApplicationCreator(resultSet.getString("CREATED_BY")); + applicationRelease.setRating(resultSet.getDouble("RATING")); + applicationReleases.add(applicationRelease); + } + return applicationReleases; + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException("Database connection exception while trying to get the " + + "release details of the application with id " + appId, e); + } catch (SQLException e) { + throw new ApplicationManagementDAOException( + "Error while getting all the release details of the app id" + appId + " application" + + ", while executing the query " + sql, e); + } finally { + Util.cleanupResources(statement, resultSet); + } + } + /** * To Update starts of an application release. * @@ -354,11 +413,13 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements throws ApplicationManagementDAOException { Connection connection; PreparedStatement statement = null; - String sql = "UPDATE AP_APP_RELEASE SET VERSION = ? AND UUID = ? AND RELEASE_TYPE = ? AND PACKAGE_NAME = ? " - + "AND APP_PRICE = ? AND STORED_LOCATION = ? AND BANNER_LOCATION = ? AND SC_1_LOCATION = ? " - + "AND SC_2_LOCATION = ? AND SC_3_LOCATION = ? AND APP_HASH_VALUE = ? AND SHARED_WITH_ALL_TENANTS = ? " - + "AND APP_META_INFO = ? AND CREATED_BY = ? AND CREATED_AT = ? WHERE AP_APP_ID = ? AND TENANT_ID = ? " - + "AND ID = ?;"; + String sql = + "UPDATE AP_APP_RELEASE " + + "SET VERSION = ? , 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 = ? " + + "WHERE AP_APP_ID = ? AND TENANT_ID = ? AND ID = ?;"; try { connection = this.getDBConnection(); statement = connection.prepareStatement(sql); @@ -368,15 +429,17 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements statement.setString(4, applicationRelease.getPackageName()); 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.setString(14, applicationRelease.getApplicationCreator()); - statement.setTimestamp(15, new Timestamp(System.currentTimeMillis())); + statement.setString(7, applicationRelease.getIconLoc()); + statement.setString(8, applicationRelease.getBannerLoc()); + 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( @@ -435,6 +498,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements applicationRelease.setPrice(resultSet.getDouble("APP_PRICE")); applicationRelease.setAppStoredLoc(resultSet.getString("STORED_LOCATION")); applicationRelease.setBannerLoc(resultSet.getString("BANNER_LOCATION")); + applicationRelease.setIconLoc(resultSet.getString("ICON_LOCATION")); applicationRelease.setScreenshotLoc1(resultSet.getString("SCREEN_SHOT_1")); applicationRelease.setScreenshotLoc2(resultSet.getString("SCREEN_SHOT_2")); applicationRelease.setScreenshotLoc3(resultSet.getString("SCREEN_SHOT_3")); @@ -442,7 +506,6 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements applicationRelease.setIsSharedWithAllTenants(resultSet.getInt("SHARED")); applicationRelease.setMetaData(resultSet.getString("APP_META_INFO")); applicationRelease.setApplicationCreator(resultSet.getString("CREATED_BY")); - applicationRelease.setRating(resultSet.getDouble("RATING")); return applicationRelease; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/BadRequestException.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/BadRequestException.java new file mode 100644 index 00000000000..3bdd8a8d9c3 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/BadRequestException.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.device.application.mgt.core.exception; + +import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; + +/** + * Custom exception class for wrapping BadRequest related exceptions. + */ + +public class BadRequestException extends ApplicationManagementException { + + public BadRequestException(String message, Throwable throwable) { + super(message, throwable); + } + + public BadRequestException(String message) { + setMessage(message); + } + +} \ 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/exception/ForbiddenException.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/ForbiddenException.java new file mode 100644 index 00000000000..9618d8bf50c --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/ForbiddenException.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.device.application.mgt.core.exception; + + +import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; + +/** + * Exception class that is corresponding to 401 Forbidden response + */ + +public class ForbiddenException extends ApplicationManagementException { + + public ForbiddenException(String message, Throwable throwable) { + super(message, throwable); + } + + public ForbiddenException(String message) { + setMessage(message); + } + +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/UnexpectedServerErrorException.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/UnexpectedServerErrorException.java new file mode 100644 index 00000000000..0409520cd18 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/UnexpectedServerErrorException.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.application.mgt.core.exception; + + +import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; + +public class UnexpectedServerErrorException extends ApplicationManagementException { + + public UnexpectedServerErrorException(String message, Throwable throwable) { + super(message, throwable); + } + + public UnexpectedServerErrorException(String message) { + setMessage(message); + } + +} \ 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 d8c7706972b..60b02ad4bf2 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java @@ -31,18 +31,21 @@ import org.wso2.carbon.device.application.mgt.common.ApplicationSubscriptionType import org.wso2.carbon.device.application.mgt.common.ApplicationType; import org.wso2.carbon.device.application.mgt.common.Filter; import org.wso2.carbon.device.application.mgt.common.LifecycleState; -import org.wso2.carbon.device.application.mgt.common.SortingOrder; import org.wso2.carbon.device.application.mgt.common.Tag; import org.wso2.carbon.device.application.mgt.common.UnrestrictedRole; import org.wso2.carbon.device.application.mgt.common.User; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; +import org.wso2.carbon.device.application.mgt.common.exception.RequestValidatingException; +import org.wso2.carbon.device.application.mgt.common.exception.ResourceManagementException; import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; +import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager; import org.wso2.carbon.device.application.mgt.core.dao.ApplicationDAO; import org.wso2.carbon.device.application.mgt.core.dao.ApplicationReleaseDAO; import org.wso2.carbon.device.application.mgt.core.dao.LifecycleStateDAO; import org.wso2.carbon.device.application.mgt.core.dao.VisibilityDAO; import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagementDAOFactory; +import org.wso2.carbon.device.application.mgt.core.dao.common.Util; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; import org.wso2.carbon.device.application.mgt.core.exception.LifeCycleManagementDAOException; import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException; @@ -51,7 +54,6 @@ import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManger; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO; import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.user.api.UserRealm; @@ -59,6 +61,7 @@ import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.utils.multitenancy.MultitenantUtils; +import java.io.InputStream; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -277,18 +280,20 @@ public class ApplicationManagerImpl implements ApplicationManager { } @Override - public Application getApplicationById(int id) throws ApplicationManagementException { + public Application getApplicationById(int appId, String state, boolean handleConnections) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); Application application; boolean isAppAllowed = false; - List applicationReleases; + List applicationReleases = null; try { - ConnectionManagerUtil.openDBConnection(); + if (handleConnections) { + ConnectionManagerUtil.openDBConnection(); + } application = ApplicationManagementDAOFactory.getApplicationDAO() - .getApplicationById(id, tenantId); + .getApplicationById(appId, tenantId); if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { - applicationReleases = getReleases(application.getId()); + applicationReleases = getReleaseInState(appId, state); application.setApplicationReleases(applicationReleases); return application; } @@ -305,14 +310,18 @@ public class ApplicationManagerImpl implements ApplicationManager { return null; } - applicationReleases = getReleases(application.getId()); + if (state != null) { + applicationReleases = getReleaseInState(appId, state); + } application.setApplicationReleases(applicationReleases); return application; } catch (UserStoreException e) { throw new ApplicationManagementException( - "User-store exception while getting application with the application id " + id); + "User-store exception while getting application with the application id " + appId); } finally { - ConnectionManagerUtil.closeDBConnection(); + if (handleConnections) { + ConnectionManagerUtil.closeDBConnection(); + } } } @@ -453,6 +462,20 @@ public class ApplicationManagerImpl implements ApplicationManager { } + @Override + public List getReleaseInState(int applicationId, String state) throws + ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + + Application application = getApplicationIfAccessible(applicationId); + if (log.isDebugEnabled()) { + log.debug("Request is received to retrieve all the releases related with the application " + application + .toString()); + } + ConnectionManagerUtil.getDBConnection(); + return this.applicationReleaseDAO.getReleaseByState(applicationId, tenantId, state); + } + @Override public List deleteApplication(int applicationId) throws ApplicationManagementException { String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); @@ -648,7 +671,7 @@ public class ApplicationManagerImpl implements ApplicationManager { * @param applicationUuid UUID of the Application. * @return Application related with the UUID */ - public ApplicationRelease getAppReleaseIfExists(int applicationId, String applicationUuid) throws + private ApplicationRelease getAppReleaseIfExists(int applicationId, String applicationUuid) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); ApplicationRelease applicationRelease; @@ -662,19 +685,16 @@ public class ApplicationManagerImpl implements ApplicationManager { throw new ApplicationManagementException("Application UUID is null. Application UUID is a required " + "parameter to get the relevant application."); } - ConnectionManagerUtil.getDBConnection(); applicationRelease = this.applicationReleaseDAO.getReleaseByIds(applicationId, applicationUuid, tenantId); if (applicationRelease == null) { - throw new ApplicationManagementException("Doesn't exist a application release for application ID: " + - applicationId + "and application UUID: " + - applicationUuid); + log.error("Doesn't exist a application release for application ID: " + applicationId + + "and application UUID: " + applicationUuid); } return applicationRelease; } - @Override - public ApplicationRelease updateRelease(int appId, ApplicationRelease applicationRelease) throws + private ApplicationRelease updateRelease(int appId, ApplicationRelease applicationRelease) throws ApplicationManagementException { validateAppReleasePayload(applicationRelease); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); @@ -682,9 +702,57 @@ public class ApplicationManagerImpl implements ApplicationManager { log.debug("Updating the Application release. UUID: " + applicationRelease.getUuid() + ", " + "Application Id: " + appId); } + + applicationRelease = this.applicationReleaseDAO.updateRelease(appId, applicationRelease, tenantId); + return applicationRelease; + + } + + @Override + public ApplicationRelease updateApplicationImageArtifact(int appId, String uuid, InputStream iconFileStream, InputStream + bannerFileStream, List attachments) + throws ApplicationManagementException, ResourceManagementException { + ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); + ApplicationRelease applicationRelease; + try { + ConnectionManagerUtil.getDBConnection(); + applicationRelease = getAppReleaseIfExists(appId, uuid); + if (applicationRelease == null) { + throw new NotFoundException("No App release associated with the app Id " + appId + "and UUID "+ uuid); + } + applicationStorageManager + .updateImageArtifacts(applicationRelease, iconFileStream, bannerFileStream, attachments); + updateRelease(appId, applicationRelease); + return applicationRelease; + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + @Override + public ApplicationRelease updateApplicationArtifact(int appId, String uuid, InputStream binaryFile) + throws ApplicationManagementException, ResourceManagementException, RequestValidatingException, DeviceManagementException { + ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); + ApplicationRelease applicationRelease; try { - ConnectionManagerUtil.openDBConnection(); - applicationRelease = this.applicationReleaseDAO.updateRelease(appId, applicationRelease, tenantId); + ConnectionManagerUtil.getDBConnection(); + applicationRelease = getAppReleaseIfExists(appId, uuid); + + Application application = getApplicationById(appId, null, false); + + List deviceTypes = Util.getDeviceManagementService().getDeviceTypes(); + for (DeviceType deviceType:deviceTypes) { + if (deviceType.getId() == application.getDeviceTypeId()) { + application.setDeviceType(deviceType.getName()); + } + } + if (applicationRelease == null) { + throw new NotFoundException("No App release associated with the app Id " + appId + "and UUID "+ uuid); + } + applicationStorageManager + .updateReleaseArtifacts(applicationRelease, application.getType(), application.getDeviceType(), + binaryFile); + updateRelease(appId, applicationRelease); return applicationRelease; } finally { ConnectionManagerUtil.closeDBConnection(); @@ -762,8 +830,6 @@ public class ApplicationManagerImpl implements ApplicationManager { throw new ApplicationManagementException("Failed to get lifecycle state", e); } catch (ApplicationManagementException e) { throw new ApplicationManagementException("Failed to get application and application management", e); - } finally { - ConnectionManagerUtil.closeDBConnection(); } return lifecycleState; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java index ccce9dddfe1..b8bfdd1ef46 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java @@ -133,8 +133,8 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager } @Override - public ApplicationRelease updateImageArtifacts(ApplicationRelease applicationRelease, InputStream iconFileStream, - InputStream bannerFileStream, List screenShotStreams) + public void updateImageArtifacts(ApplicationRelease applicationRelease, InputStream + iconFileStream, InputStream bannerFileStream, List screenShotStreams) throws ResourceManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); @@ -164,9 +164,7 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager count++; } } - return uploadImageArtifacts(applicationRelease, iconFileStream, bannerFileStream, screenShotStreams); } catch (ApplicationStorageManagementException e) { - ConnectionManagerUtil.rollbackDBTransaction(); throw new ApplicationStorageManagementException("Application Storage exception while trying to" + " update the screen-shot count for the application Release " + applicationRelease.getUuid() + " for the tenant " + tenantId, e); @@ -235,7 +233,7 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager + applicationRelease.getUuid(), e); } catch (ParsingException e) { throw new ApplicationStorageManagementException( - "Error occured while parsing the artifact file. Application release UUID is " + applicationRelease + "Error occurred while parsing the artifact file. Application release UUID is " + applicationRelease .getUuid(), e); } @@ -295,8 +293,7 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager @Override public void deleteApplicationReleaseArtifacts(String directoryPath) throws ApplicationStorageManagementException { - String artifactPath = storagePath + directoryPath; - File artifact = new File(artifactPath); + File artifact = new File(directoryPath); if (artifact.exists()) { try { From 7dd7dc8d6400c20933ead1162ce8ae75c381f2d9 Mon Sep 17 00:00:00 2001 From: inoshperera Date: Sat, 29 Sep 2018 09:40:31 +0530 Subject: [PATCH 6/9] partial fix on app update apis -2 --- .../application/mgt/common/Application.java | 12 +++ .../common/services/ApplicationManager.java | 47 +++++++++--- .../services/ApplicationStorageManager.java | 2 +- .../services/ApplicationManagementAPI.java | 53 ++++++++++--- .../impl/ApplicationManagementAPIImpl.java | 75 +++++++++---------- 5 files changed, 128 insertions(+), 61 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Application.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Application.java index ed0864599dc..87daa4eb5b2 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Application.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Application.java @@ -76,6 +76,10 @@ public class Application { value = "If unrestricted roles are defined then isRestricted value is true otherwise it is false") private boolean isRestricted; + @ApiModelProperty(name = "deviceTypeId", + value = "Id of the Related device type of the application", + example = "1, 2, 3") + private int deviceTypeId; @ApiModelProperty(name = "deviceType", value = "Related device type of the application", required = true, @@ -180,4 +184,12 @@ public class Application { public void setDeviceType(String deviceType) { this.deviceType = deviceType; } + + public int getDeviceTypeId() { + return deviceTypeId; + } + + public void setDeviceTypeId(int deviceTypeId) { + this.deviceTypeId = deviceTypeId; + } } 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 7d5af468758..9dc68b7f19c 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 @@ -25,7 +25,11 @@ import org.wso2.carbon.device.application.mgt.common.Filter; import org.wso2.carbon.device.application.mgt.common.LifecycleState; import org.wso2.carbon.device.application.mgt.common.UnrestrictedRole; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.RequestValidatingException; +import org.wso2.carbon.device.application.mgt.common.exception.ResourceManagementException; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import java.io.InputStream; import java.util.List; /** @@ -90,10 +94,14 @@ public interface ApplicationManager { * To get Application with the given Id. * * @param id id of the Application - * @return the Application identified by the UUID + * @param state state of the Application + * @param handleConnections Whether it is required to handle DB connections within(true), or if there are + * existing connection(false) + * @return the Application identified by the ID * @throws ApplicationManagementException Application Management Exception. */ - Application getApplicationById(int id) throws ApplicationManagementException; + Application getApplicationById(int id, String state, boolean handleConnections) throws + ApplicationManagementException; /** * To get an application associated with the release. @@ -130,6 +138,16 @@ public interface ApplicationManager { */ List getReleases(int applicationId) throws ApplicationManagementException; + /** + * To get the release of a particular Application. + * + * @param applicationId ID of the Application. + * @param state state of the Application. + * @return the List of the Application releases related with the particular Application. + * @throws ApplicationManagementException Application Management Exception. + */ + List getReleaseInState(int applicationId, String state) throws ApplicationManagementException; + /** * To get all the releases of a particular Application. * @@ -162,24 +180,33 @@ public interface ApplicationManager { Application getApplicationIfAccessible(int applicationId) throws ApplicationManagementException; /** - * Get the application release for given UUID if application release is exists and application id is valid one. + * To update release images such as icons, banner and screenshots. * - * @param releaseUuid UUID of the Application Release. + * @param appId ID of the Application + * @param uuid uuid of the Application + * @param iconFileStream icon file of the release + * @param bannerFileStream bannerFileStream of the release. + * @param attachments screenshot attachments of the release + * @return Updated Application Release. * @throws ApplicationManagementException Application Management Exception. */ - ApplicationRelease getAppReleaseIfExists(int applicationId, String releaseUuid) throws - ApplicationManagementException; + ApplicationRelease updateApplicationImageArtifact(int appId, String uuid, InputStream iconFileStream, InputStream + bannerFileStream, List attachments) + throws ApplicationManagementException, ResourceManagementException; + /** - * To update with a new release for an Application. + * To update release images. * * @param appId ID of the Application - * @param applicationRelease ApplicationRelease + * @param uuid uuid of the Application + * @param binaryFile binaryFile of the release. * @return Updated Application Release. * @throws ApplicationManagementException Application Management Exception. */ - ApplicationRelease updateRelease(int appId, ApplicationRelease applicationRelease) - throws ApplicationManagementException; + ApplicationRelease updateApplicationArtifact(int appId, String uuid, InputStream binaryFile) + throws ApplicationManagementException, ResourceManagementException, RequestValidatingException, DeviceManagementException; + /** * To verify whether application release is acceptable to update or not. diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java index a0770582e3f..71d414f3c07 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java @@ -52,7 +52,7 @@ public interface ApplicationStorageManager { * @param screenshots Input Streams of screenshots * @throws ResourceManagementException Resource Management Exception. */ - ApplicationRelease updateImageArtifacts(ApplicationRelease applicationRelease, InputStream iconFile, + void updateImageArtifacts(ApplicationRelease applicationRelease, InputStream iconFile, InputStream bannerFile, List screenshots) throws ResourceManagementException, ApplicationManagementException; 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/ApplicationManagementAPI.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/ApplicationManagementAPI.java index 35b70eeba57..2f916a344bb 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/ApplicationManagementAPI.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/ApplicationManagementAPI.java @@ -142,15 +142,15 @@ public interface ApplicationManagementAPI { ); @GET - @Path("/{appType}") + @Path("/{appId}") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @ApiOperation( consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON, httpMethod = "GET", - value = "get the application of requesting application type", - notes = "This will get the application identified by the application type and name, if exists", + value = "get the application of requesting application id and state", + notes = "This will get the application identified by the application id and state, if exists", tags = "Application Management", extensions = { @Extension(properties = { @@ -177,7 +177,12 @@ public interface ApplicationManagementAPI { name = "appId", value = "application Id", required = true) - @PathParam("appId") int appId + @PathParam("appId") int appId, + @ApiParam( + name = "state", + value = "state", + defaultValue = "PUBLISHED") + @QueryParam("state") String state ); @PUT @@ -323,10 +328,10 @@ public interface ApplicationManagementAPI { @PathParam("appid") int applicationId ); - @POST + @PUT @Path("/image-artifacts/{appId}/{uuid}") @Produces(MediaType.APPLICATION_JSON) - @Consumes(MediaType.MULTIPART_FORM_DATA) + @Consumes("multipart/mixed") @ApiOperation( consumes = MediaType.MULTIPART_FORM_DATA, produces = MediaType.APPLICATION_JSON, @@ -358,19 +363,47 @@ public interface ApplicationManagementAPI { response = ErrorResponse.class) }) Response updateApplicationImageArtifacts( - @ApiParam(name = "appId", value = "ID of the application", required = true) + @ApiParam( + name = "appId", + value = "ID of the application", + required = true) @PathParam("appId") int applicatioId, - @ApiParam(name = "uuid", value = "UUID of the application", required = true) + @ApiParam( + name = "uuid", + value = "UUID of the application", + required = true) @PathParam("uuid") String applicationUUID, + @ApiParam( + name = "icon", + value = "Icon of the uploading application", + required = true) @Multipart(value = "icon") Attachment iconFile, + @ApiParam( + name = "banner", + value = "Banner of the uploading application", + required = true) @Multipart(value = "banner") Attachment bannerFile, - @Multipart(value = "screenshot") List screenshots + @ApiParam( + name = "screenshot1", + value = "Screen Shots of the uploading application", + required = true) + @Multipart(value = "screenshot1") Attachment screenshot1, + @ApiParam( + name = "screenshot2", + value = "Screen Shots of the uploading application", + required = false) + @Multipart(value = "screenshot2") Attachment screenshot2, + @ApiParam( + name = "screenshot3", + value = "Screen Shots of the uploading application", + required = false) + @Multipart(value = "screenshot3") Attachment screenshot3 ); @PUT @Path("/app-artifacts/{deviceType}/{appType}/{appId}/{uuid}") @Produces(MediaType.APPLICATION_JSON) - @Consumes(MediaType.MULTIPART_FORM_DATA) + @Consumes("multipart/mixed") @ApiOperation( consumes = MediaType.MULTIPART_FORM_DATA, 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/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 6f16e1dc0fe..9108d44daf8 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 @@ -32,6 +32,7 @@ import org.wso2.carbon.device.application.mgt.common.exception.ResourceManagemen import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager; import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; import java.io.IOException; import java.io.InputStream; @@ -90,10 +91,11 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { @Consumes("application/json") @Path("/{appId}") public Response getApplication( - @PathParam("appId") int appId) { + @PathParam("appId") int appId, + @QueryParam("state") String state) { ApplicationManager applicationManager = APIUtil.getApplicationManager(); try { - Application application = applicationManager.getApplicationById(appId); + Application application = applicationManager.getApplicationById(appId, state, true); if (application == null) { return Response.status(Response.Status.NOT_FOUND).entity ("Application with application id: " + appId + " not found").build(); @@ -198,22 +200,22 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { @Override @PUT + @Consumes("multipart/mixed") + @Produces(MediaType.APPLICATION_JSON) @Path("/image-artifacts/{appId}/{uuid}") public Response updateApplicationImageArtifacts( @PathParam("appId") int appId, @PathParam("uuid") String applicationUuid, @Multipart("icon") Attachment iconFile, @Multipart("banner") Attachment bannerFile, - @Multipart("screenshot") List attachmentList) { - - ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); - ApplicationManager applicationManager = APIUtil.getApplicationManager(); - ApplicationRelease applicationRelease; + @Multipart("screenshot1") Attachment screenshot1, + @Multipart("screenshot2") Attachment screenshot2, + @Multipart("screenshot3") Attachment screenshot3) { try { InputStream iconFileStream = null; InputStream bannerFileStream = null; - List attachments = new ArrayList<>(); + List attachments = new ArrayList<>();; if (iconFile != null) { iconFileStream = iconFile.getDataHandler().getInputStream(); @@ -221,29 +223,22 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { if (bannerFile != null) { bannerFileStream = bannerFile.getDataHandler().getInputStream(); } - if (attachmentList != null && !attachmentList.isEmpty()) { - for (Attachment screenshot : attachmentList) { - attachments.add(screenshot.getDataHandler().getInputStream()); - } + + attachments.add(screenshot1.getDataHandler().getInputStream()); + if(screenshot2 != null) { + attachments.add(screenshot2.getDataHandler().getInputStream()); } - applicationRelease = applicationManager.getAppReleaseIfExists(appId, applicationUuid); - LifecycleState lifecycleState = applicationManager.getLifecycleState(appId, applicationRelease.getUuid()); - if (AppLifecycleState.PUBLISHED.toString().equals(lifecycleState.getCurrentState()) || - AppLifecycleState.DEPRECATED.toString().equals(lifecycleState.getCurrentState())) { - return Response.status(Response.Status.FORBIDDEN).entity("Can't Update the application release in " + - "PUBLISHED or DEPRECATED state. Hence please demote the application and update the application " + - "release").build(); + if(screenshot3 != null) { + attachments.add(screenshot3.getDataHandler().getInputStream()); } - applicationRelease = applicationStorageManager - .updateImageArtifacts(applicationRelease, iconFileStream, bannerFileStream, attachments); - applicationManager.updateRelease(appId, applicationRelease); - return Response.status(Response.Status.OK) - .entity("Successfully uploaded artifacts for the application " + applicationUuid).build(); + ApplicationManager applicationManager = APIUtil.getApplicationManager(); + applicationManager.updateApplicationImageArtifact(appId, + applicationUuid, iconFileStream, bannerFileStream, attachments); + + return Response.status(Response.Status.OK).entity("Successfully uploaded artifacts for the application " + + applicationUuid).build(); } catch (NotFoundException e) { - String msg = - "Couldn't found application release details or storage details or lifecycle details. Application id: " - + appId + " App release uuid: " + applicationUuid; - log.error(msg, e); + log.error(e.getMessage(), e); return APIUtil.getResponse(e, Response.Status.NOT_FOUND); } catch (ApplicationManagementException e) { String msg = "Error occurred while updating the application."; @@ -263,6 +258,7 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { @Override @PUT + @Consumes("multipart/mixed") @Path("/app-artifacts/{deviceType}/{appType}/{appId}/{uuid}") public Response updateApplicationArtifact( @PathParam("deviceType") String deviceType, @@ -270,9 +266,6 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { @PathParam("appId") int applicationId, @PathParam("uuid") String applicationUuid, @Multipart("binaryFile") Attachment binaryFile) { - ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); - ApplicationManager applicationManager = APIUtil.getApplicationManager(); - ApplicationRelease applicationRelease; try { @@ -280,16 +273,14 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { return APIUtil.getResponse("Uploading artifacts for the application is failed " + applicationUuid, Response.Status.BAD_REQUEST); } - applicationRelease = applicationManager.getAppReleaseIfExists(applicationId, applicationUuid); - applicationRelease = applicationStorageManager - .updateReleaseArtifacts(applicationRelease, appType, deviceType, - binaryFile.getDataHandler().getInputStream()); - applicationManager.updateRelease(applicationId, applicationRelease); + APIUtil.getApplicationManager().updateApplicationArtifact(applicationId, applicationUuid, + binaryFile.getDataHandler().getInputStream()); return Response.status(Response.Status.OK) .entity("Successfully uploaded artifacts for the application release. UUID is " + applicationUuid).build(); } catch (IOException e) { String msg = - "Error occured while trying to read icon, banner files for the application release" + applicationUuid; + "Error occurred while trying to read icon, banner files for the application release" + + applicationUuid; log.error(msg); return APIUtil.getResponse(new ApplicationManagementException(msg, e), Response.Status.BAD_REQUEST); @@ -305,6 +296,10 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { log.error("Error occured while handling the application artifact updating request. application release UUID: " + applicationUuid); return APIUtil.getResponse(e, Response.Status.BAD_REQUEST); + } catch (DeviceManagementException e) { + log.error("Error occurred while updating the image artifacts of the application with the uuid " + + applicationUuid, e); + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); } } @@ -369,9 +364,9 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { } } - applicationRelease = applicationStorageManager - .updateImageArtifacts(applicationRelease, iconFileStream, bannerFileStream, attachments); - applicationRelease = applicationManager.updateRelease(applicationId, applicationRelease); +// applicationRelease = applicationStorageManager +// .updateImageArtifacts(applicationRelease, iconFileStream, bannerFileStream, attachments); +// applicationRelease = applicationManager.updateRelease(applicationId, applicationRelease); return Response.status(Response.Status.OK).entity(applicationRelease).build(); } catch (ApplicationManagementException e) { From 853d095706b2d7bda2f7304cd65a528e0ee780be Mon Sep 17 00:00:00 2001 From: inoshperera Date: Sat, 29 Sep 2018 12:03:11 +0530 Subject: [PATCH 7/9] conflict fixes --- .../services/ApplicationStorageManager.java | 2 +- .../application/mgt/core/dao/common/Util.java | 22 +++++++++---------- .../GenericApplicationReleaseDAOImpl.java | 22 +++---------------- .../mgt/core/impl/ApplicationManagerImpl.java | 5 ++--- .../impl/ApplicationStorageManagerImpl.java | 3 ++- 5 files changed, 19 insertions(+), 35 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/ApplicationStorageManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java index 71d414f3c07..a0770582e3f 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java @@ -52,7 +52,7 @@ public interface ApplicationStorageManager { * @param screenshots Input Streams of screenshots * @throws ResourceManagementException Resource Management Exception. */ - void updateImageArtifacts(ApplicationRelease applicationRelease, InputStream iconFile, + ApplicationRelease updateImageArtifacts(ApplicationRelease applicationRelease, InputStream iconFile, InputStream bannerFile, List screenshots) throws ResourceManagementException, 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/common/Util.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java index 90542d2c58f..1722a420703 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java @@ -139,17 +139,17 @@ public class Util { application.setDeviceTypeId(rs.getInt("DEVICE_TYPE_ID")); } -// Tag tag = new Tag(); -// tag.setTagName(rs.getString("APP_TAG")); -// UnrestrictedRole unrestrictedRole = new UnrestrictedRole(); -// unrestrictedRole.setRole(rs.getString("ROLE")); -// if (application.getTags() != null && application.getTags().contains(tag)) { -// application.getTags().add(tag); -// } -// if (application.getUnrestrictedRoles() != null && application.getUnrestrictedRoles() -// .contains(unrestrictedRole)) { -// application.getUnrestrictedRoles().add(unrestrictedRole); -// } + Tag tag = new Tag(); + tag.setTagName(rs.getString("APP_TAG")); + UnrestrictedRole unrestrictedRole = new UnrestrictedRole(); + unrestrictedRole.setRole(rs.getString("ROLE")); + if (application.getTags() != null && application.getTags().contains(tag)) { + application.getTags().add(tag); + } + if (application.getUnrestrictedRoles() != null && application.getUnrestrictedRoles() + .contains(unrestrictedRole)) { + application.getUnrestrictedRoles().add(unrestrictedRole); + } iteration++; } } 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 6c09f147759..319a7dd0495 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 @@ -254,8 +254,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements String sql = "SELECT AR.ID AS RELEASE_ID, AR.VERSION AS RELEASE_VERSION, AR.UUID, AR.RELEASE_TYPE, AR.APP_PRICE," + " AR.STORED_LOCATION, AR.ICON_LOCATION, AR.BANNER_LOCATION, AR.SC_1_LOCATION AS SCREEN_SHOT_1, AR" + ".SC_2_LOCATION AS SCREEN_SHOT_2, AR.SC_3_LOCATION AS SCREEN_SHOT_3, AR.APP_HASH_VALUE AS HASH_VALUE, " - + "AR.SHARED_WITH_ALL_TENANTS AS SHARED, AR.APP_META_INFO, AR.CREATED_BY, AR.CREATED_AT, " - + "AR.PUBLISHED_BY, AR.PUBLISHED_AT, AR.STARS, AR.RATING FROM AP_APP_RELEASE AS " + + "AR.SHARED_WITH_ALL_TENANTS AS SHARED, AR.APP_META_INFO, AR.RATING FROM AP_APP_RELEASE AS " + "AR where AR.TENANT_ID = ? AND AR.AP_APP_ID=(SELECT AP_APP_ID" + " FROM AP_APP_LIFECYCLE_STATE WHERE AP_APP_ID = ? AND CURRENT_STATE = ? AND TENANT_ID = ?);"; @@ -269,23 +268,8 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements resultSet = statement.executeQuery(); while (resultSet.next()) { - ApplicationRelease applicationRelease = new ApplicationRelease(); - applicationRelease.setId(resultSet.getInt("RELEASE_ID")); - applicationRelease.setVersion(resultSet.getString("RELEASE_VERSION")); - applicationRelease.setUuid(resultSet.getString("UUID")); - applicationRelease.setReleaseType(resultSet.getString("RELEASE_TYPE")); - applicationRelease.setPrice(resultSet.getDouble("APP_PRICE")); - applicationRelease.setAppStoredLoc(resultSet.getString("STORED_LOCATION")); - applicationRelease.setIconLoc(resultSet.getString("ICON_LOCATION")); - applicationRelease.setBannerLoc(resultSet.getString("BANNER_LOCATION")); - applicationRelease.setScreenshotLoc1(resultSet.getString("SCREEN_SHOT_1")); - applicationRelease.setScreenshotLoc2(resultSet.getString("SCREEN_SHOT_2")); - applicationRelease.setScreenshotLoc3(resultSet.getString("SCREEN_SHOT_3")); - applicationRelease.setAppHashValue(resultSet.getString("HASH_VALUE")); - applicationRelease.setIsSharedWithAllTenants(resultSet.getInt("SHARED")); - applicationRelease.setMetaData(resultSet.getString("APP_META_INFO")); - applicationRelease.setRating(resultSet.getDouble("RATING")); - applicationReleases.add(applicationRelease); + ApplicationRelease appRelease = constructApplicationRelease(resultSet); + applicationReleases.add(appRelease); } return applicationReleases; } catch (DBConnectionException e) { 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 72991467544..fc27224669e 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 @@ -733,10 +733,9 @@ public class ApplicationManagerImpl implements ApplicationManager { if (applicationRelease == null) { throw new NotFoundException("No App release associated with the app Id " + appId + "and UUID "+ uuid); } - applicationStorageManager + ApplicationRelease updatedRelease = applicationStorageManager .updateImageArtifacts(applicationRelease, iconFileStream, bannerFileStream, attachments); - updateRelease(appId, applicationRelease); - return applicationRelease; + return updateRelease(appId, updatedRelease); } finally { ConnectionManagerUtil.closeDBConnection(); } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java index b8bfdd1ef46..37e17821ce4 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java @@ -133,7 +133,7 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager } @Override - public void updateImageArtifacts(ApplicationRelease applicationRelease, InputStream + public ApplicationRelease updateImageArtifacts(ApplicationRelease applicationRelease, InputStream iconFileStream, InputStream bannerFileStream, List screenShotStreams) throws ResourceManagementException { @@ -164,6 +164,7 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager count++; } } + return applicationRelease; } catch (ApplicationStorageManagementException e) { throw new ApplicationStorageManagementException("Application Storage exception while trying to" + " update the screen-shot count for the application Release " + applicationRelease.getUuid() + From d9d0d0a5b8634c69f2ac2533ff278d8544fd9bf0 Mon Sep 17 00:00:00 2001 From: inoshperera Date: Sat, 29 Sep 2018 12:21:23 +0530 Subject: [PATCH 8/9] add getLifecycleState check --- .../application/mgt/core/impl/ApplicationManagerImpl.java | 8 ++++++++ 1 file changed, 8 insertions(+) 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 fc27224669e..7c20c984ebe 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 @@ -46,6 +46,7 @@ import org.wso2.carbon.device.application.mgt.core.dao.VisibilityDAO; import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagementDAOFactory; import org.wso2.carbon.device.application.mgt.core.dao.common.Util; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; +import org.wso2.carbon.device.application.mgt.core.exception.ForbiddenException; import org.wso2.carbon.device.application.mgt.core.exception.LifeCycleManagementDAOException; import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException; import org.wso2.carbon.device.application.mgt.core.exception.ValidationException; @@ -733,6 +734,13 @@ public class ApplicationManagerImpl implements ApplicationManager { if (applicationRelease == null) { throw new NotFoundException("No App release associated with the app Id " + appId + "and UUID "+ uuid); } + LifecycleState lifecycleState = getLifecycleState(appId, applicationRelease.getUuid()); + if (AppLifecycleState.PUBLISHED.toString().equals(lifecycleState.getCurrentState()) || + AppLifecycleState.DEPRECATED.toString().equals(lifecycleState.getCurrentState())) { + throw new ForbiddenException("Can't Update the application release in " + + "PUBLISHED or DEPRECATED state. Hence please demote the application and update " + + "the application release"); + } ApplicationRelease updatedRelease = applicationStorageManager .updateImageArtifacts(applicationRelease, iconFileStream, bannerFileStream, attachments); return updateRelease(appId, updatedRelease); From a3b8cef1a2a58233fea534cee15e5dc0af287093 Mon Sep 17 00:00:00 2001 From: inoshperera Date: Sat, 29 Sep 2018 18:15:58 +0530 Subject: [PATCH 9/9] 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,