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 /** Constant for storing the content type to use into the transient variables map. */ 051 private static final String _CONTENT_MIXINS_KEY = CreateContentFunction.class.getName() + "$mixins"; 052 053 private static final String REQUEST_PARAMETER_PREFIX = "workflow-"; 054 055 @Override 056 protected Map _act(Redirector redirector, Map objectModel, String source, Parameters parameters, int actionId, Map inputs) throws InvalidInputException, WorkflowException 057 { 058 AmetysObjectWorkflow workflow = _workflowProvider.getAmetysObjectWorkflow(); 059 long workflowId = workflow.initialize(_getWorkflowName(objectModel, source, parameters), actionId, inputs); 060 return _getActionResult(redirector, objectModel, source, parameters, workflowId); 061 } 062 063 @Override 064 protected Map<String, Object> _getInputs(Redirector redirector, Map objectModel, String source, Parameters parameters) throws Exception 065 { 066 Map<String, Object> inputs = super._getInputs(redirector, objectModel, source, parameters); 067 068 // Provide the parameters 069 inputs.putAll(_getParameters(objectModel)); 070 071 // Provide a map for providing data to the generator 072 Request request = ObjectModelHelper.getRequest(objectModel); 073 Map<String, Object> result = new HashMap<>(); 074 request.setAttribute(ActionResultGenerator.MAP_REQUEST_ATTR, result); 075 inputs.put(AbstractWorkflowComponent.RESULT_MAP_KEY, result); 076 077 return inputs; 078 } 079 080 @Override 081 protected String _getWorkflowName(Map objectModel, String source, Parameters parameters) 082 { 083 return ObjectModelHelper.getRequest(objectModel).getParameter(REQUEST_PARAMETER_PREFIX + "workflowName"); 084 } 085 086 /** 087 * Return a map of parameters needed by the workflow that will create the content 088 * @param objectModel The cocoon object model 089 * @return the map with parameters 090 */ 091 protected Map<String, Object> _getParameters(Map objectModel) 092 { 093 Map<String, Object> parameters = new HashMap<>(); 094 095 Request request = ObjectModelHelper.getRequest(objectModel); 096 Enumeration<String> parameterNames = request.getParameterNames(); 097 098 while (parameterNames.hasMoreElements()) 099 { 100 String parameterName = parameterNames.nextElement(); 101 if (parameterName.startsWith(REQUEST_PARAMETER_PREFIX)) 102 { 103 String name = parameterName.substring(REQUEST_PARAMETER_PREFIX.length()); 104 String value = request.getParameter(parameterName); 105 106 if (name.equals(__CONTENT_TYPE_KEY)) 107 { 108 parameters.put(CreateContentFunction.CONTENT_TYPES_KEY, new String[] {value}); 109 } 110 else if (name.equals(_CONTENT_MIXINS_KEY)) 111 { 112 parameters.put(CreateContentFunction.CONTENT_MIXINS_KEY, new String[] {value}); 113 } 114 else 115 { 116 parameters.put(name, value); 117 } 118 } 119 } 120 121 return parameters; 122 } 123 124 @Override 125 protected Map _processWorkflowException(Redirector redirector, Map objectModel, String source, Parameters parameters, long actionId, WorkflowException e) throws Exception 126 { 127 if (e instanceof InvalidInputWorkflowException) 128 { 129 // Validation error 130 if (getLogger().isDebugEnabled()) 131 { 132 getLogger().debug("Validation error while executing workflow action", e); 133 } 134 135 return Collections.EMPTY_MAP; 136 } 137 else 138 { 139 // Real error 140 return super._processWorkflowException(redirector, objectModel, source, parameters, actionId, e); 141 } 142 } 143}