From f356b0df6b32a6a50da71ff216431e81f25092f3 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Mon, 13 May 2019 13:54:14 +0530 Subject: [PATCH] Improve APPM publisher APIs --- .../GenericApplicationDAOImpl.java | 8 +-- .../mgt/core/impl/ApplicationManagerImpl.java | 49 +++++++++++-------- .../ApplicationManagementPublisherAPI.java | 4 ++ ...ApplicationManagementPublisherAPIImpl.java | 5 +- 4 files changed, 41 insertions(+), 25 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 ddcdf6e0c2..fddb1c25ac 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 @@ -1031,7 +1031,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic try { Connection conn = this.getDBConnection(); List distinctTagIds = new ArrayList<>(); - String sql = "SELECT DISTINCT AP_APP_TAG.ID AS ID FROM AP_APP_TAG"; + String sql = "SELECT DISTINCT tm.AP_APP_TAG_ID AS ID FROM AP_APP_TAG_MAPPING tm"; try (PreparedStatement ps = conn.prepareStatement(sql)) { try (ResultSet rs = ps.executeQuery()) { while (rs.next()) { @@ -1325,7 +1325,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic Connection conn; String sql = "UPDATE " + "AP_APP_TAG tag " + - "SET tag.TAG_NAME = ? " + + "SET tag.TAG = ? " + "WHERE " + "tag.ID = ? AND " + "tag.TENANT_ID = ?"; @@ -1333,8 +1333,8 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setString(1, tagDTO.getTagName()); - stmt.setInt(1, tagDTO.getId()); - stmt.setInt(2, tenantId); + stmt.setInt(2, tagDTO.getId()); + stmt.setInt(3, tenantId); stmt.executeUpdate(); } } catch (DBConnectionException e) { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java index 6129424ca0..7cd4adb516 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 @@ -85,7 +85,6 @@ 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; @@ -663,28 +662,26 @@ public class ApplicationManagerImpl implements ApplicationManager { List filteringUnrestrictedRoles = filter.getUnrestrictedRoles(); if (!lifecycleStateManager.getEndState().equals(applicationDTO.getStatus())) { + //get application categories, tags and unrestricted roles. List appUnrestrictedRoles = visibilityDAO .getUnrestrictedRoles(applicationDTO.getId(), tenantId); + List appCategoryList = applicationDAO.getAppCategories(applicationDTO.getId(), tenantId); + List appTagList = applicationDAO.getAppTags(applicationDTO.getId(), tenantId); + + //Set application categories, tags and unrestricted roles to the application DTO. applicationDTO.setUnrestrictedRoles(appUnrestrictedRoles); + applicationDTO.setAppCategories(appCategoryList); + applicationDTO.setTags(appTagList); + if ((appUnrestrictedRoles.isEmpty() || hasUserRole(appUnrestrictedRoles, userName)) && ( filteringUnrestrictedRoles == null || filteringUnrestrictedRoles.isEmpty() || hasAppUnrestrictedRole(appUnrestrictedRoles, filteringUnrestrictedRoles, userName))) { if (filteringCategories != null && !filteringCategories.isEmpty()) { - List appCategoryList = applicationDAO - .getAppCategories(applicationDTO.getId(), tenantId); - applicationDTO.setAppCategories(appCategoryList); - boolean isAppCategory = filteringCategories.stream().anyMatch(appCategoryList::contains); - if (!isAppCategory) { - isSearchingApp = false; - } + isSearchingApp = filteringCategories.stream().anyMatch(appCategoryList::contains); } - if (filteringTags != null && !filteringTags.isEmpty()) { - List appTagList = applicationDAO.getAppTags(applicationDTO.getId(), tenantId); - boolean isAppTag = filteringTags.stream().anyMatch(appTagList::contains); - if (!isAppTag) { - isSearchingApp = false; - } + if (filteringTags != null && !filteringTags.isEmpty() && isSearchingApp) { + isSearchingApp = filteringTags.stream().anyMatch(appTagList::contains); } if (isSearchingApp) { filteredApplications.add(applicationDTO); @@ -701,16 +698,20 @@ public class ApplicationManagerImpl implements ApplicationManager { applicationDTO.setApplicationReleaseDTOs(filteredApplicationReleaseDTOs); } - for(ApplicationDTO appDTO : filteredApplications){ + for (ApplicationDTO appDTO : filteredApplications) { applications.add(appDtoToAppResponse(appDTO)); } - applicationList.setApplications(applications); + Pagination pagination = new Pagination(); + pagination.setCount(applications.size()); + pagination.setSize(applications.size()); + pagination.setOffset(filter.getOffset()); + pagination.setLimit(filter.getLimit()); + + applicationList.setApplications(applications); applicationList.setPagination(pagination); - applicationList.getPagination().setSize(filter.getOffset()); - applicationList.getPagination().setCount(applicationList.getApplications().size()); return applicationList; - } 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); @@ -2006,6 +2007,12 @@ public class ApplicationManagerImpl implements ApplicationManager { @Override public void updateTag(String oldTagName, String newTagName) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + if (StringUtils.isEmpty(oldTagName) || StringUtils.isEmpty(newTagName)) { + String msg = "Either old tag name or new tag name contains empty/null value. Hence please verify the " + + "request."; + log.error(msg); + throw new BadRequestException(msg); + } try { ConnectionManagerUtil.beginDBTransaction(); TagDTO tag = applicationDAO.getTagForTagName(oldTagName, tenantId); @@ -2080,10 +2087,11 @@ public class ApplicationManagerImpl implements ApplicationManager { } List applicationTags = this.applicationDAO.getAppTags(applicationDTO.getId(), tenantId); - List newApplicationTags = getDifference(applicationTags, tags); + List newApplicationTags = getDifference(tags, applicationTags); if (!newApplicationTags.isEmpty()) { List newTagIds = this.applicationDAO.getTagIdsForTagNames(newApplicationTags, tenantId); this.applicationDAO.addTagMapping(newTagIds, applicationDTO.getId(), tenantId); + ConnectionManagerUtil.commitDBTransaction(); } return Stream.concat(applicationTags.stream(), newApplicationTags.stream()) .collect(Collectors.toList()); @@ -2095,6 +2103,7 @@ public class ApplicationManagerImpl implements ApplicationManager { throw new BadRequestException(msg); } } catch (ApplicationManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); String msg = "Error occurred while accessing application tags. Application ID: " + appId; log.error(msg); throw new ApplicationManagementException(msg, 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 137ca4f241..8cf60a8430 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 @@ -922,6 +922,10 @@ public interface ApplicationManagementPublisherAPI { code = 200, message = "OK. \n Successfully update the registered tag.", response = ApplicationList.class), + @ApiResponse( + code = 400, + message = "Bad Request. \n " + + "Request contains unaccepted values for query parameters."), @ApiResponse( code = 404, message = "NOT FOUND. \n Couldn't found a tag for the given tag name.", 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 ea9bb2cc08..57b1e5c1e5 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 @@ -16,7 +16,6 @@ */ package org.wso2.carbon.device.application.mgt.publisher.api.services.impl; -import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.cxf.jaxrs.ext.multipart.Attachment; @@ -568,6 +567,10 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem applicationManager.updateTag(oldTagName, newTagName); String msg = "Tag " + oldTagName + " is updated to " + newTagName + " successfully."; return Response.status(Response.Status.OK).entity(msg).build(); + } catch (BadRequestException e) { + String msg = e.getMessage(); + log.error(msg); + return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); } catch (NotFoundException e) { String msg = e.getMessage(); log.error(msg);