From a9a608bc84c62dfaeea6dc6479d7c2a0176c8a7f Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Wed, 8 May 2019 09:45:39 +0530 Subject: [PATCH] Add APIs for getting registered tags and categories --- .../mgt/common/response/Category.java | 39 ++++++++++++ .../application/mgt/common/response/Tag.java | 39 ++++++++++++ .../common/services/ApplicationManager.java | 8 ++- .../mgt/core/dao/ApplicationDAO.java | 4 ++ .../GenericApplicationDAOImpl.java | 53 ++++++++++++++++ .../mgt/core/impl/ApplicationManagerImpl.java | 53 ++++++++++++++++ .../core/lifecycle/LifecycleStateManager.java | 1 - .../ApplicationManagementPublisherAPI.java | 63 +++++++++++++++++-- ...ApplicationManagementPublisherAPIImpl.java | 34 ++++++++++ 9 files changed, 287 insertions(+), 7 deletions(-) create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/response/Category.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/response/Tag.java diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/response/Category.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/response/Category.java new file mode 100644 index 0000000000..859d0a8bdc --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/response/Category.java @@ -0,0 +1,39 @@ +package org.wso2.carbon.device.application.mgt.common.response;/* 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 Category { + + private String categoryName; + + private boolean isCategoryDeletable; + + public String getCategoryName() { + return categoryName; + } + + public void setCategoryName(String categoryName) { + this.categoryName = categoryName; + } + + public boolean isCategoryDeletable() { + return isCategoryDeletable; + } + + public void setCategoryDeletable(boolean categoryDeletable) { + isCategoryDeletable = categoryDeletable; + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/response/Tag.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/response/Tag.java new file mode 100644 index 0000000000..ffecf96b06 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/response/Tag.java @@ -0,0 +1,39 @@ +package org.wso2.carbon.device.application.mgt.common.response;/* 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 Tag { + + private String tagName; + + private boolean isTagDeletable; + + public String getTagName() { + return tagName; + } + + public void setTagName(String tagName) { + this.tagName = tagName; + } + + public boolean isTagDeletable() { + return isTagDeletable; + } + + public void setTagDeletable(boolean tagDeletable) { + isTagDeletable = tagDeletable; + } +} 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 afd081193d..27ca453866 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 @@ -29,6 +29,8 @@ import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManage 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.response.Category; +import org.wso2.carbon.device.application.mgt.common.response.Tag; 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; @@ -228,4 +230,8 @@ public interface ApplicationManager { void addAplicationCategories(List categories) throws ApplicationManagementException; - } + List getRegisteredTags() throws ApplicationManagementException; + + List getRegisteredCategories() throws ApplicationManagementException; + +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/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 25e6b575ad..1b2a42cb38 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 @@ -53,6 +53,8 @@ public interface ApplicationDAO { List getTagIdsForTagNames (List tagNames, int tenantId) throws ApplicationManagementDAOException; + List getDistinctTagIdsInTagMapping() throws ApplicationManagementDAOException; + void addTagMapping (List tagIds, int applicationId, int tenantId) throws ApplicationManagementDAOException; List getAppTags(int appId, int tenantId) throws ApplicationManagementDAOException; @@ -65,6 +67,8 @@ public interface ApplicationDAO { List getAllCategories(int tenantId) throws ApplicationManagementDAOException; + List getDistinctCategoryIdsInCategoryMapping() throws ApplicationManagementDAOException; + void addCategories(List categories, int tenantId) throws ApplicationManagementDAOException; void addCategoryMapping(List categoryIds, int applicationId, int tenantId) 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 7e362f85e7..53fb5c48f4 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 @@ -746,6 +746,34 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } } + @Override + public List getDistinctCategoryIdsInCategoryMapping() throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to get distinct category ids for given tag names"); + } + try { + Connection conn = this.getDBConnection(); + List distinctCategoryIds = new ArrayList<>(); + String sql = "SELECT DISTINCT AP_APP_CATEGORY.ID AS ID FROM AP_APP_CATEGORY"; + try (PreparedStatement ps = conn.prepareStatement(sql)) { + try (ResultSet rs = ps.executeQuery()) { + while (rs.next()) { + distinctCategoryIds.add(rs.getInt("ID")); + } + } + } + return distinctCategoryIds; + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException( + "Error occurred while obtaining the DB connection when getting distinct category ids in tag mapping", + e); + } catch (SQLException e) { + throw new ApplicationManagementDAOException( + "Error occurred while getting distinct category ids in tag mapping", e); + } + } + + @Override public void addCategories(List categories, int tenantId) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { @@ -872,6 +900,31 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } } + @Override + public List getDistinctTagIdsInTagMapping() throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to get distinct tag ids for given tag names"); + } + try { + Connection conn = this.getDBConnection(); + List distinctTagIds = new ArrayList<>(); + String sql = "SELECT DISTINCT AP_APP_TAG.ID AS ID FROM AP_APP_TAG"; + try (PreparedStatement ps = conn.prepareStatement(sql)) { + try (ResultSet rs = ps.executeQuery()) { + while (rs.next()) { + distinctTagIds.add(rs.getInt("ID")); + } + } + } + return distinctTagIds; + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException( + "Error occurred while obtaining the DB connection when getting distinct tag ids in tag mapping", e); + } catch (SQLException e) { + throw new ApplicationManagementDAOException("Error occurred while getting distinct tag ids in tag mapping", e); + } + } + public void addTagMapping (List tagIds, int applicationId, int tenantId) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Request received in DAO Layer to add tags"); 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 ed17944967..7edc941fe4 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 @@ -50,6 +50,8 @@ import org.wso2.carbon.device.application.mgt.common.exception.ResourceManagemen import org.wso2.carbon.device.application.mgt.common.exception.TransactionManagementException; 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.response.Category; +import org.wso2.carbon.device.application.mgt.common.response.Tag; 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; @@ -1845,6 +1847,57 @@ public class ApplicationManagerImpl implements ApplicationManager { } } + @Override + public List getRegisteredTags() throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + try { + ConnectionManagerUtil.openDBConnection(); + List tags = applicationDAO.getAllTags(tenantId); + List mappedTagIds = applicationDAO.getDistinctTagIdsInTagMapping(); + List responseTagList = new ArrayList<>(); + tags.forEach(tag -> { + Tag responseTag = new Tag(); + if (!mappedTagIds.contains(tag.getId())) { + responseTag.setTagDeletable(true); + } + responseTag.setTagName(tag.getTagName()); + responseTagList.add(responseTag); + }); + return responseTagList; + } catch (ApplicationManagementDAOException e) { + String msg = "Error occurred when getting registered tags from the system."; + log.error(msg); + throw new ApplicationManagementException(msg); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + public List getRegisteredCategories() throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + try { + ConnectionManagerUtil.openDBConnection(); + List categories = applicationDAO.getAllCategories(tenantId); + List mappedCategoryIds = applicationDAO.getDistinctCategoryIdsInCategoryMapping(); + List responseCategoryList = new ArrayList<>(); + categories.forEach(category -> { + Category responseCategory = new Category(); + if (!mappedCategoryIds.contains(category.getId())) { + responseCategory.setCategoryDeletable(true); + } + responseCategory.setCategoryName(category.getCategoryName()); + responseCategoryList.add(responseCategory); + }); + return responseCategoryList; + } catch (ApplicationManagementDAOException e) { + String msg = "Error occurred when getting registered tags from the system."; + log.error(msg); + throw new ApplicationManagementException(msg); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + private void validateFilter(Filter filter) throws BadRequestException { if (filter == null) { String msg = "Filter validation is failed, Filter shouldn't be null, hence please verify the request payload"; 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 552b42640a..82ea1f6bc9 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 @@ -29,7 +29,6 @@ import org.wso2.carbon.user.api.UserRealm; import org.wso2.carbon.user.api.UserStoreException; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; 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 dceaecac72..375858a8c2 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 @@ -772,11 +772,6 @@ public interface ApplicationManagementPublisherAPI { 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.", @@ -784,4 +779,62 @@ public interface ApplicationManagementPublisherAPI { }) Response getLifecycleConfig(); + @GET + @Path("/tags") + @Produces(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "get registered application tags", + notes = "This will get registered application tags", + tags = "Application Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:app:publisher:update") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully got Application tags.", + response = ApplicationList.class), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while getting application tags.", + response = ErrorResponse.class) + }) + Response getTags(); + + @GET + @Path("/categories") + @Produces(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "get registered application categories", + notes = "This will get registered application categories.", + tags = "Application Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:app:publisher:update") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully got application categories.", + response = ApplicationList.class), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while getting application categories.", + response = ErrorResponse.class) + }) + Response getCategories(); + } 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 fee5b16049..0d3bf2f695 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 @@ -27,6 +27,8 @@ import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManageme 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.response.Category; +import org.wso2.carbon.device.application.mgt.common.response.Tag; import org.wso2.carbon.device.application.mgt.common.services.AppmDataHandler; import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationReleaseWrapper; import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationUpdateWrapper; @@ -476,6 +478,38 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem } } + @GET + @Override + @Consumes("application/json") + @Path("/tags") + public Response getTags() { + ApplicationManager applicationManager = APIUtil.getApplicationManager(); + try { + List tags = applicationManager.getRegisteredTags(); + return Response.status(Response.Status.OK).entity(tags).build(); + } catch (ApplicationManagementException e) { + String msg = "Error Occurred while getting registered tags."; + log.error(msg); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } + + @GET + @Override + @Consumes("application/json") + @Path("/categories") + public Response getCategories() { + ApplicationManager applicationManager = APIUtil.getApplicationManager(); + try { + List categories = applicationManager.getRegisteredCategories(); + return Response.status(Response.Status.OK).entity(categories).build(); + } catch (ApplicationManagementException e) { + String msg = "Error Occurred while getting registered categories."; + log.error(msg); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } + /*** * Construct the screenshot list by evaluating the availability of each screenshot. *