Add tag managing APIs for app manager

merge-requests/93/head
lasanthaDLPDS 5 years ago
parent 1b0983dafb
commit b4643d84cd

@ -240,4 +240,7 @@ public interface ApplicationManager {
void updateTag(String oldTagName, String newTagName) throws ApplicationManagementException;
List<String> addTags(List<String> tags) throws ApplicationManagementException;
List<String> addApplicationTags(int appId, List<String> tags) throws ApplicationManagementException;
}

@ -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<String> addTags(List<String> tags) throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
try {
if (tags != null && !tags.isEmpty()) {
ConnectionManagerUtil.beginDBTransaction();
List<TagDTO> registeredTags = applicationDAO.getAllTags(tenantId);
List<String> registeredTagNames = new ArrayList<>();
for (TagDTO tagDTO : registeredTags) {
registeredTagNames.add(tagDTO.getTagName());
}
List<String> 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<String> addApplicationTags(int appId, List<String> tags) throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
try {
ApplicationDTO applicationDTO = getApplication(appId);
if (tags != null && !tags.isEmpty()) {
ConnectionManagerUtil.beginDBTransaction();
List<TagDTO> registeredTags = applicationDAO.getAllTags(tenantId);
List<String> registeredTagNames = new ArrayList<>();
for (TagDTO tagDTO : registeredTags) {
registeredTagNames.add(tagDTO.getTagName());
}
List<String> 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<String> applicationTags = this.applicationDAO.getAppTags(applicationDTO.getId(), tenantId);
List<String> newApplicationTags = getDifference(applicationTags, tags);
if (!newApplicationTags.isEmpty()) {
List<Integer> 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";

@ -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<String> 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<String> tagNames
);
@GET
@Path("/categories")
@Produces(MediaType.APPLICATION_JSON)

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

Loading…
Cancel
Save