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