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;
017
018import java.util.HashSet;
019import java.util.Set;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023
024import org.ametys.cms.data.ContentValue;
025import org.ametys.cms.data.type.indexing.IndexableElementType;
026import org.ametys.cms.model.CMSDataContext;
027import org.ametys.cms.model.properties.AbstractContentProperty;
028import org.ametys.cms.model.properties.Property;
029import org.ametys.cms.repository.ModifiableContent;
030import org.ametys.cms.search.model.CriterionDefinitionAwareElementDefinition;
031import org.ametys.cms.search.model.CriterionDefinitionHelper;
032import org.ametys.cms.search.model.IndexationAwareElementDefinition;
033import org.ametys.cms.search.model.IndexationAwareElementDefinitionHelper;
034
035/**
036 * {@link Property} to get all orgUnit ancestors id in one field.
037 */
038public class OrgUnitAncestorProperty extends AbstractContentProperty<OrgUnit> implements CriterionDefinitionAwareElementDefinition<ContentValue>, IndexationAwareElementDefinition<ContentValue, OrgUnit>
039{
040    /** The criterion definition helper */
041    protected CriterionDefinitionHelper _criterionDefinitionHelper;
042    /** The indexation aware element definition helper */
043    protected IndexationAwareElementDefinitionHelper _indexationAwareElementDefinitionHelper;
044    
045    @Override
046    public void service(ServiceManager manager) throws ServiceException
047    {
048        super.service(manager);
049        _criterionDefinitionHelper = (CriterionDefinitionHelper) manager.lookup(CriterionDefinitionHelper.ROLE);
050        _indexationAwareElementDefinitionHelper = (IndexationAwareElementDefinitionHelper) manager.lookup(IndexationAwareElementDefinitionHelper.ROLE);
051    }
052    
053    @Override
054    protected Set< ? extends ModifiableContent> _getLinkedContents(OrgUnit orgUnit)
055    {
056        Set<OrgUnit> values = new HashSet<>();
057        values.add(orgUnit);
058        
059        OrgUnit ancestor = orgUnit.getParentOrgUnit();
060        while (ancestor != null)
061        {
062            values.add(ancestor);
063            ancestor = ancestor.getParentOrgUnit();
064        }
065        
066        return values;
067    }
068    
069    @Override
070    public boolean isMultiple()
071    {
072        return true;
073    }
074    
075    public String getContentTypeId()
076    {
077        return OrgUnitFactory.ORGUNIT_CONTENT_TYPE;
078    }
079    
080    @Override
081    public String getDefaultCriterionWidget()
082    {
083        return "edition.select-orgunit";
084    }
085    
086    public IndexableElementType getDefaultCriterionType()
087    {
088        CMSDataContext context = CMSDataContext.newInstance()
089                                               .withModelItem(this);
090        
091        String typeId = getType().getDefaultCriterionTypeId(context);
092        return _criterionDefinitionHelper.getCriterionDefinitionType(typeId);
093    }
094    
095    public String getSolrSortFieldName()
096    {
097        return _indexationAwareElementDefinitionHelper.getDefaultSolrSortFieldName(this);
098    }
099}