Add API for delete an application tag

feature/appm-store/pbac
lasanthaDLPDS 5 years ago
parent 69837ccd49
commit 1a3d3215a0

@ -234,4 +234,6 @@ public interface ApplicationManager {
List<Category> getRegisteredCategories() throws ApplicationManagementException;
void deleteTagMapping(int appId, String tagName) throws ApplicationManagementException;
}

@ -53,6 +53,8 @@ public interface ApplicationDAO {
List<Integer> getTagIdsForTagNames (List<String> tagNames, int tenantId) throws ApplicationManagementDAOException;
Integer getTagIdForTagName(String tagName, int tenantId) throws ApplicationManagementDAOException;
List<Integer> getDistinctTagIdsInTagMapping() throws ApplicationManagementDAOException;
void addTagMapping (List<Integer> tagIds, int applicationId, int tenantId) throws ApplicationManagementDAOException;
@ -61,6 +63,8 @@ public interface ApplicationDAO {
void deleteTagMapping (List<Integer> 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<String> getAppCategories (int appId, int tenantId) throws ApplicationManagementDAOException;

@ -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<Integer> 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()) {

@ -1873,6 +1873,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
}
}
@Override
public List<Category> 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";

@ -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)

@ -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")

Loading…
Cancel
Save