001/*
002 *  Copyright 2017 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.workflow.copy;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.ametys.cms.repository.Content;
022import org.ametys.cms.repository.ModifiableContent;
023import org.ametys.cms.workflow.copy.CreateContentByCopyFunction;
024import org.ametys.odf.ProgramItem;
025import org.ametys.odf.course.Course;
026import org.ametys.odf.courselist.CourseList;
027import org.ametys.odf.workflow.copy.CopyODFContentClientSideElement.DuplicationMode;
028
029import com.opensymphony.workflow.WorkflowException;
030
031/**
032 * OSWorkflow function to create a program item by copy of another
033 * 
034 */
035public abstract class AbstractCreateProgramItemByCopyFunction extends AbstractCreateODFContentByCopyFunction
036{
037    @Override
038    protected void _populateAdditionalData(Map transientVars, ModifiableContent content) throws WorkflowException
039    {
040        super._populateAdditionalData(transientVars, content);
041        
042        // Generate new code
043        content.getMetadataHolder().setMetadata(ProgramItem.METADATA_CODE, org.ametys.core.util.StringUtils.generateKey().toUpperCase());
044    }
045    
046    @Override
047    protected Map<String, Object> _buildCopyMap(Map transientVars, Content baseContent, String metadataSetName, String metadataSetType)
048    {
049        @SuppressWarnings("unchecked")
050        Map<String, Object> copyMap = (Map<String, Object>) transientVars.get(CreateContentByCopyFunction.COPY_MAP_KEY);
051        Map<String, Object> buildCopyMap = _copyContentMetadataHelper.buildCopyMap(baseContent, metadataSetName != null ? metadataSetName : "main", metadataSetType != null ? metadataSetType : "edition");
052        if (copyMap.containsKey(CopyODFContentClientSideElement.KEEP_CREATION_TITLE_KEY))
053        {
054            //We leave the creation title, so we remove title attribute and the metadata title will not be copied
055            buildCopyMap.remove("title");
056        }
057        copyMap.putAll(buildCopyMap);
058        
059        copyMap.put(ProgramItem.METADATA_CATALOG, null);
060        
061        DuplicationMode duplicationMode = copyMap.containsKey(CopyODFContentClientSideElement.DUPLICATION_MODE_KEY) ? DuplicationMode.valueOf((String) copyMap.get(CopyODFContentClientSideElement.DUPLICATION_MODE_KEY)) : DuplicationMode.SINGLE;
062        
063        Map<String, Object> childReferenceMap = new HashMap<>();
064        childReferenceMap.put(CopyODFContentClientSideElement.DUPLICATION_MODE_KEY, duplicationMode.toString());
065        switch (duplicationMode)
066        {
067            case SINGLE:
068                childReferenceMap.put("$mode", "reference");
069                copyMap.put(_getChildReferenceMetadataName(), childReferenceMap);
070                break;
071            case STRUCTURE_ONLY:
072                if (baseContent instanceof CourseList || baseContent instanceof Course)
073                {
074                    childReferenceMap.put("$mode", "reference");
075                    copyMap.put(_getChildReferenceMetadataName(), childReferenceMap);
076                }
077                else
078                {
079                    childReferenceMap.put("$mode", "create");
080                    childReferenceMap.put("$loaded", true);
081                    copyMap.put(_getChildReferenceMetadataName(), childReferenceMap);
082                }
083                break;
084            case FULL:
085                childReferenceMap.put("$mode", "create");
086                childReferenceMap.put("$loaded", true);
087                copyMap.put(_getChildReferenceMetadataName(), childReferenceMap);
088                break;
089            default:
090                break;
091        }
092        
093        return copyMap;
094    }
095    
096    /**
097     * Get the metadata name holding the child relation between the content to create and its children
098     * @return the metadata name
099     */
100    protected abstract String _getChildReferenceMetadataName();
101}