001/*
002 *  Copyright 2019 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;
019import java.util.function.Function;
020import java.util.stream.Collectors;
021
022import org.ametys.cms.repository.Content;
023import org.ametys.odf.course.Course;
024import org.ametys.odf.program.AbstractProgram;
025import org.ametys.odf.program.Container;
026import org.ametys.odf.program.Program;
027
028/**
029 * The catalog copy updater to update the other ODF content attribute
030 */
031public class ProgramItemReferencesCopyUpdater extends AbstractProgramItemAttributeCopyUpdater
032{
033    @Override
034    public void updateContents(String initialCatalogName, String newCatalogName, Map<Content, Content> copiedContents, Content targetParentContent)
035    {
036        Map<Content, Content> copiedAbstractPrograms = copiedContents.keySet()
037                                                                     .stream()
038                                                                     .filter(AbstractProgram.class::isInstance)
039                                                                     .collect(Collectors.toMap(Function.identity(), copiedContents::get));
040        
041        Map<Content, Content> copiedPrograms = copiedAbstractPrograms.keySet()
042                                                                     .stream()
043                                                                     .filter(Program.class::isInstance)
044                                                                     .collect(Collectors.toMap(Function.identity(), copiedContents::get));
045        
046        Map<Content, Content> copiedContainers = copiedContents.keySet()
047                .stream()
048                .filter(Container.class::isInstance)
049                .collect(Collectors.toMap(Function.identity(), copiedContents::get));
050        
051        for (Content copiedContent : copiedContents.values())
052        {
053            if (copiedContent instanceof AbstractProgram abstractProgram)
054            {
055                _updateContentAttribute(abstractProgram, "furtherStudyPrograms", copiedAbstractPrograms); // multiple - abstractProgram
056                _updateContentAttribute(abstractProgram, "furtherStudyDistribution/program", copiedAbstractPrograms); // repeater / simple - abstractProgram
057                abstractProgram.saveChanges();
058            }
059            else if (copiedContent instanceof Container container)
060            {
061                _updateContentAttribute(container, "previousYears", copiedContainers); // multiple - container
062                _updateContentAttribute(container, "nextYears", copiedContainers); // multiple - container
063                container.saveChanges();
064            }
065            else if (copiedContent instanceof Course course)
066            {
067                _updateContentAttribute(course, "shareable-programs", copiedPrograms); // multiple - container
068                course.saveChanges();
069            }
070        }
071    }
072}