001/*
002 *  Copyright 2023 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;
021
022import org.apache.commons.lang3.tuple.Pair;
023import org.slf4j.Logger;
024
025import org.ametys.cms.repository.Content;
026import org.ametys.cms.repository.ModifiableContent;
027import org.ametys.odf.course.Course;
028import org.ametys.odf.coursepart.CoursePart;
029import org.ametys.plugins.odfsync.apogee.scc.AbstractApogeeSynchronizableContentsWithCatalogCollection;
030
031/**
032 * SCC for course part contents.
033 */
034public class CoursePartSynchronizableContentsCollection extends AbstractApogeeSynchronizableContentsWithCatalogCollection
035{
036    /** SCC model id */
037    public static final String MODEL_ID = "org.ametys.plugins.odfsync.apogee.scc.coursepart";
038    
039    @Override
040    protected List<Map<String, Object>> _search(Map<String, Object> searchParameters, Logger logger)
041    {
042        return _convertBigDecimal(_apogeeDAO.searchCourseParts(getDataSourceId(), getParameterValues(), searchParameters));
043    }
044
045    @Override
046    protected String getMappingName()
047    {
048        return "coursepart";
049    }
050
051    @Override
052    protected String getChildrenAttributeName()
053    {
054        return null;
055    }
056    
057    @Override
058    protected Pair<String, Object> getParentAttribute(ModifiableContent parent)
059    {
060        if (parent instanceof Course)
061        {
062            return Pair.of(CoursePart.PARENT_COURSES, new ModifiableContent[] {parent});
063        }
064
065        return super.getParentAttribute(parent);
066    }
067    
068    @Override
069    protected Map<String, Map<String, List<Object>>> getTransformedRemoteValues(Map<String, Object> searchParameters, Logger logger)
070    {
071        Map<String, Map<String, List<Object>>> remoteValuesByContent = super.getTransformedRemoteValues(searchParameters, logger);
072        
073        if (searchParameters.containsKey("parentCode"))
074        {
075            // Retrieve the content id of the parent, needed to set course holder
076            String contentId = Optional.of(CourseSynchronizableContentsCollection.MODEL_ID)
077                    .map(_sccHelper::getSCCFromModelId)
078                    .map(courseSCC -> courseSCC.getContent(_apogeeSCCHelper.getSynchronizationLang(), (String) searchParameters.get("parentCode")))
079                    .map(Content::getId)
080                    .orElse(null);
081            
082            if (contentId != null)
083            {
084                // Add it to each map of values as the course holder
085                remoteValuesByContent.values()
086                    .stream()
087                    .forEach(remoteValues -> remoteValues.put(CoursePart.COURSE_HOLDER, List.of(contentId)));
088            }
089        }
090        
091        return remoteValuesByContent;
092    }
093}