001/* 002 * Copyright 2022 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.forms.helper; 017 018import java.util.HashMap; 019import java.util.Map; 020 021import org.apache.avalon.framework.component.Component; 022import org.apache.avalon.framework.service.ServiceException; 023import org.apache.avalon.framework.service.ServiceManager; 024import org.apache.avalon.framework.service.Serviceable; 025import org.apache.commons.lang.StringUtils; 026 027import org.ametys.cms.workflow.AmetysObjectCheckRightsCondition; 028import org.ametys.plugins.forms.repository.Form; 029import org.ametys.plugins.forms.repository.FormEntry; 030import org.ametys.plugins.workflow.support.WorkflowHelper; 031import org.ametys.plugins.workflow.support.WorkflowProvider; 032import org.ametys.runtime.plugin.component.AbstractLogEnabled; 033 034import com.opensymphony.workflow.Workflow; 035 036/** 037 * Helper for manipulating workflows for {@link Form} 038 */ 039public class FormWorkflowHelper extends AbstractLogEnabled implements Serviceable, Component 040{ 041 /** Avalon Role */ 042 public static final String ROLE = FormWorkflowHelper.class.getName(); 043 044 /** The workflow provider */ 045 protected WorkflowProvider _workflowProvider; 046 /** The workflow helper component */ 047 protected WorkflowHelper _workflowHelper; 048 049 @Override 050 public void service(ServiceManager serviceManager) throws ServiceException 051 { 052 _workflowProvider = (WorkflowProvider) serviceManager.lookup(WorkflowProvider.ROLE); 053 _workflowHelper = (WorkflowHelper) serviceManager.lookup(WorkflowHelper.ROLE); 054 } 055 056 /** 057 * Initialize workflow for form entry 058 * @param entry the form entry 059 * @throws Exception if an error occurred 060 */ 061 public void initializeWorkflow(FormEntry entry) throws Exception 062 { 063 Form form = entry.getForm(); 064 String workflowName = form.getWorkflowName(); 065 if (StringUtils.isNotBlank(workflowName)) 066 { 067 // create workflow with entry id 068 Workflow workflow = _workflowProvider.getAmetysObjectWorkflow(entry); 069 070 int initialActionId = _workflowHelper.getInitialAction(workflowName); 071 072 Map<String, Object> inputs = new HashMap<>(); 073 inputs.put(AmetysObjectCheckRightsCondition.AMETYS_OBJECT_KEY, entry); 074 075 long workflowInstanceId = workflow.initialize(workflowName, initialActionId, inputs); 076 entry.setWorkflowId(workflowInstanceId); 077 } 078 } 079}