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.Program;
026
027/**
028 * The catalog copy updater to update the other ODF content attribute
029 */
030public class ProgramItemReferencesCopyUpdater extends AbstractProgramItemAttributeCopyUpdater
031{
032    @Override
033    public void updateContents(String initialCatalogName, String newCatalogName, Map<Content, Content> copiedContents, Content targetParentContent)
034    {
035        Map<Content, Content> copiedAbstractPrograms = copiedContents.keySet()
036                                                                     .stream()
037                                                                     .filter(AbstractProgram.class::isInstance)
038                                                                     .collect(Collectors.toMap(Function.identity(), copiedContents::get));
039        
040        Map<Content, Content> copiedPrograms = copiedAbstractPrograms.keySet()
041                                                                     .stream()
042                                                                     .filter(Program.class::isInstance)
043                                                                     .collect(Collectors.toMap(Function.identity(), copiedContents::get));
044        
045        for (Content copiedContent : copiedContents.values())
046        {
047            if (copiedContent instanceof AbstractProgram abstractProgram)
048            {
049                _updateContentAttribute(abstractProgram, "furtherStudyPrograms", copiedAbstractPrograms); // multiple - abstractProgram
050                _updateContentAttribute(abstractProgram, "furtherStudyDistribution/program", copiedAbstractPrograms); // repeater / simple - abstractProgram
051                abstractProgram.saveChanges();
052            }
053            else if (copiedContent instanceof Course course)
054            {
055                _updateContentAttribute(course, "shareable-programs", copiedPrograms); // multiple - container
056                course.saveChanges();
057            }
058        }
059    }
060}