Search in sources :

Example 1 with EventSubscriptionDeclaration

use of org.activiti.engine.impl.bpmn.parser.EventSubscriptionDeclaration in project Activiti by Activiti.

the class SignalEventDefinitionParseHandler method executeParse.

protected void executeParse(BpmnParse bpmnParse, SignalEventDefinition signalDefinition) {
    Signal signal = null;
    if (bpmnParse.getBpmnModel().containsSignalId(signalDefinition.getSignalRef())) {
        signal = bpmnParse.getBpmnModel().getSignal(signalDefinition.getSignalRef());
        String signalName = signal.getName();
        signalDefinition.setSignalRef(signalName);
    }
    if (signal == null) {
        return;
    }
    ActivityImpl activity = bpmnParse.getCurrentActivity();
    if (bpmnParse.getCurrentFlowElement() instanceof StartEvent) {
        activity.setProperty("type", "signalStartEvent");
        EventSubscriptionDeclaration eventSubscriptionDeclaration = new EventSubscriptionDeclaration(signalDefinition.getSignalRef(), "signal");
        eventSubscriptionDeclaration.setActivityId(activity.getId());
        eventSubscriptionDeclaration.setStartEvent(true);
        addEventSubscriptionDeclaration(bpmnParse, eventSubscriptionDeclaration, signalDefinition, bpmnParse.getCurrentScope());
    } else if (bpmnParse.getCurrentFlowElement() instanceof IntermediateCatchEvent) {
        activity.setProperty("type", "intermediateSignalCatch");
        EventSubscriptionDeclaration eventSubscriptionDeclaration = new EventSubscriptionDeclaration(signalDefinition.getSignalRef(), "signal");
        if (signal.getScope() != null) {
            eventSubscriptionDeclaration.setConfiguration(signal.getScope());
        }
        if (getPrecedingEventBasedGateway(bpmnParse, (IntermediateCatchEvent) bpmnParse.getCurrentFlowElement()) != null) {
            eventSubscriptionDeclaration.setActivityId(activity.getId());
            addEventSubscriptionDeclaration(bpmnParse, eventSubscriptionDeclaration, signalDefinition, activity.getParent());
        } else {
            activity.setScope(true);
            addEventSubscriptionDeclaration(bpmnParse, eventSubscriptionDeclaration, signalDefinition, activity);
        }
    } else if (bpmnParse.getCurrentFlowElement() instanceof ThrowEvent) {
        ThrowEvent throwEvent = (ThrowEvent) bpmnParse.getCurrentFlowElement();
        activity.setProperty("type", "intermediateSignalThrow");
        EventSubscriptionDeclaration eventSubscriptionDeclaration = new EventSubscriptionDeclaration(signalDefinition.getSignalRef(), "signal");
        eventSubscriptionDeclaration.setAsync(signalDefinition.isAsync());
        activity.setActivityBehavior(bpmnParse.getActivityBehaviorFactory().createIntermediateThrowSignalEventActivityBehavior(throwEvent, signal, eventSubscriptionDeclaration));
    } else if (bpmnParse.getCurrentFlowElement() instanceof BoundaryEvent) {
        BoundaryEvent boundaryEvent = (BoundaryEvent) bpmnParse.getCurrentFlowElement();
        boolean interrupting = boundaryEvent.isCancelActivity();
        activity.setActivityBehavior(bpmnParse.getActivityBehaviorFactory().createBoundaryEventActivityBehavior(boundaryEvent, interrupting, activity));
        activity.setProperty("type", "boundarySignal");
        EventSubscriptionDeclaration eventSubscriptionDeclaration = new EventSubscriptionDeclaration(signalDefinition.getSignalRef(), "signal");
        eventSubscriptionDeclaration.setActivityId(activity.getId());
        if (signal.getScope() != null) {
            eventSubscriptionDeclaration.setConfiguration(signal.getScope());
        }
        addEventSubscriptionDeclaration(bpmnParse, eventSubscriptionDeclaration, signalDefinition, activity.getParent());
        if (activity.getParent() instanceof ActivityImpl) {
            ((ActivityImpl) activity.getParent()).setScope(true);
        }
    }
}
Also used : ThrowEvent(org.activiti.bpmn.model.ThrowEvent) Signal(org.activiti.bpmn.model.Signal) ActivityImpl(org.activiti.engine.impl.pvm.process.ActivityImpl) BoundaryEvent(org.activiti.bpmn.model.BoundaryEvent) IntermediateCatchEvent(org.activiti.bpmn.model.IntermediateCatchEvent) StartEvent(org.activiti.bpmn.model.StartEvent) EventSubscriptionDeclaration(org.activiti.engine.impl.bpmn.parser.EventSubscriptionDeclaration)

Example 2 with EventSubscriptionDeclaration

use of org.activiti.engine.impl.bpmn.parser.EventSubscriptionDeclaration in project Activiti by Activiti.

the class BpmnDeployer method addMessageEventSubscriptions.

@SuppressWarnings("unchecked")
protected void addMessageEventSubscriptions(ProcessDefinitionEntity processDefinition) {
    CommandContext commandContext = Context.getCommandContext();
    List<EventSubscriptionDeclaration> eventDefinitions = (List<EventSubscriptionDeclaration>) processDefinition.getProperty(BpmnParse.PROPERTYNAME_EVENT_SUBSCRIPTION_DECLARATION);
    if (eventDefinitions != null) {
        Set<String> messageNames = new HashSet<String>();
        for (EventSubscriptionDeclaration eventDefinition : eventDefinitions) {
            if (eventDefinition.getEventType().equals("message") && eventDefinition.isStartEvent()) {
                if (!messageNames.contains(eventDefinition.getEventName())) {
                    messageNames.add(eventDefinition.getEventName());
                } else {
                    throw new ActivitiException("Cannot deploy process definition '" + processDefinition.getResourceName() + "': there are multiple message event subscriptions for the message with name '" + eventDefinition.getEventName() + "'.");
                }
                // look for subscriptions for the same name in db:
                List<EventSubscriptionEntity> subscriptionsForSameMessageName = commandContext.getEventSubscriptionEntityManager().findEventSubscriptionsByName(MessageEventHandler.EVENT_HANDLER_TYPE, eventDefinition.getEventName(), processDefinition.getTenantId());
                // also look for subscriptions created in the session:
                List<MessageEventSubscriptionEntity> cachedSubscriptions = commandContext.getDbSqlSession().findInCache(MessageEventSubscriptionEntity.class);
                for (MessageEventSubscriptionEntity cachedSubscription : cachedSubscriptions) {
                    if (eventDefinition.getEventName().equals(cachedSubscription.getEventName()) && !subscriptionsForSameMessageName.contains(cachedSubscription)) {
                        subscriptionsForSameMessageName.add(cachedSubscription);
                    }
                }
                // remove subscriptions deleted in the same command
                subscriptionsForSameMessageName = commandContext.getDbSqlSession().pruneDeletedEntities(subscriptionsForSameMessageName);
                for (EventSubscriptionEntity eventSubscriptionEntity : subscriptionsForSameMessageName) {
                    // no process instance-id = it's a message start event
                    if (StringUtils.isEmpty(eventSubscriptionEntity.getProcessInstanceId())) {
                        throw new ActivitiException("Cannot deploy process definition '" + processDefinition.getResourceName() + "': there already is a message event subscription for the message with name '" + eventDefinition.getEventName() + "'.");
                    }
                }
                MessageEventSubscriptionEntity newSubscription = new MessageEventSubscriptionEntity();
                newSubscription.setEventName(eventDefinition.getEventName());
                newSubscription.setActivityId(eventDefinition.getActivityId());
                newSubscription.setConfiguration(processDefinition.getId());
                newSubscription.setProcessDefinitionId(processDefinition.getId());
                if (processDefinition.getTenantId() != null) {
                    newSubscription.setTenantId(processDefinition.getTenantId());
                }
                newSubscription.insert();
            }
        }
    }
}
Also used : ActivitiException(org.activiti.engine.ActivitiException) CommandContext(org.activiti.engine.impl.interceptor.CommandContext) List(java.util.List) ArrayList(java.util.ArrayList) EventSubscriptionDeclaration(org.activiti.engine.impl.bpmn.parser.EventSubscriptionDeclaration) MessageEventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.MessageEventSubscriptionEntity) SignalEventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.SignalEventSubscriptionEntity) MessageEventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.MessageEventSubscriptionEntity) EventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.EventSubscriptionEntity) HashSet(java.util.HashSet)

Example 3 with EventSubscriptionDeclaration

use of org.activiti.engine.impl.bpmn.parser.EventSubscriptionDeclaration in project Activiti by Activiti.

the class AbstractBpmnParseHandler method addEventSubscriptionDeclaration.

@SuppressWarnings("unchecked")
protected void addEventSubscriptionDeclaration(BpmnParse bpmnParse, EventSubscriptionDeclaration subscription, EventDefinition parsedEventDefinition, ScopeImpl scope) {
    List<EventSubscriptionDeclaration> eventDefinitions = (List<EventSubscriptionDeclaration>) scope.getProperty(PROPERTYNAME_EVENT_SUBSCRIPTION_DECLARATION);
    if (eventDefinitions == null) {
        eventDefinitions = new ArrayList<EventSubscriptionDeclaration>();
        scope.setProperty(PROPERTYNAME_EVENT_SUBSCRIPTION_DECLARATION, eventDefinitions);
    } else {
        // if this is a message event, validate that it is the only one with the provided name for this scope
        if (subscription.getEventType().equals("message")) {
            for (EventSubscriptionDeclaration eventDefinition : eventDefinitions) {
                if (eventDefinition.getEventType().equals("message") && eventDefinition.getEventName().equals(subscription.getEventName()) && eventDefinition.isStartEvent() == subscription.isStartEvent()) {
                    logger.warn("Cannot have more than one message event subscription with name '" + subscription.getEventName() + "' for scope '" + scope.getId() + "'");
                }
            }
        }
    }
    eventDefinitions.add(subscription);
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) EventSubscriptionDeclaration(org.activiti.engine.impl.bpmn.parser.EventSubscriptionDeclaration)

Example 4 with EventSubscriptionDeclaration

use of org.activiti.engine.impl.bpmn.parser.EventSubscriptionDeclaration in project Activiti by Activiti.

the class MessageEventDefinitionParseHandler method executeParse.

protected void executeParse(BpmnParse bpmnParse, MessageEventDefinition messageDefinition) {
    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());
    }
    EventSubscriptionDeclaration eventSubscription = new EventSubscriptionDeclaration(messageDefinition.getMessageRef(), "message");
    ScopeImpl scope = bpmnParse.getCurrentScope();
    ActivityImpl activity = bpmnParse.getCurrentActivity();
    if (bpmnParse.getCurrentFlowElement() instanceof StartEvent && bpmnParse.getCurrentSubProcess() != null) {
        // the scope of the event subscription is the parent of the event
        // subprocess (subscription must be created when parent is initialized)
        ScopeImpl catchingScope = ((ActivityImpl) scope).getParent();
        EventSubscriptionDeclaration eventSubscriptionDeclaration = new EventSubscriptionDeclaration(messageDefinition.getMessageRef(), "message");
        eventSubscriptionDeclaration.setActivityId(activity.getId());
        eventSubscriptionDeclaration.setStartEvent(false);
        addEventSubscriptionDeclaration(bpmnParse, eventSubscriptionDeclaration, messageDefinition, catchingScope);
    } else if (bpmnParse.getCurrentFlowElement() instanceof StartEvent) {
        activity.setProperty("type", "messageStartEvent");
        eventSubscription.setStartEvent(true);
        eventSubscription.setActivityId(activity.getId());
        addEventSubscriptionDeclaration(bpmnParse, eventSubscription, messageDefinition, bpmnParse.getCurrentProcessDefinition());
    } else if (bpmnParse.getCurrentFlowElement() instanceof IntermediateCatchEvent) {
        activity.setProperty("type", "intermediateMessageCatch");
        if (getPrecedingEventBasedGateway(bpmnParse, (IntermediateCatchEvent) bpmnParse.getCurrentFlowElement()) != null) {
            eventSubscription.setActivityId(activity.getId());
            addEventSubscriptionDeclaration(bpmnParse, eventSubscription, messageDefinition, activity.getParent());
        } else {
            activity.setScope(true);
            addEventSubscriptionDeclaration(bpmnParse, eventSubscription, messageDefinition, activity);
        }
    } else if (bpmnParse.getCurrentFlowElement() instanceof BoundaryEvent) {
        BoundaryEvent boundaryEvent = (BoundaryEvent) bpmnParse.getCurrentFlowElement();
        boolean interrupting = boundaryEvent.isCancelActivity();
        activity.setActivityBehavior(bpmnParse.getActivityBehaviorFactory().createBoundaryEventActivityBehavior(boundaryEvent, interrupting, activity));
        activity.setProperty("type", "boundaryMessage");
        EventSubscriptionDeclaration eventSubscriptionDeclaration = new EventSubscriptionDeclaration(messageDefinition.getMessageRef(), "message");
        eventSubscriptionDeclaration.setActivityId(activity.getId());
        addEventSubscriptionDeclaration(bpmnParse, eventSubscriptionDeclaration, messageDefinition, activity.getParent());
        if (activity.getParent() instanceof ActivityImpl) {
            ((ActivityImpl) activity.getParent()).setScope(true);
        }
    } else {
    // What to do here?
    }
}
Also used : Message(org.activiti.bpmn.model.Message) ActivityImpl(org.activiti.engine.impl.pvm.process.ActivityImpl) BoundaryEvent(org.activiti.bpmn.model.BoundaryEvent) IntermediateCatchEvent(org.activiti.bpmn.model.IntermediateCatchEvent) StartEvent(org.activiti.bpmn.model.StartEvent) ScopeImpl(org.activiti.engine.impl.pvm.process.ScopeImpl) EventSubscriptionDeclaration(org.activiti.engine.impl.bpmn.parser.EventSubscriptionDeclaration) BpmnModel(org.activiti.bpmn.model.BpmnModel)

Example 5 with EventSubscriptionDeclaration

use of org.activiti.engine.impl.bpmn.parser.EventSubscriptionDeclaration in project Activiti by Activiti.

the class DeploymentEntityManager method deleteDeployment.

public void deleteDeployment(String deploymentId, boolean cascade) {
    List<ProcessDefinition> processDefinitions = getDbSqlSession().createProcessDefinitionQuery().deploymentId(deploymentId).list();
    // Remove the deployment link from any model. 
    // The model will still exists, as a model is a source for a deployment model and has a different lifecycle
    List<Model> models = getDbSqlSession().createModelQueryImpl().deploymentId(deploymentId).list();
    for (Model model : models) {
        ModelEntity modelEntity = (ModelEntity) model;
        modelEntity.setDeploymentId(null);
        getModelManager().updateModel(modelEntity);
    }
    if (cascade) {
        // delete process instances
        for (ProcessDefinition processDefinition : processDefinitions) {
            String processDefinitionId = processDefinition.getId();
            getProcessInstanceManager().deleteProcessInstancesByProcessDefinition(processDefinitionId, "deleted deployment", cascade);
        }
    }
    for (ProcessDefinition processDefinition : processDefinitions) {
        String processDefinitionId = processDefinition.getId();
        // remove related authorization parameters in IdentityLink table
        getIdentityLinkManager().deleteIdentityLinksByProcDef(processDefinitionId);
        // event subscriptions
        List<EventSubscriptionEntity> eventSubscriptionEntities = getEventSubscriptionManager().findEventSubscriptionsByTypeAndProcessDefinitionId(null, processDefinitionId, // null type ==> all types
        processDefinition.getTenantId());
        for (EventSubscriptionEntity eventSubscriptionEntity : eventSubscriptionEntities) {
            eventSubscriptionEntity.delete();
        }
        getProcessDefinitionInfoManager().deleteProcessDefinitionInfo(processDefinitionId);
    }
    // delete process definitions from db
    getProcessDefinitionManager().deleteProcessDefinitionsByDeploymentId(deploymentId);
    for (ProcessDefinition processDefinition : processDefinitions) {
        // remove timer start events for current process definition:
        List<Job> timerStartJobs = Context.getCommandContext().getJobEntityManager().findJobsByTypeAndProcessDefinitionId(TimerStartEventJobHandler.TYPE, processDefinition.getId());
        if (timerStartJobs != null && timerStartJobs.size() > 0) {
            for (Job timerStartJob : timerStartJobs) {
                if (Context.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
                    Context.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.JOB_CANCELED, timerStartJob, null, null, processDefinition.getId()));
                }
                ((JobEntity) timerStartJob).delete();
            }
        }
        // If previous process definition version has a timer/message/signal start event, it must be added
        ProcessDefinitionEntity latestProcessDefinition = findLatestProcessDefinition(processDefinition);
        // Only if the currently deleted process definition is the latest version, we fall back to the previous start event type
        if (processDefinition.getId().equals(latestProcessDefinition.getId())) {
            // Try to find a previous version (it could be some versions are missing due to deletions)
            ProcessDefinition previousProcessDefinition = findNewLatestProcessDefinitionAfterRemovalOf(processDefinition);
            if (previousProcessDefinition != null) {
                // Need to resolve process definition to make sure it's parsed
                ProcessDefinitionEntity resolvedProcessDefinition = Context.getProcessEngineConfiguration().getDeploymentManager().resolveProcessDefinition((ProcessDefinitionEntity) previousProcessDefinition);
                // Timer start
                List<TimerDeclarationImpl> timerDeclarations = (List<TimerDeclarationImpl>) resolvedProcessDefinition.getProperty(BpmnParse.PROPERTYNAME_START_TIMER);
                if (timerDeclarations != null) {
                    for (TimerDeclarationImpl timerDeclaration : timerDeclarations) {
                        TimerEntity timer = timerDeclaration.prepareTimerEntity(null);
                        timer.setProcessDefinitionId(previousProcessDefinition.getId());
                        if (previousProcessDefinition.getTenantId() != null) {
                            timer.setTenantId(previousProcessDefinition.getTenantId());
                        }
                        Context.getCommandContext().getJobEntityManager().schedule(timer);
                    }
                }
                // Signal / Message start
                List<EventSubscriptionDeclaration> signalEventDefinitions = (List<EventSubscriptionDeclaration>) resolvedProcessDefinition.getProperty(BpmnParse.PROPERTYNAME_EVENT_SUBSCRIPTION_DECLARATION);
                if (signalEventDefinitions != null) {
                    for (EventSubscriptionDeclaration eventDefinition : signalEventDefinitions) {
                        if (eventDefinition.getEventType().equals("signal") && eventDefinition.isStartEvent()) {
                            SignalEventSubscriptionEntity subscriptionEntity = new SignalEventSubscriptionEntity();
                            subscriptionEntity.setEventName(eventDefinition.getEventName());
                            subscriptionEntity.setActivityId(eventDefinition.getActivityId());
                            subscriptionEntity.setProcessDefinitionId(previousProcessDefinition.getId());
                            subscriptionEntity.setTenantId(previousProcessDefinition.getTenantId());
                            subscriptionEntity.insert();
                        } else if (eventDefinition.getEventType().equals("message") && eventDefinition.isStartEvent()) {
                            MessageEventSubscriptionEntity newSubscription = new MessageEventSubscriptionEntity();
                            newSubscription.setEventName(eventDefinition.getEventName());
                            newSubscription.setActivityId(eventDefinition.getActivityId());
                            newSubscription.setConfiguration(previousProcessDefinition.getId());
                            newSubscription.setProcessDefinitionId(previousProcessDefinition.getId());
                            newSubscription.setTenantId(previousProcessDefinition.getTenantId());
                            newSubscription.insert();
                        }
                    }
                }
            }
        }
    }
    getResourceManager().deleteResourcesByDeploymentId(deploymentId);
    getDbSqlSession().delete("deleteDeployment", deploymentId);
}
Also used : TimerDeclarationImpl(org.activiti.engine.impl.jobexecutor.TimerDeclarationImpl) ProcessDefinition(org.activiti.engine.repository.ProcessDefinition) Model(org.activiti.engine.repository.Model) List(java.util.List) Job(org.activiti.engine.runtime.Job) EventSubscriptionDeclaration(org.activiti.engine.impl.bpmn.parser.EventSubscriptionDeclaration)

Aggregations

EventSubscriptionDeclaration (org.activiti.engine.impl.bpmn.parser.EventSubscriptionDeclaration)8 ArrayList (java.util.ArrayList)5 List (java.util.List)5 TimerDeclarationImpl (org.activiti.engine.impl.jobexecutor.TimerDeclarationImpl)3 ActivityImpl (org.activiti.engine.impl.pvm.process.ActivityImpl)3 BoundaryEvent (org.activiti.bpmn.model.BoundaryEvent)2 IntermediateCatchEvent (org.activiti.bpmn.model.IntermediateCatchEvent)2 StartEvent (org.activiti.bpmn.model.StartEvent)2 SignalEventSubscriptionEntity (org.activiti.engine.impl.persistence.entity.SignalEventSubscriptionEntity)2 ScopeImpl (org.activiti.engine.impl.pvm.process.ScopeImpl)2 ProcessDefinition (org.activiti.engine.repository.ProcessDefinition)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 HashSet (java.util.HashSet)1 BpmnModel (org.activiti.bpmn.model.BpmnModel)1 Message (org.activiti.bpmn.model.Message)1 Signal (org.activiti.bpmn.model.Signal)1 ThrowEvent (org.activiti.bpmn.model.ThrowEvent)1 ActivitiException (org.activiti.engine.ActivitiException)1