Improve APPM publisher APIs

feature/appm-store/pbac
lasanthaDLPDS 6 years ago
parent b48fa9df7c
commit f356b0df6b

@ -1031,7 +1031,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
try {
Connection conn = this.getDBConnection();
List<Integer> distinctTagIds = new ArrayList<>();
String sql = "SELECT DISTINCT AP_APP_TAG.ID AS ID FROM AP_APP_TAG";
String sql = "SELECT DISTINCT tm.AP_APP_TAG_ID AS ID FROM AP_APP_TAG_MAPPING tm";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
@ -1325,7 +1325,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
Connection conn;
String sql = "UPDATE " +
"AP_APP_TAG tag " +
"SET tag.TAG_NAME = ? " +
"SET tag.TAG = ? " +
"WHERE " +
"tag.ID = ? AND " +
"tag.TENANT_ID = ?";
@ -1333,8 +1333,8 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, tagDTO.getTagName());
stmt.setInt(1, tagDTO.getId());
stmt.setInt(2, tenantId);
stmt.setInt(2, tagDTO.getId());
stmt.setInt(3, tenantId);
stmt.executeUpdate();
}
} catch (DBConnectionException e) {

@ -85,7 +85,6 @@ import org.wso2.carbon.user.api.UserRealm;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.utils.multitenancy.MultitenantUtils;
import javax.ws.rs.core.Response;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
@ -663,28 +662,26 @@ public class ApplicationManagerImpl implements ApplicationManager {
List<String> filteringUnrestrictedRoles = filter.getUnrestrictedRoles();
if (!lifecycleStateManager.getEndState().equals(applicationDTO.getStatus())) {
//get application categories, tags and unrestricted roles.
List<String> appUnrestrictedRoles = visibilityDAO
.getUnrestrictedRoles(applicationDTO.getId(), tenantId);
List<String> appCategoryList = applicationDAO.getAppCategories(applicationDTO.getId(), tenantId);
List<String> appTagList = applicationDAO.getAppTags(applicationDTO.getId(), tenantId);
//Set application categories, tags and unrestricted roles to the application DTO.
applicationDTO.setUnrestrictedRoles(appUnrestrictedRoles);
applicationDTO.setAppCategories(appCategoryList);
applicationDTO.setTags(appTagList);
if ((appUnrestrictedRoles.isEmpty() || hasUserRole(appUnrestrictedRoles, userName)) && (
filteringUnrestrictedRoles == null || filteringUnrestrictedRoles.isEmpty()
|| hasAppUnrestrictedRole(appUnrestrictedRoles, filteringUnrestrictedRoles,
userName))) {
if (filteringCategories != null && !filteringCategories.isEmpty()) {
List<String> appCategoryList = applicationDAO
.getAppCategories(applicationDTO.getId(), tenantId);
applicationDTO.setAppCategories(appCategoryList);
boolean isAppCategory = filteringCategories.stream().anyMatch(appCategoryList::contains);
if (!isAppCategory) {
isSearchingApp = false;
}
}
if (filteringTags != null && !filteringTags.isEmpty()) {
List<String> appTagList = applicationDAO.getAppTags(applicationDTO.getId(), tenantId);
boolean isAppTag = filteringTags.stream().anyMatch(appTagList::contains);
if (!isAppTag) {
isSearchingApp = false;
isSearchingApp = filteringCategories.stream().anyMatch(appCategoryList::contains);
}
if (filteringTags != null && !filteringTags.isEmpty() && isSearchingApp) {
isSearchingApp = filteringTags.stream().anyMatch(appTagList::contains);
}
if (isSearchingApp) {
filteredApplications.add(applicationDTO);
@ -704,11 +701,15 @@ public class ApplicationManagerImpl implements ApplicationManager {
for (ApplicationDTO appDTO : filteredApplications) {
applications.add(appDtoToAppResponse(appDTO));
}
applicationList.setApplications(applications);
Pagination pagination = new Pagination();
pagination.setCount(applications.size());
pagination.setSize(applications.size());
pagination.setOffset(filter.getOffset());
pagination.setLimit(filter.getLimit());
applicationList.setApplications(applications);
applicationList.setPagination(pagination);
applicationList.getPagination().setSize(filter.getOffset());
applicationList.getPagination().setCount(applicationList.getApplications().size());
return applicationList;
} catch (UserStoreException e) {
throw new ApplicationManagementException(
@ -2006,6 +2007,12 @@ public class ApplicationManagerImpl implements ApplicationManager {
@Override
public void updateTag(String oldTagName, String newTagName) throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
if (StringUtils.isEmpty(oldTagName) || StringUtils.isEmpty(newTagName)) {
String msg = "Either old tag name or new tag name contains empty/null value. Hence please verify the "
+ "request.";
log.error(msg);
throw new BadRequestException(msg);
}
try {
ConnectionManagerUtil.beginDBTransaction();
TagDTO tag = applicationDAO.getTagForTagName(oldTagName, tenantId);
@ -2080,10 +2087,11 @@ public class ApplicationManagerImpl implements ApplicationManager {
}
List<String> applicationTags = this.applicationDAO.getAppTags(applicationDTO.getId(), tenantId);
List<String> newApplicationTags = getDifference(applicationTags, tags);
List<String> newApplicationTags = getDifference(tags, applicationTags);
if (!newApplicationTags.isEmpty()) {
List<Integer> newTagIds = this.applicationDAO.getTagIdsForTagNames(newApplicationTags, tenantId);
this.applicationDAO.addTagMapping(newTagIds, applicationDTO.getId(), tenantId);
ConnectionManagerUtil.commitDBTransaction();
}
return Stream.concat(applicationTags.stream(), newApplicationTags.stream())
.collect(Collectors.toList());
@ -2095,6 +2103,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
throw new BadRequestException(msg);
}
} catch (ApplicationManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
String msg = "Error occurred while accessing application tags. Application ID: " + appId;
log.error(msg);
throw new ApplicationManagementException(msg, e);

@ -922,6 +922,10 @@ public interface ApplicationManagementPublisherAPI {
code = 200,
message = "OK. \n Successfully update the registered tag.",
response = ApplicationList.class),
@ApiResponse(
code = 400,
message = "Bad Request. \n " +
"Request contains unaccepted values for query parameters."),
@ApiResponse(
code = 404,
message = "NOT FOUND. \n Couldn't found a tag for the given tag name.",

@ -16,7 +16,6 @@
*/
package org.wso2.carbon.device.application.mgt.publisher.api.services.impl;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.cxf.jaxrs.ext.multipart.Attachment;
@ -568,6 +567,10 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem
applicationManager.updateTag(oldTagName, newTagName);
String msg = "Tag " + oldTagName + " is updated to " + newTagName + " 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 (NotFoundException e) {
String msg = e.getMessage();
log.error(msg);

Loading…
Cancel
Save