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