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.courselist.CourseList;
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 */
034public class CreateCourseByCopyFunction extends AbstractCreateProgramItemByCopyFunction
035{
036    @Override
037    protected String _getContentNamePrefix()
038    {
039        return CreateCourseFunction.CONTENT_NAME_PREFIX;
040    }
041    
042    @Override
043    protected String _getObjectType(Map transientVars, Map args)
044    {
045        return CourseFactory.COURSE_NODETYPE;
046    }
047    
048    @Override
049    protected Set<String> _getChildrenReferencesName()
050    {
051        return Set.of(Course.CHILD_COURSE_LISTS);
052    }
053
054    @Override
055    protected Map<String, Object> getAdditionalCopyMap(Map transientVars, Content baseContent, String viewName, String fallbackViewName) throws WorkflowException
056    {
057        Map<String, Object> copyMap = super.getAdditionalCopyMap(transientVars, baseContent, viewName, fallbackViewName);
058        ModifiableContent targetContent = (ModifiableContent) getResultsMap(transientVars).get(CONTENT_KEY);
059        
060        // Always copy courseParts
061        Map<String, Object> childReferenceMap = new HashMap<>();
062        childReferenceMap.put("$mode", "create");
063        childReferenceMap.put(CreateCoursePartByCopyFunction.COURSE_HOLDER_KEY, targetContent.getId());
064        copyMap.put(Course.CHILD_COURSE_PARTS, childReferenceMap);
065        
066        return copyMap;
067    }
068
069    @Override
070    protected boolean _isCompatibleParent(Content parent)
071    {
072        return parent instanceof CourseList;
073    }
074
075    @Override
076    protected String _getParentAttributeName(Content parent)
077    {
078        return Course.PARENT_COURSE_LISTS;
079    }
080}