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.HashSet;
020import java.util.Map;
021import java.util.Set;
022
023import org.ametys.cms.repository.Content;
024import org.ametys.cms.repository.ModifiableContent;
025import org.ametys.odf.course.Course;
026import org.ametys.odf.course.CourseFactory;
027import org.ametys.odf.workflow.CreateCourseFunction;
028
029import com.opensymphony.workflow.WorkflowException;
030
031/**
032 * OSWorkflow function to create a course by copy of another
033 * 
034 */
035public class CreateCourseByCopyFunction extends AbstractCreateProgramItemByCopyFunction
036{
037    @Override
038    protected String _getContentNamePrefix()
039    {
040        return CreateCourseFunction.CONTENT_NAME_PREFIX;
041    }
042    
043    @Override
044    protected String _getNodeType()
045    {
046        return CourseFactory.COURSE_NODETYPE;
047    }
048    
049    @Override
050    protected void _populateAdditionalData(Map transientVars, ModifiableContent content) throws WorkflowException
051    {
052        // Add this variable for later
053        transientVars.put(CreateCoursePartByCopyFunction.COURSE_HOLDER_KEY, content.getId());
054        super._populateAdditionalData(transientVars, content);
055    }
056
057    @Override
058    protected Set<String> _getChildReferenceMetadataName()
059    {
060        Set<String> childReferenceMetadataNames = new HashSet<>();
061        childReferenceMetadataNames.add(Course.METADATA_CHILD_COURSE_LISTS);
062        return childReferenceMetadataNames;
063    }
064
065    @Override
066    protected Map<String, Object> _buildCopyMap(Map transientVars, Content baseContent, String metadataSetName, String metadataSetType)
067    {
068        Map<String, Object> copyMap = super._buildCopyMap(transientVars, baseContent, metadataSetName, metadataSetType);
069        
070        // Always copy courseParts
071        Map<String, Object> childReferenceMap = new HashMap<>();
072        childReferenceMap.put("$mode", "create");
073        childReferenceMap.put("$loaded", true);
074        childReferenceMap.put(CreateCoursePartByCopyFunction.COURSE_HOLDER_KEY, transientVars.get(CreateCoursePartByCopyFunction.COURSE_HOLDER_KEY));
075        copyMap.put(Course.METADATA_CHILD_COURSE_PARTS, childReferenceMap);
076        
077        return copyMap;
078    }
079}