From ed30867f3ad298427597fb53818a3f734bb79fd4 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Thu, 9 May 2019 15:49:56 +0530 Subject: [PATCH] Add APIs which are related to application tag management --- .../common/services/ApplicationManager.java | 9 +- .../mgt/core/dao/ApplicationDAO.java | 18 +- .../GenericApplicationDAOImpl.java | 177 ++++++++++++++++-- .../mgt/core/impl/ApplicationManagerImpl.java | 97 +++++++++- .../ApplicationManagementPublisherAPI.java | 92 ++++++++- ...pplicationManagementPublisherAdminAPI.java | 35 ++++ ...ApplicationManagementPublisherAPIImpl.java | 59 +++++- ...cationManagementPublisherAdminAPIImpl.java | 23 +++ 8 files changed, 481 insertions(+), 29 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 1aabd9597a..4628aa0bae 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 @@ -227,13 +227,18 @@ public interface ApplicationManager { void validateBinaryArtifact(Attachment binaryFile, String applicationType) throws RequestValidatingException; - void addAplicationCategories(List categories) throws ApplicationManagementException; List getRegisteredTags() throws ApplicationManagementException; List getRegisteredCategories() throws ApplicationManagementException; - void deleteTagMapping(int appId, String tagName) throws ApplicationManagementException; + void deleteApplicationTag(int appId, String tagName) throws ApplicationManagementException; + + void deleteTag(String tagName) throws ApplicationManagementException; + + void deleteUnusedTag(String tagName) throws ApplicationManagementException; + + void updateTag(String oldTagName, String newTagName) 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 ce9860f0e8..d35b474d25 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,7 +53,7 @@ public interface ApplicationDAO { List getTagIdsForTagNames (List tagNames, int tenantId) throws ApplicationManagementDAOException; - Integer getTagIdForTagName(String tagName, int tenantId) throws ApplicationManagementDAOException; + TagDTO getTagForTagName(String tagName, int tenantId) throws ApplicationManagementDAOException; List getDistinctTagIdsInTagMapping() throws ApplicationManagementDAOException; @@ -61,11 +61,21 @@ public interface ApplicationDAO { List getAppTags(int appId, int tenantId) throws ApplicationManagementDAOException; - void deleteTagMapping (List tagIds, int applicationId, int tenantId) throws ApplicationManagementDAOException; + boolean hasTagMapping(int tagId, int appId, int tenantId) throws ApplicationManagementDAOException; - void deleteTagMapping (Integer tagId, int applicationId, int tenantId) throws ApplicationManagementDAOException; + boolean hasTagMapping(int tagId, int tenantId) throws ApplicationManagementDAOException; - void deleteTagMapping (int applicationId, int tenantId) throws ApplicationManagementDAOException; + void deleteApplicationTags(List tagIds, int applicationId, int tenantId) throws ApplicationManagementDAOException; + + void deleteApplicationTags(Integer tagId, int applicationId, int tenantId) throws ApplicationManagementDAOException; + + void deleteApplicationTags(int applicationId, int tenantId) throws ApplicationManagementDAOException; + + void deleteTagMapping(int tagId, int tenantId) throws ApplicationManagementDAOException; + + void deleteTag(int tagId, int tenantId) throws ApplicationManagementDAOException; + + void updateTag(TagDTO tagDTO, int tenantId) throws ApplicationManagementDAOException; List getAppCategories (int appId, 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 557beae3a2..006614ed6f 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 @@ -901,7 +901,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } @Override - public Integer getTagIdForTagName(String tagName, int tenantId) throws ApplicationManagementDAOException { + public TagDTO getTagForTagName(String tagName, int tenantId) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Request received in DAO Layer to get tag id for given tag name."); } @@ -916,11 +916,14 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic ps.setInt(2, tenantId); try (ResultSet rs = ps.executeQuery()) { if (rs.next()) { - return rs.getInt("ID"); + TagDTO tagDTO = new TagDTO(); + tagDTO.setId(rs.getInt("ID")); + tagDTO.setTagName(tagName); + return tagDTO; } } } - return -1; + return null; } catch (DBConnectionException e) { throw new ApplicationManagementDAOException( "Error occurred while obtaining the DB connection when getting tag Id for given tag name", e); @@ -1019,7 +1022,69 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } @Override - public void deleteTagMapping (List tagIds, int applicationId, int tenantId) throws ApplicationManagementDAOException{ + public boolean hasTagMapping (int tagId, int applicationId, int tenantId) throws ApplicationManagementDAOException{ + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to verify whether tag is associated with an application."); + } + Connection conn; + String sql = "SELECT tm.AP_APP_ID AS ID " + + "FROM AP_APP_TAG_MAPPING tm " + + "WHERE " + + "tm.AP_APP_TAG_ID = ? AND " + + "tm.AP_APP_ID = ? AND " + + "tm.TENANT_ID = ?"; + try { + conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tagId); + stmt.setInt(2, applicationId); + stmt.setInt(3, tenantId); + try (ResultSet rs = stmt.executeQuery()) { + return rs.next(); + } + } + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException( + "Error occurred while obtaining the DB connection when verifying the existence of a tag mapping", + e); + } catch (SQLException e) { + throw new ApplicationManagementDAOException("Error occurred when verifying the existence of a tag mapping.", + e); + } + } + + @Override + public boolean hasTagMapping (int tagId, int tenantId) throws ApplicationManagementDAOException{ + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to verify whether tag is associated with at least one application."); + } + Connection conn; + String sql = "SELECT tm.AP_APP_ID AS ID " + + "FROM AP_APP_TAG_MAPPING tm " + + "WHERE " + + "tm.AP_APP_TAG_ID = ? AND " + + "tm.TENANT_ID = ?"; + try { + conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tagId); + stmt.setInt(2, tenantId); + try (ResultSet rs = stmt.executeQuery()) { + return rs.next(); + } + } + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException( + "Error occurred while obtaining the DB connection when verifying the existence of a tag mapping", + e); + } catch (SQLException e) { + throw new ApplicationManagementDAOException("Error occurred when verifying the existence of a tag mapping.", + e); + } + } + + @Override + public void deleteApplicationTags(List tagIds, int applicationId, int tenantId) throws ApplicationManagementDAOException{ if (log.isDebugEnabled()) { log.debug("Request received in DAO Layer to delete Tag mappings."); } @@ -1050,7 +1115,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } @Override - public void deleteTagMapping (Integer tagId, int applicationId, int tenantId) throws ApplicationManagementDAOException{ + public void deleteApplicationTags(Integer tagId, int applicationId, int tenantId) throws ApplicationManagementDAOException{ if (log.isDebugEnabled()) { log.debug("Request received in DAO Layer to delete Tag mapping."); } @@ -1078,9 +1143,9 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } @Override - public void deleteTagMapping (int applicationId, int tenantId) throws ApplicationManagementDAOException{ + public void deleteApplicationTags(int applicationId, int tenantId) throws ApplicationManagementDAOException{ if (log.isDebugEnabled()) { - log.debug("Request received in DAO Layer to delete Tag mappings."); + log.debug("Request received in DAO Layer to delete application tags."); } Connection conn; String sql = "DELETE FROM " @@ -1090,21 +1155,109 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic + "tm.TENANT_ID = ?"; try { conn = this.getDBConnection(); - try (PreparedStatement stmt = conn.prepareStatement(sql)){ + try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, applicationId); stmt.setInt(2, tenantId); stmt.executeUpdate(); } } catch (DBConnectionException e) { throw new ApplicationManagementDAOException( - "Error occurred while obtaining the DB connection when deleting tag mapping of application ID: " - + applicationId , e); + "Error occurred while obtaining the DB connection when deleting application tags for application ID: " + + applicationId, e); } catch (SQLException e) { - throw new ApplicationManagementDAOException("Error occurred when deleting tag mapping of application ID: " - + applicationId, e); + throw new ApplicationManagementDAOException( + "Error occurred when deleting application tags for application ID: " + applicationId, e); } } + @Override + public void deleteTagMapping(int tagId, int tenantId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to delete Tag mappings."); + } + Connection conn; + String sql = "DELETE FROM " + + "AP_APP_TAG_MAPPING tm " + + "WHERE " + + "tm.AP_APP_ID = ? AND " + + "tm.TENANT_ID = ?"; + try { + conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tagId); + stmt.setInt(2, tenantId); + stmt.executeUpdate(); + } + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException( + "Error occurred while obtaining the DB connection when deleting tag mapping of tag ID: " + tagId, + e); + } catch (SQLException e) { + throw new ApplicationManagementDAOException("Error occurred when deleting tag mapping of tag ID: " + tagId, + e); + } + } + + @Override + public void deleteTag(int tagId, int tenantId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to delete Tag mappings."); + } + Connection conn; + String sql = "DELETE FROM " + + "AP_APP_TAG tag " + + "WHERE " + + "tag.ID = ? AND " + + "tag.TENANT_ID = ?"; + try { + conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tagId); + stmt.setInt(2, tenantId); + stmt.executeUpdate(); + } + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException( + "Error occurred while obtaining the DB connection when deleting tag which has ID: " + tagId, + e); + } catch (SQLException e) { + throw new ApplicationManagementDAOException("Error occurred when deleting tag which has ID: " + tagId, + e); + } + } + + @Override + public void updateTag(TagDTO tagDTO, int tenantId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to update the Tag."); + } + Connection conn; + String sql = "UPDATE " + + "AP_APP_TAG tag " + + "SET tag.TAG_NAME = ? " + + "WHERE " + + "tag.ID = ? AND " + + "tag.TENANT_ID = ?"; + try { + conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1, tagDTO.getTagName()); + stmt.setInt(1, tagDTO.getId()); + stmt.setInt(2, tenantId); + stmt.executeUpdate(); + } + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException( + "Error occurred while obtaining the DB connection when updating tag which has ID: " + tagDTO + .getId(), e); + } catch (SQLException e) { + throw new ApplicationManagementDAOException( + "Error occurred when updating tag which has ID: " + tagDTO.getId(), e); + } + } + + + @Override public List getAppCategories(int appId, int tenantId) throws ApplicationManagementDAOException { 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 73cdf02775..8280402398 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 @@ -1201,7 +1201,7 @@ public class ApplicationManagerImpl implements ApplicationManager { } this.lifecycleStateDAO.deleteLifecycleStates(deletingAppReleaseIds); this.applicationReleaseDAO.deleteReleases(deletingAppReleaseIds); - this.applicationDAO.deleteTagMapping(applicationId, tenantId); + this.applicationDAO.deleteApplicationTags(applicationId, tenantId); this.applicationDAO.deleteCategoryMapping(applicationId, tenantId); this.applicationDAO.deleteApplication(applicationId, tenantId); ConnectionManagerUtil.commitDBTransaction(); @@ -1809,7 +1809,7 @@ public class ApplicationManagerImpl implements ApplicationManager { } if (!removingTagList.isEmpty()) { List removingTagIds = this.applicationDAO.getTagIdsForTagNames(removingTagList, tenantId); - this.applicationDAO.deleteTagMapping(removingTagIds, applicationId, tenantId); + this.applicationDAO.deleteApplicationTags(removingTagIds, applicationId, tenantId); applicationDAO.deleteTags(removingTagList, applicationId, tenantId); } } @@ -1900,21 +1900,104 @@ public class ApplicationManagerImpl implements ApplicationManager { } @Override - public void deleteTagMapping(int appId, String tagName) throws ApplicationManagementException { + public void deleteApplicationTag(int appId, String tagName) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); try { ApplicationDTO applicationDTO = getApplication(appId); ConnectionManagerUtil.beginDBTransaction(); - int tagId = applicationDAO.getTagIdForTagName(tagName, tenantId); - if (tagId == -1){ + TagDTO tag = applicationDAO.getTagForTagName(tagName, tenantId); + if (tag == null){ String msg = "Couldn't found a tag for tag name " + tagName + "."; log.error(msg); throw new NotFoundException(msg); } - applicationDAO.deleteTagMapping(tagId, applicationDTO.getId(), tenantId); + if (applicationDAO.hasTagMapping(tag.getId(), applicationDTO.getId(), tenantId)){ + applicationDAO.deleteApplicationTags(tag.getId(), applicationDTO.getId(), tenantId); + ConnectionManagerUtil.commitDBTransaction(); + } else { + String msg = "Tag " + tagName + " is not an application tag. Application ID: " + appId; + log.error(msg); + throw new BadRequestException(msg); + } + } catch (ApplicationManagementDAOException e) { + String msg = "Error occurred when getting tag Id or deleting tag mapping from the system."; + log.error(msg); + throw new ApplicationManagementException(msg); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + @Override + public void deleteTag(String tagName) throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + try { + ConnectionManagerUtil.beginDBTransaction(); + TagDTO tag = applicationDAO.getTagForTagName(tagName, tenantId); + if (tag == null){ + String msg = "Couldn't found a tag for tag name " + tagName + "."; + log.error(msg); + throw new NotFoundException(msg); + } + if (applicationDAO.hasTagMapping(tag.getId(), tenantId)){ + applicationDAO.deleteTagMapping(tag.getId(), tenantId); + } + applicationDAO.deleteTag(tag.getId(), tenantId); + ConnectionManagerUtil.commitDBTransaction(); + } catch (ApplicationManagementDAOException e) { + String msg = "Error occurred when getting tag Id or deleting the tag from the system."; + log.error(msg); + throw new ApplicationManagementException(msg); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + @Override + public void deleteUnusedTag(String tagName) throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + try { + ConnectionManagerUtil.beginDBTransaction(); + TagDTO tag = applicationDAO.getTagForTagName(tagName, tenantId); + if (tag == null){ + String msg = "Couldn't found a tag for tag name " + tagName + "."; + log.error(msg); + throw new NotFoundException(msg); + } + if (applicationDAO.hasTagMapping(tag.getId(), tenantId)){ + String msg = + "Tag " + tagName + " is used for applications. Hence it is not permitted to delete the tag " + + tagName; + log.error(msg); + throw new ForbiddenException(msg); + } + applicationDAO.deleteTag(tag.getId(), tenantId); + ConnectionManagerUtil.commitDBTransaction(); + } catch (ApplicationManagementDAOException e) { + String msg = "Error occurred when getting tag Ids or deleting the tag from the system."; + log.error(msg); + throw new ApplicationManagementException(msg); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + @Override + public void updateTag(String oldTagName, String newTagName) throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + try { + ConnectionManagerUtil.beginDBTransaction(); + TagDTO tag = applicationDAO.getTagForTagName(oldTagName, tenantId); + if (tag == null){ + String msg = "Couldn't found a tag for tag name " + oldTagName + "."; + log.error(msg); + throw new NotFoundException(msg); + } + tag.setTagName(newTagName); + applicationDAO.updateTag(tag, tenantId); ConnectionManagerUtil.commitDBTransaction(); } catch (ApplicationManagementDAOException e) { - String msg = "Error occurred when getting tag Ids or deleting tag mapping from the system."; + String msg = "Error occurred when getting tag Ids or deleting the tag from the system."; log.error(msg); throw new ApplicationManagementException(msg); } finally { 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 93a55d26b4..18c1e4adc4 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 @@ -831,12 +831,16 @@ public interface ApplicationManagementPublisherAPI { code = 200, message = "OK. \n Successfully delete Application tags.", response = ApplicationList.class), + @ApiResponse( + code = 400, + message = "Bad Request. \n " + + "Given tag is not an associated tag for the given application."), @ApiResponse( code = 500, message = "Internal Server Error. \n Error occurred while deleting application tags.", response = ErrorResponse.class) }) - Response deleteTagMapping( + Response deleteApplicationTag( @ApiParam( name = "appId", value = "ID of the Application", @@ -849,6 +853,92 @@ public interface ApplicationManagementPublisherAPI { @PathParam("tagName") String tagName ); + @DELETE + @Path("/tags/{tagName}") + @Produces(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "DELETE", + value = "Delete application tag", + notes = "This will delete application tag", + tags = "Application Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:app:publisher:update") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully delete registered tag.", + response = ApplicationList.class), + @ApiResponse( + code = 403, + message = "Don't have permission to delete the application tag."), + @ApiResponse( + code = 404, + message = "NOT FOUND. \n Couldn't found a tag for the given tag name.", + response = ErrorResponse.class), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while deleting registered tag.", + response = ErrorResponse.class) + }) + Response deleteUnusedTag( + @ApiParam( + name = "tagName", + value = "Tag Name", + required = true) + @PathParam("tagName") String tagName + ); + + @PUT + @Path("/tags/{oldTagName}") + @Produces(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "PUT", + value = "update an application tag", + notes = "This will update application tag", + tags = "Application Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:app:publisher:update") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully update the registered tag.", + response = ApplicationList.class), + @ApiResponse( + code = 404, + message = "NOT FOUND. \n Couldn't found a tag for the given tag name.", + response = ErrorResponse.class), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while updating registered tag.", + response = ErrorResponse.class) + }) + Response modifyTagName( + @ApiParam( + name = "oldTagName", + value = "Existing Tag Name", + required = true) + @PathParam("oldTagName") String oldTagName, + @ApiParam( + name = "tag", + value = "Modifying Tag Name", + required = true) + @QueryParam("tag") String newTagName + ); + @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/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 69ea2d9cbc..e45e3fee71 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 @@ -151,4 +151,39 @@ public interface ApplicationManagementPublisherAdminAPI { value = "application ID", required = true) @PathParam("appId") int applicatioId); + + @DELETE + @Path("/tags/{tagName}") + @Produces(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "Delete application tag", + notes = "This will delete application tag", + tags = "Application Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:admin:app:publisher:update") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully delete registered tag.", + response = ApplicationList.class), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while deleting registered tag.", + response = ErrorResponse.class) + }) + Response deleteTag( + @ApiParam( + name = "tagName", + value = "Tag Name", + required = true) + @PathParam("tagName") String tagName + ); } 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 e4bed11f24..30fafccb65 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 @@ -499,18 +499,22 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem @Override @Consumes("application/json") @Path("/{appId}/tags/{tagName}") - public Response deleteTagMapping( + public Response deleteApplicationTag( @PathParam("appId") int appId, @PathParam("tagName") String tagName) { ApplicationManager applicationManager = APIUtil.getApplicationManager(); try { - applicationManager.deleteTagMapping(appId, tagName); + applicationManager.deleteApplicationTag(appId, tagName); String msg = "Tag " + tagName + " is deleted successfully."; return Response.status(Response.Status.OK).entity(msg).build(); } catch (NotFoundException e) { String msg = e.getMessage(); log.error(msg); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + return Response.status(Response.Status.NOT_FOUND).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 deleting registered tag."; log.error(msg); @@ -518,6 +522,55 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem } } + @DELETE + @Override + @Consumes("application/json") + @Path("/tags/{tagName}") + public Response deleteUnusedTag( + @PathParam("tagName") String tagName) { + ApplicationManager applicationManager = APIUtil.getApplicationManager(); + try { + applicationManager.deleteUnusedTag(tagName); + String msg = "Tag " + tagName + " is deleted successfully."; + 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 (ForbiddenException e) { + String msg = e.getMessage(); + log.error(msg); + return Response.status(Response.Status.FORBIDDEN).entity(msg).build(); + } catch (ApplicationManagementException e) { + String msg = "Error Occurred while deleting unused tag."; + log.error(msg); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } + + @PUT + @Override + @Consumes("application/json") + @Path("/tags/{oldTagName}") + public Response modifyTagName( + @PathParam("oldTagName") String oldTagName, + @QueryParam("tag") String newTagName) { + ApplicationManager applicationManager = APIUtil.getApplicationManager(); + try { + applicationManager.updateTag(oldTagName, newTagName); + String msg = "Tag " + oldTagName + " is updated to " + newTagName + " successfully."; + 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 updating registered tag."; + log.error(msg); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } + @GET @Override @Consumes("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/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 6f2525fe3b..322d591357 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 @@ -27,6 +27,7 @@ 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.admin.ApplicationManagementPublisherAdminAPI; +import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.Path; import javax.ws.rs.PathParam; @@ -94,4 +95,26 @@ public class ApplicationManagementPublisherAdminAPIImpl implements ApplicationMa } } + @DELETE + @Override + @Consumes("application/json") + @Path("/tags/{tagName}") + public Response deleteTag( + @PathParam("tagName") String tagName) { + ApplicationManager applicationManager = APIUtil.getApplicationManager(); + try { + applicationManager.deleteTag(tagName); + String msg = "Tag " + tagName + " is deleted successfully."; + 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 deleting registered tag."; + log.error(msg); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } + }