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 */
018package org.nuxeo.ecm.platform.computedgroups;
019
020import java.io.Serializable;
021import java.util.ArrayList;
022import java.util.HashMap;
023import java.util.HashSet;
024import java.util.List;
025import java.util.Map;
026import java.util.Set;
027
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.api.DocumentModelList;
030import org.nuxeo.ecm.platform.usermanager.NuxeoPrincipalImpl;
031
032/**
033 * Base class for {@link GroupComputer} implementation that uses User attribute to compute groups.
034 *
035 * @author Thierry Delprat
036 */
037public abstract class AbstractAttributeBasedGroupComputer extends AbstractGroupComputer {
038
039    protected abstract String getAttributeForGroupComputation();
040
041    public List<String> getAllGroupIds() {
042
043        List<String> companies = new ArrayList<String>();
044        for (String userId : getUM().getUserIds()) {
045            DocumentModel doc = getUM().getUserModel(userId);
046            if (doc != null) {
047                String companyName = (String) doc.getProperty(getUM().getUserSchemaName(),
048                        getAttributeForGroupComputation());
049                if (!companies.contains(companyName)) {
050                    companies.add(companyName);
051                }
052            }
053        }
054        return companies;
055    }
056
057    public List<String> getGroupMembers(String groupName) {
058
059        Map<String, Serializable> filter = new HashMap<String, Serializable>();
060        filter.put(getAttributeForGroupComputation(), groupName);
061
062        DocumentModelList users = getUM().searchUsers(filter, null);
063
064        List<String> memberIds = new ArrayList<String>();
065
066        for (DocumentModel user : users) {
067            memberIds.add(user.getId());
068        }
069        return memberIds;
070    }
071
072    public List<String> getGroupsForUser(NuxeoPrincipalImpl nuxeoPrincipal) {
073        List<String> grpNames = new ArrayList<String>();
074        String property = (String) nuxeoPrincipal.getModel().getProperty(getUM().getUserSchemaName(),
075                getAttributeForGroupComputation());
076        if (property != null && !"".equals(property.trim())) {
077            grpNames.add(property);
078        }
079        return grpNames;
080    }
081
082    public List<String> getParentsGroupNames(String groupName) {
083        return null;
084    }
085
086    public List<String> getSubGroupsNames(String groupName) {
087        return null;
088    }
089
090    @Override
091    public List<String> searchGroups(Map<String, Serializable> filter, Set<String> fulltext) {
092
093        List<String> companies = new ArrayList<String>();
094
095        String grpName = (String) filter.get(getUM().getGroupIdField());
096        if (grpName != null) {
097            Map<String, Serializable> gFilter = new HashMap<String, Serializable>();
098            Set<String> gFulltext = new HashSet<String>();
099            gFilter.put(getAttributeForGroupComputation(), grpName);
100            gFulltext.add(getAttributeForGroupComputation());
101            for (DocumentModel userDoc : getUM().searchUsers(gFilter, gFulltext)) {
102                String companyName = (String) userDoc.getProperty(getUM().getUserSchemaName(),
103                        getAttributeForGroupComputation());
104                if (!companies.contains(companyName)) {
105                    companies.add(companyName);
106                }
107            }
108        }
109        return companies;
110    }
111}