Search in sources :

Example 6 with ViewRequestBuilder

use of com.cloudant.client.api.views.ViewRequestBuilder 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 7 with ViewRequestBuilder

use of com.cloudant.client.api.views.ViewRequestBuilder in project sw360 by eclipse.

the class ReleaseRepository method getRecentReleases.

public List<Release> getRecentReleases() {
    ViewRequestBuilder query = getConnector().createQuery(Release.class, "byCreatedOn");
    // Get the 5 last documents
    UnpaginatedRequestBuilder reqBuilder = query.newRequest(Key.Type.STRING, Object.class).limit(5).descending(true).includeDocs(false);
    return makeSummary(SummaryType.SHORT, queryForIds(reqBuilder));
}
Also used : ViewRequestBuilder(com.cloudant.client.api.views.ViewRequestBuilder) UnpaginatedRequestBuilder(com.cloudant.client.api.views.UnpaginatedRequestBuilder)

Example 8 with ViewRequestBuilder

use of com.cloudant.client.api.views.ViewRequestBuilder in project sw360 by eclipse.

the class DatabaseRepositoryCloudantClient method queryByIds.

public List<T> queryByIds(String viewName, Collection<String> ids) {
    String[] idStrs = new String[ids.size()];
    int index = 0;
    for (String str : ids) idStrs[index++] = str;
    ViewRequestBuilder query = connector.createQuery(type, viewName);
    UnpaginatedRequestBuilder reqBuilder = query.newRequest(Key.Type.STRING, Object.class).includeDocs(true).keys(idStrs);
    return queryView(reqBuilder);
}
Also used : ViewRequestBuilder(com.cloudant.client.api.views.ViewRequestBuilder) UnpaginatedRequestBuilder(com.cloudant.client.api.views.UnpaginatedRequestBuilder)

Example 9 with ViewRequestBuilder

use of com.cloudant.client.api.views.ViewRequestBuilder in project java-cloudant by cloudant.

the class ViewsTest method scalarValues.

@Test
public void scalarValues() throws Exception {
    init();
    ViewRequestBuilder builder = db.getViewRequestBuilder("example", "by_tag");
    int allTags = builder.newRequest(Key.Type.STRING, int.class).build().getSingleValue();
    assertThat(allTags, is(4));
    long couchDbTags = builder.newRequest(Key.Type.STRING, long.class).keys("couchdb").build().getSingleValue();
    assertThat(couchDbTags, is(2L));
    String javaTags = builder.newRequest(Key.Type.STRING, String.class).keys("java").build().getSingleValue();
    assertThat(javaTags, is("1"));
}
Also used : ViewRequestBuilder(com.cloudant.client.api.views.ViewRequestBuilder) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) TestWithDbPerTest(com.cloudant.tests.base.TestWithDbPerTest) Test(org.junit.jupiter.api.Test)

Example 10 with ViewRequestBuilder

use of com.cloudant.client.api.views.ViewRequestBuilder in project sw360 by eclipse.

the class DatabaseRepositoryCloudantClient method queryForIdsAsValue.

public Set<String> queryForIdsAsValue(String queryName, String startKey, String endKey) {
    ViewRequestBuilder query = connector.createQuery(type, queryName);
    UnpaginatedRequestBuilder req = query.newRequest(Key.Type.STRING, Object.class).startKey(startKey).endKey(endKey);
    return queryForIdsAsValue(req);
}
Also used : ViewRequestBuilder(com.cloudant.client.api.views.ViewRequestBuilder) UnpaginatedRequestBuilder(com.cloudant.client.api.views.UnpaginatedRequestBuilder)

Aggregations

ViewRequestBuilder (com.cloudant.client.api.views.ViewRequestBuilder)19 UnpaginatedRequestBuilder (com.cloudant.client.api.views.UnpaginatedRequestBuilder)14 Key (com.cloudant.client.api.views.Key)3 PaginationData (org.eclipse.sw360.datahandler.thrift.PaginationData)2 Component (org.eclipse.sw360.datahandler.thrift.components.Component)2 NotNull (org.jetbrains.annotations.NotNull)2 MapReduce (com.cloudant.client.api.model.DesignDocument.MapReduce)1 QueryBuilder (com.cloudant.client.api.query.QueryBuilder)1 Selector (com.cloudant.client.api.query.Selector)1 ComplexKey (com.cloudant.client.api.views.Key.ComplexKey)1 MultipleRequestBuilder (com.cloudant.client.api.views.MultipleRequestBuilder)1 ViewResponse (com.cloudant.client.api.views.ViewResponse)1 TestWithDbPerTest (com.cloudant.tests.base.TestWithDbPerTest)1 Strings (com.google.common.base.Strings)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Lists (com.google.common.collect.Lists)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1