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.HashMap;
019import java.util.List;
020import java.util.Map;
021
022import org.ametys.cms.repository.Content;
023import org.ametys.cms.repository.ModifiableContent;
024import org.ametys.cms.workflow.copy.CreateContentByCopyFunction;
025import org.ametys.odf.ProgramItem;
026import org.ametys.odf.course.Course;
027import org.ametys.odf.coursepart.CoursePart;
028import org.ametys.odf.coursepart.CoursePartFactory;
029import org.ametys.odf.workflow.CreateCoursePartFunction;
030import org.ametys.plugins.repository.data.holder.values.SynchronizableValue;
031import org.ametys.plugins.repository.data.holder.values.SynchronizableValue.Mode;
032import org.ametys.plugins.repository.jcr.NameHelper.NameComputationMode;
033import org.ametys.runtime.i18n.I18nizableText;
034
035import com.opensymphony.workflow.WorkflowException;
036
037/**
038 * OSWorkflow function to create a course part by copy of another
039 */
040public class CreateCoursePartByCopyFunction extends AbstractCreateODFContentByCopyFunction
041{
042    /** Constant for storing the course holder */
043    public static final String COURSE_HOLDER_KEY = "$courseHolder";
044    
045    @Override
046    protected void _populateAdditionalData(Map transientVars, ModifiableContent content) throws WorkflowException
047    {
048        super._populateAdditionalData(transientVars, content);
049        
050        // Generate new code
051        content.setValue(CoursePart.CODE, org.ametys.core.util.StringUtils.generateKey().toUpperCase());
052        
053        // copy catalog
054        // FIXME remove as soon as the canWrite returns really true at creation
055        content.setValue(CoursePart.CATALOG, getBaseContentForCopy(transientVars).getValue(ProgramItem.CATALOG));
056        
057        // Add the course holder from the copy map
058        @SuppressWarnings("unchecked")
059        Map<String, Object> copyMap = (Map<String, Object>) transientVars.get(CreateContentByCopyFunction.COPY_MAP_KEY);
060        if (copyMap.containsKey(COURSE_HOLDER_KEY))
061        {
062            content.setValue(CoursePart.COURSE_HOLDER, copyMap.get(COURSE_HOLDER_KEY));
063        }
064    }
065    
066    @Override
067    protected Map<String, Object> getAdditionalCopyMap(Map transientVars, Content baseContent, String viewName, String fallbackViewName)
068    {
069        Map<String, Object> additionalCopyMap = new HashMap<>();
070        additionalCopyMap.put(CoursePart.CATALOG, null);
071        
072        // Do the same as ProgramItem copy except the title is always kept
073        
074        return additionalCopyMap;
075    }
076    
077    @Override
078    protected String _getContentNamePrefix()
079    {
080        return CreateCoursePartFunction.CONTENT_NAME_PREFIX;
081    }
082    
083    @Override
084    protected String _getObjectType(Map transientVars, Map args)
085    {
086        return CoursePartFactory.COURSE_PART_NODETYPE;
087    }
088    
089    @Override
090    protected NameComputationMode _getDefaultNameComputationMode()
091    {
092        return NameComputationMode.GENERATED_KEY;
093    }
094    
095    @Override
096    protected Content _getParent(Map transientVars)
097    {
098        return null;
099    }
100
101    @Override
102    protected boolean _isCompatibleParent(Content parent)
103    {
104        return parent instanceof Course;
105    }
106
107    @Override
108    protected String _getParentAttributeName(Content parent)
109    {
110        return CoursePart.PARENT_COURSES;
111    }
112
113    @Override
114    protected SynchronizableValue _buildParentSynchronizableValue(Content parent)
115    {
116        SynchronizableValue value = new SynchronizableValue(List.of(parent.getId()));
117        value.setMode(Mode.APPEND);
118        return value;
119    }
120    
121    @Override
122    public I18nizableText getLabel()
123    {
124        return new I18nizableText("plugin.odf", "PLUGINS_ODF_CREATE_COURSE_PART_BY_COPY_FUNCTION_LABEL");
125    }
126}