001/*
002 *  Copyright 2018 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.coursepart.CoursePart;
026import org.ametys.odf.coursepart.CoursePartFactory;
027import org.ametys.odf.workflow.CreateCoursePartFunction;
028
029import com.opensymphony.workflow.WorkflowException;
030
031/**
032 * OSWorkflow function to create a course part by copy of another
033 */
034public class CreateCoursePartByCopyFunction extends AbstractCreateODFContentByCopyFunction
035{
036    /** Constant for storing the course holder */
037    public static final String COURSE_HOLDER_KEY = "$courseHolder";
038    
039    @Override
040    protected void _populateAdditionalData(Map transientVars, ModifiableContent content) throws WorkflowException
041    {
042        super._populateAdditionalData(transientVars, content);
043        
044        // Generate new code
045        content.setValue(CoursePart.CODE, org.ametys.core.util.StringUtils.generateKey().toUpperCase());
046        
047        // copy catalog
048        // FIXME remove as soon as the canWrite returns really true at creation
049        content.setValue(CoursePart.CATALOG, getBaseContentForCopy(transientVars).getValue(ProgramItem.CATALOG));
050        
051        // Add the course holder from the copy map
052        @SuppressWarnings("unchecked")
053        Map<String, Object> copyMap = (Map<String, Object>) transientVars.get(CreateContentByCopyFunction.COPY_MAP_KEY);
054        if (copyMap.containsKey(COURSE_HOLDER_KEY))
055        {
056            content.setValue(CoursePart.COURSE_HOLDER, copyMap.get(COURSE_HOLDER_KEY));
057        }
058    }
059    
060    @Override
061    protected Map<String, Object> getAdditionalCopyMap(Map transientVars, Content baseContent, String viewName, String fallbackViewName)
062    {
063        Map<String, Object> additionalCopyMap = new HashMap<>();
064        additionalCopyMap.put(CoursePart.CATALOG, null);
065        
066        // Do the same as ProgramItem copy except the title is always kept
067        
068        return additionalCopyMap;
069    }
070    
071    @Override
072    protected String _getContentNamePrefix()
073    {
074        return CreateCoursePartFunction.CONTENT_NAME_PREFIX;
075    }
076    
077    @Override
078    protected String _getNodeType()
079    {
080        return CoursePartFactory.COURSE_PART_NODETYPE;
081    }
082}