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.plugins.bpm.workflowsdef;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.context.Context;
021import org.apache.avalon.framework.context.ContextException;
022import org.apache.avalon.framework.context.Contextualizable;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026import org.apache.cocoon.components.ContextHelper;
027import org.apache.cocoon.environment.Request;
028
029import org.ametys.plugins.bpm.BPMWorkflowManager;
030
031import com.opensymphony.module.propertyset.PropertySet;
032import com.opensymphony.workflow.Register;
033import com.opensymphony.workflow.WorkflowContext;
034import com.opensymphony.workflow.WorkflowException;
035import com.opensymphony.workflow.spi.WorkflowEntry;
036
037/**
038 * Registers the value of a workflow variable
039 */
040public class RegisterVariable implements Register, Serviceable, Contextualizable
041{
042    private BPMWorkflowManager _bpmWorkflowManager;
043    private Context _context;
044
045    @Override
046    public void service(ServiceManager manager) throws ServiceException
047    {
048        _bpmWorkflowManager = (BPMWorkflowManager) manager.lookup(BPMWorkflowManager.ROLE);
049    }
050
051    public void contextualize(Context context) throws ContextException
052    {
053        _context = context;
054    }
055    
056    public Object registerVariable(WorkflowContext context, WorkflowEntry entry, Map args, PropertySet ps) throws WorkflowException
057    {
058        Request request = ContextHelper.getRequest(_context);
059        String workflowId = (String) request.getAttribute("workflowId");
060        String variableId = (String) args.get("variableId");
061        
062        if (workflowId == null || variableId == null)
063        {
064            return null;
065        }
066        
067        return _bpmWorkflowManager.getWorkflowVariable(workflowId, variableId);
068    }
069
070}