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