001/*
002 *  Copyright 2017 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.odf.orgunit.indexing;
017
018import java.util.HashSet;
019import java.util.Set;
020
021import org.apache.avalon.framework.configuration.Configurable;
022import org.apache.avalon.framework.configuration.Configuration;
023import org.apache.avalon.framework.configuration.ConfigurationException;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.avalon.framework.service.Serviceable;
027
028import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
029import org.ametys.cms.contenttype.ContentTypesHelper;
030import org.ametys.cms.contenttype.MetadataType;
031import org.ametys.cms.contenttype.indexing.CustomIndexingField;
032import org.ametys.cms.repository.Content;
033import org.ametys.odf.orgunit.OrgUnit;
034import org.ametys.runtime.i18n.I18nizableText;
035
036/**
037 * {@link CustomIndexingField} indexing all orgUnit ancestor id in one field.
038 */
039public class OrgUnitAncestorIndexingField implements CustomIndexingField, Configurable, Serviceable
040{
041    /** The content type extension point. */
042    protected ContentTypeExtensionPoint _cTypeEP;
043    /** The content type helper. */
044    protected ContentTypesHelper _cTypeHelper;
045    
046    /** The field name */
047    protected String _name;
048    /** The label field */
049    protected I18nizableText _label;
050    /** The description field */
051    protected I18nizableText _description;
052    
053    @Override
054    public void service(ServiceManager manager) throws ServiceException
055    {
056        _cTypeEP = (ContentTypeExtensionPoint) manager.lookup(ContentTypeExtensionPoint.ROLE);
057        _cTypeHelper = (ContentTypesHelper) manager.lookup(ContentTypesHelper.ROLE);
058    }
059
060    @Override
061    public void configure(Configuration configuration) throws ConfigurationException
062    {
063        _name = configuration.getAttribute("name");
064        _label = I18nizableText.parseI18nizableText(configuration.getChild("label"), "plugin.odf");
065        _description = I18nizableText.parseI18nizableText(configuration.getChild("description"), "plugin.odf");
066    }
067    
068    @Override
069    public Object[] getValues(Content content)
070    {
071        OrgUnit orgUnit = (OrgUnit) content;
072        Set<String> values = new HashSet<>();
073        values.add(orgUnit.getId());
074        
075        OrgUnit ancestor = orgUnit.getParentOrgUnit();
076        while (ancestor != null)
077        {
078            values.add(ancestor.getId());
079            ancestor = ancestor.getParentOrgUnit();
080        }
081        
082        return values.toArray(new String[values.size()]);
083    }
084
085    @Override
086    public String getName()
087    {
088        return _name;
089    }
090
091    @Override
092    public I18nizableText getLabel()
093    {
094        return _label;
095    }
096
097    @Override
098    public I18nizableText getDescription()
099    {
100        return _description;
101    }
102
103    @Override
104    public MetadataType getType()
105    {
106        return MetadataType.CONTENT;
107    }
108}