001/*
002 *  Copyright 2020 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.content;
017
018import java.util.Map;
019
020import org.ametys.cms.content.CopyReport;
021import org.ametys.cms.data.ContentValue;
022import org.ametys.cms.repository.Content;
023import org.ametys.cms.repository.ModifiableContent;
024import org.ametys.odf.courselist.CourseList;
025import org.ametys.odf.program.TraversableProgramPart;
026import org.ametys.odf.workflow.copy.AbstractCreateODFContentByCopyFunction;
027import org.ametys.odf.workflow.copy.CopyODFContentClientSideElement;
028import org.ametys.runtime.model.ElementDefinition;
029
030/**
031 * Component used for copy of content with some override for ODF content
032 * See {@link org.ametys.cms.content.CopyContentComponent}.
033 */
034public class CopyContentComponent extends org.ametys.cms.content.CopyContentComponent
035{
036    /** The avalon role */
037    @SuppressWarnings("hiding")
038    public static final String ROLE = CopyContentComponent.class.getName();
039    
040    @Override
041    protected ContentValue handleLinkedContent(ElementDefinition definition, ModifiableContent content, boolean referenceMode, Map<String, Object> copyMap, CopyReport copyReport)
042    {
043        String path = definition.getPath();
044        
045        // Override for the ODF specific case for copy of a ProgramPart in reference mode
046        // All child contents can be referenced EXCEPT the course lists which have to be always be copied (created by copy)
047        if (path.equals(TraversableProgramPart.CHILD_PROGRAM_PARTS) && referenceMode)
048        {
049            if (content instanceof CourseList)
050            {
051                // Force copy of child course lists (force create mode)
052                String targetContentId = copyLinkedContent(content, copyMap, copyReport);
053                if (targetContentId != null)
054                {
055                    return new ContentValue(_resolver, targetContentId);
056                }
057            }
058            else
059            {
060                // keep reference for other children
061                return new ContentValue(content);
062            }
063        }
064        else
065        {
066            return super.handleLinkedContent(definition, content, referenceMode, copyMap, copyReport);
067        }
068        
069        return null;
070    }
071    
072    @Override
073    protected Map<String, Object> getInputsForCopy(Content baseContent, String title, Map<String, Object> copyMap, String targetContentType, CopyReport copyReport)
074    {
075        Map<String, Object> inputs =  super.getInputsForCopy(baseContent, title, copyMap, targetContentType, copyReport);
076        
077        String parentId = copyMap != null ? (String) copyMap.get(CopyODFContentClientSideElement.PARENT_KEY) : null;
078        if (parentId != null)
079        {
080            inputs.put(AbstractCreateODFContentByCopyFunction.PARENT_KEY, parentId);
081        }
082        
083        return inputs;
084    }
085}