From b4643d84cd34a5dfefb08956408fac0d6cd863b2 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Fri, 10 May 2019 17:10:35 +0530 Subject: [PATCH] Add tag managing APIs for app manager --- .../common/services/ApplicationManager.java | 3 + .../mgt/core/impl/ApplicationManagerImpl.java | 81 ++++++++++++++++++ .../ApplicationManagementPublisherAPI.java | 85 +++++++++++++++++++ ...ApplicationManagementPublisherAPIImpl.java | 46 ++++++++++ 4 files changed, 215 insertions(+) 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 6c6676d18f..e3006717bc 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 @@ -240,4 +240,7 @@ public interface ApplicationManager { void updateTag(String oldTagName, String newTagName) throws ApplicationManagementException; + List addTags(List tags) throws ApplicationManagementException; + + List addApplicationTags(int appId, List tags) 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/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 047db1adb7..3ad1b3e557 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 @@ -101,6 +101,7 @@ import java.util.Queue; import java.util.Set; import java.util.UUID; import java.util.stream.Collectors; +import java.util.stream.Stream; /** * Default Concrete implementation of Application Management related implementations. @@ -2009,6 +2010,86 @@ public class ApplicationManagerImpl implements ApplicationManager { } } + public List addTags(List tags) throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + try { + if (tags != null && !tags.isEmpty()) { + ConnectionManagerUtil.beginDBTransaction(); + List registeredTags = applicationDAO.getAllTags(tenantId); + List registeredTagNames = new ArrayList<>(); + + for (TagDTO tagDTO : registeredTags) { + registeredTagNames.add(tagDTO.getTagName()); + } + List newTags = getDifference(tags, registeredTagNames); + if (!newTags.isEmpty()) { + this.applicationDAO.addTags(newTags, tenantId); + ConnectionManagerUtil.commitDBTransaction(); + if (log.isDebugEnabled()) { + log.debug("New tags entries are added to the AP_APP_TAG table."); + } + } + return Stream.concat(registeredTagNames.stream(), newTags.stream()).collect(Collectors.toList()); + } else{ + String msg = "Tag list is either null of empty. In order to add new tags, tag list should be a list of " + + "Stings. Therefore please verify the payload."; + log.error(msg); + throw new BadRequestException(msg); + } + } catch (ApplicationManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + String msg = "Error occurred either getting registered tags or adding new tags."; + log.error(msg); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + public List addApplicationTags(int appId, List tags) throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + try { + ApplicationDTO applicationDTO = getApplication(appId); + if (tags != null && !tags.isEmpty()) { + ConnectionManagerUtil.beginDBTransaction(); + List registeredTags = applicationDAO.getAllTags(tenantId); + List registeredTagNames = new ArrayList<>(); + + for (TagDTO tagDTO : registeredTags) { + registeredTagNames.add(tagDTO.getTagName()); + } + List newTags = getDifference(tags, registeredTagNames); + if (!newTags.isEmpty()) { + this.applicationDAO.addTags(newTags, tenantId); + if (log.isDebugEnabled()) { + log.debug("New tags entries are added to AP_APP_TAG table. App Id:" + applicationDTO.getId()); + } + } + + List applicationTags = this.applicationDAO.getAppTags(applicationDTO.getId(), tenantId); + List newApplicationTags = getDifference(applicationTags, tags); + if (!newApplicationTags.isEmpty()) { + List newTagIds = this.applicationDAO.getTagIdsForTagNames(newApplicationTags, tenantId); + this.applicationDAO.addTagMapping(newTagIds, applicationDTO.getId(), tenantId); + } + return Stream.concat(applicationTags.stream(), newApplicationTags.stream()) + .collect(Collectors.toList()); + } else { + String msg = "Tag list is either null or empty. In order to add new tags for application which has " + + "application ID: " + appId +", tag list should be a list of Stings. Therefore please " + + "verify the payload."; + log.error(msg); + throw new BadRequestException(msg); + } + } catch (ApplicationManagementDAOException e) { + String msg = "Error occurred while accessing application tags. Application ID: " + appId; + log.error(msg); + throw new ApplicationManagementException(msg, e); + } 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.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 2c3cc24a67..b1c0fa78c6 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 @@ -939,6 +939,91 @@ public interface ApplicationManagementPublisherAPI { @QueryParam("to") String newTagName ); + @POST + @Path("/tags") + @Produces(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "POST", + value = "Add new tags.", + notes = "This will add new tags for the system", + tags = "Application Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:app:publisher:update") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully add tags.", + response = ApplicationList.class), + @ApiResponse( + code = 400, + message = "Bad Request. \n Tag adding request contains unacceptable payload."), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while adding new tags.", + response = ErrorResponse.class) + }) + Response addTags( + @ApiParam( + name = "oldTagName", + value = "Existing Tag Name", + required = true) + List tagNames + ); + + @POST + @Path("/{appId}/tags") + @Produces(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "POST", + value = "Add new application tags", + notes = "This will add new 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 add application tags.", + response = ApplicationList.class), + @ApiResponse( + code = 400, + message = "Bad Request. \n Application tag adding request contains unacceptable payload."), + @ApiResponse( + code = 404, + message = "NOT FOUND. \n Couldn't found an application for the given application id.", + response = ErrorResponse.class), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while adding new application tags.", + response = ErrorResponse.class) + }) + Response addApplicationTags( + @ApiParam( + name = "oldTagName", + value = "Existing Tag Name", + required = true) + @PathParam("appId") int appId, + @ApiParam( + name = "appId", + value = "application Id", + required = true) + List tagNames + ); + @GET @Path("/categories") @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 d152f6f266..6fc60bccf3 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,6 +16,7 @@ */ package org.wso2.carbon.device.application.mgt.publisher.api.services.impl; +import org.apache.axiom.attachments.utils.BAAInputStream; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -571,6 +572,51 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem } } + @POST + @Override + @Consumes("application/json") + @Path("/tags") + public Response addTags( + 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(); + } catch (BadRequestException e) { + String msg = e.getMessage(); + log.error(msg); + return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); + } catch (ApplicationManagementException e) { + String msg = "Error Occurred while adding new tag."; + log.error(msg); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } + + @POST + @Override + @Consumes("application/json") + @Path("/{appId}/tags") + public Response addApplicationTags( + @PathParam("appId") int appId, + 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(); + } catch (NotFoundException e) { + String msg = e.getMessage(); + log.error(msg); + return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); + } catch (ApplicationManagementException e) { + String msg = "Error Occurred while adding new tags for application which has application ID: " + appId + "."; + log.error(msg); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } + @GET @Override @Consumes("application/json")