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.odf.course.Course;
025import org.ametys.odf.course.CourseFactory;
026import org.ametys.odf.workflow.CreateCourseFunction;
027
028import com.opensymphony.workflow.WorkflowException;
029
030/**
031 * OSWorkflow function to create a course by copy of another
032 */
033public class CreateCourseByCopyFunction extends AbstractCreateProgramItemByCopyFunction
034{
035    @Override
036    protected String _getContentNamePrefix()
037    {
038        return CreateCourseFunction.CONTENT_NAME_PREFIX;
039    }
040    
041    @Override
042    protected String _getNodeType()
043    {
044        return CourseFactory.COURSE_NODETYPE;
045    }
046    
047    @Override
048    protected Set<String> _getChildrenReferencesName()
049    {
050        return Set.of(Course.CHILD_COURSE_LISTS);
051    }
052
053    @Override
054    protected Map<String, Object> getAdditionalCopyMap(Map transientVars, Content baseContent, String viewName, String fallbackViewName) throws WorkflowException
055    {
056        Map<String, Object> copyMap = super.getAdditionalCopyMap(transientVars, baseContent, viewName, fallbackViewName);
057        ModifiableContent targetContent = (ModifiableContent) getResultsMap(transientVars).get(CONTENT_KEY);
058        
059        // Always copy courseParts
060        Map<String, Object> childReferenceMap = new HashMap<>();
061        childReferenceMap.put("$mode", "create");
062        childReferenceMap.put(CreateCoursePartByCopyFunction.COURSE_HOLDER_KEY, targetContent.getId());
063        copyMap.put(Course.CHILD_COURSE_PARTS, childReferenceMap);
064        
065        return copyMap;
066    }
067}