001/*
002 *  Copyright 2024 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.course;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.avalon.framework.service.Serviceable;
023
024import org.ametys.cms.repository.Content;
025import org.ametys.core.user.CurrentUserProvider;
026import org.ametys.odf.content.CopyODFContentUpdater;
027import org.ametys.odf.courselist.CourseList;
028import org.ametys.runtime.plugin.component.AbstractLogEnabled;
029
030/**
031 * Copy updater to initialize shareable fields on {@link Course}.
032 */
033public class ShareableCourseCopyUpdater extends AbstractLogEnabled implements CopyODFContentUpdater, Serviceable
034{
035    /** The current user provider */
036    protected CurrentUserProvider _currentUserProvider;
037    
038    /** The shareable course helper */
039    protected ShareableCourseHelper _shareableCourseHelper;
040    
041    public void service(ServiceManager manager) throws ServiceException
042    {
043        _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
044        _shareableCourseHelper = (ShareableCourseHelper) manager.lookup(ShareableCourseHelper.ROLE);
045    }
046    
047    @Override
048    public void updateContents(String initialCatalogName, String newCatalogName, Map<Content, Content> copiedContents, Content targetParentContent)
049    {
050        for (Content copiedContent : copiedContents.values())
051        {
052            if (copiedContent instanceof Course copiedCourse)
053            {
054                // Get created parent course list of created course content
055                CourseList courseListParent = copiedCourse.getParentCourseLists()
056                        .stream()
057                        .filter(c -> copiedContents.containsValue(c)) // filter on created course list by the copy
058                        .findFirst()
059                        .orElseGet(() -> (CourseList) targetParentContent);               
060                
061                if (_shareableCourseHelper.initializeShareableFields(copiedCourse, courseListParent, _currentUserProvider.getUser(), false))
062                {
063                    copiedCourse.saveChanges();
064                    
065                    // Create a new version
066                    copiedCourse.checkpoint();
067                }
068            }
069        }
070    }
071}