001/* 002 * Copyright 2021 Anyware Services 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 */ 016package org.ametys.web.search.systemprop; 017 018import java.util.ArrayList; 019import java.util.Collection; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025 026import org.ametys.cms.search.model.SystemProperty; 027import org.ametys.cms.search.query.MatchNoneQuery; 028import org.ametys.cms.search.query.Query; 029import org.ametys.cms.search.query.Query.Operator; 030import org.ametys.cms.search.solr.schema.SchemaDefinition; 031import org.ametys.cms.tag.CMSTag; 032import org.ametys.core.group.GroupIdentity; 033import org.ametys.core.group.GroupManager; 034import org.ametys.core.user.CurrentUserProvider; 035import org.ametys.core.user.UserIdentity; 036import org.ametys.web.tags.GroupTagProvider; 037 038/** 039 * {@link SystemProperty} which represents the profiled groups tags of a Content. 040 */ 041public class ProfiledGroupsTagsSystemProperty extends TagsSystemProperty 042{ 043 /** The current user provider */ 044 protected CurrentUserProvider _currentUserProvider; 045 046 /** The group manager */ 047 protected GroupManager _groupManager; 048 049 @Override 050 public void service(ServiceManager manager) throws ServiceException 051 { 052 super.service(manager); 053 _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE); 054 _groupManager = (GroupManager) manager.lookup(GroupManager.ROLE); 055 } 056 057 @Override 058 public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters) 059 { 060 GroupTagProvider<CMSTag> tagProvider = (GroupTagProvider<CMSTag>) _tagProviderEP.getExtension(GroupTagProvider.ROLE); 061 062 UserIdentity user = _currentUserProvider.getUser(); 063 List<String> groupsAsTag = new ArrayList<>(); 064 for (GroupIdentity group : _groupManager.getUserGroups(user)) 065 { 066 String groupDirectoryTagId = tagProvider.getGroupDirectoryTagId(group.getDirectoryId()); 067 if (!groupsAsTag.contains(groupDirectoryTagId)) 068 { 069 groupsAsTag.add(groupDirectoryTagId); 070 } 071 groupsAsTag.add(tagProvider.getGroupTagId(groupDirectoryTagId, group)); 072 } 073 074 return groupsAsTag.isEmpty() 075 ? new MatchNoneQuery() 076 : super.getQuery(groupsAsTag, operator, language, contextualParameters); 077 } 078 079 @Override 080 public Collection<SchemaDefinition> getSchemaDefinitions() 081 { 082 // Do not send schema definitions twice, as they are already sent by the TagSystemProperty itself 083 return List.of(); 084 } 085}