use of org.activiti.bpmn.model.MessageEventDefinition in project Activiti by Activiti.
the class FieldExtensionParser method parseChildElement.
public void parseChildElement(XMLStreamReader xtr, BaseElement parentElement, BpmnModel model) throws Exception {
if (!accepts(parentElement))
return;
FieldExtension extension = new FieldExtension();
BpmnXMLUtil.addXMLLocation(extension, xtr);
extension.setFieldName(xtr.getAttributeValue(null, ATTRIBUTE_FIELD_NAME));
if (StringUtils.isNotEmpty(xtr.getAttributeValue(null, ATTRIBUTE_FIELD_STRING))) {
extension.setStringValue(xtr.getAttributeValue(null, ATTRIBUTE_FIELD_STRING));
} else if (StringUtils.isNotEmpty(xtr.getAttributeValue(null, ATTRIBUTE_FIELD_EXPRESSION))) {
extension.setExpression(xtr.getAttributeValue(null, ATTRIBUTE_FIELD_EXPRESSION));
} else {
boolean readyWithFieldExtension = false;
try {
while (readyWithFieldExtension == false && xtr.hasNext()) {
xtr.next();
if (xtr.isStartElement() && ELEMENT_FIELD_STRING.equalsIgnoreCase(xtr.getLocalName())) {
extension.setStringValue(xtr.getElementText().trim());
} else if (xtr.isStartElement() && ATTRIBUTE_FIELD_EXPRESSION.equalsIgnoreCase(xtr.getLocalName())) {
extension.setExpression(xtr.getElementText().trim());
} else if (xtr.isEndElement() && getElementName().equalsIgnoreCase(xtr.getLocalName())) {
readyWithFieldExtension = true;
}
}
} catch (Exception e) {
LOGGER.warn("Error parsing field extension child elements", e);
}
}
if (parentElement instanceof ActivitiListener) {
((ActivitiListener) parentElement).getFieldExtensions().add(extension);
} else if (parentElement instanceof ServiceTask) {
((ServiceTask) parentElement).getFieldExtensions().add(extension);
} else if (parentElement instanceof SendTask) {
((SendTask) parentElement).getFieldExtensions().add(extension);
} else if (parentElement instanceof MessageEventDefinition) {
((MessageEventDefinition) parentElement).getFieldExtensions().add(extension);
}
}
use of org.activiti.bpmn.model.MessageEventDefinition in project Activiti by Activiti.
the class EndEventParseHandler method executeParse.
@Override
protected void executeParse(BpmnParse bpmnParse, EndEvent endEvent) {
EventDefinition eventDefinition = null;
if (endEvent.getEventDefinitions().size() > 0) {
eventDefinition = endEvent.getEventDefinitions().get(0);
if (eventDefinition instanceof ErrorEventDefinition) {
ErrorEventDefinition errorDefinition = (ErrorEventDefinition) eventDefinition;
if (bpmnParse.getBpmnModel().containsErrorRef(errorDefinition.getErrorRef())) {
for (Error error : bpmnParse.getBpmnModel().getErrors().values()) {
String errorCode = null;
if (error.getId().equals(errorDefinition.getErrorRef())) {
errorCode = error.getErrorCode();
}
if (StringUtils.isEmpty(errorCode)) {
logger.warn("errorCode is required for an error event " + endEvent.getId());
}
}
}
endEvent.setBehavior(bpmnParse.getActivityBehaviorFactory().createErrorEndEventActivityBehavior(endEvent, errorDefinition));
} else if (eventDefinition instanceof TerminateEventDefinition) {
endEvent.setBehavior(bpmnParse.getActivityBehaviorFactory().createTerminateEndEventActivityBehavior(endEvent));
} else if (eventDefinition instanceof CancelEventDefinition) {
endEvent.setBehavior(bpmnParse.getActivityBehaviorFactory().createCancelEndEventActivityBehavior(endEvent));
} else if (eventDefinition instanceof MessageEventDefinition) {
MessageEventDefinition messageEventDefinition = MessageEventDefinition.class.cast(eventDefinition);
Message message = bpmnParse.getBpmnModel().getMessage(messageEventDefinition.getMessageRef());
BpmnModel bpmnModel = bpmnParse.getBpmnModel();
if (bpmnModel.containsMessageId(messageEventDefinition.getMessageRef())) {
messageEventDefinition.setMessageRef(message.getName());
messageEventDefinition.setExtensionElements(message.getExtensionElements());
}
endEvent.setBehavior(bpmnParse.getActivityBehaviorFactory().createThrowMessageEndEventActivityBehavior(endEvent, messageEventDefinition, message));
} else {
endEvent.setBehavior(bpmnParse.getActivityBehaviorFactory().createNoneEndEventActivityBehavior(endEvent));
}
} else {
endEvent.setBehavior(bpmnParse.getActivityBehaviorFactory().createNoneEndEventActivityBehavior(endEvent));
}
}
use of org.activiti.bpmn.model.MessageEventDefinition in project Activiti by Activiti.
the class StartEventParseHandler method executeParse.
@Override
protected void executeParse(BpmnParse bpmnParse, StartEvent element) {
if (element.getSubProcess() != null && element.getSubProcess() instanceof EventSubProcess) {
if (CollectionUtil.isNotEmpty(element.getEventDefinitions())) {
EventDefinition eventDefinition = element.getEventDefinitions().get(0);
if (eventDefinition instanceof MessageEventDefinition) {
MessageEventDefinition messageDefinition = (MessageEventDefinition) eventDefinition;
BpmnModel bpmnModel = bpmnParse.getBpmnModel();
String messageRef = messageDefinition.getMessageRef();
if (bpmnModel.containsMessageId(messageRef)) {
Message message = bpmnModel.getMessage(messageRef);
messageDefinition.setMessageRef(message.getName());
messageDefinition.setExtensionElements(message.getExtensionElements());
}
element.setBehavior(bpmnParse.getActivityBehaviorFactory().createEventSubProcessMessageStartEventActivityBehavior(element, messageDefinition));
} else if (eventDefinition instanceof ErrorEventDefinition) {
element.setBehavior(bpmnParse.getActivityBehaviorFactory().createEventSubProcessErrorStartEventActivityBehavior(element));
}
}
} else if (CollectionUtil.isEmpty(element.getEventDefinitions())) {
element.setBehavior(bpmnParse.getActivityBehaviorFactory().createNoneStartEventActivityBehavior(element));
}
if (element.getSubProcess() == null && (CollectionUtil.isEmpty(element.getEventDefinitions()) || bpmnParse.getCurrentProcess().getInitialFlowElement() == null)) {
bpmnParse.getCurrentProcess().setInitialFlowElement(element);
}
}
use of org.activiti.bpmn.model.MessageEventDefinition in project Activiti by Activiti.
the class ProcessInstanceHelper method createAndStartProcessInstanceByMessage.
public ProcessInstance createAndStartProcessInstanceByMessage(ProcessDefinition processDefinition, String businessKey, String messageName, Map<String, Object> messageVariables, Map<String, Object> transientVariables) {
Process process = this.getActiveProcess(processDefinition);
FlowElement initialFlowElement = null;
BpmnModel bpmnModel = ProcessDefinitionUtil.getBpmnModel(processDefinition.getId());
for (FlowElement flowElement : process.getFlowElements()) {
if (flowElement instanceof StartEvent) {
StartEvent startEvent = (StartEvent) flowElement;
if (CollectionUtil.isNotEmpty(startEvent.getEventDefinitions()) && startEvent.getEventDefinitions().get(0) instanceof MessageEventDefinition) {
MessageEventDefinition messageEventDefinition = (MessageEventDefinition) startEvent.getEventDefinitions().get(0);
String messageRef = messageEventDefinition.getMessageRef();
if (messageRef.equals(messageName)) {
initialFlowElement = flowElement;
break;
} else // FIXME: We should not need to reset eventDefinition messageRef to message name
if (bpmnModel.containsMessageId(messageRef)) {
Message message = bpmnModel.getMessage(messageRef);
messageEventDefinition.setMessageRef(message.getName());
initialFlowElement = flowElement;
break;
}
}
}
}
if (initialFlowElement == null) {
throw new ActivitiException("No message start event found for process definition " + processDefinition.getId() + " and message name " + messageName);
}
Map<String, Object> processVariables = messageVariables;
// Create process instance with executions but defer to start process after dispatching ACTIVITY_MESSAGE_RECEIVED
ExecutionEntity processInstance = createProcessInstanceWithInitialFlowElement(processDefinition, businessKey, null, initialFlowElement, process);
// Dispatch message received event
dispatchStartMessageReceivedEvent(processInstance, messageName, messageVariables);
// Finally start the process
CommandContext commandContext = Context.getCommandContext();
startProcessInstance(processInstance, commandContext, processVariables, initialFlowElement, transientVariables);
return processInstance;
}
use of org.activiti.bpmn.model.MessageEventDefinition in project Activiti by Activiti.
the class ProcessInstanceHelper method startProcessInstance.
public void startProcessInstance(ExecutionEntity processInstance, CommandContext commandContext, Map<String, Object> variables, FlowElement initialFlowElement, Map<String, Object> transientVariables) {
Process process = ProcessDefinitionUtil.getProcess(processInstance.getProcessDefinitionId());
createProcessVariables(processInstance, variables, transientVariables, process);
recordStartProcessInstance(commandContext, initialFlowElement, processInstance);
// Event sub process handling
List<MessageEventSubscriptionEntity> messageEventSubscriptions = new LinkedList<>();
for (FlowElement flowElement : process.getFlowElements()) {
if (flowElement instanceof EventSubProcess) {
EventSubProcess eventSubProcess = (EventSubProcess) flowElement;
for (FlowElement subElement : eventSubProcess.getFlowElements()) {
if (subElement instanceof StartEvent) {
StartEvent startEvent = (StartEvent) subElement;
if (CollectionUtil.isNotEmpty(startEvent.getEventDefinitions())) {
EventDefinition eventDefinition = startEvent.getEventDefinitions().get(0);
if (eventDefinition instanceof MessageEventDefinition) {
MessageEventDefinition messageEventDefinition = (MessageEventDefinition) eventDefinition;
BpmnModel bpmnModel = ProcessDefinitionUtil.getBpmnModel(processInstance.getProcessDefinitionId());
if (bpmnModel.containsMessageId(messageEventDefinition.getMessageRef())) {
messageEventDefinition.setMessageRef(bpmnModel.getMessage(messageEventDefinition.getMessageRef()).getName());
}
ExecutionEntity messageExecution = commandContext.getExecutionEntityManager().createChildExecution(processInstance);
messageExecution.setCurrentFlowElement(startEvent);
messageExecution.setEventScope(true);
String messageName = getMessageName(commandContext, messageEventDefinition, messageExecution);
MessageEventSubscriptionEntity subscription = commandContext.getEventSubscriptionEntityManager().insertMessageEvent(messageName, messageExecution);
Optional<String> correlationKey = getCorrelationKey(commandContext, messageEventDefinition, messageExecution);
correlationKey.ifPresent(subscription::setConfiguration);
messageEventSubscriptions.add(subscription);
}
}
}
}
}
}
// There will always be one child execution created
ExecutionEntity execution = processInstance.getExecutions().get(0);
execution.setAppVersion(processInstance.getAppVersion());
commandContext.getAgenda().planContinueProcessOperation(execution);
if (Context.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
ActivitiEventDispatcher eventDispatcher = Context.getProcessEngineConfiguration().getEventDispatcher();
eventDispatcher.dispatchEvent(ActivitiEventBuilder.createProcessStartedEvent(execution, variables, false));
for (MessageEventSubscriptionEntity messageEventSubscription : messageEventSubscriptions) {
commandContext.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createMessageWaitingEvent(messageEventSubscription.getExecution(), messageEventSubscription.getEventName(), messageEventSubscription.getConfiguration()));
}
}
}
Aggregations