use of org.alfresco.query.PageDetails in project alfresco-remote-api by Alfresco.
the class SitesImpl method getSites.
public CollectionWithPagingInfo<MemberOfSite> getSites(String personId, Parameters parameters) {
Paging paging = parameters.getPaging();
personId = people.validatePerson(personId);
PagingRequest pagingRequest = Util.getPagingRequest(paging);
// get the sorting options
List<Pair<? extends Object, SortOrder>> sortPairs = new ArrayList<>(parameters.getSorting().size());
List<SortColumn> sortCols = parameters.getSorting();
if ((sortCols != null) && (sortCols.size() > 0)) {
for (SortColumn sortCol : sortCols) {
SiteService.SortFields sortProp = SORT_SITE_MEMBERSHIP.get(sortCol.column);
if (sortProp == null) {
throw new InvalidArgumentException("Invalid sort field: " + sortCol.column);
}
sortPairs.add(new Pair<>(sortProp, (sortCol.asc ? SortOrder.ASCENDING : SortOrder.DESCENDING)));
}
} else {
// default sort order
sortPairs.add(new Pair<SiteService.SortFields, SortOrder>(SiteService.SortFields.SiteTitle, SortOrder.ASCENDING));
}
// get the unsorted list of site memberships
List<SiteMembership> siteMembers = siteService.listSiteMemberships(personId, 0);
// sort the list of site memberships
int totalSize = siteMembers.size();
final List<SiteMembership> sortedSiteMembers = new ArrayList<>(siteMembers);
Collections.sort(sortedSiteMembers, new SiteMembershipComparator(sortPairs, SiteMembershipComparator.Type.SITES));
PageDetails pageDetails = PageDetails.getPageDetails(pagingRequest, totalSize);
List<MemberOfSite> ret = new ArrayList<>(totalSize);
List<FilterProp> filterProps = getFilterPropListOfSites(parameters);
int counter;
int totalItems = 0;
Iterator<SiteMembership> it = sortedSiteMembers.iterator();
for (counter = 0; it.hasNext(); ) {
SiteMembership siteMember = it.next();
if (filterProps != null && !includeFilter(siteMember, filterProps)) {
continue;
}
if (counter < pageDetails.getSkipCount()) {
totalItems++;
counter++;
continue;
}
if (counter <= pageDetails.getEnd() - 1) {
SiteInfo siteInfo = siteMember.getSiteInfo();
MemberOfSite memberOfSite = new MemberOfSite(siteInfo.getShortName(), siteInfo.getNodeRef(), siteMember.getRole());
ret.add(memberOfSite);
counter++;
}
totalItems++;
}
return CollectionWithPagingInfo.asPaged(paging, ret, counter < totalItems, totalItems);
}
use of org.alfresco.query.PageDetails in project alfresco-remote-api by Alfresco.
the class SitesImpl method getFavouriteSites.
private PagingResults<SiteInfo> getFavouriteSites(String userName, PagingRequest pagingRequest) {
final Collator collator = Collator.getInstance();
final Set<SiteInfo> sortedFavouriteSites = new TreeSet<SiteInfo>(new Comparator<SiteInfo>() {
@Override
public int compare(SiteInfo o1, SiteInfo o2) {
return collator.compare(o1.getTitle(), o2.getTitle());
}
});
Map<String, Serializable> prefs = preferenceService.getPreferences(userName, FAVOURITE_SITES_PREFIX);
for (Entry<String, Serializable> entry : prefs.entrySet()) {
boolean isFavourite = false;
Serializable s = entry.getValue();
if (s instanceof Boolean) {
isFavourite = (Boolean) s;
}
if (isFavourite) {
String siteShortName = entry.getKey().substring(FAVOURITE_SITES_PREFIX_LENGTH).replace(".favourited", "");
SiteInfo siteInfo = siteService.getSite(siteShortName);
if (siteInfo != null) {
sortedFavouriteSites.add(siteInfo);
}
}
}
int totalSize = sortedFavouriteSites.size();
final PageDetails pageDetails = PageDetails.getPageDetails(pagingRequest, totalSize);
final List<SiteInfo> page = new ArrayList<SiteInfo>(pageDetails.getPageSize());
Iterator<SiteInfo> it = sortedFavouriteSites.iterator();
for (int counter = 0; counter < pageDetails.getEnd() && it.hasNext(); counter++) {
SiteInfo favouriteSite = it.next();
if (counter < pageDetails.getSkipCount()) {
continue;
}
if (counter > pageDetails.getEnd() - 1) {
break;
}
page.add(favouriteSite);
}
return new PagingResults<SiteInfo>() {
@Override
public List<SiteInfo> getPage() {
return page;
}
@Override
public boolean hasMoreItems() {
return pageDetails.hasMoreItems();
}
@Override
public Pair<Integer, Integer> getTotalResultCount() {
Integer total = Integer.valueOf(sortedFavouriteSites.size());
return new Pair<Integer, Integer>(total, total);
}
@Override
public String getQueryExecutionId() {
return null;
}
};
}
use of org.alfresco.query.PageDetails in project alfresco-remote-api by Alfresco.
the class Util method wrapPagingResults.
public static <T> PagingResults<T> wrapPagingResults(Paging paging, Collection<T> result) {
if (paging == null) {
throw new InvalidArgumentException("paging object can't be null.");
}
if (result == null) {
result = Collections.emptyList();
}
PagingRequest pagingRequest = getPagingRequest(paging);
final int totalSize = result.size();
final PageDetails pageDetails = PageDetails.getPageDetails(pagingRequest, totalSize);
final List<T> page = new ArrayList<>(pageDetails.getPageSize());
Iterator<T> it = result.iterator();
for (int counter = 0, end = pageDetails.getEnd(); counter < end && it.hasNext(); counter++) {
T element = it.next();
if (counter < pageDetails.getSkipCount()) {
continue;
}
if (counter > end - 1) {
break;
}
page.add(element);
}
return new PagingResults<T>() {
@Override
public List<T> getPage() {
return page;
}
@Override
public boolean hasMoreItems() {
return pageDetails.hasMoreItems();
}
@Override
public Pair<Integer, Integer> getTotalResultCount() {
Integer total = totalSize;
return new Pair<>(total, total);
}
@Override
public String getQueryExecutionId() {
return null;
}
};
}
use of org.alfresco.query.PageDetails in project alfresco-remote-api by Alfresco.
the class RepoService method getFavouriteSites.
private PagingResults<SiteInfo> getFavouriteSites(String userName, PagingRequest pagingRequest) {
final Collator collator = Collator.getInstance();
final Set<SiteInfo> sortedFavouriteSites = new TreeSet<SiteInfo>(new Comparator<SiteInfo>() {
@Override
public int compare(SiteInfo o1, SiteInfo o2) {
return collator.compare(o1.getTitle(), o2.getTitle());
}
});
Map<String, Serializable> prefs = preferenceService.getPreferences(userName, FAVOURITE_SITES_PREFIX);
for (String key : prefs.keySet()) {
boolean isFavourite = false;
Serializable s = prefs.get(key);
if (s instanceof Boolean) {
isFavourite = (Boolean) s;
}
if (isFavourite) {
String siteShortName = key.substring(FAVOURITE_SITES_PREFIX_LENGTH);
SiteInfo siteInfo = siteService.getSite(siteShortName);
if (siteInfo != null) {
sortedFavouriteSites.add(siteInfo);
}
}
}
int totalSize = sortedFavouriteSites.size();
final PageDetails pageDetails = PageDetails.getPageDetails(pagingRequest, totalSize);
final List<SiteInfo> page = new ArrayList<SiteInfo>(pageDetails.getPageSize());
Iterator<SiteInfo> it = sortedFavouriteSites.iterator();
for (int counter = 0; counter < pageDetails.getEnd() && it.hasNext(); counter++) {
SiteInfo favouriteSite = it.next();
if (counter < pageDetails.getSkipCount()) {
continue;
}
if (counter > pageDetails.getEnd() - 1) {
break;
}
page.add(favouriteSite);
}
return new PagingResults<SiteInfo>() {
@Override
public List<SiteInfo> getPage() {
return page;
}
@Override
public boolean hasMoreItems() {
return pageDetails.hasMoreItems();
}
@Override
public Pair<Integer, Integer> getTotalResultCount() {
Integer total = Integer.valueOf(sortedFavouriteSites.size());
return new Pair<Integer, Integer>(total, total);
}
@Override
public String getQueryExecutionId() {
return null;
}
};
}
Aggregations