001/*
002 *  Copyright 2026 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;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022
023import org.ametys.cms.repository.WorkflowAwareContent;
024import org.ametys.cms.workflow.AbstractContentFunction;
025import org.ametys.cms.workflow.ContentWorkflowHelper;
026import org.ametys.cms.workflow.EditContentFunction;
027import org.ametys.odf.course.Course;
028import org.ametys.odf.coursepart.CoursePart;
029import org.ametys.plugins.repository.AmetysObjectResolver;
030import org.ametys.runtime.i18n.I18nizableText;
031
032import com.opensymphony.module.propertyset.PropertySet;
033import com.opensymphony.workflow.WorkflowException;
034
035/**
036 * If course part is modified, the course holder has to be in draft state too.
037 */
038public class DraftCourseHolderFunction extends AbstractContentFunction
039{
040    /** Ametys object resolver */
041    protected AmetysObjectResolver _resolver;
042    
043    /** Content workflow helper */
044    protected ContentWorkflowHelper _contentWorkflowHelper;
045
046    @Override
047    public void service(ServiceManager smanager) throws ServiceException
048    {
049        super.service(smanager);
050        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
051        _contentWorkflowHelper = (ContentWorkflowHelper) smanager.lookup(ContentWorkflowHelper.ROLE);
052    }
053    
054    @Override
055    public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException
056    {
057        // Retrieve current content
058        WorkflowAwareContent content = getContent(transientVars);
059        
060        if (content instanceof CoursePart coursePart)
061        {
062            Course courseHolder = coursePart.getCourseHolder();
063            
064            /* If the course holder:
065             *  - exists,
066             *  - is not the initial caller of the current workflow action (by invert relations),
067             *  - contains the current course part as a child,
068             *  - is not in draft state,
069             *  - has the action 22 available (move to draft)
070             * Then move it to draft state.
071             */
072            if (courseHolder != null
073                    && !courseHolder.getId().equals(getContextParameters(transientVars).get(EditContentFunction.INITIAL_CONTENT_ID))
074                    && courseHolder.getCourseParts().contains(coursePart)
075                    && courseHolder.getCurrentStepId() != 1
076                    && _contentWorkflowHelper.isAvailableAction(courseHolder, 22))
077            {
078                _contentWorkflowHelper.editContent(courseHolder, Map.of(), 22);
079            }
080        }
081    }
082    
083    public I18nizableText getLabel()
084    {
085        return new I18nizableText("plugin.odf", "PLUGINS_ODF_MOVE_DRAFT_COURSE_HOLDER_FUNCTION_LABEL");
086    }
087    
088    @Override
089    public FunctionType getFunctionExecType()
090    {
091        return FunctionType.POST;
092    }
093}