001/* 002 * (C) Copyright 2016 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * "Guillaume Renard" 018 */ 019 020package org.nuxeo.ecm.platform.usermanager.providers; 021 022import java.util.ArrayList; 023import java.util.Iterator; 024import java.util.List; 025 026import org.apache.commons.lang.StringUtils; 027import org.nuxeo.ecm.core.api.NuxeoGroup; 028import org.nuxeo.ecm.platform.query.api.AbstractPageProvider; 029import org.nuxeo.ecm.platform.query.api.PageProvider; 030import org.nuxeo.ecm.platform.usermanager.UserManager; 031import org.nuxeo.runtime.api.Framework; 032 033/** 034 * @since 8.2 035 */ 036public abstract class AbstractGroupMemberPageProvider<T> extends AbstractPageProvider<T> 037 implements PageProvider<T> { 038 039 private static final long serialVersionUID = 1L; 040 041 protected static int safeLongToInt(long l) { 042 if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) { 043 throw new IllegalArgumentException(l + " cannot be cast to int without changing its value."); 044 } 045 return (int) l; 046 } 047 048 protected UserManager userManager; 049 050 protected List<T> currentPage; 051 052 @Override 053 public List<T> getCurrentPage() { 054 if (currentPage == null) { 055 currentPage = new ArrayList<T>(); 056 NuxeoGroup group = (NuxeoGroup) getParameters()[0]; 057 String query = null; 058 if (getParameters().length > 1) { 059 query = (String) getParameters()[1]; 060 } 061 List<String> members = getMembers(group); 062 063 // remove entries not starting with our query 064 if (StringUtils.isNotEmpty(query) && !"*".equals(query)) { 065 query = query.toLowerCase(); 066 final Iterator<String> it = members.iterator(); 067 while (it.hasNext()) { 068 if (!it.next().toLowerCase().startsWith(query)) { 069 it.remove(); 070 } 071 } 072 } 073 074 int limit = safeLongToInt(getCurrentPageOffset() + getPageSize()); 075 if (limit > members.size()) { 076 limit = members.size(); 077 } 078 079 for (String member : members.subList(safeLongToInt(getCurrentPageOffset()), limit)) { 080 currentPage.add(getMember(member)); 081 } 082 083 setResultsCount(members.size()); 084 } 085 return currentPage; 086 } 087 088 protected abstract List<String> getMembers(NuxeoGroup group); 089 090 protected abstract T getMember(String id); 091 092 @Override 093 public long getPageLimit() { 094 long pageSize = getPageSize(); 095 if (pageSize == 0) { 096 return 0; 097 } 098 return ((NuxeoGroup) getParameters()[0]).getMemberGroups().size() / pageSize; 099 } 100 101 @Override 102 public boolean isSortable() { 103 return false; 104 } 105 106 @Override 107 protected void pageChanged() { 108 currentPage = null; 109 super.pageChanged(); 110 } 111 112 protected UserManager getUserManager() { 113 if (userManager == null) { 114 userManager = Framework.getService(UserManager.class); 115 } 116 return userManager; 117 } 118}