001/*
002 *  Copyright 2018 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.catalog;
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.odf.course.Course;
025import org.ametys.odf.coursepart.CoursePart;
026import org.ametys.odf.program.Program;
027import org.ametys.plugins.repository.AmetysObjectResolver;
028import org.ametys.runtime.plugin.component.AbstractLogEnabled;
029
030/**
031 * Copy updater to update the course holder on a {@link CoursePart}.
032 */
033public class CoursePartHolderUpdater extends AbstractLogEnabled implements CopyCatalogUpdater, Serviceable
034{
035    private AmetysObjectResolver _resolver;
036    
037    @Override
038    public void service(ServiceManager manager) throws ServiceException
039    {
040        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
041    }
042    
043    @Override
044    public void updateContent(String initialCatalogName, String newCatalogName, Program initialProgram, Program createdProgram)
045    {
046        // Nothing to do
047    }
048
049    @Override
050    public void updateContents(String initialCatalogName, String newCatalogName, Map<String, String> copiedPrograms, Map<String, String> copiedSubPrograms, Map<String, String> copiedContainers, Map<String, String> copiedCourseLists, Map<String, String> copiedCourses, Map<String, String> copiedCourseParts)
051    {
052        for (String copiedCoursePartId : copiedCourseParts.values())
053        {
054            CoursePart copiedCoursePart = _resolver.resolveById(copiedCoursePartId);
055            if (copiedCoursePart == null)
056            {
057                getLogger().warn("There is no course part with the identifier '{}'.", copiedCoursePartId);
058            }
059            else
060            {
061                Course courseHolder = copiedCoursePart.getCourseHolder();
062                if (courseHolder == null)
063                {
064                    getLogger().warn("The course part '{}' of the catalog '{}' haven't any course holder.", copiedCoursePart.getTitle(), copiedCoursePart.getCatalog());
065                }
066                else
067                {
068                    String copiedCourseHolder = copiedCourses.get(courseHolder.getId());
069                    if (copiedCourseHolder == null)
070                    {
071                        getLogger().warn("The course part '{}' of the catalog '{}' can't get the corresponding course holder with the identifier '{}'.", copiedCoursePart.getTitle(), copiedCoursePart.getCatalog(), courseHolder.getId());
072                    }
073                    else
074                    {
075                        copiedCoursePart.getMetadataHolder().setMetadata(CoursePart.METADATA_COURSE_HOLDER, copiedCourseHolder);
076                        copiedCoursePart.saveChanges();
077                    }
078                }
079            }
080        }
081    }
082}