Search in sources :

Example 1 with Selector

use of com.cloudant.client.api.query.Selector in project sw360 by eclipse.

the class UserRepository method queryViewWithPagination.

private Map<PaginationData, List<User>> queryViewWithPagination(PaginationData pageData) {
    final int rowsPerPage = pageData.getRowsPerPage();
    Map<PaginationData, List<User>> result = Maps.newHashMap();
    List<User> users = Lists.newArrayList();
    final boolean ascending = pageData.isAscending();
    final int sortColumnNo = pageData.getSortColumnNumber();
    String query = null;
    final Selector typeSelector = eq("type", "user");
    final Selector emptySecondaryDepartmentsAndRolesSelector = or(Expression.exists("secondaryDepartmentsAndRoles", false), eq("secondaryDepartmentsAndRoles", ""));
    QueryBuilder qb = new QueryBuilder(typeSelector);
    if (rowsPerPage != -1) {
        qb.limit(rowsPerPage);
    }
    qb.skip(pageData.getDisplayStart());
    switch(sortColumnNo) {
        case -1:
        case 2:
            qb = qb.useIndex("byEmailUser");
            qb = ascending ? qb.sort(Sort.asc("email")) : qb.sort(Sort.desc("email"));
            query = qb.build();
            break;
        case 0:
            qb = qb.useIndex("byFirstName");
            qb = ascending ? qb.sort(Sort.asc("givenname")) : qb.sort(Sort.desc("givenname"));
            query = qb.build();
            break;
        case 1:
            qb = qb.useIndex("byLastName");
            qb = ascending ? qb.sort(Sort.asc("lastname")) : qb.sort(Sort.desc("lastname"));
            query = qb.build();
            break;
        case 3:
            qb = qb.useIndex("byDepartment");
            qb = ascending ? qb.sort(Sort.asc("department")) : qb.sort(Sort.desc("department"));
            query = qb.build();
            break;
        case 4:
            qb = qb.useIndex("byUserGroup");
            qb = ascending ? qb.sort(Sort.asc("userGroup")) : qb.sort(Sort.desc("userGroup"));
            query = qb.build();
            break;
        case 5:
            if (ascending) {
                qb.skip(0);
            }
            qb = qb.useIndex("bySecondaryDepartmentsAndRoles");
            qb = ascending ? qb.sort(Sort.asc("secondaryDepartmentsAndRoles")) : qb.sort(Sort.desc("secondaryDepartmentsAndRoles"));
            query = qb.build();
            break;
        default:
            break;
    }
    try {
        QueryResult<User> queryResult = getConnector().getQueryResult(query, User.class);
        users = queryResult.getDocs();
        if (sortColumnNo == 5) {
            final Selector selectorSecondaryGroupsAndRoles = and(typeSelector, emptySecondaryDepartmentsAndRolesSelector);
            QueryBuilder emptySecondaryGroupsAndRolesQb = new QueryBuilder(selectorSecondaryGroupsAndRoles);
            emptySecondaryGroupsAndRolesQb.skip(pageData.getDisplayStart());
            if (rowsPerPage != -1) {
                emptySecondaryGroupsAndRolesQb.limit(rowsPerPage);
            }
            QueryResult<User> queryResultWithoutSorting = getConnector().getQueryResult(emptySecondaryGroupsAndRolesQb.build(), User.class);
            List<User> userList = queryResultWithoutSorting.getDocs();
            if (ascending) {
                userList.addAll(users);
                users = userList;
            } else {
                users.addAll(userList);
            }
        }
    } catch (Exception e) {
        log.error("Error getting users", e);
    }
    result.put(pageData, users);
    return result;
}
Also used : PaginationData(org.eclipse.sw360.datahandler.thrift.PaginationData) User(org.eclipse.sw360.datahandler.thrift.users.User) List(java.util.List) QueryBuilder(com.cloudant.client.api.query.QueryBuilder) IOException(java.io.IOException) Selector(com.cloudant.client.api.query.Selector)

Example 2 with Selector

use of com.cloudant.client.api.query.Selector in project sw360 by eclipse.

the class ProjectRepository method getAccessibleProjectsSummary.

public Map<PaginationData, List<Project>> getAccessibleProjectsSummary(User user, PaginationData pageData) {
    final int rowsPerPage = pageData.getRowsPerPage();
    final boolean ascending = pageData.isAscending();
    final int sortColumnNo = pageData.getSortColumnNumber();
    final String requestingUserEmail = user.getEmail();
    final String userBU = getBUFromOrganisation(user.getDepartment());
    List<Project> projects = new ArrayList<>();
    Map<PaginationData, List<Project>> result = Maps.newHashMap();
    String query = null;
    final Selector typeSelector = eq("type", "project");
    final Selector private_visibility_Selector = eq("visbility", "PRIVATE");
    final Selector createdBySelector = eq("createdBy", requestingUserEmail);
    final Selector getAllPrivateProjects = and(private_visibility_Selector, createdBySelector);
    final Selector everyone_visibility_Selector = eq("visbility", "EVERYONE");
    final Selector isAProjectResponsible = eq("projectResponsible", requestingUserEmail);
    final Selector isALeadArchitect = eq("leadArchitect", requestingUserEmail);
    final Selector isAModerator = PredicatedOperation.elemMatch("moderators", PredicateExpression.eq(requestingUserEmail));
    final Selector isAContributor = PredicatedOperation.elemMatch("contributors", PredicateExpression.eq(requestingUserEmail));
    final Selector meAndModorator_visibility_Selector = eq("visbility", "ME_AND_MODERATORS");
    final Selector isUserBelongToMeAndModerator = and(meAndModorator_visibility_Selector, or(createdBySelector, isAProjectResponsible, isALeadArchitect, isAModerator, isAContributor));
    final Selector buAndModorator_visibility_Selector = eq("visbility", "BUISNESSUNIT_AND_MODERATORS");
    final Selector userBuSelector = eq("businessUnit", userBU);
    boolean isClearingAdmin = PermissionUtils.isUserAtLeast(UserGroup.CLEARING_ADMIN, user);
    Selector isUserBelongToBuAndModerator = null;
    Selector[] buSelectors = null;
    Map<String, Set<UserGroup>> secondaryDepartmentsAndRoles = user.getSecondaryDepartmentsAndRoles();
    if (!CommonUtils.isNullOrEmptyMap(secondaryDepartmentsAndRoles)) {
        Set<String> secondaryUgs = secondaryDepartmentsAndRoles.keySet();
        Set<String> secondaryBus = secondaryUgs.stream().map(SW360Utils::getBUFromOrganisation).collect(Collectors.toSet());
        buSelectors = new Selector[secondaryBus.size() + 2];
        int index = 0;
        for (String secondaryBU : secondaryBus) {
            Selector buselector = eq("businessUnit", secondaryBU);
            buSelectors[index] = buselector;
            index++;
        }
    } else {
        buSelectors = new Selector[2];
    }
    buSelectors[buSelectors.length - 2] = isUserBelongToMeAndModerator;
    buSelectors[buSelectors.length - 1] = userBuSelector;
    isUserBelongToBuAndModerator = and(buAndModorator_visibility_Selector, or(buSelectors));
    Selector finalSelector = null;
    if (isClearingAdmin) {
        finalSelector = and(typeSelector, or(getAllPrivateProjects, everyone_visibility_Selector, isUserBelongToMeAndModerator, buAndModorator_visibility_Selector));
    } else {
        finalSelector = and(typeSelector, or(getAllPrivateProjects, everyone_visibility_Selector, isUserBelongToMeAndModerator, isUserBelongToBuAndModerator));
    }
    QueryBuilder qb = new QueryBuilder(finalSelector);
    if (rowsPerPage != -1) {
        qb.limit(rowsPerPage);
    }
    qb.skip(pageData.getDisplayStart());
    ViewRequestBuilder queryView = null;
    switch(sortColumnNo) {
        case 0:
            qb = qb.useIndex("byName");
            qb = ascending ? qb.sort(Sort.asc("name")) : qb.sort(Sort.desc("name"));
            query = qb.build();
            break;
        case 1:
            qb = qb.useIndex("byDesc");
            qb = ascending ? qb.sort(Sort.asc("description")) : qb.sort(Sort.desc("description"));
            query = qb.build();
            break;
        case 2:
            qb = qb.useIndex("byProjectResponsible");
            qb = ascending ? qb.sort(Sort.asc("projectResponsible")) : qb.sort(Sort.desc("projectResponsible"));
            query = qb.build();
            break;
        case 3:
        case 4:
            queryView = getConnector().createQuery(Project.class, "byState");
            break;
        default:
            break;
    }
    try {
        if (queryView != null) {
            ViewRequest<String, Object> request = queryView.newPaginatedRequest(Key.Type.STRING, Object.class).rowsPerPage(rowsPerPage).descending(!ascending).includeDocs(true).build();
            ViewResponse<String, Object> response = request.getResponse();
            response = request.getResponse();
            int pageNo = pageData.getDisplayStart() / rowsPerPage;
            List<Project> proj = new ArrayList<Project>();
            int count = pageNo == 0 ? rowsPerPage : (pageNo + 1) * rowsPerPage;
            while (projects.size() < count) {
                if (response != null) {
                    proj = response.getDocsAs(Project.class);
                }
                proj = proj.stream().filter(ProjectPermissions.isVisible(user)).collect(Collectors.toList());
                projects.addAll(proj.stream().collect(Collectors.toList()));
                response = response.nextPage();
                if (response == null) {
                    break;
                }
            }
            projects = projects.stream().skip(pageData.getDisplayStart()).limit(rowsPerPage).collect(Collectors.toList());
        } else {
            QueryResult<Project> queryResult = getConnector().getQueryResult(query, Project.class);
            projects = queryResult.getDocs();
        }
    } catch (Exception e) {
        log.error("Error getting projects", e);
    }
    result.put(pageData, projects);
    return result;
}
Also used : PaginationData(org.eclipse.sw360.datahandler.thrift.PaginationData) QueryBuilder(com.cloudant.client.api.query.QueryBuilder) ViewRequestBuilder(com.cloudant.client.api.views.ViewRequestBuilder) Project(org.eclipse.sw360.datahandler.thrift.projects.Project) Selector(com.cloudant.client.api.query.Selector)

Example 3 with Selector

use of com.cloudant.client.api.query.Selector in project sw360 by eclipse.

the class ModerationRequestRepository method queryViewWithPagination.

private Map<PaginationData, List<ModerationRequest>> queryViewWithPagination(String moderator, PaginationData pageData, boolean open) {
    final int rowsPerPage = pageData.getRowsPerPage();
    Map<PaginationData, List<ModerationRequest>> result = Maps.newHashMap();
    List<ModerationRequest> modReqs = Lists.newArrayList();
    final boolean ascending = pageData.isAscending();
    final int sortColumnNo = pageData.getSortColumnNumber();
    String query = null;
    final Selector typeSelector = eq("type", "moderation");
    final Selector openModerationState = or(eq("moderationState", "PENDING"), eq("moderationState", "INPROGRESS"));
    final Selector closedModerationState = or(eq("moderationState", "APPROVED"), eq("moderationState", "REJECTED"));
    final Selector filterByModeratorSelector = PredicatedOperation.elemMatch("moderators", PredicateExpression.eq(moderator));
    final Selector emptyComponentTypeSelector = or(Expression.exists("componentType", false), eq("componentType", ""));
    final Selector commonSelector = and(typeSelector, open ? openModerationState : closedModerationState, filterByModeratorSelector);
    QueryBuilder qb = new QueryBuilder(commonSelector);
    if (rowsPerPage != -1) {
        qb.limit(rowsPerPage);
    }
    qb.skip(pageData.getDisplayStart());
    switch(sortColumnNo) {
        case -1:
            qb = qb.useIndex("byModerators");
            qb = ascending ? qb.sort(Sort.asc("moderators")) : qb.sort(Sort.desc("moderators"));
            query = qb.build();
            break;
        case 0:
            qb = qb.useIndex("byDate");
            qb = ascending ? qb.sort(Sort.asc("timestamp")) : qb.sort(Sort.desc("timestamp"));
            query = qb.build();
            break;
        case 1:
            qb = qb.useIndex("byComponentType");
            qb = ascending ? qb.sort(Sort.asc("componentType")) : qb.sort(Sort.desc("componentType"));
            query = qb.build();
            break;
        case 2:
            qb = qb.useIndex("byDocumentName");
            qb = ascending ? qb.sort(Sort.asc("documentName")) : qb.sort(Sort.desc("documentName"));
            query = qb.build();
            break;
        case 3:
            qb = qb.useIndex("byUsers");
            qb = ascending ? qb.sort(Sort.asc("requestingUser")) : qb.sort(Sort.desc("requestingUser"));
            query = qb.build();
            break;
        case 4:
            qb = qb.useIndex("byDepartment");
            qb = ascending ? qb.sort(Sort.asc("requestingUserDepartment")) : qb.sort(Sort.desc("requestingUserDepartment"));
            query = qb.build();
            break;
        case 5:
            qb = qb.useIndex("byModerators");
            qb = ascending ? qb.sort(Sort.asc("moderators")) : qb.sort(Sort.desc("moderators"));
            query = qb.build();
            break;
        case 6:
            qb = qb.useIndex("byModerationState");
            qb = ascending ? qb.sort(Sort.asc("moderationState")) : qb.sort(Sort.desc("moderationState"));
            query = qb.build();
            break;
        default:
            break;
    }
    try {
        QueryResult<ModerationRequest> queryResult = getConnector().getQueryResult(query, ModerationRequest.class);
        modReqs = queryResult.getDocs();
        if (1 == sortColumnNo) {
            final Selector selectorCompType = and(typeSelector, open ? openModerationState : closedModerationState, filterByModeratorSelector, emptyComponentTypeSelector);
            QueryBuilder emptyCTypeQb = new QueryBuilder(selectorCompType);
            emptyCTypeQb.limit(rowsPerPage);
            emptyCTypeQb.skip(pageData.getDisplayStart());
            QueryResult<ModerationRequest> queryResultWithoutSorting = getConnector().getQueryResult(emptyCTypeQb.build(), ModerationRequest.class);
            List<ModerationRequest> mods = queryResultWithoutSorting.getDocs();
            if (ascending) {
                mods.addAll(modReqs);
                modReqs = mods;
            } else {
                modReqs.addAll(mods);
            }
        }
    } catch (Exception e) {
        log.error("Error getting moderation requests", e);
    }
    result.put(pageData, modReqs);
    return result;
}
Also used : PaginationData(org.eclipse.sw360.datahandler.thrift.PaginationData) ModerationRequest(org.eclipse.sw360.datahandler.thrift.moderation.ModerationRequest) ArrayList(java.util.ArrayList) List(java.util.List) QueryBuilder(com.cloudant.client.api.query.QueryBuilder) IOException(java.io.IOException) Selector(com.cloudant.client.api.query.Selector)

Example 4 with Selector

use of com.cloudant.client.api.query.Selector in project sw360 by eclipse.

the class ModerationRequestRepository method getRequestsByModerator.

public List<ModerationRequest> getRequestsByModerator(String moderator) {
    final Selector typeSelector = eq("type", "moderation");
    final Selector filterByModeratorSelector = PredicatedOperation.elemMatch("moderators", PredicateExpression.eq(moderator));
    final Selector finalSelector = and(typeSelector, filterByModeratorSelector);
    QueryBuilder qb = new QueryBuilder(finalSelector);
    qb.useIndex("byModerators");
    List<ModerationRequest> mrs = getConnector().getQueryResult(qb.build(), ModerationRequest.class).getDocs();
    return makeSummaryFromFullDocs(SummaryType.SHORT, mrs);
}
Also used : ModerationRequest(org.eclipse.sw360.datahandler.thrift.moderation.ModerationRequest) QueryBuilder(com.cloudant.client.api.query.QueryBuilder) Selector(com.cloudant.client.api.query.Selector)

Example 5 with Selector

use of com.cloudant.client.api.query.Selector in project sw360 by eclipse.

the class ModerationRequestRepository method getRequestsByDocumentId.

public List<ModerationRequest> getRequestsByDocumentId(String documentId) {
    final Selector typeSelector = eq("type", "moderation");
    final Selector filterByModeratorSelector = eq("documentId", documentId);
    final Selector finalSelector = and(typeSelector, filterByModeratorSelector);
    QueryBuilder qb = new QueryBuilder(finalSelector);
    qb.useIndex("byDocumentId");
    List<ModerationRequest> mrs = getConnector().getQueryResult(qb.build(), ModerationRequest.class).getDocs();
    return mrs;
}
Also used : ModerationRequest(org.eclipse.sw360.datahandler.thrift.moderation.ModerationRequest) QueryBuilder(com.cloudant.client.api.query.QueryBuilder) Selector(com.cloudant.client.api.query.Selector)

Aggregations

QueryBuilder (com.cloudant.client.api.query.QueryBuilder)6 Selector (com.cloudant.client.api.query.Selector)6 ModerationRequest (org.eclipse.sw360.datahandler.thrift.moderation.ModerationRequest)4 PaginationData (org.eclipse.sw360.datahandler.thrift.PaginationData)3 IOException (java.io.IOException)2 List (java.util.List)2 ViewRequestBuilder (com.cloudant.client.api.views.ViewRequestBuilder)1 ArrayList (java.util.ArrayList)1 Project (org.eclipse.sw360.datahandler.thrift.projects.Project)1 User (org.eclipse.sw360.datahandler.thrift.users.User)1