From 1a3d3215a049a38754a7d1e8647afe05bf2349ec Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Wed, 8 May 2019 17:59:13 +0530 Subject: [PATCH] Add API for delete an application tag --- .../common/services/ApplicationManager.java | 2 + .../mgt/core/dao/ApplicationDAO.java | 4 ++ .../GenericApplicationDAOImpl.java | 57 +++++++++++++++++++ .../mgt/core/impl/ApplicationManagerImpl.java | 24 ++++++++ .../ApplicationManagementPublisherAPI.java | 41 +++++++++++++ ...ApplicationManagementPublisherAPIImpl.java | 24 ++++++++ 6 files changed, 152 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 27ca453866..1aabd9597a 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 @@ -234,4 +234,6 @@ public interface ApplicationManager { List getRegisteredCategories() throws ApplicationManagementException; + void deleteTagMapping(int appId, String tagName) 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 1b2a42cb38..ce9860f0e8 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; + Integer getTagIdForTagName(String tagName, int tenantId) throws ApplicationManagementDAOException; + List getDistinctTagIdsInTagMapping() throws ApplicationManagementDAOException; void addTagMapping (List tagIds, int applicationId, int tenantId) throws ApplicationManagementDAOException; @@ -61,6 +63,8 @@ public interface ApplicationDAO { void deleteTagMapping (List tagIds, int applicationId, int tenantId) throws ApplicationManagementDAOException; + void deleteTagMapping (Integer tagId, int applicationId, int tenantId) throws ApplicationManagementDAOException; + void deleteTagMapping (int applicationId, 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 53fb5c48f4..557beae3a2 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 @@ -900,6 +900,35 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } } + @Override + public Integer getTagIdForTagName(String tagName, int tenantId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to get tag id for given tag name."); + } + try { + Connection conn = this.getDBConnection(); + String sql = "SELECT AP_APP_TAG.ID AS ID" + + " FROM AP_APP_TAG " + + "WHERE AP_APP_TAG.TAG = ? AND " + + "AP_APP_TAG.TENANT_ID = ?"; + try (PreparedStatement ps = conn.prepareStatement(sql)) { + ps.setString(1, tagName); + ps.setInt(2, tenantId); + try (ResultSet rs = ps.executeQuery()) { + if (rs.next()) { + return rs.getInt("ID"); + } + } + } + return -1; + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException( + "Error occurred while obtaining the DB connection when getting tag Id for given tag name", e); + } catch (SQLException e) { + throw new ApplicationManagementDAOException("SQL Error occurred while getting tag Id for tag name.", e); + } + } + @Override public List getDistinctTagIdsInTagMapping() throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { @@ -1020,6 +1049,34 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } } + @Override + public void deleteTagMapping (Integer tagId, int applicationId, int tenantId) throws ApplicationManagementDAOException{ + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to delete Tag mapping."); + } + Connection conn; + String sql = "DELETE 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); + stmt.executeUpdate(); + } + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException( + "Error occurred while obtaining the DB connection when deleting a tag mapping", e); + } catch (SQLException e) { + throw new ApplicationManagementDAOException("SQL Error occurred when deleting a tag mapping", e); + } + } + @Override public void deleteTagMapping (int applicationId, 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 7edc941fe4..73cdf02775 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 @@ -1873,6 +1873,7 @@ public class ApplicationManagerImpl implements ApplicationManager { } } + @Override public List getRegisteredCategories() throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); try { @@ -1898,6 +1899,29 @@ public class ApplicationManagerImpl implements ApplicationManager { } } + @Override + public void deleteTagMapping(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){ + String msg = "Couldn't found a tag for tag name " + tagName + "."; + log.error(msg); + throw new NotFoundException(msg); + } + applicationDAO.deleteTagMapping(tagId, applicationDTO.getId(), tenantId); + ConnectionManagerUtil.commitDBTransaction(); + } catch (ApplicationManagementDAOException e) { + String msg = "Error occurred when getting tag Ids or deleting tag mapping 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.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 375858a8c2..93a55d26b4 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 @@ -45,6 +45,7 @@ import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationWrapper; import java.util.List; import javax.validation.Valid; import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; @@ -808,6 +809,46 @@ public interface ApplicationManagementPublisherAPI { }) Response getTags(); + @DELETE + @Path("/{appId}/tags/{tagName}") + @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 delete Application tags.", + response = ApplicationList.class), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while deleting application tags.", + response = ErrorResponse.class) + }) + Response deleteTagMapping( + @ApiParam( + name = "appId", + value = "ID of the Application", + required = true) + @PathParam("appId") int applicationId, + @ApiParam( + name = "tagName", + value = "Tag Name", + required = true) + @PathParam("tagName") String tagName + ); + @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 0d3bf2f695..e4bed11f24 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 @@ -50,6 +50,7 @@ import java.util.Map; import javax.activation.DataHandler; import javax.validation.Valid; import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; @@ -494,6 +495,29 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem } } + @DELETE + @Override + @Consumes("application/json") + @Path("/{appId}/tags/{tagName}") + public Response deleteTagMapping( + @PathParam("appId") int appId, + @PathParam("tagName") String tagName) { + ApplicationManager applicationManager = APIUtil.getApplicationManager(); + try { + applicationManager.deleteTagMapping(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(); + } 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(); + } + } + @GET @Override @Consumes("application/json")