From c6440a779d5f324112dffa5c5a8e5733bc21dfb9 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Mon, 13 May 2019 01:52:24 +0530 Subject: [PATCH] Add support for application category list adding for an application. --- .../mgt/common/LifecycleChanger.java | 32 +++++ .../mgt/common/LifecycleState.java | 8 ++ .../mgt/common/dto/ApplicationDTO.java | 12 +- .../mgt/common/response/Application.java | 12 +- .../common/services/ApplicationManager.java | 6 +- .../common/wrapper/ApplicationWrapper.java | 12 +- .../GenericApplicationDAOImpl.java | 2 +- .../GenericLifecycleStateDAOImpl.java | 6 +- .../mgt/core/impl/ApplicationManagerImpl.java | 120 ++++++++++-------- .../dto/ApplicationsDTO.java | 7 +- .../management/ApplicationManagementTest.java | 5 +- .../ApplicationManagementPublisherAPI.java | 11 +- ...ApplicationManagementPublisherAPIImpl.java | 38 +++--- ...cationManagementPublisherAdminAPIImpl.java | 5 +- .../dbscripts/cdm/application-mgt/h2.sql | 2 +- 15 files changed, 176 insertions(+), 102 deletions(-) create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/LifecycleChanger.java diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/LifecycleChanger.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/LifecycleChanger.java new file mode 100644 index 0000000000..abb39642f9 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/LifecycleChanger.java @@ -0,0 +1,32 @@ +/* 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; + +public class LifecycleChanger { + + private String action; + private String reason; + + public String getAction() { return action; } + + public void setAction(String action) { this.action = action; } + + public String getReason() { return reason; } + + public void setReason(String reason) { this.reason = reason; } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/LifecycleState.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/LifecycleState.java index f499ba948e..11260d3505 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/LifecycleState.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/LifecycleState.java @@ -49,6 +49,10 @@ public class LifecycleState { value = "Timestamp of the lifecycle has been updated") private Timestamp updatedAt; + @ApiModelProperty(name = "resonForChange", + value = "Reason for the application release lifecycle change from previous state to current state.") + private String resonForChange; + public String getCurrentState() { return currentState; } @@ -88,4 +92,8 @@ public class LifecycleState { public void setNextStates(List nextStates) { this.nextStates = nextStates; } + + public String getResonForChange() { return resonForChange; } + + public void setResonForChange(String resonForChange) { this.resonForChange = resonForChange; } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/dto/ApplicationDTO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/dto/ApplicationDTO.java index d6eceeb30c..c91f53972f 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/dto/ApplicationDTO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/dto/ApplicationDTO.java @@ -41,11 +41,11 @@ public class ApplicationDTO { required = true) private String description; - @ApiModelProperty(name = "appCategory", + @ApiModelProperty(name = "appCategories", value = "Category of the application", required = true, example = "Educational, Gaming, Travel, Entertainment etc") - private String appCategory; + private List appCategories; @ApiModelProperty(name = "type", value = "Type of the application", @@ -108,13 +108,11 @@ public class ApplicationDTO { public void setName(String name) { this.name = name; } - public String getAppCategory() { - return appCategory; + public List getAppCategories() { + return appCategories; } - public void setAppCategory(String appCategory) { - this.appCategory = appCategory; - } + public void setAppCategories(List appCategories) { this.appCategories = appCategories; } public List getTags() { return tags; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/response/Application.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/response/Application.java index b1d536297b..2afa90f8d9 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/response/Application.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/response/Application.java @@ -35,11 +35,11 @@ public class Application { required = true) private String description; - @ApiModelProperty(name = "appCategory", + @ApiModelProperty(name = "appCategories", value = "CategoryDTO of the application", required = true, example = "Educational, Gaming, Travel, Entertainment etc") - private String appCategory; + private List appCategories; @ApiModelProperty(name = "type", value = "Type of the application", @@ -88,12 +88,12 @@ public class Application { public void setName(String name) { this.name = name; } - public String getAppCategory() { - return appCategory; + public List getAppCategories() { + return appCategories; } - public void setAppCategory(String appCategory) { - this.appCategory = appCategory; + public void setAppCategories(List appCategories) { + this.appCategories = appCategories; } public List getTags() { return tags; } 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 5a0e27ceb7..04144d47d1 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 @@ -20,6 +20,7 @@ package org.wso2.carbon.device.application.mgt.common.services; import org.apache.cxf.jaxrs.ext.multipart.Attachment; import org.wso2.carbon.device.application.mgt.common.ApplicationArtifact; +import org.wso2.carbon.device.application.mgt.common.LifecycleChanger; import org.wso2.carbon.device.application.mgt.common.dto.ApplicationDTO; import org.wso2.carbon.device.application.mgt.common.ApplicationList; import org.wso2.carbon.device.application.mgt.common.dto.ApplicationReleaseDTO; @@ -147,10 +148,11 @@ public interface ApplicationManager { * To get all the releases of a particular ApplicationDTO. * * @param releaseUuid UUID of the ApplicationDTO Release. - * @param stateName Lifecycle state to change the app + * @param lifecycleChanger Lifecycle changer that contains the action and the reson for the change. * @throws ApplicationManagementException ApplicationDTO Management Exception. */ - void changeLifecycleState(String releaseUuid, String stateName) throws ApplicationManagementException; + void changeLifecycleState(String releaseUuid, LifecycleChanger lifecycleChanger) + throws ApplicationManagementException; /** * To update release images such as icons, banner and screenshots. 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 3a02f1b3dc..02c05b3b0d 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 @@ -35,11 +35,11 @@ public class ApplicationWrapper { required = true) private String description; - @ApiModelProperty(name = "appCategory", + @ApiModelProperty(name = "appCategories", value = "CategoryDTO of the application", required = true, example = "Educational, Gaming, Travel, Entertainment etc") - private String appCategory; + private List appCategories; @ApiModelProperty(name = "type", value = "Type of the application", @@ -85,12 +85,10 @@ public class ApplicationWrapper { public void setName(String name) { this.name = name; } - public String getAppCategory() { - return appCategory; - } + public List getAppCategories() { return appCategories; } - public void setAppCategory(String appCategory) { - this.appCategory = appCategory; + public void setAppCategories(List appCategories) { + this.appCategories = appCategories; } public List getTags() { return tags; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/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 54ae39a719..ddcdf6e0c2 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 @@ -1478,7 +1478,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic application.setId(rs.getInt("APP_ID")); application.setName(rs.getString("APP_NAME")); application.setType(rs.getString("APP_TYPE")); - application.setAppCategory(rs.getString("APP_CATEGORY")); +// application.setAppCategories(rs.getString("APP_CATEGORY")); application.setSubType(rs.getString("SUB_TYPE")); application.setPaymentCurrency(rs.getString("CURRENCY")); // application.setIsRestricted(rs.getBoolean("RESTRICTED")); 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/lifecyclestate/GenericLifecycleStateDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/lifecyclestate/GenericLifecycleStateDAOImpl.java index f022bb7fea..73b1951b06 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/lifecyclestate/GenericLifecycleStateDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/lifecyclestate/GenericLifecycleStateDAOImpl.java @@ -174,9 +174,10 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif + "TENANT_ID, " + "UPDATED_BY, " + "UPDATED_AT, " + + "REASON, " + "AP_APP_RELEASE_ID, " + "AP_APP_ID) " - + "VALUES (?, ?, ?, ?, ?, ?, (SELECT AP_APP_ID FROM AP_APP_RELEASE WHERE ID = ?));"; + + "VALUES (?, ?, ?, ?, ?, ?, ?, (SELECT AP_APP_ID FROM AP_APP_RELEASE WHERE ID = ?));"; Calendar calendar = Calendar.getInstance(); Timestamp timestamp = new Timestamp(calendar.getTime().getTime()); @@ -187,8 +188,9 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif stmt.setInt(3, tenantId); stmt.setString(4, state.getUpdatedBy()); stmt.setTimestamp(5, timestamp); - stmt.setInt(6, appReleaseId); + stmt.setString(6, state.getResonForChange()); stmt.setInt(7, appReleaseId); + stmt.setInt(8, appReleaseId); stmt.executeUpdate(); } catch (DBConnectionException e) { log.error("Error occurred while obtaining the DB connection.", 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 6590116063..6129424ca0 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 @@ -29,6 +29,7 @@ import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.application.mgt.common.ApplicationArtifact; import org.wso2.carbon.device.application.mgt.common.ApplicationInstaller; +import org.wso2.carbon.device.application.mgt.common.LifecycleChanger; import org.wso2.carbon.device.application.mgt.common.Pagination; import org.wso2.carbon.device.application.mgt.common.config.RatingConfiguration; import org.wso2.carbon.device.application.mgt.common.dto.ApplicationDTO; @@ -84,6 +85,7 @@ import org.wso2.carbon.user.api.UserRealm; import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.utils.multitenancy.MultitenantUtils; +import javax.ws.rs.core.Response; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; @@ -96,7 +98,6 @@ import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.Optional; import java.util.Queue; import java.util.Set; import java.util.UUID; @@ -149,7 +150,7 @@ public class ApplicationManagerImpl implements ApplicationManager { ApplicationDTO applicationDTO; List unrestrictedRoles; - Optional category; + List categoryIds = new ArrayList<>(); List tags; //validating and verifying application data @@ -189,7 +190,7 @@ public class ApplicationManagerImpl implements ApplicationManager { } List registeredCategories = this.applicationDAO.getAllCategories(tenantId); - String categoryName = applicationWrapper.getAppCategory(); + List appCategories = applicationWrapper.getAppCategories(); if (registeredCategories.isEmpty()) { ConnectionManagerUtil.rollbackDBTransaction(); @@ -198,13 +199,21 @@ public class ApplicationManagerImpl implements ApplicationManager { 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); + for (String cat : appCategories) { + boolean isValidCategory = false; + for (CategoryDTO obj : registeredCategories) { + if (cat.equals(obj.getCategoryName())) { + categoryIds.add(obj.getId()); + isValidCategory = true; + break; + } + } + if (!isValidCategory) { + String msg = "Application Creating request contains invalid categories. Hence please verify the " + + "application creating payload."; + log.error(msg); + throw new BadRequestException(msg); + } } } catch (DBConnectionException e) { String msg = "Error occurred while getting database connection."; @@ -260,6 +269,9 @@ public class ApplicationManagerImpl implements ApplicationManager { if (log.isDebugEnabled()) { log.debug("New ApplicationDTO entry added to AP_APP table. App Id:" + appId); } + //add application categories + this.applicationDAO.addCategoryMapping(categoryIds, appId, tenantId); + //adding application unrestricted roles if (unrestrictedRoles != null && !unrestrictedRoles.isEmpty()) { this.visibilityDAO.addUnrestrictedRoles(unrestrictedRoles, appId, tenantId); @@ -269,15 +281,6 @@ public class ApplicationManagerImpl implements ApplicationManager { } } - /* - 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 - this way. - */ - List categoryIds = new ArrayList<>(); - categoryIds.add(category.get().getId()); - this.applicationDAO.addCategoryMapping(categoryIds, appId, tenantId); - //adding application tags if (tags != null && !tags.isEmpty()) { List registeredTags = applicationDAO.getAllTags(tenantId); @@ -623,6 +626,11 @@ public class ApplicationManagerImpl implements ApplicationManager { @Override public ApplicationList getApplications(Filter filter) throws ApplicationManagementException { + if (filter == null) { + String msg = "Request Payload is null"; + log.error(msg); + throw new BadRequestException(msg); + } int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); ApplicationList applicationList = new ApplicationList(); @@ -631,24 +639,23 @@ public class ApplicationManagerImpl implements ApplicationManager { List filteredApplications = new ArrayList<>(); DeviceType deviceType = null; - try { - //set default values - if (filter.getLimit() == 0) { - filter.setLimit(20); - } - String deviceTypename = filter.getDeviceType(); - if (!StringUtils.isEmpty(deviceTypename)) { - deviceType = getDeviceTypeData(deviceTypename); - } - if (deviceType == null) { - deviceType = new DeviceType(); - deviceType.setId(-1); - } + //set default values + if (!StringUtils.isEmpty(filter.getDeviceType())) { + deviceType = getDeviceTypeData(filter.getDeviceType()); + } + if (filter.getLimit() == 0) { + filter.setLimit(20); + } + + if (deviceType == null) { + deviceType = new DeviceType(); + deviceType.setId(-1); + } + try { 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 applicationDTO : appDTOs) { boolean isSearchingApp = true; List filteringTags = filter.getTags(); @@ -656,14 +663,18 @@ public class ApplicationManagerImpl implements ApplicationManager { List filteringUnrestrictedRoles = filter.getUnrestrictedRoles(); if (!lifecycleStateManager.getEndState().equals(applicationDTO.getStatus())) { - List appUnrestrictedRoles = visibilityDAO.getUnrestrictedRoles(applicationDTO.getId(), tenantId); + List appUnrestrictedRoles = visibilityDAO + .getUnrestrictedRoles(applicationDTO.getId(), tenantId); + applicationDTO.setUnrestrictedRoles(appUnrestrictedRoles); if ((appUnrestrictedRoles.isEmpty() || hasUserRole(appUnrestrictedRoles, userName)) && ( filteringUnrestrictedRoles == null || filteringUnrestrictedRoles.isEmpty() || hasAppUnrestrictedRole(appUnrestrictedRoles, filteringUnrestrictedRoles, userName))) { if (filteringCategories != null && !filteringCategories.isEmpty()) { - List appTagList = applicationDAO.getAppCategories(applicationDTO.getId(), tenantId); - boolean isAppCategory = filteringCategories.stream().anyMatch(appTagList::contains); + List appCategoryList = applicationDAO + .getAppCategories(applicationDTO.getId(), tenantId); + applicationDTO.setAppCategories(appCategoryList); + boolean isAppCategory = filteringCategories.stream().anyMatch(appCategoryList::contains); if (!isAppCategory) { isSearchingApp = false; } @@ -699,9 +710,7 @@ public class ApplicationManagerImpl implements ApplicationManager { applicationList.getPagination().setSize(filter.getOffset()); applicationList.getPagination().setCount(applicationList.getApplications().size()); return applicationList; - } catch (UnexpectedServerErrorException e){ - throw new ApplicationManagementException(e.getMessage(), e); - }catch (UserStoreException e) { + } catch (UserStoreException e) { throw new ApplicationManagementException( "User-store exception while checking whether the user " + userName + " of tenant " + tenantId + " has the publisher permission", e); @@ -843,9 +852,8 @@ public class ApplicationManagerImpl implements ApplicationManager { List tags = this.applicationDAO.getAppTags(appId, tenantId); List categories = this.applicationDAO.getAppCategories(appId, tenantId); applicationDTO.setTags(tags); - //todo when support to add multiple categories this has to be changed if (!categories.isEmpty()){ - applicationDTO.setAppCategory(categories.get(0)); + applicationDTO.setAppCategories(categories); } if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { return appDtoToAppResponse(applicationDTO); @@ -1595,10 +1603,17 @@ public class ApplicationManagerImpl implements ApplicationManager { } @Override - public void changeLifecycleState(String releaseUuid, String stateName) + public void changeLifecycleState(String releaseUuid, LifecycleChanger lifecycleChanger) throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + if (lifecycleChanger == null || StringUtils.isEmpty(lifecycleChanger.getAction())) { + String msg = "The Action is null or empty. Please verify the request."; + log.error(msg); + throw new BadRequestException(msg); + } + try { ConnectionManagerUtil.beginDBTransaction(); ApplicationReleaseDTO applicationReleaseDTO = this.applicationReleaseDAO @@ -1609,23 +1624,24 @@ public class ApplicationManagerImpl implements ApplicationManager { log.error(msg); throw new NotFoundException(msg); } - if (lifecycleStateManager - .isValidStateChange(applicationReleaseDTO.getCurrentState(), stateName, userName, tenantId)) { - if (lifecycleStateManager.isInstallableState(stateName) && applicationReleaseDAO + .isValidStateChange(applicationReleaseDTO.getCurrentState(), lifecycleChanger.getAction(), userName, + tenantId)) { + if (lifecycleStateManager.isInstallableState(lifecycleChanger.getAction()) && applicationReleaseDAO .hasExistInstallableAppRelease(applicationReleaseDTO.getUuid(), lifecycleStateManager.getInstallableState(), tenantId)) { String msg = "Installable application release is already registered for the application. " + "Therefore it is not permitted to change the lifecycle state from " - + applicationReleaseDTO.getCurrentState() + " to " + stateName; + + applicationReleaseDTO.getCurrentState() + " to " + lifecycleChanger.getAction(); log.error(msg); throw new ForbiddenException(msg); } LifecycleState lifecycleState = new LifecycleState(); - lifecycleState.setCurrentState(stateName); + lifecycleState.setCurrentState(lifecycleChanger.getAction()); lifecycleState.setPreviousState(applicationReleaseDTO.getCurrentState()); lifecycleState.setUpdatedBy(userName); - applicationReleaseDTO.setCurrentState(stateName); + lifecycleState.setResonForChange(lifecycleChanger.getReason()); + applicationReleaseDTO.setCurrentState(lifecycleChanger.getAction()); if (this.applicationReleaseDAO.updateRelease(applicationReleaseDTO, tenantId) == null) { String msg = "Application release updating is failed/."; log.error(msg); @@ -1635,7 +1651,7 @@ public class ApplicationManagerImpl implements ApplicationManager { ConnectionManagerUtil.commitDBTransaction(); } else { String msg = "Invalid lifecycle state transition from '" + applicationReleaseDTO.getCurrentState() + "'" - + " to '" + stateName + "'"; + + " to '" + lifecycleChanger.getAction() + "'"; log.error(msg); throw new ApplicationManagementException(msg); } @@ -2347,7 +2363,7 @@ public class ApplicationManagerImpl implements ApplicationManager { log.error(msg); throw new RequestValidatingException(msg); } - if (StringUtils.isEmpty(applicationWrapper.getAppCategory())) { + if (applicationWrapper.getAppCategories().isEmpty()) { String msg = "Application category can't be empty."; log.error(msg); throw new RequestValidatingException(msg); @@ -2459,7 +2475,7 @@ public class ApplicationManagerImpl implements ApplicationManager { ApplicationDTO applicationDTO = new ApplicationDTO(); applicationDTO.setName(applicationWrapper.getName()); applicationDTO.setDescription(applicationWrapper.getDescription()); - applicationDTO.setAppCategory(applicationWrapper.getAppCategory()); + applicationDTO.setAppCategories(applicationWrapper.getAppCategories()); applicationDTO.setType(applicationWrapper.getType()); applicationDTO.setSubType(applicationWrapper.getSubType()); applicationDTO.setPaymentCurrency(applicationWrapper.getPaymentCurrency()); @@ -2492,7 +2508,7 @@ public class ApplicationManagerImpl implements ApplicationManager { application.setId(applicationDTO.getId()); application.setName(applicationDTO.getName()); application.setDescription(applicationDTO.getDescription()); - application.setAppCategory(applicationDTO.getAppCategory()); + application.setAppCategories(applicationDTO.getAppCategories()); application.setType(applicationDTO.getType()); application.setSubType(applicationDTO.getSubType()); application.setPaymentCurrency(applicationDTO.getPaymentCurrency()); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/dto/ApplicationsDTO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/dto/ApplicationsDTO.java index 2e55d41696..f06f4f14c3 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/dto/ApplicationsDTO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/dto/ApplicationsDTO.java @@ -2,12 +2,17 @@ package org.wso2.carbon.device.application.mgt.core.dto; import org.wso2.carbon.device.application.mgt.common.dto.ApplicationDTO; +import java.util.ArrayList; +import java.util.List; + public class ApplicationsDTO { public static ApplicationDTO getApp1() { ApplicationDTO app = new ApplicationDTO(); + List categories = new ArrayList<>(); - app.setAppCategory("Test Category"); + categories.add("Test Category"); + app.setAppCategories(categories); app.setDescription("Test app Description"); app.setDeviceTypeId(1); app.setName("First Test App"); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/management/ApplicationManagementTest.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/management/ApplicationManagementTest.java index 5c3d4ffa6e..edfc733a28 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/management/ApplicationManagementTest.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/management/ApplicationManagementTest.java @@ -52,7 +52,10 @@ public class ApplicationManagementTest extends BaseTestCase { ApplicationWrapper applicationWrapper = new ApplicationWrapper(); - applicationWrapper.setAppCategory("Test Category"); + List categories = new ArrayList<>(); + categories.add("Test Category"); + applicationWrapper.setAppCategories(categories); + applicationWrapper.setDescription("Test Description"); applicationWrapper.setDeviceType("android"); applicationWrapper.setName("Test Application"); 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 b1c0fa78c6..e5e3bf582c 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 @@ -35,6 +35,7 @@ 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.LifecycleChanger; 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; @@ -729,6 +730,10 @@ public interface ApplicationManagementPublisherAPI { code = 400, message = "Bad Request. \n " + "Lifecycle State changing request contains unacceptable or vulnerable data"), + @ApiResponse( + code = 403, + message = "Don't have permission to move the lifecycle state of a given application release" + + " to the given lifecycle state."), @ApiResponse( code = 404, message = "NOT FOUND. \n Error occurred while adding new lifecycle state.", @@ -745,10 +750,10 @@ public interface ApplicationManagementPublisherAPI { required = true) @PathParam("uuid") String applicationUuid, @ApiParam( - name = "action", - value = "Changing lifecycle state", + name = "LifecycleChanger", + value = "Lifecycle Changer which contains the action and the reason for the lifecycle change.", required = true) - @QueryParam("action") String action + LifecycleChanger lifecycleChanger ); @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 f38fb5bdc7..ea9bb2cc08 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 @@ -35,6 +35,7 @@ import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationUpdateWr 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.UnexpectedServerErrorException; 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.common.exception.ApplicationManagementException; @@ -84,11 +85,15 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem .entity("Couldn't find any application for the requested query.").build(); } return Response.status(Response.Status.OK).entity(applications).build(); - } catch(BadRequestException e){ + } catch (BadRequestException e) { 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) { + } catch (UnexpectedServerErrorException e) { + String msg = "Error Occured when getting supported device types by Entgra IoTS"; + log.error(msg); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } catch (ApplicationManagementException e) { String msg = "Error occurred while getting the application list for publisher "; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); @@ -442,16 +447,19 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem @Path("/life-cycle/{uuid}") public Response addLifecycleState( @PathParam("uuid") String applicationUuid, - @QueryParam("action") String action) { + @Valid LifecycleChanger lifecycleChanger) { ApplicationManager applicationManager = APIUtil.getApplicationManager(); try { - if (StringUtils.isEmpty(action)) { - String msg = "The Action is null or empty. Please verify the request."; - log.error(msg); - return Response.status(Response.Status.BAD_REQUEST).build(); - } - - applicationManager.changeLifecycleState( applicationUuid, action); + applicationManager.changeLifecycleState(applicationUuid, lifecycleChanger); + } catch (BadRequestException e) { + String msg = "Request payload contains invalid data, hence veryfy the request payload."; + log.error(msg, e); + return Response.status(Response.Status.BAD_REQUEST).build(); + } catch (ForbiddenException e) { + String msg = "You are trying to move the application release into incompatible state for application " + + "which has application ID: " + applicationUuid; + log.error(msg, e); + return Response.status(Response.Status.FORBIDDEN).build(); } catch (NotFoundException e) { String msg = "Could,t find application release for application release uuid: " + applicationUuid; log.error(msg, e); @@ -579,9 +587,8 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem List tagNames) { ApplicationManager applicationManager = APIUtil.getApplicationManager(); try { - applicationManager.addTags(tagNames); - String msg = "New application tags are added successfully."; - return Response.status(Response.Status.OK).entity(msg).build(); + List tags = applicationManager.addTags(tagNames); + return Response.status(Response.Status.OK).entity(tags).build(); } catch (BadRequestException e) { String msg = e.getMessage(); log.error(msg); @@ -602,9 +609,8 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem List tagNames) { ApplicationManager applicationManager = APIUtil.getApplicationManager(); try { - applicationManager.addApplicationTags(appId, tagNames); - String msg = "New tags are added successfully, for application which has application ID: " + appId +"."; - return Response.status(Response.Status.OK).entity(msg).build(); + List applicationTags = applicationManager.addApplicationTags(appId, tagNames); + return Response.status(Response.Status.OK).entity(applicationTags).build(); } catch (NotFoundException e) { String msg = e.getMessage(); log.error(msg); 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 52db518a10..9890d037b8 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 @@ -130,9 +130,8 @@ public class ApplicationManagementPublisherAdminAPIImpl implements ApplicationMa List categoryNames) { ApplicationManager applicationManager = APIUtil.getApplicationManager(); try { - applicationManager.addCategories(categoryNames); - String msg = "New application categories are added successfully."; - return Response.status(Response.Status.OK).entity(msg).build(); + List categories = applicationManager.addCategories(categoryNames); + return Response.status(Response.Status.OK).entity(categories).build(); } catch (BadRequestException e) { String msg = e.getMessage(); log.error(msg); diff --git a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/h2.sql b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/h2.sql index e959c638cd..5450d3941d 100644 --- a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/h2.sql +++ b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/h2.sql @@ -80,7 +80,7 @@ CREATE TABLE IF NOT EXISTS AP_APP_LIFECYCLE_STATE( UPDATED_AT TIMESTAMP NOT NULL, AP_APP_RELEASE_ID INTEGER NOT NULL, AP_APP_ID INTEGER NOT NULL, - REASON VARCHAR(45) NULL, + REASON TEXT DEFAULT NULL, PRIMARY KEY (ID), CONSTRAINT fk_AP_APP_LIFECYCLE_STATE_AP_APP_RELEASE1 FOREIGN KEY (AP_APP_RELEASE_ID)