use of org.alfresco.query.PagingResults 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.PagingResults in project alfresco-remote-api by Alfresco.
the class SiteAdminSitesGet method executeImpl.
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
String currentUser = AuthenticationUtil.getFullyAuthenticatedUser();
// check the current user access rights
if (!siteService.isSiteAdmin(currentUser)) {
// Note: security, no message to indicate why
throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Resource not found.");
}
// Create paging
final ScriptPagingDetails paging = new ScriptPagingDetails(getIntParameter(req, MAX_ITEMS, DEFAULT_MAX_ITEMS_PER_PAGE), getIntParameter(req, SKIP_COUNT, 0));
// request a total count of found items
paging.setRequestTotalCountMax(Integer.MAX_VALUE);
final List<FilterProp> filterProp = getFilterProperties(req.getParameter(NAME_FILTER));
final List<Pair<QName, Boolean>> sortProps = new ArrayList<Pair<QName, Boolean>>();
sortProps.add(new Pair<QName, Boolean>(ContentModel.PROP_NAME, true));
PagingResults<SiteInfo> pagingResults = AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork<PagingResults<SiteInfo>>() {
public PagingResults<SiteInfo> doWork() throws Exception {
return siteService.listSites(filterProp, sortProps, paging);
}
}, AuthenticationUtil.getAdminUserName());
List<SiteInfo> result = pagingResults.getPage();
List<SiteState> sites = new ArrayList<SiteState>(result.size());
for (SiteInfo info : result) {
sites.add(SiteState.create(info, siteService.listMembers(info.getShortName(), null, SiteModel.SITE_MANAGER, 0), currentUser, nodeService, personService));
}
Map<String, Object> sitesData = new HashMap<String, Object>(6);
// Site data
sitesData.put("items", sites);
// Paging data
sitesData.put("count", result.size());
sitesData.put("hasMoreItems", pagingResults.hasMoreItems());
sitesData.put("totalItems", (pagingResults.getTotalResultCount() == null ? -1 : pagingResults.getTotalResultCount().getFirst()));
sitesData.put("skipCount", paging.getSkipCount());
sitesData.put("maxItems", paging.getMaxItems());
// Create the model from the site and pagination data
Map<String, Object> model = new HashMap<String, Object>(1);
model.put("data", sitesData);
return model;
}
use of org.alfresco.query.PagingResults 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