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.workflow.component; 017 018import java.util.List; 019import java.util.Map; 020 021import org.apache.avalon.framework.service.ServiceException; 022import org.apache.avalon.framework.service.ServiceManager; 023 024import org.ametys.plugins.workflow.AbstractWorkflowComponent; 025import org.ametys.plugins.workflow.EnhancedCondition; 026import org.ametys.plugins.workflow.repository.WorkflowAwareAmetysObject; 027import org.ametys.plugins.workflow.support.WorkflowElementDefinitionHelper; 028import org.ametys.plugins.workflow.support.WorkflowProvider; 029import org.ametys.plugins.workflow.support.WorkflowProvider.AmetysObjectWorkflow; 030import org.ametys.runtime.i18n.I18nizableText; 031 032import com.opensymphony.module.propertyset.PropertySet; 033import com.opensymphony.workflow.WorkflowException; 034import com.opensymphony.workflow.spi.Step; 035 036/** 037 * OSWorkflow condition to test the current step of an Ametys object 038 * The following configuration have to be used:<br> 039 * 040 * <pre> 041 * <condition type="avalon"> 042 * <arg name="role">org.ametys.plugins.workflow.component.AmetysObjectCurrentStepCondition</arg> 043 * <arg name="object-key">content</arg> 044 * <arg name="step">2</arg> 045 * </condition> 046 * </pre> 047 * 048 */ 049public class AmetysObjectCurrentStepCondition extends AbstractWorkflowComponent implements EnhancedCondition 050{ 051 private WorkflowProvider _workflowProvider; 052 053 @Override 054 public void service(ServiceManager manager) throws ServiceException 055 { 056 super.service(manager); 057 _workflowProvider = (WorkflowProvider) manager.lookup(WorkflowProvider.ROLE); 058 } 059 060 public boolean passesCondition(Map transientVars, Map args, PropertySet ps) throws WorkflowException 061 { 062 int step = Integer.parseInt((String) args.get("step")); 063 064 WorkflowAwareAmetysObject ametysObject = (WorkflowAwareAmetysObject) transientVars.get(_getAmetysObjectKey(args)); 065 if (ametysObject == null) 066 { 067 throw new WorkflowException("Unable to retrieve the object"); 068 } 069 070 AmetysObjectWorkflow workflow = _workflowProvider.getAmetysObjectWorkflow(ametysObject, true); 071 072 long workflowId = ametysObject.getWorkflowId(); 073 074 List<Step> currentSteps = workflow.getCurrentSteps(workflowId); 075 for (Step currentStep : currentSteps) 076 { 077 if (currentStep.getStepId() == step) 078 { 079 return true; 080 } 081 } 082 083 return false; 084 } 085 086 /** 087 * Get the key to get the {@link WorkflowAwareAmetysObject} into the transient vars. 088 * @param args Arguments of the condition 089 * @return A key to get the {@link WorkflowAwareAmetysObject} 090 */ 091 protected Object _getAmetysObjectKey(Map args) 092 { 093 return args.get("object-key"); 094 } 095 096 @Override 097 public List<WorkflowArgument> getArguments() 098 { 099 return List.of( 100 WorkflowElementDefinitionHelper.getElementDefinition( 101 "step", 102 new I18nizableText("plugin.workflow", "PLUGINS_WORKFLOW_EDITOR_AMETYS_OBJECT_CURRENT_STEP_CONDITION_ARGUMENT_STEP_LABEL"), 103 new I18nizableText("plugin.workflow", "PLUGINS_WORKFLOW_EDITOR_AMETYS_OBJECT_CURRENT_STEP_CONDITION_ARGUMENT_STEP_DESCRIPTION"), 104 true, 105 false 106 ), 107 WorkflowElementDefinitionHelper.getElementDefinition( 108 "object-key", 109 new I18nizableText("plugin.workflow", "PLUGINS_WORKFLOW_EDITOR_AMETYS_OBJECT_CURRENT_STEP_CONDITION_ARGUMENT_OBJECT_KEY_LABEL"), 110 new I18nizableText("plugin.workflow", "PLUGINS_WORKFLOW_EDITOR_AMETYS_OBJECT_CURRENT_STEP_CONDITION_ARGUMENT_OBJECT_KEY_DESCRIPTION"), 111 true, 112 false 113 ) 114 ); 115 } 116 117 @Override 118 public I18nizableText getLabel() 119 { 120 return new I18nizableText("plugin.workflow", "UITOOL_WORKFLOW_EDITOR_AMETYS_OBJECT_CURRENT_STEP_CONDITION_LABEL"); 121 } 122 123 @Override 124 public I18nizableText getFullLabel(Map<String, String> argumentsValues) 125 { 126 @SuppressWarnings("cast") 127 String stepId = (String) argumentsValues.get("step"); 128 return new I18nizableText("plugin.workflow", "UITOOL_WORKFLOW_EDITOR_AMETYS_OBJECT_CURRENT_STEP_CONDITION_DESCRIPTION", List.of(stepId)); 129 } 130}