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.Set;
019import java.util.stream.Collectors;
020
021import org.apache.avalon.framework.configuration.Configurable;
022import org.apache.avalon.framework.configuration.Configuration;
023import org.apache.avalon.framework.configuration.ConfigurationException;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.avalon.framework.service.Serviceable;
027
028import org.ametys.cms.contenttype.MetadataType;
029import org.ametys.cms.contenttype.indexing.CustomIndexingField;
030import org.ametys.cms.data.ContentValue;
031import org.ametys.cms.repository.Content;
032import org.ametys.odf.program.AbstractProgram;
033import org.ametys.runtime.i18n.I18nizableText;
034
035/**
036 * {@link CustomIndexingField} indexing acquired skills of child courses on a {@link AbstractProgram}
037 */
038public class ProgramSkillsIndexingField implements CustomIndexingField, Configurable, Serviceable
039{
040    /** The name of indexing field holding the computed acquired skills */
041    public static final String PROGRAM_SKILLS_INDEXING_FIELD = "indexedAcquiredSkills";
042    
043    /** The field name */
044    protected String _name;
045    /** The label field */
046    protected I18nizableText _label;
047    /** The description field */
048    protected I18nizableText _description;
049    /** The skills helper */
050    protected ODFSkillsHelper _skillsHelper;
051    
052    public void service(ServiceManager smanager) throws ServiceException
053    {
054        _skillsHelper = (ODFSkillsHelper) smanager.lookup(ODFSkillsHelper.ROLE);
055    }
056    
057    @Override
058    public void configure(Configuration configuration) throws ConfigurationException
059    {
060        _name = configuration.getAttribute("name");
061        _label = I18nizableText.parseI18nizableText(configuration.getChild("label"), "plugin.odf");
062        _description = I18nizableText.parseI18nizableText(configuration.getChild("description"), "plugin.odf");
063    }
064    
065    public Object[] getValues(Content content)
066    {
067        if (content instanceof AbstractProgram)
068        {
069            Set<ContentValue> values = _skillsHelper.getComputedSkills((AbstractProgram) content, 2);
070            Set<String> ids = values.stream()
071                .map(ContentValue::getContentId)
072                .collect(Collectors.toSet());
073            
074            return ids.toArray(new String[ids.size()]);
075        }
076        
077        return null;
078    }
079    
080    @Override
081    public String getName()
082    {
083        return _name;
084    }
085
086    @Override
087    public I18nizableText getLabel()
088    {
089        return _label;
090    }
091
092    @Override
093    public I18nizableText getDescription()
094    {
095        return _description;
096    }
097    
098    @Override
099    public MetadataType getType()
100    {
101        return MetadataType.CONTENT;
102    }
103    
104}