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.odf.skill;
017
018import java.util.Optional;
019import java.util.Set;
020import java.util.stream.Collectors;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024
025import org.ametys.cms.data.ContentValue;
026import org.ametys.cms.data.type.indexing.IndexableElementType;
027import org.ametys.cms.model.CMSDataContext;
028import org.ametys.cms.model.properties.AbstractContentProperty;
029import org.ametys.cms.model.properties.Property;
030import org.ametys.cms.repository.Content;
031import org.ametys.cms.repository.ModifiableContent;
032import org.ametys.cms.search.model.CriterionDefinitionAwareElementDefinition;
033import org.ametys.cms.search.model.CriterionDefinitionHelper;
034import org.ametys.cms.search.model.IndexationAwareElementDefinition;
035import org.ametys.cms.search.model.IndexationAwareElementDefinitionHelper;
036import org.ametys.odf.enumeration.OdfReferenceTableHelper;
037import org.ametys.odf.program.AbstractProgram;
038
039/**
040 * {@link Property} for acquired skills of child courses on a {@link AbstractProgram}
041 */
042public class ProgramSkillsProperty extends AbstractContentProperty<Content> implements CriterionDefinitionAwareElementDefinition<ContentValue>, IndexationAwareElementDefinition<ContentValue, Content>
043{
044    /** The name of property holding the computed acquired skills */
045    public static final String PROGRAM_SKILLS_PROPERTY_NAME = "acquiredSkills";
046    
047    private ODFSkillsHelper _skillsHelper;
048    private CriterionDefinitionHelper _criterionDefinitionHelper;
049    private IndexationAwareElementDefinitionHelper _indexationAwareElementDefinitionHelper;
050    
051    @Override
052    public void service(ServiceManager manager) throws ServiceException
053    {
054        super.service(manager);
055        _skillsHelper = (ODFSkillsHelper) manager.lookup(ODFSkillsHelper.ROLE);
056        _criterionDefinitionHelper = (CriterionDefinitionHelper) manager.lookup(CriterionDefinitionHelper.ROLE);
057        _indexationAwareElementDefinitionHelper = (IndexationAwareElementDefinitionHelper) manager.lookup(IndexationAwareElementDefinitionHelper.ROLE);
058    }
059    
060    @Override
061    protected Set< ? extends ModifiableContent> _getLinkedContents(Content content)
062    {
063        if (content instanceof AbstractProgram abstractProgram)
064        {
065            Set<ModifiableContent> values = _skillsHelper.getComputedSkills(abstractProgram, 2)
066                                           .stream()
067                                           .map(ContentValue::getContentIfExists)
068                                           .flatMap(Optional::stream)
069                                           .collect(Collectors.toSet());
070            return values;
071        }
072        
073        return null;
074    }
075    
076    @Override
077    public boolean isMultiple()
078    {
079        return true;
080    }
081    
082    public String getContentTypeId()
083    {
084        return OdfReferenceTableHelper.SKILL;
085    }
086    
087    public IndexableElementType getDefaultCriterionType()
088    {
089        CMSDataContext context = CMSDataContext.newInstance()
090                                               .withModelItem(this);
091        
092        String typeId = getType().getDefaultCriterionTypeId(context);
093        return _criterionDefinitionHelper.getCriterionDefinitionType(typeId);
094    }
095    
096    public String getSolrSortFieldName()
097    {
098        return _indexationAwareElementDefinitionHelper.getDefaultSolrSortFieldName(this);
099    }
100}