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.model.properties.AbstractContentProperty;
027import org.ametys.cms.model.properties.Property;
028import org.ametys.cms.repository.Content;
029import org.ametys.cms.repository.ModifiableContent;
030import org.ametys.odf.enumeration.OdfReferenceTableHelper;
031import org.ametys.odf.program.AbstractProgram;
032
033/**
034 * {@link Property} for acquired skills of child courses on a {@link AbstractProgram}
035 */
036public class ProgramSkillsProperty extends AbstractContentProperty<Content>
037{
038    /** The name of indexing field holding the computed acquired skills */
039    public static final String PROGRAM_SKILLS_PROPERTY_NAME = "acquiredSkills";
040    
041    private ODFSkillsHelper _skillsHelper;
042    
043    @Override
044    public void service(ServiceManager manager) throws ServiceException
045    {
046        super.service(manager);
047        _skillsHelper = (ODFSkillsHelper) manager.lookup(ODFSkillsHelper.ROLE);
048    }
049    
050    @Override
051    protected Set< ? extends ModifiableContent> _getLinkedContents(Content content)
052    {
053        if (content instanceof AbstractProgram abstractProgram)
054        {
055            Set<ModifiableContent> values = _skillsHelper.getComputedSkills(abstractProgram, 2)
056                                           .stream()
057                                           .map(ContentValue::getContentIfExists)
058                                           .filter(Optional::isPresent)
059                                           .map(Optional::get)
060                                           .collect(Collectors.toSet());
061            return values;
062        }
063        
064        return null;
065    }
066    
067    @Override
068    public boolean isMultiple()
069    {
070        return true;
071    }
072    
073    public String getContentTypeId()
074    {
075        return OdfReferenceTableHelper.SKILL;
076    }
077}