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.cms.workflow; 017 018import java.util.Collections; 019import java.util.Enumeration; 020import java.util.HashMap; 021import java.util.Map; 022 023import org.apache.avalon.framework.parameters.Parameters; 024import org.apache.cocoon.environment.ObjectModelHelper; 025import org.apache.cocoon.environment.Redirector; 026import org.apache.cocoon.environment.Request; 027 028import org.ametys.core.cocoon.ActionResultGenerator; 029import org.ametys.plugins.workflow.AbstractWorkflowComponent; 030import org.ametys.plugins.workflow.cocoon.InitializeWorkflowAction; 031import org.ametys.plugins.workflow.support.WorkflowProvider.AmetysObjectWorkflow; 032 033import com.opensymphony.workflow.InvalidInputException; 034import com.opensymphony.workflow.WorkflowException; 035 036/** 037 * Action for initializing a transition on a content's workflow.<p> 038 * The following parameters are supported: 039 * <dl> 040 * <dt>workflowName 041 * <dd>the name of the workflow to instantiate 042 * <dt>actionId 043 * <dd>the id of the action to fire 044 * </dl> 045 */ 046public class InitializeContentWorkflowAction extends InitializeWorkflowAction 047{ 048 /** Constant for storing the content type to use into the transient variables map. */ 049 private static final String __CONTENT_TYPE_KEY = CreateContentFunction.class.getName() + "$contentType"; 050 051 private static final String REQUEST_PARAMETER_PREFIX = "workflow-"; 052 053 @Override 054 protected Map _act(Redirector redirector, Map objectModel, String source, Parameters parameters, int actionId, Map inputs) throws InvalidInputException, WorkflowException 055 { 056 AmetysObjectWorkflow workflow = _workflowProvider.getAmetysObjectWorkflow(); 057 long workflowId = workflow.initialize(_getWorkflowName(objectModel, source, parameters), actionId, inputs); 058 return _getActionResult(redirector, objectModel, source, parameters, workflowId); 059 } 060 061 @Override 062 protected Map<String, Object> _getInputs(Redirector redirector, Map objectModel, String source, Parameters parameters) throws Exception 063 { 064 Map<String, Object> inputs = super._getInputs(redirector, objectModel, source, parameters); 065 066 // Provide the parameters 067 inputs.putAll(_getParameters(objectModel)); 068 069 // Provide a map for providing data to the generator 070 Request request = ObjectModelHelper.getRequest(objectModel); 071 Map<String, Object> result = new HashMap<>(); 072 request.setAttribute(ActionResultGenerator.MAP_REQUEST_ATTR, result); 073 inputs.put(AbstractWorkflowComponent.RESULT_MAP_KEY, result); 074 075 return inputs; 076 } 077 078 @Override 079 protected String _getWorkflowName(Map objectModel, String source, Parameters parameters) 080 { 081 return ObjectModelHelper.getRequest(objectModel).getParameter(REQUEST_PARAMETER_PREFIX + "workflowName"); 082 } 083 084 /** 085 * Return a map of parameters needed by the workflow that will create the content 086 * @param objectModel The cocoon object model 087 * @return the map with parameters 088 */ 089 protected Map<String, Object> _getParameters(Map objectModel) 090 { 091 Map<String, Object> parameters = new HashMap<>(); 092 093 Request request = ObjectModelHelper.getRequest(objectModel); 094 Enumeration<String> parameterNames = request.getParameterNames(); 095 096 while (parameterNames.hasMoreElements()) 097 { 098 String parameterName = parameterNames.nextElement(); 099 if (parameterName.startsWith(REQUEST_PARAMETER_PREFIX)) 100 { 101 String name = parameterName.substring(REQUEST_PARAMETER_PREFIX.length()); 102 String value = request.getParameter(parameterName); 103 104 if (name.equals(__CONTENT_TYPE_KEY)) 105 { 106 parameters.put(CreateContentFunction.CONTENT_TYPES_KEY, new String[] {value}); 107 } 108 else 109 { 110 parameters.put(name, value); 111 } 112 } 113 } 114 115 return parameters; 116 } 117 118 @Override 119 protected Map _processWorkflowException(Redirector redirector, Map objectModel, String source, Parameters parameters, long actionId, WorkflowException e) throws Exception 120 { 121 if (e instanceof InvalidInputWorkflowException) 122 { 123 // Validation error 124 if (getLogger().isDebugEnabled()) 125 { 126 getLogger().debug("Validation error while executing workflow action", e); 127 } 128 129 return Collections.EMPTY_MAP; 130 } 131 else 132 { 133 // Real error 134 return super._processWorkflowException(redirector, objectModel, source, parameters, actionId, e); 135 } 136 } 137}