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 { try {
Connection conn = this.getDBConnection(); Connection conn = this.getDBConnection();
List<Integer> distinctTagIds = new ArrayList<>(); 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 (PreparedStatement ps = conn.prepareStatement(sql)) {
try (ResultSet rs = ps.executeQuery()) { try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) { while (rs.next()) {
@ -1325,7 +1325,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
Connection conn; Connection conn;
String sql = "UPDATE " + String sql = "UPDATE " +
"AP_APP_TAG tag " + "AP_APP_TAG tag " +
"SET tag.TAG_NAME = ? " + "SET tag.TAG = ? " +
"WHERE " + "WHERE " +
"tag.ID = ? AND " + "tag.ID = ? AND " +
"tag.TENANT_ID = ?"; "tag.TENANT_ID = ?";
@ -1333,8 +1333,8 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
conn = this.getDBConnection(); conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, tagDTO.getTagName()); stmt.setString(1, tagDTO.getTagName());
stmt.setInt(1, tagDTO.getId()); stmt.setInt(2, tagDTO.getId());
stmt.setInt(2, tenantId); stmt.setInt(3, tenantId);
stmt.executeUpdate(); stmt.executeUpdate();
} }
} catch (DBConnectionException e) { } 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.user.api.UserStoreException;
import org.wso2.carbon.utils.multitenancy.MultitenantUtils; import org.wso2.carbon.utils.multitenancy.MultitenantUtils;
import javax.ws.rs.core.Response;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -663,28 +662,26 @@ public class ApplicationManagerImpl implements ApplicationManager {
List<String> filteringUnrestrictedRoles = filter.getUnrestrictedRoles(); List<String> filteringUnrestrictedRoles = filter.getUnrestrictedRoles();
if (!lifecycleStateManager.getEndState().equals(applicationDTO.getStatus())) { if (!lifecycleStateManager.getEndState().equals(applicationDTO.getStatus())) {
//get application categories, tags and unrestricted roles.
List<String> appUnrestrictedRoles = visibilityDAO List<String> appUnrestrictedRoles = visibilityDAO
.getUnrestrictedRoles(applicationDTO.getId(), tenantId); .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.setUnrestrictedRoles(appUnrestrictedRoles);
applicationDTO.setAppCategories(appCategoryList);
applicationDTO.setTags(appTagList);
if ((appUnrestrictedRoles.isEmpty() || hasUserRole(appUnrestrictedRoles, userName)) && ( if ((appUnrestrictedRoles.isEmpty() || hasUserRole(appUnrestrictedRoles, userName)) && (
filteringUnrestrictedRoles == null || filteringUnrestrictedRoles.isEmpty() filteringUnrestrictedRoles == null || filteringUnrestrictedRoles.isEmpty()
|| hasAppUnrestrictedRole(appUnrestrictedRoles, filteringUnrestrictedRoles, || hasAppUnrestrictedRole(appUnrestrictedRoles, filteringUnrestrictedRoles,
userName))) { userName))) {
if (filteringCategories != null && !filteringCategories.isEmpty()) { if (filteringCategories != null && !filteringCategories.isEmpty()) {
List<String> appCategoryList = applicationDAO isSearchingApp = filteringCategories.stream().anyMatch(appCategoryList::contains);
.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;
} }
if (filteringTags != null && !filteringTags.isEmpty() && isSearchingApp) {
isSearchingApp = filteringTags.stream().anyMatch(appTagList::contains);
} }
if (isSearchingApp) { if (isSearchingApp) {
filteredApplications.add(applicationDTO); filteredApplications.add(applicationDTO);
@ -704,11 +701,15 @@ public class ApplicationManagerImpl implements ApplicationManager {
for (ApplicationDTO appDTO : filteredApplications) { for (ApplicationDTO appDTO : filteredApplications) {
applications.add(appDtoToAppResponse(appDTO)); applications.add(appDtoToAppResponse(appDTO));
} }
applicationList.setApplications(applications);
Pagination pagination = new Pagination(); 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.setPagination(pagination);
applicationList.getPagination().setSize(filter.getOffset());
applicationList.getPagination().setCount(applicationList.getApplications().size());
return applicationList; return applicationList;
} catch (UserStoreException e) { } catch (UserStoreException e) {
throw new ApplicationManagementException( throw new ApplicationManagementException(
@ -2006,6 +2007,12 @@ public class ApplicationManagerImpl implements ApplicationManager {
@Override @Override
public void updateTag(String oldTagName, String newTagName) throws ApplicationManagementException { public void updateTag(String oldTagName, String newTagName) throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); 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 { try {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
TagDTO tag = applicationDAO.getTagForTagName(oldTagName, tenantId); 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> applicationTags = this.applicationDAO.getAppTags(applicationDTO.getId(), tenantId);
List<String> newApplicationTags = getDifference(applicationTags, tags); List<String> newApplicationTags = getDifference(tags, applicationTags);
if (!newApplicationTags.isEmpty()) { if (!newApplicationTags.isEmpty()) {
List<Integer> newTagIds = this.applicationDAO.getTagIdsForTagNames(newApplicationTags, tenantId); List<Integer> newTagIds = this.applicationDAO.getTagIdsForTagNames(newApplicationTags, tenantId);
this.applicationDAO.addTagMapping(newTagIds, applicationDTO.getId(), tenantId); this.applicationDAO.addTagMapping(newTagIds, applicationDTO.getId(), tenantId);
ConnectionManagerUtil.commitDBTransaction();
} }
return Stream.concat(applicationTags.stream(), newApplicationTags.stream()) return Stream.concat(applicationTags.stream(), newApplicationTags.stream())
.collect(Collectors.toList()); .collect(Collectors.toList());
@ -2095,6 +2103,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
throw new BadRequestException(msg); throw new BadRequestException(msg);
} }
} catch (ApplicationManagementDAOException e) { } catch (ApplicationManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
String msg = "Error occurred while accessing application tags. Application ID: " + appId; String msg = "Error occurred while accessing application tags. Application ID: " + appId;
log.error(msg); log.error(msg);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);

@ -922,6 +922,10 @@ public interface ApplicationManagementPublisherAPI {
code = 200, code = 200,
message = "OK. \n Successfully update the registered tag.", message = "OK. \n Successfully update the registered tag.",
response = ApplicationList.class), response = ApplicationList.class),
@ApiResponse(
code = 400,
message = "Bad Request. \n " +
"Request contains unaccepted values for query parameters."),
@ApiResponse( @ApiResponse(
code = 404, code = 404,
message = "NOT FOUND. \n Couldn't found a tag for the given tag name.", 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; 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.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.cxf.jaxrs.ext.multipart.Attachment; import org.apache.cxf.jaxrs.ext.multipart.Attachment;
@ -568,6 +567,10 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem
applicationManager.updateTag(oldTagName, newTagName); applicationManager.updateTag(oldTagName, newTagName);
String msg = "Tag " + oldTagName + " is updated to " + newTagName + " successfully."; String msg = "Tag " + oldTagName + " is updated to " + newTagName + " successfully.";
return Response.status(Response.Status.OK).entity(msg).build(); 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) { } catch (NotFoundException e) {
String msg = e.getMessage(); String msg = e.getMessage();
log.error(msg); log.error(msg);

Loading…
Cancel
Save