001/*
002 *  Copyright 2020 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.plugins.workspaces.members.solr;
017
018import org.apache.avalon.framework.configuration.Configurable;
019import org.apache.avalon.framework.configuration.Configuration;
020import org.apache.avalon.framework.configuration.ConfigurationException;
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.avalon.framework.service.Serviceable;
024
025import org.ametys.cms.contenttype.MetadataType;
026import org.ametys.cms.contenttype.indexing.CustomIndexingField;
027import org.ametys.cms.repository.Content;
028import org.ametys.core.user.UserIdentity;
029import org.ametys.plugins.workspaces.indexing.solr.SolrWorkspacesConstants;
030import org.ametys.plugins.workspaces.project.ProjectManager;
031import org.ametys.plugins.workspaces.project.objects.Project;
032import org.ametys.runtime.i18n.I18nizableText;
033import org.ametys.runtime.plugin.component.PluginAware;
034
035/**
036 * {@link CustomIndexingField} to index member's projects
037 */
038public class ProjectIndexingField implements CustomIndexingField, Configurable, Serviceable, PluginAware
039{
040    /** The label field */
041    protected I18nizableText _label;
042    /** The description field */
043    protected I18nizableText _description;
044    
045    private ProjectManager _projectManager;
046    
047    private String _pluginName;
048    
049    public void setPluginInfo(String pluginName, String featureName, String id)
050    {
051        _pluginName = pluginName;
052    }
053    
054    @Override
055    public void configure(Configuration configuration) throws ConfigurationException
056    {
057        _label = I18nizableText.parseI18nizableText(configuration.getChild("label"), "plugin" + _pluginName);
058        _description = I18nizableText.parseI18nizableText(configuration.getChild("description"), "plugin" + _pluginName);
059    }
060
061    public void service(ServiceManager manager) throws ServiceException
062    {
063        _projectManager = (ProjectManager) manager.lookup(ProjectManager.ROLE);
064    }
065
066    public String getName()
067    {
068        return SolrWorkspacesConstants.PROJECT_ID;
069    }
070
071    public I18nizableText getLabel()
072    {
073        return _label;
074    }
075
076    public I18nizableText getDescription()
077    {
078        return _description;
079    }
080    
081    public MetadataType getType()
082    {
083        return MetadataType.STRING;
084    }
085
086    public Object[] getValues(Content content)
087    {
088        UserIdentity identity = content.getValue("user", false, null);
089
090        if (identity != null)
091        {
092            return _projectManager.getUserProjects(identity)
093                    .keySet()
094                    .stream()
095                    .map(Project::getId)
096                    .toArray(String[]::new);
097        }
098        else
099        {
100            return new String[0];
101        }
102    }
103
104}