001/*
002 *  Copyright 2023 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.program.properties;
017
018import java.util.Set;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022
023import org.ametys.cms.data.ContentValue;
024import org.ametys.cms.data.type.indexing.IndexableElementType;
025import org.ametys.cms.model.CMSDataContext;
026import org.ametys.cms.model.properties.AbstractContentProperty;
027import org.ametys.cms.repository.Content;
028import org.ametys.cms.repository.ModifiableContent;
029import org.ametys.cms.search.model.CriterionDefinitionAwareElementDefinition;
030import org.ametys.cms.search.model.CriterionDefinitionHelper;
031import org.ametys.cms.search.model.IndexationAwareElementDefinition;
032import org.ametys.cms.search.model.IndexationAwareElementDefinitionHelper;
033import org.ametys.odf.ODFHelper;
034import org.ametys.odf.ProgramItem;
035import org.ametys.odf.coursepart.CoursePart;
036import org.ametys.odf.program.SubProgramFactory;
037
038/**
039 * Property to get the parent subprograms of a program item or course part.
040 */
041public class ParentSubProgramsProperty extends AbstractContentProperty<Content> implements CriterionDefinitionAwareElementDefinition<ContentValue>, IndexationAwareElementDefinition<ContentValue, Content>
042{
043    /** The ODF Helper */
044    protected ODFHelper _odfHelper;
045    /** The criterion definition helper */
046    protected CriterionDefinitionHelper _criterionDefinitionHelper;
047    /** The indexation aware element definition helper */
048    protected IndexationAwareElementDefinitionHelper _indexationAwareElementDefinitionHelper;
049    
050    @Override
051    public void service(ServiceManager manager) throws ServiceException
052    {
053        super.service(manager);
054        _odfHelper = (ODFHelper) manager.lookup(ODFHelper.ROLE);
055        _criterionDefinitionHelper = (CriterionDefinitionHelper) manager.lookup(CriterionDefinitionHelper.ROLE);
056        _indexationAwareElementDefinitionHelper = (IndexationAwareElementDefinitionHelper) manager.lookup(IndexationAwareElementDefinitionHelper.ROLE);
057    }
058    
059    @Override
060    protected Set<? extends ModifiableContent> _getLinkedContents(Content content)
061    {
062        if (content instanceof ProgramItem programItem)
063        {
064            return _odfHelper.getParentSubPrograms(programItem, true);
065        }
066        if (content instanceof CoursePart coursePart)
067        {
068            return _odfHelper.getParentSubPrograms(coursePart, true);
069        }
070        return null;
071    }
072    
073    @Override
074    public boolean isMultiple()
075    {
076        return true;
077    }
078
079    public String getContentTypeId()
080    {
081        return SubProgramFactory.SUBPROGRAM_CONTENT_TYPE;
082    }
083    
084    public IndexableElementType getDefaultCriterionType()
085    {
086        CMSDataContext context = CMSDataContext.newInstance()
087                                               .withModelItem(this);
088        
089        String typeId = getType().getDefaultCriterionTypeId(context);
090        return _criterionDefinitionHelper.getCriterionDefinitionType(typeId);
091    }
092    
093    public String getSolrSortFieldName()
094    {
095        return _indexationAwareElementDefinitionHelper.getDefaultSolrSortFieldName(this);
096    }
097}