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.Map;
019
020import org.ametys.cms.repository.Content;
021import org.ametys.cms.repository.ModifiableContent;
022import org.ametys.cms.workflow.copy.CreateContentByCopyFunction;
023import org.ametys.odf.coursepart.CoursePart;
024import org.ametys.odf.coursepart.CoursePartFactory;
025import org.ametys.odf.workflow.CreateCoursePartFunction;
026
027import com.opensymphony.workflow.WorkflowException;
028
029/**
030 * OSWorkflow function to create a course part by copy of another
031 */
032public class CreateCoursePartByCopyFunction extends AbstractCreateODFContentByCopyFunction
033{
034    /** Constant for storing the course holder */
035    public static final String COURSE_HOLDER_KEY = CreateCoursePartByCopyFunction.class.getName() + "$courseHolder";
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(CoursePart.METADATA_CODE, org.ametys.core.util.StringUtils.generateKey().toUpperCase());
044        
045        // Add the course holder from the copy map
046        Map<String, Object> copyMap = (Map<String, Object>) transientVars.get(CreateContentByCopyFunction.COPY_MAP_KEY);
047        if (copyMap.containsKey(COURSE_HOLDER_KEY))
048        {
049            content.getMetadataHolder().setMetadata(CoursePart.METADATA_COURSE_HOLDER, (String) copyMap.get(COURSE_HOLDER_KEY));
050        }
051    }
052    
053    @Override
054    protected Map<String, Object> _buildCopyMap(Map transientVars, Content baseContent, String metadataSetName, String metadataSetType)
055    {
056        // Do the same as ProgramItem copy except the title is always kept
057        Map<String, Object> copyMap = (Map<String, Object>) transientVars.get(CreateContentByCopyFunction.COPY_MAP_KEY);
058        copyMap.putAll(_copyContentMetadataHelper.buildCopyMap(baseContent, metadataSetName != null ? metadataSetName : "main", metadataSetType != null ? metadataSetType : "edition"));
059        copyMap.put(CoursePart.METADATA_CATALOG, null);
060        return copyMap;
061    }
062    
063    @Override
064    protected String _getContentNamePrefix()
065    {
066        return CreateCoursePartFunction.CONTENT_NAME_PREFIX;
067    }
068    
069    @Override
070    protected String _getNodeType()
071    {
072        return CoursePartFactory.COURSE_PART_NODETYPE;
073    }
074}