001/*
002 *  Copyright 2019 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.ose.export.utils;
017
018import java.util.Collection;
019import java.util.Collections;
020import java.util.List;
021import java.util.Objects;
022import java.util.Optional;
023import java.util.Set;
024import java.util.stream.Collectors;
025
026import org.apache.avalon.framework.service.ServiceException;
027import org.apache.avalon.framework.service.ServiceManager;
028import org.apache.commons.lang3.StringUtils;
029
030import org.ametys.cms.data.ContentValue;
031import org.ametys.cms.repository.Content;
032import org.ametys.odf.ProgramItem;
033import org.ametys.odf.course.Course;
034import org.ametys.odf.enumeration.OdfReferenceTableEntry;
035import org.ametys.odf.enumeration.OdfReferenceTableHelper;
036import org.ametys.odf.orgunit.OrgUnit;
037import org.ametys.odf.ose.export.OSEConstants;
038import org.ametys.odf.ose.export.impl.odf.LogUtils;
039import org.ametys.odf.program.AbstractProgram;
040import org.ametys.odf.program.Container;
041import org.ametys.plugins.odfpilotage.helper.PilotageHelper;
042import org.ametys.plugins.repository.AmetysObjectResolver;
043import org.ametys.plugins.repository.AmetysRepositoryException;
044import org.ametys.runtime.model.ModelHelper;
045
046/**
047 * A retriever used like a helper to retrieve some ODF elements from ODF items.
048 */
049public class ElementRetriever extends PilotageHelper
050{
051    /** Avalon Role */
052    public static final String ROLE = ElementRetriever.class.getName();
053    
054    /** The cache id for step holders by program item */
055    private static final String __STEP_HOLDERS_BY_ITEM_CACHE_ID = ElementRetriever.class.getName() + "$stepHoldersByItem";
056    
057    /** The resolver */
058    protected AmetysObjectResolver _resolver;
059
060    /** The helper for reference tables from ODF */
061    protected OdfReferenceTableHelper _refTableHelper; 
062     
063    @Override
064    public void service(ServiceManager manager) throws ServiceException
065    {
066        super.service(manager);
067        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
068        _refTableHelper = (OdfReferenceTableHelper) manager.lookup(OdfReferenceTableHelper.ROLE);
069    }
070
071    @Override
072    protected String _getStepsHolderByItemCacheId()
073    {
074        // The cache id is different than its superclass
075        return __STEP_HOLDERS_BY_ITEM_CACHE_ID;
076    }
077    
078    @Override
079    protected Set<Container> _getStepsToCache(ProgramItem programItem, String yearId)
080    {
081        return _mustNotExport(programItem) ? Collections.EMPTY_SET : super._getStepsToCache(programItem, yearId);
082    }
083    
084    private boolean _mustNotExport(ProgramItem programItem)
085    {
086        if (programItem instanceof Course || programItem instanceof Container)
087        {
088            return ((Content) programItem).getValue(OSEConstants.NO_OSE_EXPORT_ATTRIBUTE_NAME, false, false);
089        }
090        else
091        {
092            return false;
093        }
094    }
095    
096    /**
097     * Retrieve the {@link OrgUnit}s of a given {@link ProgramItem}
098     * @param programItem The program item
099     * @return the {@link OrgUnit}s of the given program item
100     */
101    public Set<OrgUnit> retrieveOrgUnits(ProgramItem programItem)
102    {
103        return _retrieveOrgUnits(programItem);
104    }
105    
106    private Set<OrgUnit> _retrieveOrgUnits(ProgramItem parent)
107    {
108        return Optional.of(parent)
109                .map(this::_getDirectOrgUnit)
110                .map(Collections::singleton)
111                .orElseGet(() -> _retrieveOrgUnitsFromParents(parent));
112    }
113    
114    private Set<OrgUnit> _retrieveOrgUnitsFromParents(ProgramItem programItem)
115    {
116        return _odfHelper.getParentProgramItems(programItem)
117                .stream()
118                .map(this::_retrieveOrgUnits)
119                .flatMap(Set::stream)
120                .collect(Collectors.toSet());
121    }
122    
123    private OrgUnit _getDirectOrgUnit(ProgramItem programItem)
124    {
125        List<String> orgUnitIds = programItem.getOrgUnits();
126        return _resolveAllAndGetFirst(orgUnitIds, (ProgramItem & Content) programItem);
127    }
128    
129    private <T extends ProgramItem & Content> OrgUnit _resolveAllAndGetFirst(Collection<String> orgUnitIds, T programElement)
130    {
131        List<OrgUnit> orgUnits = orgUnitIds.stream()
132                .map(this::_resolve)
133                .filter(Objects::nonNull)
134                .collect(Collectors.toList());
135        
136        if (orgUnits.size() > 1)
137        {
138            LogUtils.programElementWarningOrgUnits(getLogger(), programElement, orgUnits);
139        }
140        
141        return orgUnits.isEmpty() ? null : orgUnits.get(0);
142    }
143    
144    private OrgUnit _resolve(String id)
145    {
146        try
147        {
148            return _resolver.resolveById(id);
149        }
150        catch (AmetysRepositoryException e)
151        {
152            // Silently fail
153            return null;
154        }
155    }
156
157    /**
158     * Retrieve the degrees of a given {@link Container}
159     * @param container The container
160     * @return the degrees of the given container
161     */
162    public Set<OdfReferenceTableEntry> retrieveDegree(Container container)
163    {
164        return _odfHelper.getParentPrograms(container)
165                .stream()
166                .map(p -> p.<ContentValue>getValue(AbstractProgram.DEGREE))
167                .filter(Objects::nonNull)
168                .map(ContentValue::getContentIfExists)
169                .flatMap(Optional::stream)
170                .map(OdfReferenceTableEntry::new)
171                .collect(Collectors.toSet());
172    }
173
174    /**
175     * Get the potential steps holder of the program item. A step is a container of year type and it can be set manually on intermediate courses.
176     * @param programItem The {@link ProgramItem} on which we have to retrieve the potential steps holder
177     * @return the potential steps holder of the program item
178     */
179    public Set<Container> retrieveStepsHolder(ProgramItem programItem)
180    {
181        Optional<String> yearId = getYearId();
182        if (yearId.isPresent())
183        {
184            return _getStepsHolder(programItem, yearId.get());
185        }
186        
187        return Set.of();
188    }
189    
190    /**
191     * Get the potential period types of the program item. It can be retrieved on courses or containers. The algorithm doesn't search in the parent of semester containers.
192     * @param programItem The {@link ProgramItem} on which we have to retrieve the potential period types
193     * @return the potential period types of the program item
194     */
195    public Set<OdfReferenceTableEntry> retrievePeriodTypes(ProgramItem programItem)
196    {
197        return getSemesterId()
198            .map(id -> _retrievePeriodTypes(programItem, id))
199            .orElse(Collections.EMPTY_SET);
200    }
201
202    private Set<OdfReferenceTableEntry> _retrievePeriodTypes(ProgramItem programItem, String semesterId)
203    {
204        return _getPeriodType(programItem) // Try to get the period type on the current program item
205            .map(Collections::singleton)
206            .orElseGet(() -> _searchPeriodTypesInParents(programItem, semesterId)); // Or search in the parent program items
207    }
208    
209    private Set<OdfReferenceTableEntry> _searchPeriodTypesInParents(ProgramItem programItem, String semesterId)
210    {
211        if (isContainerOfNature(programItem, semesterId))
212        {
213            return Collections.EMPTY_SET;
214        }
215        
216        // Only if programItem not a semester
217        return _odfHelper.getParentProgramItems(programItem) // Get the direct parent program items
218            .stream()
219            .map(parent -> _retrievePeriodTypes(parent, semesterId)) // Retrieve the period types for all parents
220            .flatMap(Set::stream)
221            .collect(Collectors.toSet());
222    }
223    
224    private Optional<OdfReferenceTableEntry> _getPeriodType(ProgramItem programItem)
225    {
226        return Optional.of(programItem)
227            .map(Content.class::cast) // Cast to Content (ProgramItem is always a Content)
228            .filter(c -> ModelHelper.hasModelItem("period", c.getModel())) // Filter if the attribute "period" is not in the model
229            .map(c -> c.<ContentValue>getValue("period")) // Get the period
230            .flatMap(ContentValue::getContentIfExists)
231            .map(c -> c.<ContentValue>getValue("type")) // Get the period type
232            .flatMap(ContentValue::getContentIfExists)
233            .map(OdfReferenceTableEntry::new);
234    }
235
236    
237    /**
238     * Get the semester container nature identifier.
239     * @return an {@link Optional} of the semester identifier
240     */
241    public Optional<String> getSemesterId()
242    {
243        return Optional.of(_refTableHelper)
244                .map(rth -> rth.getItemFromCode(OdfReferenceTableHelper.CONTAINER_NATURE, "semestre"))
245                .map(OdfReferenceTableEntry::getId)
246                .filter(StringUtils::isNotBlank);
247    }
248}