001/*
002 *  Copyright 2010 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;
019import java.util.Set;
020
021import org.apache.avalon.framework.activity.Initializable;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024
025import org.ametys.cms.repository.WorkflowAwareContent;
026import org.ametys.cms.workflow.AbstractContentWorkflowComponent;
027import org.ametys.cms.workflow.ContentWorkflowHelper;
028import org.ametys.odf.program.Program;
029import org.ametys.odf.program.ProgramPart;
030import org.ametys.plugins.repository.AmetysObjectResolver;
031import org.ametys.plugins.repository.AmetysRepositoryException;
032
033import com.opensymphony.module.propertyset.PropertySet;
034import com.opensymphony.workflow.FunctionProvider;
035import com.opensymphony.workflow.WorkflowException;
036
037/**
038 * OSWorkflow function to edit a {@link Program} content.
039 */
040public class ChangeProgramStateFunction extends AbstractContentWorkflowComponent implements FunctionProvider, Initializable
041{
042    private static final int __EDIT_WORKFLOW_ACTION_ID = 22;
043    
044    /** Ametys object resolver. */
045    protected AmetysObjectResolver _resolver;
046    /** Content workflow helper */
047    protected ContentWorkflowHelper _contentWorkflowHelper;
048    
049    public void initialize() throws Exception
050    {
051        _contentWorkflowHelper = (ContentWorkflowHelper) _manager.lookup(ContentWorkflowHelper.ROLE);
052    }
053    
054    @Override
055    public void service(ServiceManager manager) throws ServiceException
056    {
057        super.service(manager);
058        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
059    }
060    
061    @Override
062    public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException
063    {
064        // Retrieve current content
065        WorkflowAwareContent content = getContent(transientVars);
066        
067        if (content instanceof ProgramPart)
068        {
069            Set<Program> programs = ((ProgramPart) content).getRootPrograms();
070            
071            for (Program program : programs)
072            {
073                try
074                {
075                    if (program.getCurrentStepId() != 1)
076                    {
077                        _contentWorkflowHelper.doAction(program, __EDIT_WORKFLOW_ACTION_ID);
078                    }
079                }
080                catch (AmetysRepositoryException e)
081                {
082                    // FIXME : le current step n'existe pas si la formation viens d'etre créée.
083                }
084            }
085        }
086    }
087
088    /**
089     * Retrieve the content associated with the workflow.
090     * @param transientVars the parameters from the call.
091     * @return the content.
092     * @throws WorkflowException if the content is not found.
093     */
094    @Override
095    protected WorkflowAwareContent getContent(Map transientVars) throws WorkflowException
096    {
097        WorkflowAwareContent content = (WorkflowAwareContent) transientVars.get(CONTENT_KEY);
098        
099        if (content == null)
100        {
101            content = getContent(transientVars);
102            
103            if (content == null)
104            {
105                @SuppressWarnings("unchecked")
106                Map<String, Object> map = (Map<String, Object>) transientVars.get(RESULT_MAP_KEY);
107                if (map != null)
108                {
109                    String contentId = (String) map.get("contentId");
110                    if (contentId != null)
111                    {
112                        content = _resolver.resolveById(contentId);
113                    }
114                    else
115                    {
116                        throw new WorkflowException("Unable to retrieve content");
117                    }
118                }
119            }
120        }
121        
122        // Found in transient variables map
123        return content;
124    }
125}