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;
028import org.ametys.runtime.i18n.I18nizableText;
029
030import com.opensymphony.workflow.WorkflowException;
031
032/**
033 * OSWorkflow function to create a course by copy of another
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 _getObjectType(Map transientVars, Map args)
045    {
046        return CourseFactory.COURSE_NODETYPE;
047    }
048    
049    @Override
050    protected Set<String> _getChildrenReferencesName()
051    {
052        return Set.of(Course.CHILD_COURSE_LISTS);
053    }
054
055    @Override
056    protected Map<String, Object> getAdditionalCopyMap(Map transientVars, Content baseContent, String viewName, String fallbackViewName) throws WorkflowException
057    {
058        Map<String, Object> copyMap = super.getAdditionalCopyMap(transientVars, baseContent, viewName, fallbackViewName);
059        ModifiableContent targetContent = (ModifiableContent) getResultsMap(transientVars).get(CONTENT_KEY);
060        
061        // Always copy courseParts
062        Map<String, Object> childReferenceMap = new HashMap<>();
063        childReferenceMap.put("$mode", "create");
064        childReferenceMap.put(CreateCoursePartByCopyFunction.COURSE_HOLDER_KEY, targetContent.getId());
065        copyMap.put(Course.CHILD_COURSE_PARTS, childReferenceMap);
066        
067        return copyMap;
068    }
069
070    @Override
071    protected boolean _isCompatibleParent(Content parent)
072    {
073        return parent instanceof CourseList;
074    }
075
076    @Override
077    protected String _getParentAttributeName(Content parent)
078    {
079        return Course.PARENT_COURSE_LISTS;
080    }
081    
082    @Override
083    public I18nizableText getLabel()
084    {
085        return new I18nizableText("plugin.odf", "PLUGINS_ODF_CREATE_COURSE_BY_COPY_FUNCTION_LABEL");
086    }
087}