From eb6998f29709d8eed85d6e4e513f40eb1ff39cb6 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Mon, 29 Apr 2019 18:07:27 +0530 Subject: [PATCH 01/11] Fix application release update method --- .../mgt/common/ApplicationArtifact.java | 3 +- .../common/services/ApplicationManager.java | 8 +- .../services/ApplicationStorageManager.java | 1 - .../wrapper/ApplicationReleaseWrapper.java | 26 +- .../wrapper/ApplicationUpdateWrapper.java | 26 +- .../common/wrapper/ApplicationWrapper.java | 26 +- .../mgt/core/dao/ApplicationDAO.java | 2 + .../GenericApplicationDAOImpl.java | 37 +++ .../GenericApplicationReleaseDAOImpl.java | 3 +- .../mgt/core/impl/ApplicationManagerImpl.java | 276 +++++++++++------- .../impl/ApplicationStorageManagerImpl.java | 5 - .../ApplicationManagementPublisherAPI.java | 59 +++- ...ApplicationManagementPublisherAPIImpl.java | 76 ++--- 13 files changed, 330 insertions(+), 218 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/ApplicationArtifact.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/ApplicationArtifact.java index 1b44fb9e7fb..a8ae90ff589 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/ApplicationArtifact.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/ApplicationArtifact.java @@ -1,4 +1,5 @@ -package org.wso2.carbon.device.application.mgt.common;/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. +package org.wso2.carbon.device.application.mgt.common; +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. * * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except 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 eb5f11a022d..ae452e5f558 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 @@ -175,16 +175,16 @@ public interface ApplicationManager { * @param applicationId ID of the application * @param releaseUuid UUID of the application release * @param deviceType Supported device type of the application - * @param applicationRelease {@link ApplicationReleaseDTO} + * @param applicationReleaseWrapper {@link ApplicationReleaseDTO} * @param binaryFileStram {@link InputStream} of the binary file * @param iconFileStream {@link InputStream} of the icon * @param bannerFileStream {@link InputStream} of the banner * @param attachments {@link List} of {@link InputStream} of attachments * @return If the application release is updated correctly True returns, otherwise retuen False */ - boolean updateRelease(int applicationId, String releaseUuid, String deviceType, ApplicationReleaseDTO applicationRelease, - InputStream binaryFileStram, InputStream iconFileStream, InputStream bannerFileStream, - List attachments) throws ApplicationManagementException; + boolean updateRelease(String deviceType, String applicationType, String releaseUuid, + ApplicationReleaseWrapper applicationReleaseWrapper, ApplicationArtifact applicationArtifact) + throws ApplicationManagementException; /*** * To validate the application creating request 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 26e426227e5..759e6136283 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 @@ -22,7 +22,6 @@ package org.wso2.carbon.device.application.mgt.common.services; import org.wso2.carbon.device.application.mgt.common.ApplicationInstaller; import org.wso2.carbon.device.application.mgt.common.dto.ApplicationReleaseDTO; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException; -import org.wso2.carbon.device.application.mgt.common.exception.RequestValidatingException; import org.wso2.carbon.device.application.mgt.common.exception.ResourceManagementException; import java.io.InputStream; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationReleaseWrapper.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationReleaseWrapper.java index cef7d31e0cb..3d991776474 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationReleaseWrapper.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationReleaseWrapper.java @@ -1,20 +1,18 @@ -/* - * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) 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 + * Entgra (Pvt) Ltd. 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. + * 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.common.wrapper; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationUpdateWrapper.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationUpdateWrapper.java index 88e3f959079..81d0f1714f1 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationUpdateWrapper.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationUpdateWrapper.java @@ -1,20 +1,18 @@ -/* - * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) 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 + * Entgra (Pvt) Ltd. 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. + * 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.common.wrapper; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationWrapper.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationWrapper.java index b83bbc6a7de..3a02f1b3dcf 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationWrapper.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationWrapper.java @@ -1,20 +1,18 @@ -/* - * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) 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 + * Entgra (Pvt) Ltd. 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. + * 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.common.wrapper; 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 efd1bdec20b..d43819d949d 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 @@ -207,5 +207,7 @@ public interface ApplicationDAO { * @throws ApplicationManagementDAOException if unable to fetch the ApplicationDTO from the data store. */ ApplicationDTO getApplicationByRelease(String appReleaseUUID, int tenantId) throws ApplicationManagementDAOException; + + String getApplicationSubTypeByUUID(String uuid, int tenantId) throws ApplicationManagementDAOException; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java index f003862396f..64dc0bd9847 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 @@ -1095,4 +1095,41 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic Util.cleanupResources(stmt, rs); } } + + @Override + public String getApplicationSubTypeByUUID(String uuid, int tenantId) throws ApplicationManagementDAOException { + Connection conn; + String sql; + try { + conn = this.getDBConnection(); + sql = "SELECT AP_APP.SUB_TYPE AS SUB_TYPE " + + "FROM AP_APP " + + "WHERE " + + "AP_APP.ID = (SELECT AP_APP_RELEASE.AP_APP_ID " + + " FROM AP_APP_RELEASE " + + " WHERE AP_APP_RELEASE.UUID = ?) " + + "AND AP_APP.TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)){ + stmt.setString(1, uuid); + stmt.setInt(2, tenantId); + try(ResultSet rs = stmt.executeQuery()){ + if (rs.next()){ + return rs.getString("SUB_TYPE"); + } + return null; + } + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection to get subscription type of the application " + + "for given application release uuid. UUID:." + uuid; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while getting application subscribe type for given application release UUID: " + + uuid + " from database."; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); + } + } + } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java index d8d473932cb..ad5f520570b 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 @@ -241,7 +241,8 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements + "AR.APP_META_INFO AS RELEASE_META_INFO, " + "AR.SUPPORTED_OS_VERSIONS AS RELEASE_SUP_OS_VERSIONS, " + "AR.RATING AS RELEASE_RATING, " - + "AR.CURRENT_STATE AS RELEASE_CURRENT_STATE, AR.RATED_USERS AS RATED_USER_COUNT " + + "AR.CURRENT_STATE AS RELEASE_CURRENT_STATE, " + + "AR.RATED_USERS AS RATED_USER_COUNT " + "FROM AP_APP_RELEASE AS AR " + "WHERE AR.UUID = ? AND AR.TENAT_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 7a1f683ba61..d04b68ac58e 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 @@ -416,6 +416,91 @@ public class ApplicationManagerImpl implements ApplicationManager { return applicationReleaseDTO; } + private ApplicationReleaseDTO updateApplicationReleaseArtifacts(String applicationType, String deviceType, + ApplicationReleaseDTO applicationReleaseDTO, ApplicationArtifact applicationArtifact) + throws ResourceManagementException, ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); + + // The application executable artifacts such as apks are uploaded. + if (ApplicationType.ENTERPRISE.toString().equals(applicationType)) { + try { + byte[] content = IOUtils.toByteArray(applicationArtifact.getInstallerStream()); + + try(ByteArrayInputStream binaryClone = new ByteArrayInputStream(content)){ + String md5OfApp = StorageManagementUtil.getMD5(binaryClone); + + if (md5OfApp == null) { + String msg = "Error occurred while md5sum value retrieving process: application UUID " + + applicationReleaseDTO.getUuid(); + log.error(msg); + throw new ApplicationStorageManagementException(msg); + } + if (!applicationReleaseDTO.getAppHashValue().equals(md5OfApp)){ + applicationReleaseDTO.setInstallerName(applicationArtifact.getInstallerName()); + + try (ByteArrayInputStream binary = new ByteArrayInputStream(content)) { + ApplicationInstaller applicationInstaller = applicationStorageManager + .getAppInstallerData(binary, deviceType); + String packagename = applicationInstaller.getPackageName(); + + ConnectionManagerUtil.getDBConnection(); + if (applicationReleaseDAO.isActiveReleaseExisitForPackageName(packagename, tenantId, + lifecycleStateManager.getEndState())) { + String msg = "Application release is already exist for the package name: " + packagename + + ". Either you can delete all application releases for package " + packagename + " or " + + "you can add this app release as an new application release, under the existing " + + "application."; + log.error(msg); + throw new ApplicationManagementException(msg); + } + applicationReleaseDTO.setVersion(applicationInstaller.getVersion()); + applicationReleaseDTO.setPackageName(packagename); + + if (this.applicationReleaseDAO + .verifyReleaseExistenceByHash(md5OfApp, tenantId)) { + throw new BadRequestException( + "Application release exists for the uploaded binary file. Application Type: " + + applicationType + " Device Tyep: " + deviceType); + } + + applicationReleaseDTO.setAppHashValue(md5OfApp); + try (ByteArrayInputStream binaryDuplicate = new ByteArrayInputStream(content)) { + applicationReleaseDTO = applicationStorageManager + .uploadReleaseArtifact(applicationReleaseDTO,applicationType, + deviceType, binaryDuplicate); + } + } + } + + } + + + } catch (IOException e) { + String msg = + "Error occurred when getting byte array of binary file. Installer name: " + applicationArtifact + .getInstallerName(); + log.error(msg); + throw new ApplicationStorageManagementException(msg); + } catch (DBConnectionException e) { + String msg = "Error occurred when getting database connection for verifying application package existence."; + log.error(msg); + throw new ApplicationManagementException(msg, e); + } catch (ApplicationManagementDAOException e) { + String msg = "Error occurred when executing the query for verifying application release existence for " + + "the package."; + log.error(msg); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } else if (ApplicationType.WEB_CLIP.toString().equals(applicationType)) { + applicationReleaseDTO.setVersion(Constants.DEFAULT_VERSION); + applicationReleaseDTO.setInstallerName(applicationReleaseDTO.getUrl()); + } + return applicationReleaseDTO; + } + private ApplicationReleaseDTO addImageArtifacts(ApplicationReleaseDTO applicationReleaseDTO, ApplicationArtifact applicationArtifact) throws ResourceManagementException { ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); @@ -432,13 +517,49 @@ public class ApplicationManagerImpl implements ApplicationManager { applicationReleaseDTO.setScreenshotName1(scName); } else if (counter == 2) { applicationReleaseDTO.setScreenshotName2(scName); + } else if (counter == 3) { + applicationReleaseDTO.setScreenshotName3(scName); + } + counter++; + } + + // Upload images + applicationReleaseDTO = applicationStorageManager + .uploadImageArtifacts(applicationReleaseDTO, applicationArtifact.getIconStream(), + applicationArtifact.getBannerStream(), new ArrayList<>(screenshots.values())); + return applicationReleaseDTO; + } + private ApplicationReleaseDTO updateImageArtifacts(ApplicationReleaseDTO applicationReleaseDTO, + ApplicationArtifact applicationArtifact) throws ResourceManagementException{ + ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); + + applicationReleaseDTO.setIconName(applicationArtifact.getIconName()); + applicationReleaseDTO.setBannerName(applicationArtifact.getBannerName()); + + Map screenshots = applicationArtifact.getScreenshots(); + List screenshotNames = new ArrayList<>(screenshots.keySet()); + + int counter = 1; + for (String scName : screenshotNames) { + if (counter == 1) { + applicationReleaseDTO.setScreenshotName1(scName); + } else if (counter == 2) { + applicationReleaseDTO.setScreenshotName2(scName); } else if (counter == 3) { applicationReleaseDTO.setScreenshotName3(scName); } counter++; } + int numOfScreenshots = screenshotNames.size(); + if (numOfScreenshots == 1) { + applicationReleaseDTO.setScreenshotName2(null); + applicationReleaseDTO.setScreenshotName3(null); + } else if (numOfScreenshots == 2) { + applicationReleaseDTO.setScreenshotName3(null); + } + // Upload images applicationReleaseDTO = applicationStorageManager .uploadImageArtifacts(applicationReleaseDTO, applicationArtifact.getIconStream(), @@ -1683,132 +1804,75 @@ public class ApplicationManagerImpl implements ApplicationManager { return lifecycleState; } - //todo check whether package names are same @Override - public boolean updateRelease(int applicationId, String releaseUuid, String deviceType, - ApplicationReleaseDTO updateRelease, InputStream binaryFileStram, InputStream iconFileStream, - InputStream bannerFileStream, List attachments) throws ApplicationManagementException { + public boolean updateRelease(String deviceType, String applicationType, String releaseUuid, + ApplicationReleaseWrapper applicationReleaseWrapper, ApplicationArtifact applicationArtifact) + throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - ApplicationReleaseDTO release; - ApplicationDTO app = null; - ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); - DeviceType deviceTypeObj; - boolean isAdminUser; - try { - // Getting the device type details to get device type ID for internal mappings - deviceTypeObj = Util.getDeviceManagementService().getDeviceType(deviceType); - isAdminUser = isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION); - ConnectionManagerUtil.beginDBTransaction(); - app = this.applicationDAO.getApplicationById(applicationId, tenantId); - release = this.applicationReleaseDAO.getReleaseByIds(applicationId, releaseUuid, tenantId); + ApplicationReleaseDTO applicationReleaseDTO = this.applicationReleaseDAO + .getReleaseByUUID(releaseUuid, tenantId); - if (app == null) { - ConnectionManagerUtil.rollbackDBTransaction(); - throw new NotFoundException( - "Couldn't found an application for updating. ApplicationDTO id: " + applicationId); + if (applicationReleaseDTO == null) { + String msg = "Couldn't found an application release for updating. Application release UUID: " + releaseUuid; + log.error(msg); + throw new NotFoundException(msg); } - if (deviceTypeObj == null || deviceTypeObj.getId() != app.getDeviceTypeId()) { - ConnectionManagerUtil.rollbackDBTransaction(); - throw new BadRequestException( - "Request to update application release for Invalid device type. Device type: " + deviceType - + " application ID " + applicationId + " ApplicationDTO Release UUID " + releaseUuid); - } - if (release == null) { - ConnectionManagerUtil.rollbackDBTransaction(); - throw new NotFoundException( - "Couldn't found an application realise for updating. ApplicationDTO id: " + applicationId - + " and application release UUID: " + releaseUuid); + if (!lifecycleStateManager.isUpdatableState(applicationReleaseDTO.getCurrentState())) { + String msg = "Application release in " + applicationReleaseDTO.getCurrentState() + + " state. Therefore you are not allowed to update the application release. Hence, " + + "please move application release from " + applicationReleaseDTO.getCurrentState() + + " to updatable state."; + log.error(msg); + throw new ForbiddenException(msg); } - String releaseType = updateRelease.getReleaseType(); - Double price = updateRelease.getPrice(); - String metaData = updateRelease.getMetaData(); + DeviceType deviceTypeObj = getDeviceTypeData(deviceType); + String releaseType = applicationReleaseWrapper.getReleaseType(); + Double price = applicationReleaseWrapper.getPrice(); + String metaData = applicationReleaseWrapper.getMetaData(); - if (price < 0.0 || (price == 0.0 && ApplicationSubscriptionType.PAID.toString().equals(app.getSubType())) - || (price > 0.0 && ApplicationSubscriptionType.FREE.toString().equals(app.getSubType()))) { - ConnectionManagerUtil.rollbackDBTransaction(); + String applicationSubType = this.applicationDAO.getApplicationSubTypeByUUID(releaseUuid, tenantId); + if (applicationSubType == null) { + String msg = "Couldn't find an application subscription type for the application release UUID: " + releaseUuid; + log.error(msg); + throw new ApplicationManagementException(msg); + } + + if (price < 0.0 || (price == 0.0 && ApplicationSubscriptionType.PAID.toString().equals(applicationSubType)) + || (price > 0.0 && ApplicationSubscriptionType.FREE.toString().equals(applicationSubType))) { throw new BadRequestException( "Invalid app release payload for updating application release. ApplicationDTO price is " + price - + " for " + app.getSubType() + " application. ApplicationDTO ID: " + applicationId - + ", ApplicationDTO Release UUID " + releaseUuid + " and supported device type is " - + deviceType); + + " for " + applicationSubType + " application., Application Release UUID " + + releaseUuid + " and supported device type is " + deviceType); } - release.setPrice(price); - if (releaseType != null) { - release.setReleaseType(releaseType); + applicationReleaseDTO.setPrice(price); + if (!StringUtils.isEmpty(releaseType)) { + applicationReleaseDTO.setReleaseType(releaseType); } - if (metaData != null) { - release.setMetaData(metaData); - } - - List unrestrictedRoles = app.getUnrestrictedRoles(); - - String applicationReleaseCreatedUser = lifecycleStateDAO - .getAppReleaseCreatedUsername(applicationId, releaseUuid, tenantId); - - if (!isAdminUser && !(!unrestrictedRoles.isEmpty() && hasUserRole(unrestrictedRoles, userName)) - && !userName.equals(applicationReleaseCreatedUser)) { - ConnectionManagerUtil.rollbackDBTransaction(); - throw new ForbiddenException("You are not authorized user to update application"); - } - - //todo try to remove this DB call and get it when getting application release - LifecycleStateDTO lifecycleState = this.lifecycleStateDAO.getLatestLifeCycleState(applicationId, releaseUuid); - if (!AppLifecycleState.CREATED.toString().equals(lifecycleState.getCurrentState()) - && !AppLifecycleState.IN_REVIEW.toString().equals(lifecycleState.getCurrentState()) - && !AppLifecycleState.REJECTED.toString().equals(lifecycleState.getCurrentState())) { - ConnectionManagerUtil.rollbackDBTransaction(); - throw new ForbiddenException( - "You can't update application release which is in " + lifecycleState.getCurrentState() - + " State"); + if (!StringUtils.isEmpty(metaData)) { + applicationReleaseDTO.setMetaData(metaData); } -// release = applicationStorageManager -// .deleteImageArtifacts(release, iconFileStream, bannerFileStream, attachments); -// release = applicationStorageManager -// .copyImageArtifactsAndDeleteInstaller(release, app.getType(), deviceType, binaryFileStram); - return applicationReleaseDAO.updateRelease(release, tenantId) != null; - } catch (DeviceManagementException e) { - throw new ApplicationManagementException("Error occured when validating the device type " + deviceType, e); - } catch (UserStoreException e) { - throw new ApplicationManagementException( - "Error occured while verifying whether user is admin user or not. Username " + userName - + " tenant id " + tenantId, e); - } catch (LifeCycleManagementDAOException e) { - ConnectionManagerUtil.rollbackDBTransaction(); - throw new ApplicationManagementException( - "Error Occured when getting lifecycle state of the application release of application UUID: " - + releaseUuid, e); + applicationReleaseDTO = updateApplicationReleaseArtifacts(applicationType, deviceTypeObj.getName(), + applicationReleaseDTO, applicationArtifact); + applicationReleaseDTO = updateImageArtifacts(applicationReleaseDTO, applicationArtifact); + return applicationReleaseDAO.updateRelease(applicationReleaseDTO, tenantId) != null; } catch (ApplicationManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); - throw new ApplicationManagementException( - "Error occured when updating ApplicationDTO release. ApplicationDTO ID " + applicationId - + " ApplicationDTO Release UUID: " + releaseUuid, e); + String msg = "Error occured when updating Application release. ApplicationDTO ID ApplicationDTO Release " + + "UUID: " + releaseUuid; + log.error(msg); + throw new ApplicationManagementException(msg, e); + } catch (ResourceManagementException e) { + String msg = "Error occured when updating application release artifact in the file system. Application " + + "release UUID:" + releaseUuid; + log.error(msg); + throw new ApplicationManagementException(msg, e); } -// catch (ApplicationStorageManagementException e) { -// ConnectionManagerUtil.rollbackDBTransaction(); -// throw new ApplicationManagementException( -// "Error occured when updating application release artifact. ApplicationDTO ID " + applicationId -// + " ApplicationDTO release UUID: " + releaseUuid, e); -// } -// catch (ResourceManagementException e) { -// // updating images -// ConnectionManagerUtil.rollbackDBTransaction(); -// throw new ApplicationManagementException( -// "Error occured when updating image artifact of the application release. ApplicationDTO ID: " -// + applicationId + " ApplicationDTO release UUID: " + releaseUuid, e); -// } catch (RequestValidatingException e) { -// ConnectionManagerUtil.rollbackDBTransaction(); -// throw new ApplicationManagementException( -// "Error occured when validating application release artifact for device type " + deviceType -// + " And application type " + app.getType() + ". Applicationn ID: " + applicationId -// + " ApplicationDTO release UUID: " + releaseUuid); -// } } 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 b695405bc10..053aa3bf73c 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 @@ -78,7 +78,6 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager public ApplicationReleaseDTO uploadImageArtifacts(ApplicationReleaseDTO applicationRelease, InputStream iconFileStream, InputStream bannerFileStream, List screenShotStreams) throws ResourceManagementException { - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String artifactDirectoryPath; String iconStoredLocation; String bannerStoredLocation; @@ -122,10 +121,6 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager } catch (IOException e) { throw new ApplicationStorageManagementException("IO Exception while saving the screens hots for " + "the application " + applicationRelease.getUuid(), e); - } catch (ApplicationStorageManagementException e) { - throw new ApplicationStorageManagementException("ApplicationDTO Management DAO exception while trying to " - + "update the screen-shot count for the application " + applicationRelease.getUuid() + - " for the tenant id " + tenantId, e); } } 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/ApplicationManagementPublisherAPI.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/ApplicationManagementPublisherAPI.java index 5ffaa35752b..30dbfb2fb40 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/ApplicationManagementPublisherAPI.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/ApplicationManagementPublisherAPI.java @@ -535,7 +535,7 @@ public interface ApplicationManagementPublisherAPI { ); @PUT - @Path("/{deviceType}/{appId}/{uuid}") + @Path("/app-release/{deviceType}/{appType}/{uuid}") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.MULTIPART_FORM_DATA) @ApiOperation( @@ -567,21 +567,56 @@ public interface ApplicationManagementPublisherAPI { response = ErrorResponse.class) }) Response updateApplicationRelease( - @ApiParam(name = "deviceType", value = "Supported device type of the application", required = true) + @ApiParam( + name = "deviceType", + value = "Supported device type of the application", + required = true) @PathParam("deviceType") String deviceType, - @ApiParam(name = "appId", value = "Identifier of the ApplicationDTO", required = true) - @PathParam("appId") int applicationId, - @ApiParam(name = "UUID", value = "Unique identifier of the ApplicationDTO Release", required = true) + @ApiParam( + name = "appType", + value = "Type of the application", + required = true) + @PathParam("appType") String appType, + @ApiParam( + name = "UUID", + value = "Unique identifier of the ApplicationDTO Release", + required = true) @PathParam("uuid") String applicationUUID, - @Multipart(value = "applicationRelease", required = false, type = "application/json") ApplicationReleaseDTO applicationRelease, - @Multipart(value = "binaryFile", required = false) Attachment binaryFile, - @Multipart(value = "icon", required = false) Attachment iconFile, - @Multipart(value = "banner", required = false) Attachment bannerFile, - @ApiParam(name = "screenshot1", value = "Screen Shots of the uploading application", required = true) + @ApiParam( + name = "applicationReleaseWrapper", + value = "Application release wrapper which is going to update.", + required = true) + @Multipart( + value = "applicationReleaseWrapper", + type = "application/json") + ApplicationReleaseWrapper applicationReleaseWrapper, + @ApiParam( + name = "binaryFile", + value = "Application installer file.", + required = true) + @Multipart(value = "binaryFile") Attachment binaryFile, + @ApiParam( + name = "icon", + value = "Icon file of the application release.", + required = true) + @Multipart(value = "icon") Attachment iconFile, + @ApiParam( + name = "banner", + value = "banner file of the application release.", + required = true) + @Multipart(value = "banner") Attachment bannerFile, + @ApiParam( + name = "screenshot1", + value = "First screenshot of the uploading application", + required = true) @Multipart(value = "screenshot1") Attachment screenshot1, - @ApiParam(name = "screenshot2", value = "Screen Shots of the uploading application") + @ApiParam( + name = "screenshot2", + value = "Second screenshot 2 of the uploading application") @Multipart(value = "screenshot2") Attachment screenshot2, - @ApiParam(name = "screenshot3", value = "Screen Shots of the uploading application") + @ApiParam( + name = "screenshot3", + value = "Third screenshot of the uploading application") @Multipart(value = "screenshot3") Attachment screenshot3); @GET 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/ApplicationManagementPublisherAPIImpl.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/ApplicationManagementPublisherAPIImpl.java index be37e9e3b2c..a72767a6ed6 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/ApplicationManagementPublisherAPIImpl.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/ApplicationManagementPublisherAPIImpl.java @@ -288,6 +288,7 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem try { ApplicationManager applicationManager = APIUtil.getApplicationManager(); + applicationManager.validateBinaryArtifact(binaryFile, appType); if (!ApplicationType.ENTERPRISE.toString().equals(appType)) { String msg = "If ApplicationDTO type is " + appType + ", therefore you don't have application release artifact to update for application release UUID: " @@ -295,7 +296,7 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem log.error(msg); return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); } - applicationManager.validateBinaryArtifact(binaryFile, appType); + applicationManager.updateApplicationArtifact(deviceType, appType, applicationReleaseUuid, constructApplicationArtifact(binaryFile, null, null, null)); return Response.status(Response.Status.OK) @@ -341,18 +342,14 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem } } - /* - //todo ---------------------- - */ - @Override @PUT - @Path("/{deviceType}/{appId}/{uuid}") + @Path("/app-release/{deviceType}/{appType}/{uuid}") public Response updateApplicationRelease( @PathParam("deviceType") String deviceType, - @PathParam("appId") int applicationId, + @PathParam("appType") String appType, @PathParam("uuid") String applicationUUID, - @Multipart("applicationRelease") ApplicationReleaseDTO applicationRelease, + @Multipart("applicationRelease") ApplicationReleaseWrapper applicationReleaseWrapper, @Multipart("binaryFile") Attachment binaryFile, @Multipart("icon") Attachment iconFile, @Multipart("banner") Attachment bannerFile, @@ -360,50 +357,35 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem @Multipart("screenshot2") Attachment screenshot2, @Multipart("screenshot3") Attachment screenshot3) { ApplicationManager applicationManager = APIUtil.getApplicationManager(); - InputStream iconFileStream; - InputStream bannerFileStream; - InputStream binaryFileStram; - List attachments = new ArrayList<>(); - List attachmentList = new ArrayList<>(); + List screenshots = new ArrayList<>(); if (screenshot1 != null){ - attachmentList.add(screenshot1); + screenshots.add(screenshot1); } if (screenshot2 != null) { - attachmentList.add(screenshot2); + screenshots.add(screenshot2); } if (screenshot3 != null) { - attachmentList.add(screenshot3); + screenshots.add(screenshot3); } - if (iconFile == null || bannerFile == null || binaryFile == null || attachmentList.isEmpty()){ - String msg = "Invalid data is received for application release updating. application id: " + applicationId - + " and application release UUID: " + applicationUUID; - return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); - } - try { - binaryFileStram = binaryFile.getDataHandler().getInputStream(); - iconFileStream = iconFile.getDataHandler().getInputStream(); - bannerFileStream = bannerFile.getDataHandler().getInputStream(); - for (Attachment screenshot : attachmentList) { - attachments.add(screenshot.getDataHandler().getInputStream()); - } - boolean status = applicationManager - .updateRelease(applicationId, applicationUUID, deviceType, applicationRelease, binaryFileStram, - iconFileStream, bannerFileStream, attachments); - if (!status){ - log.error("ApplicationDTO release updating is failed. Please contact the administrator. ApplicationDTO id: " - + applicationId + ", ApplicationDTO release UUID: " + applicationUUID + ", Supported device type: " - + deviceType); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(applicationRelease).build(); + applicationManager.validateBinaryArtifact(binaryFile, appType); + applicationManager.validateImageArtifacts(iconFile, bannerFile, screenshots); + if (!applicationManager.updateRelease(deviceType, appType, applicationUUID, applicationReleaseWrapper, + constructApplicationArtifact(binaryFile, iconFile, bannerFile, screenshots))) { + log.error("Application release updating is failed. Please contact the administrator. " + + "ApplicationDTO release UUID: " + applicationUUID + ", Supported device type: " + deviceType); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(applicationReleaseWrapper).build(); } - return Response.status(Response.Status.OK).entity("ApplicationDTO release is successfully updated.").build(); - } catch(BadRequestException e){ - String msg = "Invalid request to update application release for application release UUID " + applicationUUID; + + return Response.status(Response.Status.OK).entity("Application release is successfully updated.").build(); + } catch (BadRequestException e) { + String msg = + "Invalid request to update application release for application release UUID " + applicationUUID; log.error(msg, e); return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); } catch (NotFoundException e) { - String msg = "Couldn't found application or application release for application id: " + applicationId - + " and application release UUID " + applicationUUID; + String msg = + "Couldn't found application or application release for application release UUID " + applicationUUID; log.error(msg, e); return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); } catch (ForbiddenException e) { @@ -411,18 +393,20 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem + applicationUUID; log.error(msg, e); return Response.status(Response.Status.FORBIDDEN).entity(msg).build(); - } - catch (ApplicationManagementException e) { + } catch (ApplicationManagementException e) { String msg = "Error while updating the application release of the application with UUID " + applicationUUID; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } catch (IOException e) { - String msg = "Error while updating the release artifacts of the application with UUID " + applicationUUID; - log.error(msg, e); + } catch (RequestValidatingException e) { + String msg = "Error occurred while updating the application release in the file system"; + log.error(msg); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } } + /* +//todo ---------------------- +*/ @DELETE @Path("/{appid}") public Response deleteApplication( From 04c03c9f46ea9ba6e4937c3592f92a2db6735e66 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Tue, 30 Apr 2019 22:31:21 +0530 Subject: [PATCH 02/11] Fix application release adding API --- .../common/services/ApplicationManager.java | 11 +- .../mgt/core/impl/ApplicationManagerImpl.java | 143 +++++++++++------- 2 files changed, 89 insertions(+), 65 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 ae452e5f558..1a9cc58b6c3 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 @@ -172,14 +172,11 @@ public interface ApplicationManager { /*** * - * @param applicationId ID of the application - * @param releaseUuid UUID of the application release - * @param deviceType Supported device type of the application + * @param deviceType Device type which is supported for the Application. + * @param applicationType Application Type + * @param releaseUuid UUID of the application release. * @param applicationReleaseWrapper {@link ApplicationReleaseDTO} - * @param binaryFileStram {@link InputStream} of the binary file - * @param iconFileStream {@link InputStream} of the icon - * @param bannerFileStream {@link InputStream} of the banner - * @param attachments {@link List} of {@link InputStream} of attachments + * @param applicationArtifact {@link ApplicationArtifact} * @return If the application release is updated correctly True returns, otherwise retuen False */ boolean updateRelease(String deviceType, String applicationType, String releaseUuid, 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 d04b68ac58e..4a161c6a4e6 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 @@ -147,7 +147,7 @@ public class ApplicationManagerImpl implements ApplicationManager { applicationDTO.getApplicationReleaseDTOs().clear(); ApplicationReleaseDTO applicationReleaseDTO = addApplicationReleaseArtifacts(applicationDTO.getType(), - applicationWrapper.getDeviceType(), initialApplicationReleaseDTO, applicationArtifact); + applicationWrapper.getDeviceType(), initialApplicationReleaseDTO, applicationArtifact, false); applicationDTO.getApplicationReleaseDTOs().add(addImageArtifacts(applicationReleaseDTO, applicationArtifact)); } catch (UnexpectedServerErrorException e) { String msg = "Error occurred when getting Device Type data."; @@ -243,7 +243,6 @@ public class ApplicationManagerImpl implements ApplicationManager { categoryIds.add(category.get().getId()); this.applicationDAO.addCategoryMapping(categoryIds,appId,tenantId); - //adding application tags List tags = applicationWrapper.getTags(); if (!tags.isEmpty()) { @@ -334,7 +333,7 @@ public class ApplicationManagerImpl implements ApplicationManager { } private ApplicationReleaseDTO addApplicationReleaseArtifacts(String applicationType, String deviceType, - ApplicationReleaseDTO applicationReleaseDTO, ApplicationArtifact applicationArtifact) + ApplicationReleaseDTO applicationReleaseDTO, ApplicationArtifact applicationArtifact, boolean isNewRelease) throws ResourceManagementException, ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); @@ -355,7 +354,7 @@ public class ApplicationManagerImpl implements ApplicationManager { String packagename = applicationInstaller.getPackageName(); ConnectionManagerUtil.getDBConnection(); - if (applicationReleaseDAO.isActiveReleaseExisitForPackageName(packagename, tenantId, + if (!isNewRelease && applicationReleaseDAO.isActiveReleaseExisitForPackageName(packagename, tenantId, lifecycleStateManager.getEndState())) { String msg = "Application release is already exist for the package name: " + packagename + ". Either you can delete all application releases for package " + packagename + " or " @@ -378,7 +377,7 @@ public class ApplicationManagerImpl implements ApplicationManager { .verifyReleaseExistenceByHash(md5OfApp, tenantId)) { throw new BadRequestException( "Application release exists for the uploaded binary file. Application Type: " - + applicationType + " Device Tyep: " + deviceType); + + applicationType + " Device Type: " + deviceType); } applicationReleaseDTO.setAppHashValue(md5OfApp); @@ -465,10 +464,13 @@ public class ApplicationManagerImpl implements ApplicationManager { } applicationReleaseDTO.setAppHashValue(md5OfApp); + String deletingAppHashValue = applicationReleaseDTO.getAppHashValue(); try (ByteArrayInputStream binaryDuplicate = new ByteArrayInputStream(content)) { applicationReleaseDTO = applicationStorageManager .uploadReleaseArtifact(applicationReleaseDTO,applicationType, deviceType, binaryDuplicate); + applicationStorageManager.copyImageArtifactsAndDeleteInstaller(deletingAppHashValue, + applicationReleaseDTO); } } } @@ -534,8 +536,13 @@ public class ApplicationManagerImpl implements ApplicationManager { ApplicationArtifact applicationArtifact) throws ResourceManagementException{ ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); + applicationStorageManager.deleteImageArtifacts(applicationReleaseDTO); + applicationReleaseDTO.setIconName(applicationArtifact.getIconName()); applicationReleaseDTO.setBannerName(applicationArtifact.getBannerName()); + applicationReleaseDTO.setScreenshotName1(null); + applicationReleaseDTO.setScreenshotName2(null); + applicationReleaseDTO.setScreenshotName3(null); Map screenshots = applicationArtifact.getScreenshots(); List screenshotNames = new ArrayList<>(screenshots.keySet()); @@ -552,14 +559,6 @@ public class ApplicationManagerImpl implements ApplicationManager { counter++; } - int numOfScreenshots = screenshotNames.size(); - if (numOfScreenshots == 1) { - applicationReleaseDTO.setScreenshotName2(null); - applicationReleaseDTO.setScreenshotName3(null); - } else if (numOfScreenshots == 2) { - applicationReleaseDTO.setScreenshotName3(null); - } - // Upload images applicationReleaseDTO = applicationStorageManager .uploadImageArtifacts(applicationReleaseDTO, applicationArtifact.getIconStream(), @@ -687,36 +686,25 @@ public class ApplicationManagerImpl implements ApplicationManager { } @Override - public ApplicationRelease createRelease(int applicationId, - ApplicationReleaseWrapper applicationReleaseWrapper, ApplicationArtifact applicationArtifact) - throws ApplicationManagementException { + public ApplicationRelease createRelease(int applicationId, ApplicationReleaseWrapper applicationReleaseWrapper, + ApplicationArtifact applicationArtifact) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); ApplicationRelease applicationRelease; if (log.isDebugEnabled()) { log.debug("ApplicationDTO release request is received for the application id: " + applicationId); } - try { + ApplicationDTO applicationDTO = getApplication(applicationId); + ApplicationReleaseDTO applicationReleaseDTO = uploadReleaseArtifacts(applicationReleaseWrapper, + applicationDTO, applicationArtifact); ConnectionManagerUtil.beginDBTransaction(); - ApplicationDTO applicationDTO = this.applicationDAO.getApplicationById(applicationId, tenantId); - if (applicationDTO == null) { - String msg = "Couldn't find application for the application Id: " + applicationId; - log.error(msg); - throw new NotFoundException(msg); - } - DeviceType deviceType = getDeviceTypeData(applicationDTO.getDeviceTypeId()); - ApplicationReleaseDTO applicationReleaseDTO = addApplicationReleaseArtifacts(applicationDTO.getType(), - deviceType.getName(), releaseWrapperToReleaseDTO(applicationReleaseWrapper), - applicationArtifact); - applicationReleaseDTO = addImageArtifacts(applicationReleaseDTO, applicationArtifact); - String initialstate = lifecycleStateManager.getInitialState(); applicationReleaseDTO.setCurrentState(initialstate); LifecycleStateDTO lifecycleState = getLifecycleStateInstance(initialstate, initialstate); - this.lifecycleStateDAO - .addLifecycleState(lifecycleState, applicationId, applicationReleaseDTO.getUuid(), tenantId); applicationReleaseDTO = this.applicationReleaseDAO .createRelease(applicationReleaseDTO, applicationDTO.getId(), tenantId); + this.lifecycleStateDAO + .addLifecycleState(lifecycleState, applicationId, applicationReleaseDTO.getUuid(), tenantId); applicationRelease = releaseDtoToRelease(applicationReleaseDTO); ConnectionManagerUtil.commitDBTransaction(); return applicationRelease; @@ -738,13 +726,48 @@ public class ApplicationManagerImpl implements ApplicationManager { ConnectionManagerUtil.rollbackDBTransaction(); throw new ApplicationManagementException( "Error occurred while adding new application release for application " + applicationId, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + private ApplicationDTO getApplication(int applicationId) throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + try { + ConnectionManagerUtil.openDBConnection(); + ApplicationDTO applicationDTO = this.applicationDAO.getApplicationById(applicationId, tenantId); + if (applicationDTO == null) { + String msg = "Couldn't find application for the application Id: " + applicationId; + log.error(msg); + throw new NotFoundException(msg); + } + return applicationDTO; + } catch (DBConnectionException e) { + throw new ApplicationManagementException( + "Error occurred while obtaining the database connection for getting application for the application ID: " + + applicationId, e); + } catch (ApplicationManagementDAOException e) { + throw new ApplicationManagementException( + "Error occurred while getting application data for application ID: " + applicationId, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + private ApplicationReleaseDTO uploadReleaseArtifacts(ApplicationReleaseWrapper applicationReleaseWrapper, + ApplicationDTO applicationDTO, ApplicationArtifact applicationArtifact) + throws ApplicationManagementException { + try { + DeviceType deviceType = getDeviceTypeData(applicationDTO.getDeviceTypeId()); + ApplicationReleaseDTO applicationReleaseDTO = addApplicationReleaseArtifacts(applicationDTO.getType(), + deviceType.getName(), releaseWrapperToReleaseDTO(applicationReleaseWrapper), applicationArtifact, + true); + return addImageArtifacts(applicationReleaseDTO, applicationArtifact); } catch (ResourceManagementException e) { - ConnectionManagerUtil.rollbackDBTransaction(); String msg = - "Error occurred while uploading application release artifacts. Application ID: " + applicationId; + "Error occurred while uploading application release artifacts. Application ID: " + applicationDTO + .getId(); throw new ApplicationManagementException(msg, e); - } finally { - ConnectionManagerUtil.closeDBConnection(); } } @@ -755,14 +778,9 @@ public class ApplicationManagerImpl implements ApplicationManager { ApplicationDTO applicationDTO; boolean isVisibleApp = false; try { - ConnectionManagerUtil.openDBConnection(); - applicationDTO = this.applicationDAO.getApplicationById(appId, tenantId); - if (applicationDTO == null) { - String msg = "Couldn't find an application for application Id: " + appId; - log.error(msg); - throw new NotFoundException(msg); - } + applicationDTO = getApplication(appId); + ConnectionManagerUtil.openDBConnection(); List filteredApplicationReleaseDTOs = new ArrayList<>(); for (ApplicationReleaseDTO applicationReleaseDTO : applicationDTO.getApplicationReleaseDTOs()) { if (!applicationReleaseDTO.getCurrentState().equals(lifecycleStateManager.getEndState()) && ( @@ -1270,12 +1288,9 @@ public class ApplicationManagerImpl implements ApplicationManager { } @Override - //todo with this implementation user has to provide all image artifacts in order to update one image artifact as - // well. Therefore enhance this later. public void updateApplicationImageArtifact(String uuid, ApplicationArtifact applicationArtifact) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); ApplicationReleaseDTO applicationReleaseDTO; try { @@ -1295,8 +1310,6 @@ public class ApplicationManagerImpl implements ApplicationManager { + " and it is not an release updatable state. Hence please move the application release" + " into updatable state and retry the operation."); } - applicationStorageManager.deleteImageArtifacts(applicationReleaseDTO); - applicationReleaseDTO = this.applicationReleaseDAO .updateRelease(addImageArtifacts(applicationReleaseDTO, applicationArtifact), tenantId); if (applicationReleaseDTO == null) { @@ -1333,7 +1346,6 @@ public class ApplicationManagerImpl implements ApplicationManager { public void updateApplicationArtifact(String deviceType, String appType, String uuid, ApplicationArtifact applicationArtifact) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); boolean isValidDeviceType = false; List deviceTypes; try { @@ -1366,10 +1378,8 @@ public class ApplicationManagerImpl implements ApplicationManager { throw new NotFoundException(msg); } - String deletingAppHashValue = applicationReleaseDTO.getAppHashValue(); - applicationReleaseDTO = addApplicationReleaseArtifacts(appType, deviceType, applicationReleaseDTO, + applicationReleaseDTO = updateApplicationReleaseArtifacts(appType, deviceType, applicationReleaseDTO, applicationArtifact); - applicationStorageManager.copyImageArtifactsAndDeleteInstaller(deletingAppHashValue, applicationReleaseDTO); applicationReleaseDTO = this.applicationReleaseDAO.updateRelease(applicationReleaseDTO, tenantId); if (applicationReleaseDTO == null) { ConnectionManagerUtil.rollbackDBTransaction(); @@ -1831,9 +1841,7 @@ public class ApplicationManagerImpl implements ApplicationManager { } DeviceType deviceTypeObj = getDeviceTypeData(deviceType); - String releaseType = applicationReleaseWrapper.getReleaseType(); Double price = applicationReleaseWrapper.getPrice(); - String metaData = applicationReleaseWrapper.getMetaData(); String applicationSubType = this.applicationDAO.getApplicationSubTypeByUUID(releaseUuid, tenantId); if (applicationSubType == null) { @@ -1850,17 +1858,33 @@ public class ApplicationManagerImpl implements ApplicationManager { + releaseUuid + " and supported device type is " + deviceType); } applicationReleaseDTO.setPrice(price); - if (!StringUtils.isEmpty(releaseType)) { - applicationReleaseDTO.setReleaseType(releaseType); + applicationReleaseDTO.setIsSharedWithAllTenants(applicationReleaseDTO.getIsSharedWithAllTenants()); + + String supportedOSVersions = applicationReleaseWrapper.getSupportedOsVersions(); + if (!StringUtils.isEmpty(supportedOSVersions)) { + //todo check OS versions are supported or not + applicationReleaseDTO.setSupportedOsVersions(supportedOSVersions); + } + if (!StringUtils.isEmpty(applicationReleaseWrapper.getDescription())) { + applicationReleaseDTO.setDescription(applicationReleaseWrapper.getDescription()); + } + if (!StringUtils.isEmpty(applicationReleaseWrapper.getReleaseType())) { + applicationReleaseDTO.setReleaseType(applicationReleaseWrapper.getReleaseType()); } - if (!StringUtils.isEmpty(metaData)) { - applicationReleaseDTO.setMetaData(metaData); + if (!StringUtils.isEmpty(applicationReleaseWrapper.getMetaData())) { + applicationReleaseDTO.setMetaData(applicationReleaseWrapper.getMetaData()); } applicationReleaseDTO = updateApplicationReleaseArtifacts(applicationType, deviceTypeObj.getName(), applicationReleaseDTO, applicationArtifact); applicationReleaseDTO = updateImageArtifacts(applicationReleaseDTO, applicationArtifact); - return applicationReleaseDAO.updateRelease(applicationReleaseDTO, tenantId) != null; + + boolean updateStatus = applicationReleaseDAO.updateRelease(applicationReleaseDTO, tenantId) != null; + if (!updateStatus) { + ConnectionManagerUtil.rollbackDBTransaction(); + } + ConnectionManagerUtil.commitDBTransaction(); + return updateStatus; } catch (ApplicationManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); String msg = "Error occured when updating Application release. ApplicationDTO ID ApplicationDTO Release " @@ -1872,6 +1896,8 @@ public class ApplicationManagerImpl implements ApplicationManager { + "release UUID:" + releaseUuid; log.error(msg); throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); } } @@ -2050,6 +2076,7 @@ public class ApplicationManagerImpl implements ApplicationManager { String basePath = artifactDownloadEndpoint + Constants.FORWARD_SLASH + applicationReleaseDTO.getUuid(); ApplicationRelease applicationRelease = new ApplicationRelease(); applicationRelease.setDescription(applicationReleaseDTO.getDescription()); + applicationRelease.setVersion(applicationReleaseDTO.getVersion()); applicationRelease.setUuid(applicationReleaseDTO.getUuid()); applicationRelease.setReleaseType(applicationReleaseDTO.getReleaseType()); applicationRelease.setPrice(applicationReleaseDTO.getPrice()); From d79c11c7627164678f4513f030196a2917431644 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Wed, 1 May 2019 19:48:27 +0530 Subject: [PATCH 03/11] Add admin API interface for app publisher --- .../common/services/ApplicationManager.java | 2 +- .../mgt/core/impl/ApplicationManagerImpl.java | 90 ++++-------- .../ApplicationManagementPublisherAPI.java | 17 ++- ...pplicationManagementPublisherAdminAPI.java | 136 ++++++++++++++++++ ...ApplicationManagementPublisherAPIImpl.java | 53 +------ ...cationManagementPublisherAdminAPIImpl.java | 110 ++++++++++++++ 6 files changed, 293 insertions(+), 115 deletions(-) create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/admin/ApplicationManagementPublisherAdminAPI.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/admin/ApplicationManagementPublisherAdminAPIImpl.java 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 1a9cc58b6c3..12f64c65866 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,7 +68,7 @@ public interface ApplicationManager { * @param applicationId ID for tha application * @throws ApplicationManagementException ApplicationDTO Management Exception */ - List deleteApplication(int applicationId) throws ApplicationManagementException; + void deleteApplication(int applicationId) throws ApplicationManagementException; /** * Delete an application identified by the unique 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 4a161c6a4e6..68f64b4a96e 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 @@ -476,8 +476,6 @@ public class ApplicationManagerImpl implements ApplicationManager { } } - - } catch (IOException e) { String msg = "Error occurred when getting byte array of binary file. Installer name: " + applicationArtifact @@ -1045,80 +1043,52 @@ public class ApplicationManagerImpl implements ApplicationManager { // return applicationReleases; // } - @Override public List deleteApplication(int applicationId) throws ApplicationManagementException { - String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + @Override + public void deleteApplication(int applicationId) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); List storedLocations = new ArrayList<>(); - ApplicationDTO application; - + ApplicationDTO applicationDTO; + if (log.isDebugEnabled()) { + log.debug("Request is received to delete applications which are related with the application id " + + applicationId); + } try { ConnectionManagerUtil.beginDBTransaction(); - application = this.applicationDAO.getApplicationById(applicationId, tenantId); - - if (application == null) { - throw new NotFoundException("Couldn't found an application for ApplicationDTO ID: " + applicationId); - } + applicationDTO = this.applicationDAO.getApplicationById(applicationId, tenantId); - if (!isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION) && !application - .getUnrestrictedRoles().isEmpty() && hasUserRole(application.getUnrestrictedRoles(), userName)) { - throw new ForbiddenException( - "You don't have permission to delete this application. In order to delete an application you " - + "need to have required permission. ApplicationDTO ID: " + applicationId); - } - List applicationReleases = getReleases(application, null); - if (log.isDebugEnabled()) { - log.debug("Request is received to delete applications which are related with the application id " - + applicationId); + if (applicationDTO == null) { + throw new NotFoundException("Couldn't found an application for Application ID: " + applicationId); } - for (ApplicationReleaseDTO applicationRelease : applicationReleases) { - LifecycleStateDTO appLifecycleState = this.lifecycleStateDAO - .getLatestLifeCycleState(applicationId, applicationRelease.getUuid()); - LifecycleStateDTO newAppLifecycleState = getLifecycleStateInstance(AppLifecycleState.REMOVED.toString(), - appLifecycleState.getCurrentState()); - if (lifecycleStateManager.isValidStateChange(newAppLifecycleState.getPreviousState(), - newAppLifecycleState.getCurrentState(), userName, tenantId)) { - this.lifecycleStateDAO - .addLifecycleState(newAppLifecycleState, applicationId, applicationRelease.getUuid(), - tenantId); - } else { - String currentState = appLifecycleState.getCurrentState(); - List lifecycleFlow = searchLifecycleStateFlow(currentState, - AppLifecycleState.REMOVED.toString()); - for (String nextState : lifecycleFlow) { - LifecycleStateDTO lifecycleState = getLifecycleStateInstance(nextState, currentState); - if (lifecycleStateManager.isValidStateChange(currentState, nextState, userName, tenantId)) { - this.lifecycleStateDAO - .addLifecycleState(lifecycleState, applicationId, applicationRelease.getUuid(), - tenantId); - } else { - ConnectionManagerUtil.rollbackDBTransaction(); - throw new ApplicationManagementException( - "Can't delete application release which has the UUID:" + applicationRelease - .getUuid() - + " and its belongs to the application which has application ID:" - + applicationId + " You have to move the lifecycle state from " - + currentState + " to acceptable state"); - } - currentState = nextState; - } + List applicationReleaseDTOs = applicationDTO.getApplicationReleaseDTOs(); + List activeApplicationReleaseDTOs = new ArrayList<>(); + for (ApplicationReleaseDTO applicationReleaseDTO : applicationReleaseDTOs) { + if (!applicationReleaseDTO.getCurrentState().equals(lifecycleStateManager.getEndState())){ + activeApplicationReleaseDTOs.add(applicationReleaseDTO); } - storedLocations.add(applicationRelease.getAppHashValue()); + storedLocations.add(applicationReleaseDTO.getAppHashValue()); + } + if (!activeApplicationReleaseDTOs.isEmpty()) { + String msg = "There are application releases which are not in the state " + lifecycleStateManager + .getEndState() + ". Hence you are not allowed to delete the application"; + log.error(msg); + throw new ForbiddenException(msg); } 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"; + applicationStorageManager.deleteAllApplicationReleaseArtifacts(storedLocations); + } catch (ApplicationManagementDAOException e) { + String msg = "Error occurred when getting application data for application id: " + applicationId; log.error(msg); throw new ApplicationManagementException(msg, e); - } catch (LifeCycleManagementDAOException e) { - ConnectionManagerUtil.rollbackDBTransaction(); - String msg = "Error occured while changing the application lifecycle state into REMOVED state."; + } catch (ApplicationStorageManagementException e) { + String msg = "Error occurred when deleting application artifacts in the file system. Application id: " + + applicationId; log.error(msg); - throw new ApplicationManagementException(msg, e); + throw new ApplicationManagementException(msg); } finally { ConnectionManagerUtil.closeDBConnection(); } - return storedLocations; } private List searchLifecycleStateFlow(String start, String finish) throws 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/ApplicationManagementPublisherAPI.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/ApplicationManagementPublisherAPI.java index 30dbfb2fb40..8b7b1093cba 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/ApplicationManagementPublisherAPI.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/ApplicationManagementPublisherAPI.java @@ -80,7 +80,7 @@ import javax.ws.rs.core.Response; name = "Get ApplicationDTO Details", description = "Get application details", key = "perm:app:publisher:view", - permissions = {"/app-mgt/publisher/application/update"} + permissions = {"/app-mgt/publisher/application/view"} ), @Scope( name = "Update an ApplicationDTO", @@ -91,8 +91,7 @@ import javax.ws.rs.core.Response; } ) @Path("/applications") -@Api(value = "ApplicationDTO Management", description = "This API carries all application management related operations " + - "such as get all the applications, add application, etc.") +@Api(value = "ApplicationDTO Management") @Produces(MediaType.APPLICATION_JSON) public interface ApplicationManagementPublisherAPI { @@ -377,7 +376,7 @@ public interface ApplicationManagementPublisherAPI { @DELETE @Consumes("application/json") - @Path("/{appid}") + @Path("/{appId}") @ApiOperation( consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON, @@ -400,14 +399,20 @@ public interface ApplicationManagementPublisherAPI { @ApiResponse( code = 500, message = "Internal Server Error. \n Error occurred while deleting the application.", - response = ErrorResponse.class) + response = ErrorResponse.class), + @ApiResponse( + code = 403, + message = "Don't have permission to delete the application"), + @ApiResponse( + code = 404, + message = "Application not found"), }) Response deleteApplication( @ApiParam( name = "UUID", value = "Unique identifier of the ApplicationDTO", required = true) - @PathParam("appid") int applicationId + @PathParam("appId") int applicationId ); @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/admin/ApplicationManagementPublisherAdminAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/admin/ApplicationManagementPublisherAdminAPI.java new file mode 100644 index 00000000000..b8b973d5820 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/admin/ApplicationManagementPublisherAdminAPI.java @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2017, 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.publisher.api.services.admin; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; +import io.swagger.annotations.Extension; +import io.swagger.annotations.ExtensionProperty; +import io.swagger.annotations.Info; +import io.swagger.annotations.SwaggerDefinition; +import io.swagger.annotations.Tag; +import org.apache.cxf.jaxrs.ext.multipart.Attachment; +import org.apache.cxf.jaxrs.ext.multipart.Multipart; +import org.wso2.carbon.apimgt.annotations.api.Scope; +import org.wso2.carbon.apimgt.annotations.api.Scopes; +import org.wso2.carbon.device.application.mgt.common.ApplicationList; +import org.wso2.carbon.device.application.mgt.common.ErrorResponse; +import org.wso2.carbon.device.application.mgt.common.Filter; +import org.wso2.carbon.device.application.mgt.common.dto.ApplicationDTO; +import org.wso2.carbon.device.application.mgt.common.dto.ApplicationReleaseDTO; +import org.wso2.carbon.device.application.mgt.common.response.ApplicationRelease; +import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationReleaseWrapper; +import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationUpdateWrapper; +import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationWrapper; + +import javax.validation.Valid; +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.util.List; + +/** + * APIs to handle application management related tasks. + */ +@SwaggerDefinition( + info = @Info( + version = "1.0.0", + title = "ApplicationDTO Management Publisher Service", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = "name", value = "ApplicationManagementPublisherAdminService"), + @ExtensionProperty(name = "context", value = "/api/application-mgt-publisher/v1.0/admin/applications"), + }) + } + ), + tags = { + @Tag(name = "application_management, device_management", description = "App publisher related Admin APIs") + } +) +@Scopes( + scopes = { + @Scope( + name = "Delete Application Release", + description = "Delete Application Release", + key = "perm:admin:app:publisher:update", + permissions = {"/app-mgt/publisher/admin/application/update"} + ) + } +) +@Path("/admin/applications") +@Api(value = "ApplicationDTO Management") +@Produces(MediaType.APPLICATION_JSON) +public interface ApplicationManagementPublisherAdminAPI { + + String SCOPE = "scope"; + + @DELETE + @Path("/{appid}/{uuid}") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "get all applications", + notes = "This will get all applications", + tags = "ApplicationDTO Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:app:publisher:view") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully delete application releaset.", + response = ApplicationList.class), + @ApiResponse( + code = 404, + message = "Not Found. There doesn't have an application release with UUID" + + "query."), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while deleting application release.", + response = ErrorResponse.class) + }) Response deleteApplicationRelease( + @ApiParam( + name = "appId", + value = "application Id", + required = true) + @PathParam("appid") int applicationId, + @ApiParam( + name = "uuid", + value = "application release UUID", + required = true) + @PathParam("uuid") String releaseUuid); +} 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/ApplicationManagementPublisherAPIImpl.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/ApplicationManagementPublisherAPIImpl.java index a72767a6ed6..4b5ab8369ba 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/ApplicationManagementPublisherAPIImpl.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/ApplicationManagementPublisherAPIImpl.java @@ -23,7 +23,6 @@ import org.apache.commons.logging.LogFactory; import org.apache.cxf.jaxrs.ext.multipart.Attachment; import org.apache.cxf.jaxrs.ext.multipart.Multipart; import org.wso2.carbon.device.application.mgt.common.*; -import org.wso2.carbon.device.application.mgt.common.dto.ApplicationReleaseDTO; import org.wso2.carbon.device.application.mgt.common.dto.LifecycleStateDTO; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException; import org.wso2.carbon.device.application.mgt.common.exception.RequestValidatingException; @@ -376,7 +375,6 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem + "ApplicationDTO release UUID: " + applicationUUID + ", Supported device type: " + deviceType); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(applicationReleaseWrapper).build(); } - return Response.status(Response.Status.OK).entity("Application release is successfully updated.").build(); } catch (BadRequestException e) { String msg = @@ -408,16 +406,13 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem //todo ---------------------- */ @DELETE - @Path("/{appid}") - public Response deleteApplication( - @PathParam("appid") int applicationId) { + @Path("/{appId}") + public Response deleteApplication(@PathParam("appId") int applicationId) { ApplicationManager applicationManager = APIUtil.getApplicationManager(); - ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); try { - List storedLocations = applicationManager.deleteApplication(applicationId); - applicationStorageManager.deleteAllApplicationReleaseArtifacts(storedLocations); - String responseMsg = "Successfully deleted the application and application releases: " + applicationId; - return Response.status(Response.Status.OK).entity(responseMsg).build(); + applicationManager.deleteApplication(applicationId); + return Response.status(Response.Status.OK) + .entity("Successfully deleted the application for application ID: " + applicationId).build(); } catch (NotFoundException e) { String msg = "Couldn't found application for application id: " + applicationId + " to delete the application"; @@ -431,44 +426,6 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem String msg = "Error occurred while deleting the application: " + applicationId; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } catch (ApplicationStorageManagementException e) { - String msg = "Error occurred while deleting the application storage: " + applicationId; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } - } - - @DELETE - @Path("/{appid}/{uuid}") - public Response deleteApplicationRelease( - @PathParam("appid") int applicationId, - @PathParam("uuid") String releaseUuid) { - ApplicationManager applicationManager = APIUtil.getApplicationManager(); - ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); - try { - String storedLocation = applicationManager.deleteApplicationRelease(applicationId, releaseUuid); - applicationStorageManager.deleteApplicationReleaseArtifacts(storedLocation); - String responseMsg = "Successfully deleted the application release of: " + applicationId + ""; - return Response.status(Response.Status.OK).entity(responseMsg).build(); - } catch (NotFoundException e) { - String msg = "Couldn't found application release which is having application id: " + applicationId - + " and application release UUID:" + releaseUuid; - log.error(msg, e); - return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); - } catch (ForbiddenException e) { - String msg = - "You don't have require permission to delete the application release which has UUID " + releaseUuid - + " and application ID " + applicationId; - log.error(msg, e); - return Response.status(Response.Status.FORBIDDEN).entity(msg).build(); - }catch (ApplicationManagementException e) { - String msg = "Error occurred while deleting the application: " + applicationId; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } catch (ApplicationStorageManagementException e) { - String msg = "Error occurred while deleting the application storage: " + applicationId; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/admin/ApplicationManagementPublisherAdminAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/admin/ApplicationManagementPublisherAdminAPIImpl.java new file mode 100644 index 00000000000..f360f9b01b4 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/admin/ApplicationManagementPublisherAdminAPIImpl.java @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2017, 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.publisher.api.services.impl.admin; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.cxf.jaxrs.ext.multipart.Attachment; +import org.apache.cxf.jaxrs.ext.multipart.Multipart; +import org.wso2.carbon.device.application.mgt.common.ApplicationArtifact; +import org.wso2.carbon.device.application.mgt.common.ApplicationList; +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.dto.LifecycleStateDTO; +import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.RequestValidatingException; +import org.wso2.carbon.device.application.mgt.common.response.Application; +import org.wso2.carbon.device.application.mgt.common.response.ApplicationRelease; +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.wrapper.ApplicationReleaseWrapper; +import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationUpdateWrapper; +import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationWrapper; +import org.wso2.carbon.device.application.mgt.core.exception.BadRequestException; +import org.wso2.carbon.device.application.mgt.core.exception.ForbiddenException; +import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException; +import org.wso2.carbon.device.application.mgt.core.util.APIUtil; +import org.wso2.carbon.device.application.mgt.publisher.api.services.ApplicationManagementPublisherAPI; +import org.wso2.carbon.device.application.mgt.publisher.api.services.admin.ApplicationManagementPublisherAdminAPI; + +import javax.activation.DataHandler; +import javax.validation.Valid; +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Implementation of Application Management related APIs. + */ +@Produces({"application/json"}) +@Path("/applications") +public class ApplicationManagementPublisherAdminAPIImpl implements ApplicationManagementPublisherAdminAPI { + + private static Log log = LogFactory.getLog(ApplicationManagementPublisherAdminAPIImpl.class); + + @DELETE + @Path("/{appid}/{uuid}") + public Response deleteApplicationRelease( + @PathParam("appid") int applicationId, + @PathParam("uuid") String releaseUuid) { + ApplicationManager applicationManager = APIUtil.getApplicationManager(); + ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); + try { + String storedLocation = applicationManager.deleteApplicationRelease(applicationId, releaseUuid); + applicationStorageManager.deleteApplicationReleaseArtifacts(storedLocation); + String responseMsg = "Successfully deleted the application release of: " + applicationId + ""; + return Response.status(Response.Status.OK).entity(responseMsg).build(); + } catch (NotFoundException e) { + String msg = "Couldn't found application release which is having application id: " + applicationId + + " and application release UUID:" + releaseUuid; + log.error(msg, e); + return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); + } catch (ForbiddenException e) { + String msg = + "You don't have require permission to delete the application release which has UUID " + releaseUuid + + " and application ID " + applicationId; + log.error(msg, e); + return Response.status(Response.Status.FORBIDDEN).entity(msg).build(); + }catch (ApplicationManagementException e) { + String msg = "Error occurred while deleting the application: " + applicationId; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } catch (ApplicationStorageManagementException e) { + String msg = "Error occurred while deleting the application storage: " + applicationId; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } + +} From 5399ae1a3cc3d344fe4baf89eaf6b12d9712b8cc Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Wed, 1 May 2019 20:08:43 +0530 Subject: [PATCH 04/11] Add subscription management exception class --- .../ApplicationManagementDAOException.java | 26 +++++++-------- .../SubscriptionManagementDAOException.java | 33 +++++++++++++++++++ 2 files changed, 45 insertions(+), 14 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/SubscriptionManagementDAOException.java diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/ApplicationManagementDAOException.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/ApplicationManagementDAOException.java index 44c78804a6d..c9059f3ea95 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/ApplicationManagementDAOException.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/ApplicationManagementDAOException.java @@ -1,20 +1,18 @@ -/* - * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) 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 + * Entgra (Pvt) Ltd. 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. + * 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; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/SubscriptionManagementDAOException.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/SubscriptionManagementDAOException.java new file mode 100644 index 00000000000..8419ed643db --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/SubscriptionManagementDAOException.java @@ -0,0 +1,33 @@ +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. 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 thrown during the ApplicationDTO Management DAO operations. + */ +public class SubscriptionManagementDAOException extends ApplicationManagementException { + + public SubscriptionManagementDAOException(String message, Throwable throwable) { + super(message, throwable); + } + + public SubscriptionManagementDAOException(String message) { + super(message, new Exception()); + } +} From 6dafe59eab3e746165c4fce529fe58cdbed65e2e Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Thu, 2 May 2019 02:42:30 +0530 Subject: [PATCH 05/11] Improve application create functionality --- .../GenericApplicationDAOImpl.java | 2 +- .../mgt/core/impl/ApplicationManagerImpl.java | 228 +++++++++++------- .../ApplicationManagementPublisherAPI.java | 1 + ...ApplicationManagementPublisherAPIImpl.java | 25 +- 4 files changed, 153 insertions(+), 103 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java index 64dc0bd9847..5565eb43b84 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 @@ -197,7 +197,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic if (!StringUtils.isEmpty(filter.getAppReleaseState())) { sql += " AND AP_APP_RELEASE.CURRENT_STATE = ?"; } - if (deviceTypeId > 0) { + if (deviceTypeId != -1) { sql += " AND AP_APP.DEVICE_TYPE_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 68f64b4a96e..3007cc8607f 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 @@ -87,6 +87,7 @@ import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; @@ -135,79 +136,126 @@ public class ApplicationManagerImpl implements ApplicationManager { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - ApplicationDTO applicationDTO; - Application application; if (log.isDebugEnabled()) { log.debug("Application create request is received for the tenant : " + tenantId + " From" + " the user : " + userName); } - try { - applicationDTO = appWrapperToAppDTO(applicationWrapper); - ApplicationReleaseDTO initialApplicationReleaseDTO = applicationDTO.getApplicationReleaseDTOs().get(0); - applicationDTO.getApplicationReleaseDTOs().clear(); - ApplicationReleaseDTO applicationReleaseDTO = addApplicationReleaseArtifacts(applicationDTO.getType(), - applicationWrapper.getDeviceType(), initialApplicationReleaseDTO, applicationArtifact, false); - applicationDTO.getApplicationReleaseDTOs().add(addImageArtifacts(applicationReleaseDTO, applicationArtifact)); - } catch (UnexpectedServerErrorException e) { - String msg = "Error occurred when getting Device Type data."; - log.error(msg); - throw new ApplicationManagementException(msg, e); - } catch (ResourceManagementException e) { - String msg = "Error Occured when uploading artifacts of the application: " + applicationWrapper.getName(); - log.error(msg); - throw new ApplicationManagementException(msg, e); - } + ApplicationDTO applicationDTO; + List unrestrictedRoles; + Optional category; + List tags; + //validating and verifying application data try { - List applicationReleaseEntities = new ArrayList<>(); - ApplicationReleaseDTO applicationReleaseDTO; + ConnectionManagerUtil.openDBConnection(); + applicationDTO = appWrapperToAppDTO(applicationWrapper); + unrestrictedRoles = applicationWrapper.getUnrestrictedRoles(); + tags = applicationWrapper.getTags(); + + if (unrestrictedRoles != null && !unrestrictedRoles.isEmpty()) { + if (!isValidRestrictedRole(unrestrictedRoles)) { + String msg = "Unrestricted role list contain role/roles which are not in the user store."; + log.error(msg); + throw new ApplicationManagementException(msg); + } + if (!hasUserRole(unrestrictedRoles, userName)) { + String msg = "You are trying to restrict the visibility of the application for a role set, but " + + "in order to perform the action at least one role should be assigned to user: " + + userName; + log.error(msg); + throw new BadRequestException(msg); + } + } Filter filter = new Filter(); filter.setFullMatch(true); filter.setAppName(applicationDTO.getName().trim()); filter.setOffset(0); filter.setLimit(1); - - ConnectionManagerUtil.beginDBTransaction(); List applicationList = applicationDAO .getApplications(filter, applicationDTO.getDeviceTypeId(), tenantId); if (!applicationList.isEmpty()) { - String msg = - "Already an application registered with same name - " + applicationList.get(0) - .getName(); + String msg = "Already an application registered with same name - " + applicationList.get(0).getName() + + " for the device type " + applicationWrapper.getDeviceType(); log.error(msg); throw new RequestValidatingException(msg); } + List registeredCategories = this.applicationDAO.getAllCategories(tenantId); + String categoryName = applicationWrapper.getAppCategory(); + + if (registeredCategories.isEmpty()) { + ConnectionManagerUtil.rollbackDBTransaction(); + String msg = "Registered application category set is empty. Since it is mandatory to add application " + + "category when adding new application, registered application category list shouldn't be null."; + log.error(msg); + throw new ApplicationManagementException(msg); + } + category = registeredCategories.stream().filter(obj -> obj.getCategoryName().equals(categoryName)) + .findAny(); + if (!category.isPresent()) { + ConnectionManagerUtil.rollbackDBTransaction(); + String msg = "Request contains invalid category: " + categoryName; + log.error(msg); + throw new ApplicationManagementException(msg); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while getting database connection."; + log.error(msg); + throw new ApplicationManagementException(msg, e); + } catch (UnexpectedServerErrorException e) { + String msg = "Error occurred when getting Device Type data."; + log.error(msg); + throw new ApplicationManagementException(msg, e); + } catch (ApplicationManagementDAOException e) { + String msg = "Error occurred while getting data which is related to application. application name: " + + applicationWrapper.getName() + " and application type: " + applicationWrapper.getType(); + log.error(msg); + throw new ApplicationManagementException(msg, e); + } catch (UserStoreException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + String msg = "Error occurred when validating the unrestricted roles given for the application"; + log.error(msg); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + + //uploading application artifacts + try { + ApplicationReleaseDTO applicationReleaseDTO = applicationDTO.getApplicationReleaseDTOs().get(0); + applicationReleaseDTO = addApplicationReleaseArtifacts(applicationDTO.getType(), + applicationWrapper.getDeviceType(), applicationReleaseDTO, applicationArtifact, false); + applicationReleaseDTO = addImageArtifacts(applicationReleaseDTO, applicationArtifact); + applicationDTO.getApplicationReleaseDTOs().clear(); + applicationDTO.getApplicationReleaseDTOs().add(applicationReleaseDTO); + } catch (ResourceManagementException e) { + String msg = "Error Occured when uploading artifacts of the application: " + applicationWrapper.getName(); + log.error(msg); + throw new ApplicationManagementException(msg, e); + } + + //insert application data into databse + ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); + ApplicationReleaseDTO applicationReleaseDTO = applicationDTO.getApplicationReleaseDTOs().get(0); + try { + List applicationReleaseEntities = new ArrayList<>(); + + ConnectionManagerUtil.beginDBTransaction(); // Insert to application table int appId = this.applicationDAO.createApplication(applicationDTO, tenantId); if (appId == -1) { - log.error("ApplicationDTO creation is Failed"); + log.error("Application data storing is Failed."); ConnectionManagerUtil.rollbackDBTransaction(); + deleteApplicationArtifacts(Collections.singletonList(applicationReleaseDTO.getAppHashValue())); return null; } else { if (log.isDebugEnabled()) { log.debug("New ApplicationDTO entry added to AP_APP table. App Id:" + appId); } //adding application unrestricted roles - List unrestrictedRoles = applicationWrapper.getUnrestrictedRoles(); - if (!unrestrictedRoles.isEmpty()) { - if (!isValidRestrictedRole(unrestrictedRoles)) { - ConnectionManagerUtil.rollbackDBTransaction(); - String msg = "Unrestricted role list contain role/roles which are not in the user store."; - log.error(msg); - throw new ApplicationManagementException(msg); - } - - if (!hasUserRole(unrestrictedRoles, userName)){ - ConnectionManagerUtil.rollbackDBTransaction(); - String msg = - "You are trying to restrict the visibility of the application for a role set, but in order to perform the action at least one role should be assigned to user: " - + userName; - log.error(msg); - throw new BadRequestException(msg); - } + if (unrestrictedRoles != null && !unrestrictedRoles.isEmpty()) { this.visibilityDAO.addUnrestrictedRoles(unrestrictedRoles, appId, tenantId); if (log.isDebugEnabled()) { log.debug("New restricted roles to app ID mapping added to AP_UNRESTRICTED_ROLE table." @@ -215,25 +263,6 @@ public class ApplicationManagerImpl implements ApplicationManager { } } - List registeredCategories = this.applicationDAO.getAllCategories(tenantId); - String categoryName = applicationWrapper.getAppCategory(); - Optional category = registeredCategories.stream() - .filter(obj -> obj.getCategoryName().equals(categoryName)).findAny(); - - if (registeredCategories.isEmpty()) { - ConnectionManagerUtil.rollbackDBTransaction(); - String msg = "Registered application category set is empty category: " + categoryName; - log.error(msg); - throw new ApplicationManagementException(msg); - } - if (!category.isPresent()){ - ConnectionManagerUtil.rollbackDBTransaction(); - String msg = "Request contains invalid category: " + categoryName; - log.error(msg); - throw new ApplicationManagementException(msg); - - } - /* In current flow, allow to add one category for an application. If it is required to add multiple categories DAO layer is implemented to match with that requirement. Hence logic is also implemented @@ -241,11 +270,10 @@ public class ApplicationManagerImpl implements ApplicationManager { */ List categoryIds = new ArrayList<>(); categoryIds.add(category.get().getId()); - this.applicationDAO.addCategoryMapping(categoryIds,appId,tenantId); + this.applicationDAO.addCategoryMapping(categoryIds, appId, tenantId); //adding application tags - List tags = applicationWrapper.getTags(); - if (!tags.isEmpty()) { + if (tags != null && !tags.isEmpty()) { List registeredTags = applicationDAO.getAllTags(tenantId); List registeredTagNames = new ArrayList<>(); List tagIds = new ArrayList<>(); @@ -277,61 +305,84 @@ public class ApplicationManagerImpl implements ApplicationManager { log.debug("Creating a new release. App Id:" + appId); } String initialLifecycleState = lifecycleStateManager.getInitialState(); - applicationReleaseDTO = applicationDTO.getApplicationReleaseDTOs().get(0); applicationReleaseDTO.setCurrentState(initialLifecycleState); - applicationReleaseDTO = this.applicationReleaseDAO.createRelease(applicationReleaseDTO, appId, tenantId); + applicationReleaseDTO = this.applicationReleaseDAO + .createRelease(applicationReleaseDTO, appId, tenantId); LifecycleStateDTO lifecycleStateDTO = getLifecycleStateInstance(initialLifecycleState, initialLifecycleState); this.lifecycleStateDAO .addLifecycleState(lifecycleStateDTO, appId, applicationReleaseDTO.getUuid(), tenantId); applicationReleaseEntities.add(applicationReleaseDTO); applicationDTO.setApplicationReleaseDTOs(applicationReleaseEntities); - application = appDtoToAppResponse(applicationDTO); + Application application = appDtoToAppResponse(applicationDTO); ConnectionManagerUtil.commitDBTransaction(); + return application; } - return application; } catch (LifeCycleManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); - String msg = "Error occurred while adding lifecycle state. application name: " + applicationWrapper.getName() - + " application type: is " + applicationWrapper.getType(); + String msg = + "Error occurred while adding lifecycle state. application name: " + applicationWrapper.getName() + + " application type: is " + applicationWrapper.getType(); log.error(msg); + try { + applicationStorageManager.deleteAllApplicationReleaseArtifacts( + Collections.singletonList(applicationReleaseDTO.getAppHashValue())); + } catch (ApplicationStorageManagementException ex) { + String errorLog = + "Error occurred when deleting application artifacts. Application artifacts are tried to " + + "delete because of lifecycle state adding issue in the application creating operation."; + log.error(errorLog); + throw new ApplicationManagementException(errorLog, e); + } throw new ApplicationManagementException(msg, e); } catch (ApplicationManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); String msg = "Error occurred while adding application or application release. application name: " + applicationWrapper.getName() + " application type: " + applicationWrapper.getType(); log.error(msg); + deleteApplicationArtifacts(Collections.singletonList(applicationReleaseDTO.getAppHashValue())); throw new ApplicationManagementException(msg, e); - } catch(LifecycleManagementException e){ + } catch (LifecycleManagementException e) { ConnectionManagerUtil.rollbackDBTransaction(); String msg = "Error occurred when getting initial lifecycle state. application name: " + applicationWrapper .getName() + " application type: is " + applicationWrapper.getType(); log.error(msg); + deleteApplicationArtifacts(Collections.singletonList(applicationReleaseDTO.getAppHashValue())); throw new ApplicationManagementException(msg, e); - }catch (DBConnectionException e) { + } catch (DBConnectionException e) { String msg = "Error occurred while getting database connection."; log.error(msg); throw new ApplicationManagementException(msg, e); } catch (VisibilityManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); - String msg = "Error occurred while adding unrestricted roles. application name: " + applicationWrapper.getName() - + " application type: " + applicationWrapper.getType(); + String msg = + "Error occurred while adding unrestricted roles. application name: " + applicationWrapper.getName() + + " application type: " + applicationWrapper.getType(); log.error(msg); + deleteApplicationArtifacts(Collections.singletonList(applicationReleaseDTO.getAppHashValue())); throw new ApplicationManagementException(msg, e); } catch (TransactionManagementException e) { String msg = "Error occurred while disabling AutoCommit."; log.error(msg); throw new ApplicationManagementException(msg, e); - } catch (UserStoreException e) { - ConnectionManagerUtil.rollbackDBTransaction(); - String msg = "Error occurred when validating the unrestricted roles given for the application"; - log.error(msg); - throw new ApplicationManagementException(msg, e); } finally { ConnectionManagerUtil.closeDBConnection(); } } + private void deleteApplicationArtifacts(List directoryPaths) throws ApplicationManagementException { + ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); + + try { + applicationStorageManager.deleteAllApplicationReleaseArtifacts(directoryPaths); + } catch (ApplicationStorageManagementException e) { + String errorLog = "Error occurred when deleting application artifacts. directory paths: ." + directoryPaths + .toString(); + log.error(errorLog); + throw new ApplicationManagementException(errorLog, e); + } + } + private ApplicationReleaseDTO addApplicationReleaseArtifacts(String applicationType, String deviceType, ApplicationReleaseDTO applicationReleaseDTO, ApplicationArtifact applicationArtifact, boolean isNewRelease) throws ResourceManagementException, ApplicationManagementException { @@ -576,9 +627,6 @@ public class ApplicationManagerImpl implements ApplicationManager { try { //set default values - if (StringUtils.isEmpty(filter.getSortBy())) { - filter.setSortBy("ASC"); - } if (filter.getLimit() == 0) { filter.setLimit(20); } @@ -586,16 +634,15 @@ public class ApplicationManagerImpl implements ApplicationManager { if (!StringUtils.isEmpty(deviceTypename)) { deviceType = getDeviceTypeData(deviceTypename); } - - ConnectionManagerUtil.openDBConnection(); - - validateFilter(filter); if (deviceType == null) { - appDTOs = applicationDAO.getApplications(filter, 0, tenantId); - } else { - appDTOs = applicationDAO.getApplications(filter, deviceType.getId(), tenantId); + deviceType = new DeviceType(); + deviceType.setId(-1); } + ConnectionManagerUtil.openDBConnection(); + validateFilter(filter); + appDTOs = applicationDAO.getApplications(filter, deviceType.getId(), tenantId); + //todo as a performance improvement get these data from DB. Consider where in clause. for (ApplicationDTO app : appDTOs) { boolean isSearchingApp = true; List filteringTags = filter.getTags(); @@ -905,6 +952,7 @@ public class ApplicationManagerImpl implements ApplicationManager { } private String[] getRoleNames() throws UserStoreException { + //todo check role by role UserRealm userRealm = CarbonContext.getThreadLocalCarbonContext().getUserRealm(); if (userRealm != null) { return userRealm.getUserStoreManager().getRoleNames(); 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/ApplicationManagementPublisherAPI.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/ApplicationManagementPublisherAPI.java index 8b7b1093cba..ec80a874d80 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/ApplicationManagementPublisherAPI.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/ApplicationManagementPublisherAPI.java @@ -407,6 +407,7 @@ public interface ApplicationManagementPublisherAPI { code = 404, message = "Application not found"), }) + //todo add new scope and permission Response deleteApplication( @ApiParam( name = "UUID", 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/ApplicationManagementPublisherAPIImpl.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/ApplicationManagementPublisherAPIImpl.java index 4b5ab8369ba..fb53b354ed2 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/ApplicationManagementPublisherAPIImpl.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/ApplicationManagementPublisherAPIImpl.java @@ -84,7 +84,7 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem } return Response.status(Response.Status.OK).entity(applications).build(); } catch(BadRequestException e){ - String msg = "Incompatible request payload is found. Please try with valid reuest payload."; + String msg = "Incompatible request payload is found. Please try with valid request payload."; log.error(msg, e); return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); }catch (ApplicationManagementException e) { @@ -161,7 +161,7 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem if (application != null) { return Response.status(Response.Status.CREATED).entity(application).build(); } else { - String msg = "ApplicationDTO creation is failed"; + String msg = "Application creation is failed"; log.error(msg); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } @@ -285,9 +285,6 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem @Multipart("binaryFile") Attachment binaryFile) { try { - ApplicationManager applicationManager = APIUtil.getApplicationManager(); - - applicationManager.validateBinaryArtifact(binaryFile, appType); if (!ApplicationType.ENTERPRISE.toString().equals(appType)) { String msg = "If ApplicationDTO type is " + appType + ", therefore you don't have application release artifact to update for application release UUID: " @@ -295,7 +292,8 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem log.error(msg); return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); } - + ApplicationManager applicationManager = APIUtil.getApplicationManager(); + applicationManager.validateBinaryArtifact(binaryFile, appType); applicationManager.updateApplicationArtifact(deviceType, appType, applicationReleaseUuid, constructApplicationArtifact(binaryFile, null, null, null)); return Response.status(Response.Status.OK) @@ -371,9 +369,10 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem applicationManager.validateImageArtifacts(iconFile, bannerFile, screenshots); if (!applicationManager.updateRelease(deviceType, appType, applicationUUID, applicationReleaseWrapper, constructApplicationArtifact(binaryFile, iconFile, bannerFile, screenshots))) { - log.error("Application release updating is failed. Please contact the administrator. " - + "ApplicationDTO release UUID: " + applicationUUID + ", Supported device type: " + deviceType); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(applicationReleaseWrapper).build(); + String msg ="Application release updating is failed. Please contact the administrator. " + + "ApplicationDTO release UUID: " + applicationUUID + ", Supported device type: " + deviceType; + log.error(msg); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } return Response.status(Response.Status.OK).entity("Application release is successfully updated.").build(); } catch (BadRequestException e) { @@ -402,9 +401,7 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem } } - /* -//todo ---------------------- -*/ + @DELETE @Path("/{appId}") public Response deleteApplication(@PathParam("appId") int applicationId) { @@ -429,6 +426,10 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem } } + + /* +//todo ---------------------- +*/ @GET @Path("/lifecycle/{appId}/{uuid}") public Response getLifecycleState( From b991ed22ee44d9a6f55d47ad8261a2d943aa3cd7 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Thu, 2 May 2019 03:00:32 +0530 Subject: [PATCH 06/11] Improve get applications method --- .../mgt/core/impl/ApplicationManagerImpl.java | 36 +++++++++---------- 1 file changed, 16 insertions(+), 20 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 3007cc8607f..c9629d8d068 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 @@ -643,49 +643,45 @@ public class ApplicationManagerImpl implements ApplicationManager { validateFilter(filter); appDTOs = applicationDAO.getApplications(filter, deviceType.getId(), tenantId); //todo as a performance improvement get these data from DB. Consider where in clause. - for (ApplicationDTO app : appDTOs) { + for (ApplicationDTO applicationDTO : appDTOs) { boolean isSearchingApp = true; List filteringTags = filter.getTags(); List filteringCategories = filter.getAppCategories(); List filteringUnrestrictedRoles = filter.getUnrestrictedRoles(); - if (!lifecycleStateManager.getEndState().equals(app.getStatus())) { - List appUnrestrictedRoles = visibilityDAO.getUnrestrictedRoles(app.getId(), tenantId); + if (!lifecycleStateManager.getEndState().equals(applicationDTO.getStatus())) { + List appUnrestrictedRoles = visibilityDAO.getUnrestrictedRoles(applicationDTO.getId(), tenantId); if ((appUnrestrictedRoles.isEmpty() || hasUserRole(appUnrestrictedRoles, userName)) && ( filteringUnrestrictedRoles == null || filteringUnrestrictedRoles.isEmpty() || hasAppUnrestrictedRole(appUnrestrictedRoles, filteringUnrestrictedRoles, userName))) { if (filteringCategories != null && !filteringCategories.isEmpty()) { - List appTagList = applicationDAO.getAppCategories(app.getId(), tenantId); - boolean isAppCategory = false; - for (String category : filteringCategories) { - if (appTagList.contains(category)) { - isAppCategory = true; - break; - } - } + List appTagList = applicationDAO.getAppCategories(applicationDTO.getId(), tenantId); + boolean isAppCategory = filteringCategories.stream().anyMatch(appTagList::contains); if (!isAppCategory) { isSearchingApp = false; } } if (filteringTags != null && !filteringTags.isEmpty()) { - List appTagList = applicationDAO.getAppTags(app.getId(), tenantId); - boolean isAppTag = false; - for (String tag : filteringTags) { - if (appTagList.contains(tag)) { - isAppTag = true; - break; - } - } + List appTagList = applicationDAO.getAppTags(applicationDTO.getId(), tenantId); + boolean isAppTag = filteringTags.stream().anyMatch(appTagList::contains); if (!isAppTag) { isSearchingApp = false; } } if (isSearchingApp) { - filteredApplications.add(app); + filteredApplications.add(applicationDTO); } } } + + List filteredApplicationReleaseDTOs = new ArrayList<>(); + for (ApplicationReleaseDTO applicationReleaseDTO : applicationDTO.getApplicationReleaseDTOs()) { + if (!applicationReleaseDTO.getCurrentState().equals(lifecycleStateManager.getEndState())) { + filteredApplicationReleaseDTOs.add(applicationReleaseDTO); + } + } + applicationDTO.setApplicationReleaseDTOs(filteredApplicationReleaseDTOs); } for(ApplicationDTO appDTO : filteredApplications){ From 16e2da3c0bb18c95598e5681c37c374f513356dd Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Thu, 2 May 2019 13:48:32 +0530 Subject: [PATCH 07/11] Add API for getting application release --- .../mgt/addons/ApiOriginFilter.java | 9 ++-- .../mgt/addons/FileStreamingOutput.java | 25 +++++----- .../mgt/addons/JSONMessageHandler.java | 7 ++- .../mgt/addons/MultipartCustomProvider.java | 32 ++++++------- .../mgt/addons/ValidationInterceptor.java | 9 ++-- .../jaxrs/AnnotationExclusionStrategy.java | 26 +++++----- .../application/mgt/addons/jaxrs/Exclude.java | 26 +++++----- .../common/services/ApplicationManager.java | 12 ++++- .../mgt/core/dao/VisibilityDAO.java | 2 + .../GenericApplicationReleaseDAOImpl.java | 2 +- .../visibility/GenericVisibilityDAOImpl.java | 36 ++++++++++++++ .../mgt/core/impl/ApplicationManagerImpl.java | 47 +++++++++++++++++++ .../ApplicationManagementPublisherAPI.java | 42 +++++++++++++++++ ...ApplicationManagementPublisherAPIImpl.java | 31 ++++++++++++ 14 files changed, 232 insertions(+), 74 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/ApiOriginFilter.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/ApiOriginFilter.java index 7b34c432328..25446e95135 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/ApiOriginFilter.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/ApiOriginFilter.java @@ -1,17 +1,16 @@ -/* - * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. * - * WSO2 Inc. licenses this file to you under the Apache License, + * Entgra (Pvt) Ltd. 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 + * 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 + * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/FileStreamingOutput.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/FileStreamingOutput.java index ee89b209ed1..a1ad912d196 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/FileStreamingOutput.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/FileStreamingOutput.java @@ -1,19 +1,18 @@ -/* * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) 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 + * Entgra (Pvt) Ltd. 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. + * 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.addons; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/JSONMessageHandler.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/JSONMessageHandler.java index 3b6a778a942..6951e02b2ea 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/JSONMessageHandler.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/JSONMessageHandler.java @@ -1,12 +1,11 @@ -/* - * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. * - * WSO2 Inc. licenses this file to you under the Apache License, + * Entgra (Pvt) Ltd. 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 + * 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 diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/MultipartCustomProvider.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/MultipartCustomProvider.java index 5e0bd801903..b1370f18138 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/MultipartCustomProvider.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/MultipartCustomProvider.java @@ -1,20 +1,20 @@ package org.wso2.carbon.device.application.mgt.addons;/* -* Copyright (c) 2017, 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. -* -*/ +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. 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. + */ import com.google.gson.Gson; import com.google.gson.GsonBuilder; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/ValidationInterceptor.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/ValidationInterceptor.java index a05ecd82e1c..d36e9986b1e 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/ValidationInterceptor.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/ValidationInterceptor.java @@ -1,17 +1,16 @@ -/* - * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. * - * WSO2 Inc. licenses this file to you under the Apache License, + * Entgra (Pvt) Ltd. 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 + * 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 + * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/jaxrs/AnnotationExclusionStrategy.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/jaxrs/AnnotationExclusionStrategy.java index 1bb39f70875..d2a92c4c27c 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/jaxrs/AnnotationExclusionStrategy.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/jaxrs/AnnotationExclusionStrategy.java @@ -1,20 +1,18 @@ -/* - * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) 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 + * Entgra (Pvt) Ltd. 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. + * 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.addons.jaxrs; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/jaxrs/Exclude.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/jaxrs/Exclude.java index fc53d16a8b4..e185ececcbd 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/jaxrs/Exclude.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/jaxrs/Exclude.java @@ -1,20 +1,18 @@ -/* - * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) 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 + * Entgra (Pvt) Ltd. 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. + * 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.addons.jaxrs; 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 12f64c65866..5daff08cf7c 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 @@ -33,7 +33,6 @@ import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationReleaseW import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationUpdateWrapper; import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationWrapper; -import java.io.InputStream; import java.util.List; /** @@ -89,7 +88,7 @@ public interface ApplicationManager { ApplicationList getApplications(Filter filter) throws ApplicationManagementException; /** - * To get the ApplicationDTO for given Id. + * To get the Application for given Id. * * @param id id of the ApplicationDTO * @param state state of the ApplicationDTO @@ -98,6 +97,15 @@ public interface ApplicationManager { */ Application getApplicationById(int id, String state) throws ApplicationManagementException; + /** + * To get the Application Release for given uuid. + * + * @param uuid uuid of the ApplicationDTO + * @return the Application Release identified by the UUID + * @throws ApplicationManagementException Application Management Exception. + */ + ApplicationRelease getApplicationReleaseByUUID(String uuid) throws ApplicationManagementException; + /** * To get the ApplicationDTO for given application relase 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/VisibilityDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/VisibilityDAO.java index 9ee48dc2507..4a4b1f30ed7 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/VisibilityDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/VisibilityDAO.java @@ -40,6 +40,8 @@ public interface VisibilityDAO { List getUnrestrictedRoles(int applicationId, int tenantId) throws VisibilityManagementDAOException; + List getUnrestrictedRolesByUUID(String uuid, int tenantId) throws VisibilityManagementDAOException; + void deleteUnrestrictedRoles(List unrestrictedRoles, int applicationId, int tenantId) throws VisibilityManagementDAOException; 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 ad5f520570b..f7c9c634c9c 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 @@ -244,7 +244,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements + "AR.CURRENT_STATE AS RELEASE_CURRENT_STATE, " + "AR.RATED_USERS AS RATED_USER_COUNT " + "FROM AP_APP_RELEASE AS AR " - + "WHERE AR.UUID = ? AND AR.TENAT_ID = ?"; + + "WHERE AR.UUID = ? AND AR.TENANT_ID = ?"; try { connection = this.getDBConnection(); 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/visibility/GenericVisibilityDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/visibility/GenericVisibilityDAOImpl.java index 51360b4a47b..26f88165b05 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/visibility/GenericVisibilityDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/visibility/GenericVisibilityDAOImpl.java @@ -104,6 +104,42 @@ public class GenericVisibilityDAOImpl extends AbstractDAOImpl implements Visibil } } + + @Override + public List getUnrestrictedRolesByUUID(String uuid, int tenantId) throws VisibilityManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to get unrestricted roles for UUID: " + uuid); + } + Connection conn; + List unrestrictedRoles = new ArrayList<>(); + String sql = "SELECT ROLE FROM AP_UNRESTRICTED_ROLE " + + "WHERE " + + "AP_APP_ID = (SELECT AR.AP_APP_ID FROM AP_APP_RELEASE AR WHERE AR.UUID = ? AND AR.TENANT_ID = ? ) " + + "AND TENANT_ID = ?"; + try { + conn = this.getDBConnection(); + conn.setAutoCommit(false); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1, uuid); + stmt.setInt(2, tenantId); + stmt.setInt(3, tenantId); + try (ResultSet rs = stmt.executeQuery();) { + while (rs.next()) { + unrestrictedRoles.add(rs.getString("ROLE")); + } + return unrestrictedRoles; + } + } + } catch (DBConnectionException e) { + throw new VisibilityManagementDAOException( + "Error occurred while obtaining the DB connection when getting unrestricted roles for UUID: " + + uuid, e); + } catch (SQLException e) { + throw new VisibilityManagementDAOException( + "Error occurred while getting unrestricted roles for UUID: " + uuid, e); + } + } + @Override public void deleteUnrestrictedRoles(List unrestrictedRoles, int applicationId, int tenantId) throws VisibilityManagementDAOException { if (log.isDebugEnabled()) { 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 c9629d8d068..75afd8132cb 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 @@ -874,6 +874,53 @@ public class ApplicationManagerImpl implements ApplicationManager { } } + @Override + public ApplicationRelease getApplicationReleaseByUUID(String uuid) throws ApplicationManagementException{ + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + boolean isVisibleAppRelease = false; + try { + ConnectionManagerUtil.openDBConnection(); + ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO.getReleaseByUUID(uuid, tenantId); + if (applicationReleaseDTO == null) { + String msg = "Couldn't find an application release for the UUID: " + uuid; + log.error(msg); + throw new NotFoundException(msg); + } + if (applicationReleaseDTO.getCurrentState().equals(lifecycleStateManager.getEndState())) { + return null; + } + + List unrestrictedRoles = this.visibilityDAO.getUnrestrictedRolesByUUID(uuid, tenantId); + if (!unrestrictedRoles.isEmpty()) { + if (hasUserRole(unrestrictedRoles, userName)) { + isVisibleAppRelease = true; + } + } else { + isVisibleAppRelease = true; + } + + if (!isVisibleAppRelease) { + String msg = "You are trying to access release of visibility restricted application. You don't have " + + "required roles to view this application,"; + log.error(msg); + throw new ForbiddenException(msg); + } + return releaseDtoToRelease(applicationReleaseDTO); + } catch (LifecycleManagementException e) { + String msg = "Error occurred when getting the end state of the application lifecycle flow"; + log.error(msg); + throw new ApplicationManagementException(msg, e); + } catch (UserStoreException e) { + String msg = "User-store exception while getting application with the application release UUID: " + uuid; + log.error(msg); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + @Override public ApplicationDTO getApplicationByUuid(String uuid, String state) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); 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/ApplicationManagementPublisherAPI.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/ApplicationManagementPublisherAPI.java index ec80a874d80..47f2f64c1a4 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/ApplicationManagementPublisherAPI.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/ApplicationManagementPublisherAPI.java @@ -180,6 +180,48 @@ public interface ApplicationManagementPublisherAPI { @QueryParam("state") String state ); + @GET + @Path("/release/{uuid}") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "get the application release of requesting application UUID and state", + notes = "This will get the application release identified by the application release uuid and state.", + tags = "ApplicationDTO Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:app:publisher:view") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully retrieved relevant application release.", + response = ApplicationDTO.class), + @ApiResponse( + code = 403, + message = "Don't have permission to access the application release"), + @ApiResponse( + code = 404, + message = "Application release not found"), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while getting relevant application release.", + response = ErrorResponse.class) + }) + Response getApplicationRelease( + @ApiParam( + name = "uuid", + value = "application release uuid", + required = true) + @PathParam("uuid") String uuid + ); + @PUT @Path("/{appId}") @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/ApplicationManagementPublisherAPIImpl.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/ApplicationManagementPublisherAPIImpl.java index fb53b354ed2..a38e0880ba7 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/ApplicationManagementPublisherAPIImpl.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/ApplicationManagementPublisherAPIImpl.java @@ -125,6 +125,37 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem } } + @GET + @Consumes("application/json") + @Path("/release/{uuid}") + public Response getApplicationRelease( + @PathParam("uuid") String uuid) { + ApplicationManager applicationManager = APIUtil.getApplicationManager(); + try { + ApplicationRelease applicationRelease = applicationManager.getApplicationReleaseByUUID(uuid); + if (applicationRelease == null){ + String msg = "Application release is in the end state of the application lifecycle flow."; + log.error(msg); + return Response.status(Response.Status.OK).entity(msg).build(); + } + return Response.status(Response.Status.OK).entity(applicationRelease).build(); + } catch (NotFoundException e) { + String msg = "Application Release with UUID: " + uuid + " is not found"; + log.error(msg, e); + return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); + } catch(ForbiddenException e){ + String msg = "You don't have permission to access the application release. application release UUID: : " + + uuid; + log.error(msg); + return Response.status(Response.Status.FORBIDDEN).entity(msg).build(); + } + catch (ApplicationManagementException e) { + String msg = "Error occurred while getting application release for UUID: " + uuid; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } + @POST @Consumes("multipart/mixed") public Response createApplication( From f95f872732dc9c018fb258e0e3c2a309dfe90ad7 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Thu, 2 May 2019 18:38:15 +0530 Subject: [PATCH 08/11] Add application release deleting API --- .../mgt/common/dto/DeviceSubscriptionDTO.java | 68 ++++++++ .../common/services/ApplicationManager.java | 2 +- .../mgt/core/dao/SubscriptionDAO.java | 5 + .../application/mgt/core/dao/common/Util.java | 26 +++ .../GenericSubscriptionDAOImpl.java | 53 ++++++- .../mgt/core/impl/ApplicationManagerImpl.java | 149 ++++++++++-------- ...pplicationManagementPublisherAdminAPI.java | 33 ++-- ...cationManagementPublisherAdminAPIImpl.java | 14 +- 8 files changed, 244 insertions(+), 106 deletions(-) create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/dto/DeviceSubscriptionDTO.java diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/dto/DeviceSubscriptionDTO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/dto/DeviceSubscriptionDTO.java new file mode 100644 index 00000000000..c523c66a530 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/dto/DeviceSubscriptionDTO.java @@ -0,0 +1,68 @@ +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. 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.common.dto; + +import java.sql.Timestamp; + +public class DeviceSubscriptionDTO { + + private int id; + private String subscribedBy; + private Timestamp subscribedTimestamp; + private boolean isUnsubscribed; + private String unsubscribedBy; + private Timestamp unsubscribedTimestapm; + private String subscribedFrom; + private int deviceId; + + public int getId() { return id; } + + public void setId(int id) { this.id = id; } + + public String getSubscribedBy() { return subscribedBy; } + + public void setSubscribedBy(String subscribedBy) { this.subscribedBy = subscribedBy; } + + public Timestamp getSubscribedTimestamp() { return subscribedTimestamp; } + + public void setSubscribedTimestamp(Timestamp subscribedTimestamp) { + this.subscribedTimestamp = subscribedTimestamp; + } + + public boolean isUnsubscribed() { return isUnsubscribed; } + + public void setUnsubscribed(boolean unsubscribed) { isUnsubscribed = unsubscribed; } + + public String getUnsubscribedBy() { return unsubscribedBy; } + + public void setUnsubscribedBy(String unsubscribedBy) { this.unsubscribedBy = unsubscribedBy; } + + public Timestamp getUnsubscribedTimestapm() { return unsubscribedTimestapm; } + + public void setUnsubscribedTimestapm(Timestamp unsubscribedTimestapm) { + this.unsubscribedTimestapm = unsubscribedTimestapm; + } + + public String getSubscribedFrom() { return subscribedFrom; } + + public void setSubscribedFrom(String subscribedFrom) { this.subscribedFrom = subscribedFrom; } + + public int getDeviceId() { return deviceId; } + + public void setDeviceId(int deviceId) { this.deviceId = deviceId; } +} 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 5daff08cf7c..ad6f457be66 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 @@ -76,7 +76,7 @@ public interface ApplicationManager { * @param releaseUuid UUID of tha application release * @throws ApplicationManagementException ApplicationDTO Management Exception */ - String deleteApplicationRelease(int applicationId, String releaseUuid) throws ApplicationManagementException; + void deleteApplicationRelease(int applicationId, String releaseUuid) throws ApplicationManagementException; /** * To get the applications based on the search filter. diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java index 2045bf62ca6..cd46b6a8b31 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java @@ -20,6 +20,7 @@ package org.wso2.carbon.device.application.mgt.core.dao; import org.wso2.carbon.device.application.mgt.common.dto.ApplicationDTO; import org.wso2.carbon.device.application.mgt.common.dto.ApplicationReleaseDTO; +import org.wso2.carbon.device.application.mgt.common.dto.DeviceSubscriptionDTO; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; @@ -86,4 +87,8 @@ public interface SubscriptionDAO { */ void subscribeGroupToApplication(int tenantId, String subscribedBy, List groupList, int appId, int releaseId) throws ApplicationManagementDAOException; + + public List getDeviceSubscriptions(int appReleaseId, int tenantId) throws + ApplicationManagementDAOException; + } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java index 08d7738b14f..2a5031f843d 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 @@ -26,6 +26,7 @@ import org.wso2.carbon.device.application.mgt.common.dto.ApplicationDTO; import org.wso2.carbon.device.application.mgt.common.PaginationRequest; import org.wso2.carbon.device.application.mgt.common.dto.ApplicationReleaseDTO; +import org.wso2.carbon.device.application.mgt.common.dto.DeviceSubscriptionDTO; 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; @@ -94,6 +95,31 @@ public class Util { return applications; } + /** + * To create list of device subscription objects from the result set retrieved from the Database. + * + * @param rs ResultSet + * @return List of device subscriptions that is retrieved from the Database. + * @throws SQLException SQL Exception + * @throws JSONException JSONException. + */ + public static List loadDeviceSubscriptions(ResultSet rs) throws SQLException { + List deviceSubscriptionDTOS = new ArrayList<>(); + while (rs.next()) { + DeviceSubscriptionDTO deviceSubscriptionDTO = new DeviceSubscriptionDTO(); + deviceSubscriptionDTO.setId(rs.getInt("ID")); + deviceSubscriptionDTO.setSubscribedBy(rs.getString("SUBSCRIBED_BY")); + deviceSubscriptionDTO.setSubscribedTimestamp(rs.getTimestamp("SUBSCRIBED_AT")); + deviceSubscriptionDTO.setUnsubscribed(rs.getBoolean("IS_UNSUBSCRIBED")); + deviceSubscriptionDTO.setUnsubscribedBy(rs.getString("UNSUBSCRIBED_BY")); + deviceSubscriptionDTO.setUnsubscribedTimestapm(rs.getTimestamp("UNSUBSCRIBED_AT")); + deviceSubscriptionDTO.setSubscribedFrom(rs.getString("SUBSCRIBED_FROM")); + deviceSubscriptionDTO.setDeviceId(rs.getInt("DEVICE_ID")); + deviceSubscriptionDTOS.add(deviceSubscriptionDTO); + } + return deviceSubscriptionDTOS; + } + /** * Populates {@link ApplicationReleaseDTO} object with the result obtained from the database. * 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/subscription/GenericSubscriptionDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java index e5e4253691e..7eeb71046c6 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java @@ -19,16 +19,21 @@ package org.wso2.carbon.device.application.mgt.core.dao.impl.subscription; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.json.JSONException; +import org.wso2.carbon.device.application.mgt.common.dto.ApplicationDTO; +import org.wso2.carbon.device.application.mgt.common.dto.DeviceSubscriptionDTO; import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; import org.wso2.carbon.device.application.mgt.core.dao.SubscriptionDAO; import org.wso2.carbon.device.application.mgt.core.dao.common.Util; import org.wso2.carbon.device.application.mgt.core.dao.impl.AbstractDAOImpl; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; +import org.wso2.carbon.device.application.mgt.core.exception.UnexpectedServerErrorException; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import java.sql.Connection; import java.sql.PreparedStatement; +import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; @@ -44,7 +49,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc conn = this.getDBConnection(); long time = System.currentTimeMillis() / 1000; String sql = "INSERT INTO AP_DEVICE_SUBSCRIPTION(TENANT_ID, SUBSCRIBED_BY, SUBSCRIBED_TIMESTAMP, " - + "DM_DEVICE_ID, AP_APP_RELEASE_ID, AP_APP_ID, INSTALL_STATUS) VALUES (?, ?, ?, ?, ?, ?)"; + + "DM_DEVICE_ID, AP_APP_RELEASE_ID, AP_APP_ID, INSTALL_STATUS) VALUES (?, ?, ?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(sql); for (Device device : deviceList) { stmt.setInt(1, tenantId); @@ -168,4 +173,50 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc Util.cleanupResources(stmt, null); } } + + @Override + public List getDeviceSubscriptions(int appReleaseId, int tenantId) throws + ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Getting device subscriptions for the application release id " + appReleaseId + + " from the database"); + } + Connection conn; + try { + conn = this.getDBConnection(); + String sql = "SELECT " + + "DS.ID AS ID, " + + "DS.SUBSCRIBED_BY AS SUBSCRIBED_BY, " + + "DS.SUBSCRIBED_TIMESTAMP AS SUBSCRIBED_AT, " + + "DS.UNSUBSCRIBED AS IS_UNSUBSCRIBED, " + + "DS.UNSUBSCRIBED_BY AS UNSUBSCRIBED_BY, " + + "DS.UNSUBSCRIBED_TIMESTAMP AS UNSUBSCRIBED_AT, " + + "DS.SUBSCRIBED_FROM AS SUBSCRIBED_FROM, " + + "DS.DM_DEVICE_ID AS DEVICE_ID " + + "FROM AP_DEVICE_SUBSCRIPTION DS " + + "WHERE DS.AP_APP_RELEASE_ID = ? AND DS.TENANT_ID=?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, appReleaseId); + stmt.setInt(2, tenantId); + try (ResultSet rs = stmt.executeQuery()) { + if (log.isDebugEnabled()) { + log.debug("Successfully retrieved device subscriptions for application release id " + + appReleaseId); + } + return Util.loadDeviceSubscriptions(rs); + } + } + } catch (SQLException e) { + String msg = + "Error occurred while getting device subscription data for application ID: " + appReleaseId + "."; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); + } catch (DBConnectionException e) { + String msg = + "Error occurred while obtaining the DB connection for getting device subscription for applicationID: " + + appReleaseId + "."; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); + } + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/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 75afd8132cb..95a5a17807a 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 @@ -39,6 +39,7 @@ 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.dto.CategoryDTO; import org.wso2.carbon.device.application.mgt.common.Filter; +import org.wso2.carbon.device.application.mgt.common.dto.DeviceSubscriptionDTO; import org.wso2.carbon.device.application.mgt.common.dto.LifecycleStateDTO; import org.wso2.carbon.device.application.mgt.common.dto.TagDTO; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; @@ -59,6 +60,7 @@ import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager; 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.SubscriptionDAO; 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; @@ -109,6 +111,7 @@ public class ApplicationManagerImpl implements ApplicationManager { private ApplicationDAO applicationDAO; private ApplicationReleaseDAO applicationReleaseDAO; private LifecycleStateDAO lifecycleStateDAO; + private SubscriptionDAO subscriptionDAO; private LifecycleStateManager lifecycleStateManager; public ApplicationManagerImpl() { @@ -121,6 +124,7 @@ public class ApplicationManagerImpl implements ApplicationManager { this.applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO(); this.lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO(); this.applicationReleaseDAO = ApplicationManagementDAOFactory.getApplicationReleaseDAO(); + this.subscriptionDAO = ApplicationManagementDAOFactory.getSubscriptionDAO(); } /*** @@ -1230,86 +1234,91 @@ public class ApplicationManagerImpl implements ApplicationManager { } @Override - public String deleteApplicationRelease(int applicationId, String releaseUuid) + public void deleteApplicationRelease(int applicationId, String releaseUuid) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - ApplicationDTO application; + ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); try { ConnectionManagerUtil.beginDBTransaction(); - application = this.applicationDAO.getApplicationById(applicationId, tenantId); - if (application == null) { - throw new NotFoundException("Couldn't find an application for application ID: " + applicationId); + ApplicationReleaseDTO applicationReleaseDTO = this.applicationReleaseDAO + .getReleaseByUUID(releaseUuid, tenantId); + if (applicationReleaseDTO == null) { + String msg = "Couldn't find an application release for application release UUID: " + releaseUuid; + log.error(msg); + throw new NotFoundException(msg); } - if (!isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION) && !application - .getUnrestrictedRoles().isEmpty() && hasUserRole(application.getUnrestrictedRoles(), userName)) { - throw new ForbiddenException( - "You don't have permission for deleting application release. ApplicationDTO id: " + applicationId - + " and release UUID: " + releaseUuid); - } - - ApplicationReleaseDTO applicationRelease = this.applicationReleaseDAO - .getReleaseByIds(applicationId, releaseUuid, tenantId); - if (applicationRelease == null) { - throw new NotFoundException("Couldn't find an application release for application ID: " + applicationId - + " and release UUID: " + releaseUuid); - } - LifecycleStateDTO appLifecycleState = this.lifecycleStateDAO - .getLatestLifeCycleState(applicationId, releaseUuid); - if (appLifecycleState == null) { - throw new NotFoundException( - "Couldn't find an lifecycle sate for application ID: " + applicationId + " and UUID: " - + releaseUuid); - } - String currentState = appLifecycleState.getCurrentState(); - if (AppLifecycleState.DEPRECATED.toString().equals(currentState) || AppLifecycleState.REJECTED.toString() - .equals(currentState) || AppLifecycleState.UNPUBLISHED.toString().equals(currentState)) { - LifecycleStateDTO newAppLifecycleState = getLifecycleStateInstance(AppLifecycleState.REMOVED.toString(), - appLifecycleState.getCurrentState()); - if (lifecycleStateManager.isValidStateChange(newAppLifecycleState.getPreviousState(), - newAppLifecycleState.getCurrentState(), userName, tenantId)) { - this.lifecycleStateDAO - .addLifecycleState(newAppLifecycleState, applicationId, applicationRelease.getUuid(), - tenantId); - ConnectionManagerUtil.commitDBTransaction(); - } else { - List lifecycleFlow = searchLifecycleStateFlow(currentState, - AppLifecycleState.REMOVED.toString()); - for (String nextState : lifecycleFlow) { - LifecycleStateDTO lifecycleState = getLifecycleStateInstance(nextState, currentState); - if (lifecycleStateManager.isValidStateChange(currentState, nextState, userName, tenantId)) { - this.lifecycleStateDAO - .addLifecycleState(lifecycleState, applicationId, applicationRelease.getUuid(), - tenantId); - } else { - ConnectionManagerUtil.rollbackDBTransaction(); - throw new ApplicationManagementException( - "Can't delete the application release, You have to move the " - + "lifecycle state from " + currentState + " to " + nextState); - } - currentState = nextState; - } + + if (!applicationReleaseDTO.getCurrentState().equals(lifecycleStateManager.getInitialState())) { + String msg = "Application state is not in the initial state: " + lifecycleStateManager.getInitialState() + + ". Therefore you are not permitted to delete the application release."; + log.error(msg); + throw new ForbiddenException(msg); + } + List deviceSubscriptionDTOS = subscriptionDAO + .getDeviceSubscriptions(applicationReleaseDTO.getId(), tenantId); + for (DeviceSubscriptionDTO deviceSubscriptionDTO : deviceSubscriptionDTOS) { + if (!deviceSubscriptionDTO.isUnsubscribed()) { + String msg = "This application is subscribed to device/s. Therefore you are not permitted to delete " + + "the application release."; + log.error(msg); + throw new ForbiddenException(msg); } - } 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(); + //todo delete application release data ON delete cascade + applicationStorageManager.deleteApplicationReleaseArtifacts(applicationReleaseDTO.getAppHashValue()); + + // LifecycleStateDTO appLifecycleState = this.lifecycleStateDAO +// .getLatestLifeCycleState(applicationId, releaseUuid); +// if (appLifecycleState == null) { +// throw new NotFoundException( +// "Couldn't find an lifecycle sate for application ID: " + applicationId + " and UUID: " +// + releaseUuid); +// } +// String currentState = appLifecycleState.getCurrentState(); +// if (AppLifecycleState.DEPRECATED.toString().equals(currentState) || AppLifecycleState.REJECTED.toString() +// .equals(currentState) || AppLifecycleState.UNPUBLISHED.toString().equals(currentState)) { +// LifecycleStateDTO newAppLifecycleState = getLifecycleStateInstance(AppLifecycleState.REMOVED.toString(), +// appLifecycleState.getCurrentState()); +// if (lifecycleStateManager.isValidStateChange(newAppLifecycleState.getPreviousState(), +// newAppLifecycleState.getCurrentState(), userName, tenantId)) { +// this.lifecycleStateDAO +// .addLifecycleState(newAppLifecycleState, applicationId, applicationRelease.getUuid(), +// tenantId); +// ConnectionManagerUtil.commitDBTransaction(); +// } else { +// List lifecycleFlow = searchLifecycleStateFlow(currentState, +// AppLifecycleState.REMOVED.toString()); +// for (String nextState : lifecycleFlow) { +// LifecycleStateDTO lifecycleState = getLifecycleStateInstance(nextState, currentState); +// if (lifecycleStateManager.isValidStateChange(currentState, nextState, userName, tenantId)) { +// this.lifecycleStateDAO +// .addLifecycleState(lifecycleState, applicationId, applicationRelease.getUuid(), +// tenantId); +// } else { +// ConnectionManagerUtil.rollbackDBTransaction(); +// throw new ApplicationManagementException( +// "Can't delete the application release, You have to move the " +// + "lifecycle state from " + currentState + " to " + nextState); +// } +// currentState = nextState; +// } +// } +// } else { +// throw new ApplicationManagementException( +// "Can't delete the application release, You have to move the " + "lifecycle state from " +// + currentState + " to acceptable " + "state"); +// } +// return applicationReleaseDTO.getAppHashValue(); } catch (ApplicationManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); - throw new ApplicationManagementDAOException( - "Error ocured when getting application data or application release data for application id of " - + applicationId + " application release UUID of the " + releaseUuid); - } catch (LifeCycleManagementDAOException e) { + String msg = "Error ocured when getting application data or application release data for application id of " + + applicationId + " application release UUID of the " + releaseUuid; + throw new ApplicationManagementDAOException(msg, e); + } catch (ApplicationStorageManagementException e) { ConnectionManagerUtil.rollbackDBTransaction(); - throw new ApplicationManagementException( - "Error occured when deleting application release for application ID of " + applicationId - + " and application release UUID of " + releaseUuid, e); - } catch (UserStoreException e) { - throw new ApplicationManagementException( - "Error occured when checking permission for executing application release update. ApplicationDTO ID: " - + applicationId + " and ApplicationDTO UUID: " + releaseUuid); + String msg = "Error occured when deleteing the application release artifact from the file system. Application release UUID: " + releaseUuid; + log.error(msg); + throw new ApplicationManagementException(msg, e); } finally { 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/admin/ApplicationManagementPublisherAdminAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/admin/ApplicationManagementPublisherAdminAPI.java index b8b973d5820..2f2979d39ea 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/admin/ApplicationManagementPublisherAdminAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/admin/ApplicationManagementPublisherAdminAPI.java @@ -28,33 +28,18 @@ import io.swagger.annotations.ExtensionProperty; import io.swagger.annotations.Info; import io.swagger.annotations.SwaggerDefinition; import io.swagger.annotations.Tag; -import org.apache.cxf.jaxrs.ext.multipart.Attachment; -import org.apache.cxf.jaxrs.ext.multipart.Multipart; import org.wso2.carbon.apimgt.annotations.api.Scope; import org.wso2.carbon.apimgt.annotations.api.Scopes; import org.wso2.carbon.device.application.mgt.common.ApplicationList; import org.wso2.carbon.device.application.mgt.common.ErrorResponse; -import org.wso2.carbon.device.application.mgt.common.Filter; -import org.wso2.carbon.device.application.mgt.common.dto.ApplicationDTO; -import org.wso2.carbon.device.application.mgt.common.dto.ApplicationReleaseDTO; -import org.wso2.carbon.device.application.mgt.common.response.ApplicationRelease; -import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationReleaseWrapper; -import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationUpdateWrapper; -import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationWrapper; -import javax.validation.Valid; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import java.util.List; /** * APIs to handle application management related tasks. @@ -92,19 +77,19 @@ public interface ApplicationManagementPublisherAdminAPI { String SCOPE = "scope"; @DELETE - @Path("/{appid}/{uuid}") + @Path("/release/{appId}/{uuid}") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @ApiOperation( consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON, - httpMethod = "GET", - value = "get all applications", - notes = "This will get all applications", - tags = "ApplicationDTO Management", + httpMethod = "DELETE", + value = "Delete application release.", + notes = "This will delete application release for given UUID", + tags = "Application Management", extensions = { @Extension(properties = { - @ExtensionProperty(name = SCOPE, value = "perm:app:publisher:view") + @ExtensionProperty(name = SCOPE, value = "perm:admin:app:publisher:update") }) } ) @@ -112,11 +97,11 @@ public interface ApplicationManagementPublisherAdminAPI { value = { @ApiResponse( code = 200, - message = "OK. \n Successfully delete application releaset.", + message = "OK. \n Successfully delete application release.", response = ApplicationList.class), @ApiResponse( code = 404, - message = "Not Found. There doesn't have an application release with UUID" + + message = "Not Found. There doesn't have an application release for UUID" + "query."), @ApiResponse( code = 500, @@ -127,7 +112,7 @@ public interface ApplicationManagementPublisherAdminAPI { name = "appId", value = "application Id", required = true) - @PathParam("appid") int applicationId, + @PathParam("appId") int applicationId, @ApiParam( name = "uuid", value = "application release UUID", diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/admin/ApplicationManagementPublisherAdminAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/admin/ApplicationManagementPublisherAdminAPIImpl.java index f360f9b01b4..56270db7e96 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/admin/ApplicationManagementPublisherAdminAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/admin/ApplicationManagementPublisherAdminAPIImpl.java @@ -74,16 +74,14 @@ public class ApplicationManagementPublisherAdminAPIImpl implements ApplicationMa private static Log log = LogFactory.getLog(ApplicationManagementPublisherAdminAPIImpl.class); @DELETE - @Path("/{appid}/{uuid}") + @Path("/release/{appId}/{uuid}") public Response deleteApplicationRelease( - @PathParam("appid") int applicationId, + @PathParam("appId") int applicationId, @PathParam("uuid") String releaseUuid) { ApplicationManager applicationManager = APIUtil.getApplicationManager(); - ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); try { - String storedLocation = applicationManager.deleteApplicationRelease(applicationId, releaseUuid); - applicationStorageManager.deleteApplicationReleaseArtifacts(storedLocation); - String responseMsg = "Successfully deleted the application release of: " + applicationId + ""; + applicationManager.deleteApplicationRelease(applicationId, releaseUuid); + String responseMsg = "Successfully deleted the application release for uuid: " + releaseUuid + ""; return Response.status(Response.Status.OK).entity(responseMsg).build(); } catch (NotFoundException e) { String msg = "Couldn't found application release which is having application id: " + applicationId @@ -100,10 +98,6 @@ public class ApplicationManagementPublisherAdminAPIImpl implements ApplicationMa String msg = "Error occurred while deleting the application: " + applicationId; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } catch (ApplicationStorageManagementException e) { - String msg = "Error occurred while deleting the application storage: " + applicationId; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } } From 1100db4d6763712c891c4a312eac18ca631d005f Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Fri, 3 May 2019 17:01:40 +0530 Subject: [PATCH 09/11] Add lifecycle retrieving API --- .../mgt/api/services/ConfigRetrieveAPI.java | 28 ++++++ .../services/impl/ConfigRetrieveAPIImpl.java | 25 +++++ .../mgt/common/config/LifecycleGraph.java | 70 ++++++++++++++ .../common/config/LifecycleStateVertex.java | 91 +++++++++++++++++++ .../mgt/common/services/AppmDataHandler.java | 4 + .../mgt/core/impl/AppmDataHandlerImpl.java | 15 ++- .../core/lifecycle/LifecycleStateManager.java | 33 +++++++ 7 files changed, 265 insertions(+), 1 deletion(-) create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/config/LifecycleGraph.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/config/LifecycleStateVertex.java diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ConfigRetrieveAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ConfigRetrieveAPI.java index 15c76d251ed..f4e3130c97d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ConfigRetrieveAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ConfigRetrieveAPI.java @@ -81,4 +81,32 @@ public interface ConfigRetrieveAPI { }) Response getUiConfig(); + @GET + @Path("/lifecycle-config") + @Produces(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "get application management UI configuration", + notes = "This will get all UI configuration of application management" + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully got Lifecycle Config.", + response = ApplicationList.class), + @ApiResponse( + code = 404, + message = "Not Found. There doesn't have an defined in app management " + + "configuration file." + + "query."), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while getting the lifecycle config.", + response = ErrorResponse.class) + }) + Response getLifecycleConfig(); + } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ConfigRetrieveAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ConfigRetrieveAPIImpl.java index fe7c97ef640..b00ee9a9b90 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ConfigRetrieveAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ConfigRetrieveAPIImpl.java @@ -20,7 +20,9 @@ package org.wso2.carbon.device.application.mgt.api.services.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.application.mgt.api.services.ConfigRetrieveAPI; +import org.wso2.carbon.device.application.mgt.common.config.LifecycleStateVertex; import org.wso2.carbon.device.application.mgt.common.config.UIConfiguration; +import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; import org.wso2.carbon.device.application.mgt.common.services.AppmDataHandler; import org.wso2.carbon.device.application.mgt.core.util.APIUtil; @@ -29,6 +31,9 @@ import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; +import java.util.HashMap; +import java.util.List; +import java.util.Map; /** * Implementation of ApplicationDTO Management related APIs. @@ -49,4 +54,24 @@ public class ConfigRetrieveAPIImpl implements ConfigRetrieveAPI { return Response.status(Response.Status.OK).entity(uiConfiguration).build(); } + @GET + @Override + @Consumes("application/json") + @Path("/lifecycle-config") + public Response getLifecycleConfig() { + AppmDataHandler dataHandler = APIUtil.getDataHandler(); + Map> verticesObject = new HashMap<>(); + Map> vertices = null; + try { + vertices = dataHandler.getLifecycleConfiguration().getAdjVertices(); + for (LifecycleStateVertex vt : vertices.keySet()) { + verticesObject.put(vt.getLabel(), vertices.get(vt)); + } + return Response.status(Response.Status.OK).entity(verticesObject).build(); + } catch (LifecycleManagementException e) { + String msg = "Error Occurred while accessing lifecycle manager."; + log.error(msg); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/config/LifecycleGraph.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/config/LifecycleGraph.java new file mode 100644 index 00000000000..9b3116c7a74 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/config/LifecycleGraph.java @@ -0,0 +1,70 @@ +package org.wso2.carbon.device.application.mgt.common.config;/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. 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. + */ + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class LifecycleGraph { + + private Map> adjVertices; + + public LifecycleGraph() { + this.adjVertices = new HashMap<>() ; + } + + public void addVertex(String label) { + adjVertices.putIfAbsent(new LifecycleStateVertex(label), new ArrayList<>()); + } + + public void addVertex(LifecycleStateVertex vertext) { + adjVertices.putIfAbsent(vertext, new ArrayList<>()); + } + + void removeVertex(String label) { + LifecycleStateVertex v = new LifecycleStateVertex(label); + adjVertices.values() + .stream() + .map(e -> e.remove(v)) + .collect(Collectors.toList()); + adjVertices.remove(new LifecycleStateVertex(label)); + } + + public void addEdge(String label1, String label2) { + LifecycleStateVertex v1 = new LifecycleStateVertex(label1); + LifecycleStateVertex v2 = new LifecycleStateVertex(label2); + adjVertices.get(v1).add(v2); +// adjVertices.get(v2).add(v1); + } + + public void removeEdge(String label1, String label2) { + LifecycleStateVertex v1 = new LifecycleStateVertex(label1); + LifecycleStateVertex v2 = new LifecycleStateVertex(label2); + List eV1 = adjVertices.get(v1); + List eV2 = adjVertices.get(v2); + if (eV1 != null) + eV1.remove(v2); + if (eV2 != null) + eV2.remove(v1); + } + + public Map> getAdjVertices() { + return adjVertices; + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/config/LifecycleStateVertex.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/config/LifecycleStateVertex.java new file mode 100644 index 00000000000..17743db9d80 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/config/LifecycleStateVertex.java @@ -0,0 +1,91 @@ +package org.wso2.carbon.device.application.mgt.common.config;/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. 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. + */ + +public class LifecycleStateVertex { + private String label; + private boolean isAppUpdatable; + private boolean isAppInstallable; + private boolean isInitialState; + private boolean isEndState; + + public LifecycleStateVertex(String label) { + this.label = label; + } + + public String getLabel() { + return label; + } + + public boolean isAppUpdatable() { + return isAppUpdatable; + } + + public void setAppUpdatable(boolean appUpdatable) { + isAppUpdatable = appUpdatable; + } + + public boolean isAppInstallable() { + return isAppInstallable; + } + + public void setAppInstallable(boolean appInstallable) { + isAppInstallable = appInstallable; + } + + public boolean isInitialState() { + return isInitialState; + } + + public void setInitialState(boolean initialState) { + isInitialState = initialState; + } + + public boolean isEndState() { + return isEndState; + } + + public void setEndState(boolean endState) { + isEndState = endState; + } + + @Override + public int hashCode(){ + return label == null ? 0 : label.hashCode(); + } + + // Overriding equals() to compare two LifecycleStateVertex objects + @Override + public boolean equals(Object o) { + + // If the object is compared with itself then return true + if (o == this) { + return true; + } + + /* Check if o is an instance of LifecycleStateVertex or not + "null instanceof [type]" also returns false */ + if (!(o instanceof LifecycleStateVertex)) { + return false; + } + + // typecast o to Complex so that we can compare data members + LifecycleStateVertex c = (LifecycleStateVertex) o; + + // Compare the equality of label name and return accordingly + return label.equals(c.label); + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/AppmDataHandler.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/AppmDataHandler.java index 508d7afa3b9..f633c2e3594 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/AppmDataHandler.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/AppmDataHandler.java @@ -17,8 +17,10 @@ package org.wso2.carbon.device.application.mgt.common.services; +import org.wso2.carbon.device.application.mgt.common.config.LifecycleGraph; import org.wso2.carbon.device.application.mgt.common.config.UIConfiguration; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; import java.io.InputStream; @@ -30,5 +32,7 @@ public interface AppmDataHandler { */ UIConfiguration getUIConfiguration(); + LifecycleGraph getLifecycleConfiguration() throws LifecycleManagementException; + InputStream getArtifactStream(String uuid, String artifactName) 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/impl/AppmDataHandlerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/AppmDataHandlerImpl.java index 9a0aee2efe3..5de4f7f1341 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/AppmDataHandlerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/AppmDataHandlerImpl.java @@ -20,8 +20,10 @@ package org.wso2.carbon.device.application.mgt.core.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.application.mgt.common.config.LifecycleGraph; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager; import org.wso2.carbon.device.application.mgt.common.services.AppmDataHandler; import org.wso2.carbon.device.application.mgt.common.config.UIConfiguration; @@ -30,6 +32,8 @@ import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagem 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.NotFoundException; +import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; +import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManager; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; import org.wso2.carbon.device.application.mgt.core.util.Constants; @@ -37,12 +41,16 @@ import java.io.InputStream; public class AppmDataHandlerImpl implements AppmDataHandler { - private UIConfiguration uiConfiguration; private static final Log log = LogFactory.getLog(AppmDataHandlerImpl.class); + private UIConfiguration uiConfiguration; + private LifecycleStateManager lifecycleStateManager; + public AppmDataHandlerImpl(UIConfiguration config) { this.uiConfiguration = config; + lifecycleStateManager = DataHolder.getInstance().getLifecycleStateManager(); + } @Override @@ -50,6 +58,11 @@ public class AppmDataHandlerImpl implements AppmDataHandler { return this.uiConfiguration; } + @Override + public LifecycleGraph getLifecycleConfiguration() throws LifecycleManagementException { + return lifecycleStateManager.getLifecyccleStateGraph(); + } + @Override public InputStream getArtifactStream(String uuid, String artifactName) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManager.java index 03598a7f43f..a33eaedab12 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManager.java @@ -19,6 +19,8 @@ package org.wso2.carbon.device.application.mgt.core.lifecycle; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.application.mgt.common.config.LifecycleGraph; +import org.wso2.carbon.device.application.mgt.common.config.LifecycleStateVertex; import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; import org.wso2.carbon.device.application.mgt.core.lifecycle.config.LifecycleState; @@ -62,6 +64,37 @@ public class LifecycleStateManager { } } + public LifecycleGraph getLifecyccleStateGraph() throws LifecycleManagementException { + LifecycleGraph lifecycleGraph = new LifecycleGraph(); + Map lifecycleStatesDup = lifecycleStates; + + for (State state : lifecycleStatesDup.values()) { + Set proceedingStateNames = state.getProceedingStates(); + String stateName = state.getState(); + LifecycleStateVertex lifecycleStateVertex = new LifecycleStateVertex(stateName); + if (isInitialState(stateName)) { + lifecycleStateVertex.setInitialState(true); + } + if (isUpdatableState(stateName)) { + lifecycleStateVertex.setAppUpdatable(true); + } + if (isEndState(stateName)) { + lifecycleStateVertex.setEndState(true); + } + if (isInstallableState(stateName)) { + lifecycleStateVertex.setAppInstallable(true); + } + lifecycleGraph.addVertex(lifecycleStateVertex); + if (proceedingStateNames != null) { + proceedingStateNames.forEach(proceedingStateName -> { + lifecycleGraph.addVertex(proceedingStateName); + lifecycleGraph.addEdge(stateName, proceedingStateName); + }); + } + } + return lifecycleGraph; + } + public Set getNextLifecycleStates(String currentLifecycleState) { return lifecycleStates.get(currentLifecycleState.toUpperCase()).getProceedingStates(); From 7ef9485b7d8ee5bd5a401240f4a7db44e27f2734 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Fri, 3 May 2019 21:05:46 +0530 Subject: [PATCH 10/11] Fix issues in lifecycle retrieving API --- .../services/impl/ConfigRetrieveAPIImpl.java | 17 +++----- .../device/application/mgt/common}/State.java | 2 +- .../mgt/common}/config/LifecycleState.java | 2 +- .../mgt/common/services/AppmDataHandler.java | 5 ++- .../mgt/core/config/Configuration.java | 2 +- .../mgt/core/impl/AppmDataHandlerImpl.java | 7 ++-- ...ApplicationManagementServiceComponent.java | 2 +- .../core/lifecycle/LifecycleStateManager.java | 39 ++++--------------- .../ConfigurationTest.java | 2 +- .../LifeCycleStateManagerTest.java | 4 +- .../LifecycleManagementTest.java | 2 +- 11 files changed, 29 insertions(+), 55 deletions(-) rename components/application-mgt/{org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle => org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common}/State.java (95%) rename components/application-mgt/{org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle => org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common}/config/LifecycleState.java (96%) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ConfigRetrieveAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ConfigRetrieveAPIImpl.java index b00ee9a9b90..65269f512ab 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ConfigRetrieveAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ConfigRetrieveAPIImpl.java @@ -20,7 +20,6 @@ package org.wso2.carbon.device.application.mgt.api.services.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.application.mgt.api.services.ConfigRetrieveAPI; -import org.wso2.carbon.device.application.mgt.common.config.LifecycleStateVertex; import org.wso2.carbon.device.application.mgt.common.config.UIConfiguration; import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; import org.wso2.carbon.device.application.mgt.common.services.AppmDataHandler; @@ -31,9 +30,6 @@ import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; -import java.util.HashMap; -import java.util.List; -import java.util.Map; /** * Implementation of ApplicationDTO Management related APIs. @@ -51,6 +47,11 @@ public class ConfigRetrieveAPIImpl implements ConfigRetrieveAPI { public Response getUiConfig() { AppmDataHandler dataHandler = APIUtil.getDataHandler(); UIConfiguration uiConfiguration = dataHandler.getUIConfiguration(); + if (uiConfiguration == null){ + String msg = "UI configuration is not initiated."; + log.error(msg); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } return Response.status(Response.Status.OK).entity(uiConfiguration).build(); } @@ -60,14 +61,8 @@ public class ConfigRetrieveAPIImpl implements ConfigRetrieveAPI { @Path("/lifecycle-config") public Response getLifecycleConfig() { AppmDataHandler dataHandler = APIUtil.getDataHandler(); - Map> verticesObject = new HashMap<>(); - Map> vertices = null; try { - vertices = dataHandler.getLifecycleConfiguration().getAdjVertices(); - for (LifecycleStateVertex vt : vertices.keySet()) { - verticesObject.put(vt.getLabel(), vertices.get(vt)); - } - return Response.status(Response.Status.OK).entity(verticesObject).build(); + return Response.status(Response.Status.OK).entity(dataHandler.getLifecycleConfiguration()).build(); } catch (LifecycleManagementException e) { String msg = "Error Occurred while accessing lifecycle manager."; log.error(msg); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/State.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/State.java similarity index 95% rename from components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/State.java rename to components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/State.java index 17aefa51477..cec1555baf5 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/State.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/State.java @@ -1,4 +1,4 @@ -package org.wso2.carbon.device.application.mgt.core.lifecycle; +package org.wso2.carbon.device.application.mgt.common; import java.util.HashSet; import java.util.List; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/config/LifecycleState.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/config/LifecycleState.java similarity index 96% rename from components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/config/LifecycleState.java rename to components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/config/LifecycleState.java index 7e971754700..298ec1c57a2 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/config/LifecycleState.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/config/LifecycleState.java @@ -1,4 +1,4 @@ -package org.wso2.carbon.device.application.mgt.core.lifecycle.config; +package org.wso2.carbon.device.application.mgt.common.config; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/AppmDataHandler.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/AppmDataHandler.java index f633c2e3594..7b8a49d1773 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/AppmDataHandler.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/AppmDataHandler.java @@ -17,12 +17,13 @@ package org.wso2.carbon.device.application.mgt.common.services; -import org.wso2.carbon.device.application.mgt.common.config.LifecycleGraph; +import org.wso2.carbon.device.application.mgt.common.State; import org.wso2.carbon.device.application.mgt.common.config.UIConfiguration; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; import java.io.InputStream; +import java.util.Map; public interface AppmDataHandler { /** @@ -32,7 +33,7 @@ public interface AppmDataHandler { */ UIConfiguration getUIConfiguration(); - LifecycleGraph getLifecycleConfiguration() throws LifecycleManagementException; + Map getLifecycleConfiguration() throws LifecycleManagementException; InputStream getArtifactStream(String uuid, String artifactName) 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/config/Configuration.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/config/Configuration.java index 364dd81ac41..bc4b706fede 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/config/Configuration.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/config/Configuration.java @@ -20,7 +20,7 @@ package org.wso2.carbon.device.application.mgt.core.config; import org.wso2.carbon.device.application.mgt.common.config.RatingConfiguration; import org.wso2.carbon.device.application.mgt.common.config.UIConfiguration; -import org.wso2.carbon.device.application.mgt.core.lifecycle.config.LifecycleState; +import org.wso2.carbon.device.application.mgt.common.config.LifecycleState; import java.util.List; import javax.xml.bind.annotation.XmlElement; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/AppmDataHandlerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/AppmDataHandlerImpl.java index 5de4f7f1341..9ea7d644f7a 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/AppmDataHandlerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/AppmDataHandlerImpl.java @@ -20,7 +20,7 @@ package org.wso2.carbon.device.application.mgt.core.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.device.application.mgt.common.config.LifecycleGraph; +import org.wso2.carbon.device.application.mgt.common.State; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException; import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; @@ -38,6 +38,7 @@ import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; import org.wso2.carbon.device.application.mgt.core.util.Constants; import java.io.InputStream; +import java.util.Map; public class AppmDataHandlerImpl implements AppmDataHandler { @@ -59,8 +60,8 @@ public class AppmDataHandlerImpl implements AppmDataHandler { } @Override - public LifecycleGraph getLifecycleConfiguration() throws LifecycleManagementException { - return lifecycleStateManager.getLifecyccleStateGraph(); + public Map getLifecycleConfiguration() throws LifecycleManagementException { + return lifecycleStateManager.getLifecycleConfig(); } @Override 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 ecf8280eb02..15810529f9a 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 @@ -32,7 +32,7 @@ import org.wso2.carbon.device.application.mgt.common.config.UIConfiguration; import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagementDAOFactory; import org.wso2.carbon.device.application.mgt.core.impl.AppmDataHandlerImpl; import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManager; -import org.wso2.carbon.device.application.mgt.core.lifecycle.config.LifecycleState; +import org.wso2.carbon.device.application.mgt.common.config.LifecycleState; import org.wso2.carbon.device.application.mgt.core.util.ApplicationManagementUtil; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.ndatasource.core.DataSourceService; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManager.java index a33eaedab12..078fce37e49 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManager.java @@ -19,11 +19,10 @@ package org.wso2.carbon.device.application.mgt.core.lifecycle; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.device.application.mgt.common.config.LifecycleGraph; -import org.wso2.carbon.device.application.mgt.common.config.LifecycleStateVertex; +import org.wso2.carbon.device.application.mgt.common.State; import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; -import org.wso2.carbon.device.application.mgt.core.lifecycle.config.LifecycleState; +import org.wso2.carbon.device.application.mgt.common.config.LifecycleState; import org.wso2.carbon.device.mgt.common.permission.mgt.PermissionManagementException; import org.wso2.carbon.device.mgt.core.permission.mgt.PermissionUtils; import org.wso2.carbon.device.mgt.core.search.mgt.Constants; @@ -64,35 +63,13 @@ public class LifecycleStateManager { } } - public LifecycleGraph getLifecyccleStateGraph() throws LifecycleManagementException { - LifecycleGraph lifecycleGraph = new LifecycleGraph(); - Map lifecycleStatesDup = lifecycleStates; - - for (State state : lifecycleStatesDup.values()) { - Set proceedingStateNames = state.getProceedingStates(); - String stateName = state.getState(); - LifecycleStateVertex lifecycleStateVertex = new LifecycleStateVertex(stateName); - if (isInitialState(stateName)) { - lifecycleStateVertex.setInitialState(true); - } - if (isUpdatableState(stateName)) { - lifecycleStateVertex.setAppUpdatable(true); - } - if (isEndState(stateName)) { - lifecycleStateVertex.setEndState(true); - } - if (isInstallableState(stateName)) { - lifecycleStateVertex.setAppInstallable(true); - } - lifecycleGraph.addVertex(lifecycleStateVertex); - if (proceedingStateNames != null) { - proceedingStateNames.forEach(proceedingStateName -> { - lifecycleGraph.addVertex(proceedingStateName); - lifecycleGraph.addEdge(stateName, proceedingStateName); - }); - } + public Map getLifecycleConfig() throws LifecycleManagementException { + if (lifecycleStates == null){ + String msg = "Lifecycle configuration in not initialized."; + log.error(msg); + throw new LifecycleManagementException(msg); } - return lifecycleGraph; + return lifecycleStates; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/ConfigurationTest.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/ConfigurationTest.java index cd6803a1cbf..8d982de6163 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/ConfigurationTest.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/ConfigurationTest.java @@ -21,7 +21,7 @@ import org.junit.Assert; import org.testng.annotations.Test; 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.application.mgt.core.lifecycle.config.LifecycleState; +import org.wso2.carbon.device.application.mgt.common.config.LifecycleState; import java.util.List; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/LifeCycleStateManagerTest.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/LifeCycleStateManagerTest.java index 7dbbfe33043..95069938afc 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/LifeCycleStateManagerTest.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/LifeCycleStateManagerTest.java @@ -1,8 +1,8 @@ package org.wso2.carbon.device.application.mgt.core; import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManager; -import org.wso2.carbon.device.application.mgt.core.lifecycle.State; -import org.wso2.carbon.device.application.mgt.core.lifecycle.config.LifecycleState; +import org.wso2.carbon.device.application.mgt.common.State; +import org.wso2.carbon.device.application.mgt.common.config.LifecycleState; import java.util.HashMap; import java.util.List; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/LifecycleManagementTest.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/LifecycleManagementTest.java index 4d610fe62a1..496b2318188 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/LifecycleManagementTest.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/LifecycleManagementTest.java @@ -7,7 +7,7 @@ import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManageme 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.application.mgt.core.lifecycle.LifecycleStateManager; -import org.wso2.carbon.device.application.mgt.core.lifecycle.config.LifecycleState; +import org.wso2.carbon.device.application.mgt.common.config.LifecycleState; import java.util.List; import java.util.Set; From d0f48a2c72ce719133a763c601515575f040667b Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Fri, 3 May 2019 21:10:03 +0530 Subject: [PATCH 11/11] Remove lifecycle graph classes --- .../mgt/common/config/LifecycleGraph.java | 70 -------------- .../common/config/LifecycleStateVertex.java | 91 ------------------- 2 files changed, 161 deletions(-) delete mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/config/LifecycleGraph.java delete mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/config/LifecycleStateVertex.java diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/config/LifecycleGraph.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/config/LifecycleGraph.java deleted file mode 100644 index 9b3116c7a74..00000000000 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/config/LifecycleGraph.java +++ /dev/null @@ -1,70 +0,0 @@ -package org.wso2.carbon.device.application.mgt.common.config;/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. - * - * Entgra (Pvt) Ltd. 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. - */ - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - -public class LifecycleGraph { - - private Map> adjVertices; - - public LifecycleGraph() { - this.adjVertices = new HashMap<>() ; - } - - public void addVertex(String label) { - adjVertices.putIfAbsent(new LifecycleStateVertex(label), new ArrayList<>()); - } - - public void addVertex(LifecycleStateVertex vertext) { - adjVertices.putIfAbsent(vertext, new ArrayList<>()); - } - - void removeVertex(String label) { - LifecycleStateVertex v = new LifecycleStateVertex(label); - adjVertices.values() - .stream() - .map(e -> e.remove(v)) - .collect(Collectors.toList()); - adjVertices.remove(new LifecycleStateVertex(label)); - } - - public void addEdge(String label1, String label2) { - LifecycleStateVertex v1 = new LifecycleStateVertex(label1); - LifecycleStateVertex v2 = new LifecycleStateVertex(label2); - adjVertices.get(v1).add(v2); -// adjVertices.get(v2).add(v1); - } - - public void removeEdge(String label1, String label2) { - LifecycleStateVertex v1 = new LifecycleStateVertex(label1); - LifecycleStateVertex v2 = new LifecycleStateVertex(label2); - List eV1 = adjVertices.get(v1); - List eV2 = adjVertices.get(v2); - if (eV1 != null) - eV1.remove(v2); - if (eV2 != null) - eV2.remove(v1); - } - - public Map> getAdjVertices() { - return adjVertices; - } -} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/config/LifecycleStateVertex.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/config/LifecycleStateVertex.java deleted file mode 100644 index 17743db9d80..00000000000 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/config/LifecycleStateVertex.java +++ /dev/null @@ -1,91 +0,0 @@ -package org.wso2.carbon.device.application.mgt.common.config;/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. - * - * Entgra (Pvt) Ltd. 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. - */ - -public class LifecycleStateVertex { - private String label; - private boolean isAppUpdatable; - private boolean isAppInstallable; - private boolean isInitialState; - private boolean isEndState; - - public LifecycleStateVertex(String label) { - this.label = label; - } - - public String getLabel() { - return label; - } - - public boolean isAppUpdatable() { - return isAppUpdatable; - } - - public void setAppUpdatable(boolean appUpdatable) { - isAppUpdatable = appUpdatable; - } - - public boolean isAppInstallable() { - return isAppInstallable; - } - - public void setAppInstallable(boolean appInstallable) { - isAppInstallable = appInstallable; - } - - public boolean isInitialState() { - return isInitialState; - } - - public void setInitialState(boolean initialState) { - isInitialState = initialState; - } - - public boolean isEndState() { - return isEndState; - } - - public void setEndState(boolean endState) { - isEndState = endState; - } - - @Override - public int hashCode(){ - return label == null ? 0 : label.hashCode(); - } - - // Overriding equals() to compare two LifecycleStateVertex objects - @Override - public boolean equals(Object o) { - - // If the object is compared with itself then return true - if (o == this) { - return true; - } - - /* Check if o is an instance of LifecycleStateVertex or not - "null instanceof [type]" also returns false */ - if (!(o instanceof LifecycleStateVertex)) { - return false; - } - - // typecast o to Complex so that we can compare data members - LifecycleStateVertex c = (LifecycleStateVertex) o; - - // Compare the equality of label name and return accordingly - return label.equals(c.label); - } -}