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.List;
020import java.util.Map;
021import java.util.Optional;
022import java.util.Set;
023
024import org.ametys.cms.repository.Content;
025import org.ametys.cms.repository.ModifiableContent;
026import org.ametys.cms.workflow.copy.CreateContentByCopyFunction;
027import org.ametys.odf.ProgramItem;
028import org.ametys.odf.courselist.CourseList;
029import org.ametys.odf.workflow.copy.CopyODFContentClientSideElement.DuplicationMode;
030import org.ametys.plugins.repository.data.holder.values.SynchronizableValue;
031import org.ametys.plugins.repository.data.holder.values.SynchronizableValue.Mode;
032
033import com.opensymphony.workflow.WorkflowException;
034
035/**
036 * OSWorkflow function to create a program item by copy of another
037 */
038public abstract class AbstractCreateProgramItemByCopyFunction extends AbstractCreateODFContentByCopyFunction
039{
040    @Override
041    protected Map<String, Object> getAdditionalCopyMap(Map transientVars, Content baseContent, String viewName, String fallbackViewName) throws WorkflowException
042    {
043        Map<String, Object> additionalCopyMap = new HashMap<>();
044        additionalCopyMap.put(ProgramItem.CATALOG, null);
045        
046        @SuppressWarnings("unchecked")
047        DuplicationMode duplicationMode = Optional.ofNullable(transientVars)
048                .map(tv -> tv.get(CreateContentByCopyFunction.COPY_MAP_KEY))
049                .map(copyMap -> (Map<String, Object>) copyMap)
050                .map(copyMap -> copyMap.get(CopyODFContentClientSideElement.DUPLICATION_MODE_KEY))
051                .map(String.class::cast)
052                .map(DuplicationMode::valueOf)
053                .orElse(DuplicationMode.SINGLE);
054        
055        Set<String> childrendReferencesNames = _getChildrenReferencesName();
056        if (!childrendReferencesNames.isEmpty())
057        {
058            boolean isValidDuplicationMode = true;
059            Map<String, Object> childReferenceMap = new HashMap<>();
060            childReferenceMap.put(CopyODFContentClientSideElement.DUPLICATION_MODE_KEY, duplicationMode.toString());
061            switch (duplicationMode)
062            {
063                case SINGLE:
064                    childReferenceMap.put("$mode", "reference");
065                    break;
066                case STRUCTURE_ONLY:
067                    if (baseContent instanceof CourseList)
068                    {
069                        childReferenceMap.put("$mode", "reference");
070                    }
071                    else
072                    {
073                        childReferenceMap.put("$mode", "create");
074                    }
075                    break;
076                case FULL:
077                    childReferenceMap.put("$mode", "create");
078                    break;
079                default:
080                    isValidDuplicationMode = false;
081                    break;
082            }
083            
084            if (isValidDuplicationMode)
085            {
086                for (String childrendReferenceName : childrendReferencesNames)
087                {
088                    additionalCopyMap.put(childrendReferenceName, childReferenceMap);
089                }
090            }
091        }
092        
093        return additionalCopyMap;
094    }
095    
096    @Override
097    protected SynchronizableValue _buildParentSynchronizableValue(Content parent)
098    {
099        SynchronizableValue value = new SynchronizableValue(List.of(parent.getId()));
100        value.setMode(Mode.APPEND);
101        return value;
102    }
103    
104    @Override
105    protected void _populateAdditionalData(Map transientVars, ModifiableContent content) throws WorkflowException
106    {
107        super._populateAdditionalData(transientVars, content);
108        
109        // Generate new code
110        content.setValue(ProgramItem.CODE, org.ametys.core.util.StringUtils.generateKey().toUpperCase());
111        
112        // copy catalog
113        // FIXME remove as soon as the canWrite returns really true at creation
114        content.setValue(ProgramItem.CATALOG, getBaseContentForCopy(transientVars).getValue(ProgramItem.CATALOG));
115    }
116    
117    /**
118     * Get the attributes names holding the children relations between the content to create and its children
119     * @return the attributes names
120     */
121    protected abstract Set<String> _getChildrenReferencesName();
122}