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.Collections; 026import java.util.List; 027import java.util.Map; 028import java.util.Set; 029 030import org.nuxeo.ecm.core.query.sql.model.MultiExpression; 031import org.nuxeo.ecm.core.query.sql.model.QueryBuilder; 032import org.nuxeo.ecm.directory.memory.MapExpressionEvaluator; 033import org.nuxeo.ecm.platform.usermanager.UserManager; 034import org.nuxeo.runtime.api.Framework; 035 036/** 037 * Base class for {@link GroupComputer} implementation. Provides a naive implementation for searchGroups method. 038 * 039 * @author Thierry Delprat 040 */ 041public abstract class AbstractGroupComputer implements GroupComputer { 042 043 protected UserManager getUM() { 044 return Framework.getService(UserManager.class); 045 } 046 047 /** 048 * Default implementation that searches on all ids for a match. 049 */ 050 @Override 051 public List<String> searchGroups(Map<String, Serializable> filter, Set<String> fulltext) { 052 053 List<String> result = new ArrayList<>(); 054 String grpName = (String) filter.get(getUM().getGroupIdField()); 055 if (grpName != null) { 056 List<String> allGroupIds = getAllGroupIds(); 057 if (allGroupIds != null) { 058 for (String vGroupName : allGroupIds) { 059 if (vGroupName.startsWith(grpName)) { 060 if (!result.contains(vGroupName)) { 061 result.add(vGroupName); 062 } 063 } 064 } 065 } 066 } 067 return result; 068 } 069 070 @Override 071 public List<String> searchGroups(QueryBuilder queryBuilder) { 072 ArrayList<String> groupIds = new ArrayList<>(); 073 String groupIdField = Framework.getService(UserManager.class).getGroupIdField(); 074 List<String> allGroupIds = getAllGroupIds(); 075 if (allGroupIds != null) { 076 MultiExpression expression = queryBuilder.predicate(); 077 MapExpressionEvaluator evaluator = new MapExpressionEvaluator(); 078 for (String groupId : allGroupIds) { 079 Map<String, Object> entry = Collections.singletonMap(groupIdField, groupId); 080 if (evaluator.matchesEntry(expression, entry)) { 081 groupIds.add(groupId); 082 } 083 } 084 } 085 return groupIds; 086 } 087 088 /** 089 * Default implementation that returns true if method {@link GroupComputer#getAllGroupIds()} contains given group 090 * name. 091 */ 092 @Override 093 public boolean hasGroup(String name) { 094 List<String> allGroupIds = getAllGroupIds(); 095 if (allGroupIds != null) { 096 return allGroupIds.contains(name); 097 } 098 return false; 099 } 100 101}