|
| 1 | +/* Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | + * you may not use this file except in compliance with the License. |
| 3 | + * You may obtain a copy of the License at |
| 4 | + * |
| 5 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software |
| 8 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + * See the License for the specific language governing permissions and |
| 11 | + * limitations under the License. |
| 12 | + */ |
| 13 | +package org.flowable.cmmn.engine.impl.eventregistry; |
| 14 | + |
| 15 | +import java.util.Collections; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.function.Function; |
| 19 | +import java.util.stream.Collectors; |
| 20 | + |
| 21 | +import org.apache.commons.lang3.StringUtils; |
| 22 | +import org.flowable.cmmn.converter.CmmnXmlConstants; |
| 23 | +import org.flowable.cmmn.model.BaseElement; |
| 24 | +import org.flowable.cmmn.model.ExtensionElement; |
| 25 | +import org.flowable.common.engine.api.variable.VariableContainer; |
| 26 | +import org.flowable.eventregistry.api.runtime.EventInstance; |
| 27 | +import org.flowable.eventregistry.api.runtime.EventPayloadInstance; |
| 28 | + |
| 29 | +public class CmmnEventInstanceOutParameterHandlerImpl implements CmmnEventInstanceOutParameterHandler { |
| 30 | + |
| 31 | + /** |
| 32 | + * Processes the 'out parameters' of an {@link EventInstance} and stores the corresponding variables on the {@link VariableContainer}. |
| 33 | + * Typically used when mapping incoming event payload into a runtime instance. |
| 34 | + */ |
| 35 | + @Override |
| 36 | + public void handleOutParameters(VariableContainer variableContainer, BaseElement baseElement, EventInstance eventInstance) { |
| 37 | + List<ExtensionElement> outParameters = baseElement.getExtensionElements() |
| 38 | + .getOrDefault(CmmnXmlConstants.ELEMENT_EVENT_OUT_PARAMETER, Collections.emptyList()); |
| 39 | + if (!outParameters.isEmpty()) { |
| 40 | + Map<String, EventPayloadInstance> payloadInstances = eventInstance.getPayloadInstances() |
| 41 | + .stream() |
| 42 | + .collect(Collectors.toMap(EventPayloadInstance::getDefinitionName, Function.identity())); |
| 43 | + |
| 44 | + for (ExtensionElement outParameter : outParameters) { |
| 45 | + String payloadSourceName = outParameter.getAttributeValue(null, CmmnXmlConstants.ATTRIBUTE_IOPARAMETER_SOURCE); |
| 46 | + EventPayloadInstance payloadInstance = payloadInstances.get(payloadSourceName); |
| 47 | + String variableName = outParameter.getAttributeValue(null, CmmnXmlConstants.ATTRIBUTE_IOPARAMETER_TARGET); |
| 48 | + if (StringUtils.isNotEmpty(variableName)) { |
| 49 | + boolean isTransient = Boolean.parseBoolean(outParameter.getAttributeValue(null, "transient")); |
| 50 | + Object value = payloadInstance != null ? payloadInstance.getValue() : null; |
| 51 | + if (isTransient) { |
| 52 | + variableContainer.setTransientVariable(variableName, value); |
| 53 | + } else { |
| 54 | + variableContainer.setVariable(variableName, value); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments