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