Search in sources :

Example 1 with ViewRequestBuilder

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

the class ComponentRepository method getRecentComponentsSummary.

public Map<PaginationData, List<Component>> getRecentComponentsSummary(User user, PaginationData pageData) {
    final int rowsPerPage = pageData.getRowsPerPage();
    Map<PaginationData, List<Component>> result = Maps.newHashMap();
    List<Component> components = Lists.newArrayList();
    final boolean ascending = pageData.isAscending();
    final int sortColumnNo = pageData.getSortColumnNumber();
    ViewRequestBuilder query;
    switch(sortColumnNo) {
        case -1:
            query = getConnector().createQuery(Component.class, "byCreatedOn");
            break;
        case 0:
            query = getConnector().createQuery(Component.class, "byvendor");
            break;
        case 1:
            query = getConnector().createQuery(Component.class, "byname");
            break;
        case 2:
            query = getConnector().createQuery(Component.class, "bymainlicense");
            break;
        case 3:
            query = getConnector().createQuery(Component.class, "bycomponenttype");
            break;
        default:
            query = getConnector().createQuery(Component.class, "all");
            break;
    }
    ViewRequest<String, Object> request = null;
    if (rowsPerPage == -1) {
        request = query.newRequest(Key.Type.STRING, Object.class).descending(!ascending).includeDocs(true).build();
    } else {
        request = query.newPaginatedRequest(Key.Type.STRING, Object.class).rowsPerPage(rowsPerPage).descending(!ascending).includeDocs(true).build();
    }
    ViewResponse<String, Object> response = null;
    try {
        response = request.getResponse();
        int pageNo = pageData.getDisplayStart() / rowsPerPage;
        int i = 1;
        while (i <= pageNo) {
            response = response.nextPage();
            i++;
        }
        components = response.getDocsAs(Component.class);
    } catch (Exception e) {
        log.error("Error getting recent components", e);
    }
    components = makeSummaryWithPermissionsFromFullDocs(SummaryType.SUMMARY, components, user);
    pageData.setTotalRowCount(response.getTotalRowCount());
    result.put(pageData, components);
    return result;
}
Also used : PaginationData(org.eclipse.sw360.datahandler.thrift.PaginationData) ViewRequestBuilder(com.cloudant.client.api.views.ViewRequestBuilder) Component(org.eclipse.sw360.datahandler.thrift.components.Component)

Example 2 with ViewRequestBuilder

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

the class AttachmentContentRepository method getOnlyRemoteAttachments.

public List<AttachmentContent> getOnlyRemoteAttachments() {
    ViewRequestBuilder query = getConnector().createQuery(AttachmentContent.class, "onlyRemotes");
    UnpaginatedRequestBuilder req = query.newRequest(Key.Type.STRING, Object.class).includeDocs(true);
    return queryView(req);
}
Also used : ViewRequestBuilder(com.cloudant.client.api.views.ViewRequestBuilder) UnpaginatedRequestBuilder(com.cloudant.client.api.views.UnpaginatedRequestBuilder)

Example 3 with ViewRequestBuilder

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

the class AttachmentUsageRepository method getAttachmentUsageCount.

public Map<Map<String, String>, Integer> getAttachmentUsageCount(Map<String, Set<String>> attachments, String filter) {
    ViewRequestBuilder viewQuery = createUsagesByAttachmentQuery(filter);
    List<String[]> complexKeysList = prepareKeys(attachments, filter);
    Key.ComplexKey[] compexKeys = new Key.ComplexKey[complexKeysList.size()];
    for (int i = 0; i < compexKeys.length; i++) {
        Key.ComplexKey key = Key.complex(complexKeysList.get(i));
        compexKeys[i] = key;
    }
    UnpaginatedRequestBuilder<com.cloudant.client.api.views.Key.ComplexKey, Object> reqBuilder = viewQuery.newRequest(Key.Type.COMPLEX, Object.class).reduce(true).group(true).keys(compexKeys);
    ViewResponse<com.cloudant.client.api.views.Key.ComplexKey, Object> result = queryViewForComplexKeys(reqBuilder);
    return result.getRows().stream().collect(Collectors.toMap(key -> {
        String json = key.getKey().toJson();
        String replace = json.replace("[", "").replace("]", "").replaceAll("\"", "");
        List<String> relIdAttachmentToUsageType = new ArrayList<String>(Arrays.asList(replace.split(",")));
        return ImmutableMap.of(relIdAttachmentToUsageType.get(0), relIdAttachmentToUsageType.get(1));
    }, val -> ((Double) val.getValue()).intValue()));
}
Also used : Arrays(java.util.Arrays) ImmutableMap(com.google.common.collect.ImmutableMap) Key(com.cloudant.client.api.views.Key) Set(java.util.Set) MapReduce(com.cloudant.client.api.model.DesignDocument.MapReduce) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) DatabaseConnectorCloudant(org.eclipse.sw360.datahandler.cloudantclient.DatabaseConnectorCloudant) ArrayList(java.util.ArrayList) UnpaginatedRequestBuilder(com.cloudant.client.api.views.UnpaginatedRequestBuilder) ViewRequestBuilder(com.cloudant.client.api.views.ViewRequestBuilder) Strings(com.google.common.base.Strings) AttachmentUsage(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentUsage) List(java.util.List) ViewResponse(com.cloudant.client.api.views.ViewResponse) Lists(com.google.common.collect.Lists) Map(java.util.Map) View(org.ektorp.support.View) Entry(java.util.Map.Entry) DatabaseRepositoryCloudantClient(org.eclipse.sw360.datahandler.cloudantclient.DatabaseRepositoryCloudantClient) NotNull(org.jetbrains.annotations.NotNull) MultipleRequestBuilder(com.cloudant.client.api.views.MultipleRequestBuilder) ViewRequestBuilder(com.cloudant.client.api.views.ViewRequestBuilder) ArrayList(java.util.ArrayList) List(java.util.List) Key(com.cloudant.client.api.views.Key)

Example 4 with ViewRequestBuilder

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

the class AttachmentUsageRepository method getUsedAttachments.

public List<AttachmentUsage> getUsedAttachments(String usedById, String filter) {
    ViewRequestBuilder viewQuery = getConnector().createQuery(AttachmentUsage.class, "usedAttachmentsUsageType");
    UnpaginatedRequestBuilder reqBuilder = viewQuery.newRequest(Key.Type.COMPLEX, Object.class).includeDocs(true).reduce(false).keys(Key.complex(new String[] { usedById, filter }));
    return queryView(reqBuilder);
}
Also used : ViewRequestBuilder(com.cloudant.client.api.views.ViewRequestBuilder) UnpaginatedRequestBuilder(com.cloudant.client.api.views.UnpaginatedRequestBuilder)

Example 5 with ViewRequestBuilder

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

the class AttachmentUsageRepository method getUsageForAttachment.

public List<AttachmentUsage> getUsageForAttachment(String ownerId, String attachmentContentId) {
    ViewRequestBuilder viewQuery = getConnector().createQuery(AttachmentUsage.class, "usagesByAttachment");
    UnpaginatedRequestBuilder reqBuilder = viewQuery.newRequest(Key.Type.COMPLEX, Object.class).keys(Key.complex(new String[] { ownerId, attachmentContentId })).includeDocs(true).reduce(false);
    return queryView(reqBuilder);
}
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