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;
}
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));
}
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);
}
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"));
}
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);
}
Aggregations