001/*
002 * (C) Copyright 2006-2007 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 *     Thierry Delprat
018 * *
019 */
020
021package org.nuxeo.ecm.platform.computedgroups;
022
023import java.io.Serializable;
024import java.util.ArrayList;
025import java.util.List;
026import java.util.Map;
027import java.util.Set;
028
029import org.nuxeo.ecm.platform.usermanager.UserManager;
030import org.nuxeo.runtime.api.Framework;
031
032/**
033 * Base class for {@link GroupComputer} implementation. Provides a naive implementation for searchGroups method.
034 *
035 * @author Thierry Delprat
036 */
037public abstract class AbstractGroupComputer implements GroupComputer {
038
039    protected UserManager getUM() {
040        return Framework.getLocalService(UserManager.class);
041    }
042
043    /**
044     * Default implementation that searches on all ids for a match.
045     */
046    public List<String> searchGroups(Map<String, Serializable> filter, Set<String> fulltext) {
047
048        List<String> result = new ArrayList<String>();
049        String grpName = (String) filter.get(getUM().getGroupIdField());
050        if (grpName != null) {
051            List<String> allGroupIds = getAllGroupIds();
052            if (allGroupIds != null) {
053                for (String vGroupName : allGroupIds) {
054                    if (vGroupName.startsWith(grpName)) {
055                        if (!result.contains(vGroupName)) {
056                            result.add(vGroupName);
057                        }
058                    }
059                }
060            }
061        }
062        return result;
063    }
064
065    /**
066     * Default implementation that returns true if method {@link GroupComputer#getAllGroupIds()} contains given group
067     * name.
068     */
069    public boolean hasGroup(String name) {
070        List<String> allGroupIds = getAllGroupIds();
071        if (allGroupIds != null) {
072            return allGroupIds.contains(name);
073        }
074        return false;
075    }
076
077}