001/*
002 *  Copyright 2025 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.plugins.odfsync.apogee.scc.impl;
017
018import java.util.List;
019import java.util.Map;
020import java.util.Optional;
021import java.util.stream.Stream;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.commons.lang3.StringUtils;
026
027import org.ametys.cms.repository.Content;
028import org.ametys.odf.ProgramItem;
029import org.ametys.odf.enumeration.OdfReferenceTableEntry;
030import org.ametys.odf.enumeration.OdfReferenceTableHelper;
031import org.ametys.odf.program.Container;
032import org.ametys.plugins.odfsync.apogee.scc.AbstractPreviousYearsSynchronizableContentsCollection;
033
034/**
035 * SCC for nbStudents fields for container contents.
036 */
037public class ContainerNbStudentsSynchronizableContentsCollection extends AbstractPreviousYearsSynchronizableContentsCollection
038{
039    /** The ODF reference table helper */
040    protected OdfReferenceTableHelper _odfRefTableHelper;
041    
042    private Optional<String> _yearId = Optional.empty();
043    
044    @Override
045    public void service(ServiceManager manager) throws ServiceException
046    {
047        super.service(manager);
048        _odfRefTableHelper = (OdfReferenceTableHelper) manager.lookup(OdfReferenceTableHelper.ROLE);
049    }
050    
051    @Override
052    public String getIdField()
053    {
054        return ProgramItem.CODE;
055    }
056    
057    @Override
058    protected String getCurrentYearAttributeName()
059    {
060        return "numberOfStudentsCurrentYear";
061    }
062    
063    @Override
064    protected String getPrecedingYearAttributeName()
065    {
066        return "numberOfStudentsPrecedingYear";
067    }
068    
069    @Override
070    protected Stream<Content> getContents()
071    {
072        return super.getContents().filter(container -> isContainerOfTypeYear((Container) container));
073    }
074    
075    @Override
076    protected String getSyncCodeItemName()
077    {
078        return "apogeeSyncCodeNbStudents";
079    }
080    
081    @Override
082    protected List<Map<String, Object>> executeApogeeRequest(Map<String, Object> parameters)
083    {
084        return _apogeePreviousYearsFieldsDAO.getYearNbStudents(getDataSourceId(), getParameterValues(), parameters);
085    }
086    
087    /**
088     * Determine if the container nature equals to "annee"
089     * @param container The container
090     * @return <code>true</code> if the current container nature equals to "annee"
091     * @deprecated For 5.0, use org.ametys.plugins.odfpilotage.helper.PilotageHelper#isContainerOfTypeYear(Container), this method will be removed.
092     */
093    @Deprecated
094    private boolean isContainerOfTypeYear(Container container)
095    {
096        return getYearId()
097                .map(id -> StringUtils.equals(id, container.getNature()))
098                .orElse(false);
099    }
100    
101    /**
102     * Get the year container nature identifier.
103     * @return an {@link Optional} of the year identifier
104     * @deprecated For 5.0, use org.ametys.plugins.odfpilotage.helper.PilotageHelper#getYearId(Container), this method will be removed.
105     */
106    @Deprecated
107    private synchronized Optional<String> getYearId()
108    {
109        if (_yearId.isEmpty())
110        {
111            _yearId = Optional.of(_odfRefTableHelper)
112                .map(rth -> rth.getItemFromCode(OdfReferenceTableHelper.CONTAINER_NATURE, "annee"))
113                .map(OdfReferenceTableEntry::getId)
114                .filter(StringUtils::isNotBlank);
115        }
116        return _yearId;
117    }
118}