001/*
002 * (C) Copyright 2013 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 *     Thomas Roger
018 */
019
020package org.nuxeo.ecm.platform.usermanager.providers;
021
022import java.io.Serializable;
023import java.util.ArrayList;
024import java.util.Collections;
025import java.util.List;
026import java.util.Map;
027
028import org.apache.commons.lang.StringUtils;
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.ecm.directory.SizeLimitExceededException;
033import org.nuxeo.ecm.platform.query.api.AbstractPageProvider;
034import org.nuxeo.ecm.platform.usermanager.UserManager;
035import org.nuxeo.runtime.api.Framework;
036
037/**
038 * Abstract Page provider listing groups.
039 * <p>
040 * This page provider requires one parameter: the first one to be filled with the search string.
041 * <p>
042 * This page provider requires the property {@link #GROUPS_LISTING_MODE_PROPERTY} to be filled with a the listing mode
043 * to use.
044 *
045 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
046 * @since 5.8
047 */
048public abstract class AbstractGroupsPageProvider<T> extends AbstractPageProvider<T> {
049
050    protected static final String GROUPS_LISTING_MODE_PROPERTY = "groupsListingMode";
051
052    protected static final String ALL_MODE = "all";
053
054    protected static final String SEARCH_ONLY_MODE = "search_only";
055
056    protected static final String SEARCH_OVERFLOW_ERROR_MESSAGE = "label.security.searchOverFlow";
057
058    private static final Log log = LogFactory.getLog(AbstractGroupsPageProvider.class);
059
060    private static final long serialVersionUID = 1L;
061
062    protected List<DocumentModel> pageGroups;
063
064    public List<DocumentModel> computeCurrentPage() {
065        if (pageGroups == null) {
066            error = null;
067            errorMessage = null;
068            pageGroups = new ArrayList<DocumentModel>();
069
070            List<DocumentModel> groups = new ArrayList<DocumentModel>();
071            try {
072                String groupListingMode = getGroupListingMode();
073                if (ALL_MODE.equals(groupListingMode)) {
074                    groups = searchAllGroups();
075                } else if (SEARCH_ONLY_MODE.equals(groupListingMode)) {
076                    groups = searchGroups();
077                }
078            } catch (SizeLimitExceededException slee) {
079                error = slee;
080                errorMessage = SEARCH_OVERFLOW_ERROR_MESSAGE;
081                log.warn(slee.getMessage(), slee);
082            }
083
084            if (!hasError()) {
085                long resultsCount = groups.size();
086                setResultsCount(resultsCount);
087                // post-filter the results "by hand" to handle pagination
088                long pageSize = getMinMaxPageSize();
089                if (pageSize == 0) {
090                    pageGroups.addAll(groups);
091                } else {
092                    // handle offset
093                    long offset = getCurrentPageOffset();
094                    if (offset <= resultsCount) {
095                        for (int i = Long.valueOf(offset).intValue(); i < resultsCount && i < offset + pageSize; i++) {
096                            pageGroups.add(groups.get(i));
097                        }
098                    }
099                }
100            }
101        }
102        return pageGroups;
103    }
104
105    protected List<DocumentModel> searchAllGroups() {
106        return Framework.getService(UserManager.class).searchGroups(Collections.<String, Serializable> emptyMap(), null);
107    }
108
109    protected List<DocumentModel> searchGroups() {
110        UserManager userManager = Framework.getService(UserManager.class);
111        List<DocumentModel> groups = new ArrayList<DocumentModel>();
112        String searchString = getFirstParameter();
113        if ("*".equals(searchString)) {
114            groups = searchAllGroups();
115        } else if (!StringUtils.isEmpty(searchString)) {
116            groups = userManager.searchGroups(searchString);
117        }
118        return groups;
119    }
120
121    protected String getGroupListingMode() {
122        Map<String, Serializable> props = getProperties();
123        if (props.containsKey(GROUPS_LISTING_MODE_PROPERTY)) {
124            return (String) props.get(GROUPS_LISTING_MODE_PROPERTY);
125        }
126        return SEARCH_ONLY_MODE;
127    }
128
129    protected String getFirstParameter() {
130        Object[] parameters = getParameters();
131        if (parameters.length > 0) {
132            String param = (String) parameters[0];
133            if (param != null) {
134                return param.trim();
135            }
136        }
137        return "";
138    }
139
140    /**
141     * This page provider does not support sort for now => override what may be contributed in the definition
142     */
143    @Override
144    public boolean isSortable() {
145        return false;
146    }
147
148    @Override
149    protected void pageChanged() {
150        pageGroups = null;
151        super.pageChanged();
152    }
153
154    @Override
155    public void refresh() {
156        pageGroups = null;
157        super.refresh();
158    }
159
160}