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.ametys.cms.contenttype.ContentTypeExtensionPoint;
022import org.ametys.cms.contenttype.ContentTypesHelper;
023import org.ametys.cms.contenttype.MetadataDefinition;
024import org.ametys.cms.contenttype.MetadataType;
025import org.ametys.cms.contenttype.indexing.CustomIndexingField;
026import org.ametys.cms.repository.Content;
027import org.ametys.odf.orgunit.OrgUnit;
028import org.ametys.runtime.i18n.I18nizableText;
029import org.apache.avalon.framework.configuration.Configurable;
030import org.apache.avalon.framework.configuration.Configuration;
031import org.apache.avalon.framework.configuration.ConfigurationException;
032import org.apache.avalon.framework.service.ServiceException;
033import org.apache.avalon.framework.service.ServiceManager;
034import org.apache.avalon.framework.service.Serviceable;
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 metadata definition (taken from the first metadata). */
049    protected MetadataDefinition _definition;
050    /** The label field */
051    protected I18nizableText _label;
052    /** The description field */
053    protected I18nizableText _description;
054    
055    @Override
056    public void service(ServiceManager manager) throws ServiceException
057    {
058        _cTypeEP = (ContentTypeExtensionPoint) manager.lookup(ContentTypeExtensionPoint.ROLE);
059        _cTypeHelper = (ContentTypesHelper) manager.lookup(ContentTypesHelper.ROLE);
060    }
061
062    @Override
063    public void configure(Configuration configuration) throws ConfigurationException
064    {
065        _name = configuration.getAttribute("name");
066        _label = I18nizableText.parseI18nizableText(configuration.getChild("label"), "plugin.odf");
067        _description = I18nizableText.parseI18nizableText(configuration.getChild("description"), "plugin.odf");
068    }
069    
070    @Override
071    public Object[] getValues(Content content)
072    {
073        OrgUnit orgUnit = (OrgUnit) content;
074        Set<String> values = new HashSet<>();
075        values.add(orgUnit.getId());
076        
077        OrgUnit ancestor = orgUnit.getParentOrgUnit();
078        while (ancestor != null)
079        {
080            values.add(ancestor.getId());
081            ancestor = ancestor.getParentOrgUnit();
082        }
083        
084        return values.toArray(new String[values.size()]);
085    }
086
087    @Override
088    public String getName()
089    {
090        return _name;
091    }
092
093    @Override
094    public I18nizableText getLabel()
095    {
096        return _label;
097    }
098
099    @Override
100    public I18nizableText getDescription()
101    {
102        return _description;
103    }
104
105    @Override
106    public MetadataType getType()
107    {
108        return MetadataType.CONTENT;
109    }
110}