001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Thierry Delprat
016 * *
017 */
018
019package org.nuxeo.ecm.platform.computedgroups;
020
021import java.io.Serializable;
022import java.util.ArrayList;
023import java.util.List;
024import java.util.Map;
025import java.util.Set;
026
027import org.nuxeo.ecm.platform.usermanager.UserManager;
028import org.nuxeo.runtime.api.Framework;
029
030/**
031 * Base class for {@link GroupComputer} implementation. Provides a naive implementation for searchGroups method.
032 *
033 * @author Thierry Delprat
034 */
035public abstract class AbstractGroupComputer implements GroupComputer {
036
037    protected UserManager getUM() {
038        return Framework.getLocalService(UserManager.class);
039    }
040
041    /**
042     * Default implementation that searches on all ids for a match.
043     */
044    public List<String> searchGroups(Map<String, Serializable> filter, Set<String> fulltext) {
045
046        List<String> result = new ArrayList<String>();
047        String grpName = (String) filter.get(getUM().getGroupIdField());
048        if (grpName != null) {
049            List<String> allGroupIds = getAllGroupIds();
050            if (allGroupIds != null) {
051                for (String vGroupName : allGroupIds) {
052                    if (vGroupName.startsWith(grpName)) {
053                        if (!result.contains(vGroupName)) {
054                            result.add(vGroupName);
055                        }
056                    }
057                }
058            }
059        }
060        return result;
061    }
062
063    /**
064     * Default implementation that returns true if method {@link GroupComputer#getAllGroupIds()} contains given group
065     * name.
066     */
067    public boolean hasGroup(String name) {
068        List<String> allGroupIds = getAllGroupIds();
069        if (allGroupIds != null) {
070            return allGroupIds.contains(name);
071        }
072        return false;
073    }
074
075}