001/*
002 *  Copyright 2026 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.Optional;
019import java.util.Set;
020import java.util.stream.Collectors;
021import java.util.stream.Stream;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025
026import org.ametys.cms.data.ContentValue;
027import org.ametys.cms.model.properties.AbstractIndexableContentProperty;
028import org.ametys.cms.repository.Content;
029import org.ametys.cms.repository.ModifiableContent;
030import org.ametys.odf.ODFHelper;
031import org.ametys.odf.ProgramItem;
032import org.ametys.odf.enumeration.OdfReferenceTableHelper;
033
034/**
035 * Property on the computed period.
036 */
037public class ComputedPeriodProperty extends AbstractIndexableContentProperty<Content>
038{
039    private ODFHelper _odfHelper;
040
041    @Override
042    public void service(ServiceManager manager) throws ServiceException
043    {
044        super.service(manager);
045        _odfHelper = (ODFHelper) manager.lookup(ODFHelper.ROLE);
046    }
047    
048    public String getContentTypeId()
049    {
050        return OdfReferenceTableHelper.PERIOD;
051    }
052    
053    @Override
054    protected Set<? extends ModifiableContent> _getLinkedContents(Content content)
055    {
056        Set<ModifiableContent> periods = _getPeriods(content)
057                .distinct()
058                .collect(Collectors.toSet());
059        
060        // Do not return periods if there are multiple
061        return periods.size() > 1 ? Set.of() : periods;
062    }
063    
064    private Stream<ModifiableContent> _getPeriods(Content content)
065    {
066        return Optional.of(content)
067            .filter(c -> c.hasValue("period"))
068            .map(c -> c.<ContentValue>getValue("period"))
069            .flatMap(ContentValue::getContentIfExists)
070            .map(Stream::of)
071            // If not found, get periods from parents
072            .orElseGet(() -> _getPeriodsFromParents((ProgramItem) content));
073    }
074    
075    private Stream<ModifiableContent> _getPeriodsFromParents(ProgramItem programItem)
076    {
077        return _odfHelper.getParentProgramItems(programItem)
078            .stream()
079            .map(Content.class::cast)
080            .flatMap(this::_getPeriods);
081    }
082}