protected void deleteProcessInstancesForProcessDefinitions(List<ProcessDefinition> processDefinitions) { for (ProcessDefinition processDefinition : processDefinitions) { getExecutionEntityManager().deleteProcessInstancesByProcessDefinition(processDefinition.getId(), "deleted deployment", true); } }
protected void deleteEventSubscriptions(ProcessDefinition processDefinition) { EventSubscriptionEntityManager eventSubscriptionEntityManager = getEventSubscriptionEntityManager(); eventSubscriptionEntityManager.deleteEventSubscriptionsForProcessDefinition(processDefinition.getId()); }
protected void deleteProcessDefinitionIdentityLinks(ProcessDefinition processDefinition) { getIdentityLinkEntityManager().deleteIdentityLinksByProcDef(processDefinition.getId()); }
@Override public List<ActivityImpl> getUserTasksByProcDefKey(String procDefKey) { ProcessDefinition definition = repositoryService.createProcessDefinitionQuery().processDefinitionKey(procDefKey).orderByProcessDefinitionVersion().desc().list().get(0); String procDefId = definition.getId(); List<ActivityImpl> activities = findActivities(procDefId, activity -> "userTask".equals(activity.getProperty("type"))); // if (null != activities) { activities.sort(Comparator.comparing(ProcessElementImpl::getId)); } return activities; }
protected ProcessInstance createAndStartProcessInstance(ProcessDefinition processDefinition, String businessKey, String processInstanceName, Map<String, Object> variables, Map<String, Object> transientVariables, boolean startProcessInstance) { CommandContext commandContext = Context.getCommandContext(); // Todo: ideally, context should be passed here // Do not start process a process instance if the process definition is suspended if (ProcessDefinitionUtil.isProcessDefinitionSuspended(processDefinition.getId())) { throw new ActivitiException("Cannot start process instance. Process definition " + processDefinition.getName() + " (id = " + processDefinition.getId() + ") is suspended"); } // Get model from cache Process process = ProcessDefinitionUtil.getProcess(processDefinition.getId()); if (process == null) { throw new ActivitiException("Cannot start process instance. Process model " + processDefinition.getName() + " (id = " + processDefinition.getId() + ") could not be found"); } FlowElement initialFlowElement = process.getInitialFlowElement(); if (initialFlowElement == null) { throw new ActivitiException("No start element found for process definition " + processDefinition.getId()); } return createAndStartProcessInstanceWithInitialFlowElement(processDefinition, businessKey, processInstanceName, initialFlowElement, process, variables, transientVariables, startProcessInstance); }
@Override public Map<String, Object> getUserTasksByProcDefKey(String procDefKey) { String definitionId = repositoryService.createProcessDefinitionQuery().processDefinitionKey(procDefKey).orderByProcessDefinitionVersion().desc().list().get(0).getId(); List<ActivityImpl> activities = bpmActivityService.getUserTasksByProcDefId(definitionId); Map<String, Object> map = new HashMap<>(); for (ActivityImpl activity : activities) { map.put(activity.getId(), activity.getProperty("name")); } return map; }
protected void removeRelatedJobs(ProcessDefinition processDefinition) { List<JobEntity> timerJobs = getJobEntityManager() .findJobsByProcessDefinitionId(processDefinition.getId()); if (timerJobs != null && timerJobs.size() > 0) { for (JobEntity timerJob : timerJobs) { if (getEventDispatcher().isEnabled()) { getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.JOB_CANCELED, timerJob, null, null, processDefinition.getId())); } getJobEntityManager().delete(timerJob); } } }
protected void removeTimerSuspendProcesDefJobs(ProcessDefinition processDefinition) { List<JobEntity> timerJobs = getJobEntityManager() .findJobsByTypeAndProcessDefinitionId(TimerSuspendProcessDefinitionHandler.TYPE, processDefinition.getId()); if (timerJobs != null && timerJobs.size() > 0) { for (JobEntity timerJob : timerJobs) { if (getEventDispatcher().isEnabled()) { getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.JOB_CANCELED, timerJob, null, null, processDefinition.getId())); } getJobEntityManager().delete(timerJob); } } }
protected void removeTimerStartJobs(ProcessDefinition processDefinition) { List<TimerJobEntity> timerStartJobs = getTimerJobEntityManager() .findJobsByTypeAndProcessDefinitionId(TimerStartEventJobHandler.TYPE, processDefinition.getId()); if (timerStartJobs != null && timerStartJobs.size() > 0) { for (TimerJobEntity timerStartJob : timerStartJobs) { if (getEventDispatcher().isEnabled()) { getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.JOB_CANCELED, timerStartJob, null, null, processDefinition.getId())); } getTimerJobEntityManager().delete(timerStartJob); } } }
protected List<ProcessInstance> fetchProcessInstancesPage(CommandContext commandContext, ProcessDefinition processDefinition, int currentPageStartIndex) { if (SuspensionState.ACTIVE.equals(getProcessDefinitionSuspensionState())) { return new ProcessInstanceQueryImpl(commandContext).processDefinitionId(processDefinition.getId()).suspended() .listPage(currentPageStartIndex, commandContext.getProcessEngineConfiguration().getBatchSizeProcessInstances()); } else { return new ProcessInstanceQueryImpl(commandContext).processDefinitionId(processDefinition.getId()).active() .listPage(currentPageStartIndex, commandContext.getProcessEngineConfiguration().getBatchSizeProcessInstances()); } }
@Override public List<ActivityImpl> getActivitiesByKey(String procDefKey, String activityId) { ProcessDefinition definition = repositoryService.createProcessDefinitionQuery() .processDefinitionKey(procDefKey) .orderByProcessDefinitionVersion() .desc() .singleResult(); return getActivitiesById(definition.getId(), activityId); }
protected void validateAndSwitchVersionOfExecution(CommandContext commandContext, ExecutionEntity execution, ProcessDefinition newProcessDefinition) { // check that the new process definition version contains the current activity org.activiti.bpmn.model.Process process = ProcessDefinitionUtil.getProcess(newProcessDefinition.getId()); if (execution.getActivityId() != null && process.getFlowElement(execution.getActivityId(), true) == null) { throw new ActivitiException("The new process definition " + "(key = '" + newProcessDefinition.getKey() + "') " + "does not contain the current activity " + "(id = '" + execution.getActivityId() + "') " + "of the process instance " + "(id = '" + processInstanceId + "')."); } // switch the process instance to the new process definition version execution.setProcessDefinitionId(newProcessDefinition.getId()); execution.setProcessDefinitionName(newProcessDefinition.getName()); execution.setProcessDefinitionKey(newProcessDefinition.getKey()); // and change possible existing tasks (as the process definition id is stored there too) List<TaskEntity> tasks = commandContext.getTaskEntityManager().findTasksByExecutionId(execution.getId()); for (TaskEntity taskEntity : tasks) { taskEntity.setProcessDefinitionId(newProcessDefinition.getId()); commandContext.getHistoryManager().recordTaskProcessDefinitionChange(taskEntity.getId(), newProcessDefinition.getId()); } }
/** * Creates and deploys the one task process. See {@link #createOneTaskTestProcess()}. * * @return The process definition id (NOT the process definition key) of deployed one task process. */ public String deployOneTaskTestProcess() { BpmnModel bpmnModel = createOneTaskTestProcess(); Deployment deployment = repositoryService.createDeployment().addBpmnModel("oneTasktest.bpmn20.xml", bpmnModel).deploy(); deploymentIdsForAutoCleanup.add(deployment.getId()); // For auto-cleanup ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult(); return processDefinition.getId(); }
public String deployTwoTasksTestProcess() { BpmnModel bpmnModel = createTwoTasksTestProcess(); Deployment deployment = repositoryService.createDeployment().addBpmnModel("twoTasksTestProcess.bpmn20.xml", bpmnModel).deploy(); deploymentIdsForAutoCleanup.add(deployment.getId()); // For auto-cleanup ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult(); return processDefinition.getId(); }
public ProcessDefinition from(org.activiti.engine.repository.ProcessDefinition internalProcessDefinition) { ProcessDefinitionImpl processDefinition = new ProcessDefinitionImpl(); processDefinition.setId(internalProcessDefinition.getId()); processDefinition.setName(internalProcessDefinition.getName()); processDefinition.setDescription(internalProcessDefinition.getDescription()); processDefinition.setVersion(internalProcessDefinition.getVersion()); processDefinition.setKey(internalProcessDefinition.getKey()); BpmnModel model = repositoryService.getBpmnModel(internalProcessDefinition.getId()); processDefinition.setFormKey(model.getStartFormKey(internalProcessDefinition.getKey())); return processDefinition; } }
/** * Resolving the process definition will fetch the BPMN 2.0, parse it and store the {@link BpmnModel} in memory. */ public ProcessDefinitionCacheEntry resolveProcessDefinition(ProcessDefinition processDefinition) { String processDefinitionId = processDefinition.getId(); String deploymentId = processDefinition.getDeploymentId(); ProcessDefinitionCacheEntry cachedProcessDefinition = processDefinitionCache.get(processDefinitionId); if (cachedProcessDefinition == null) { CommandContext commandContext = Context.getCommandContext(); DeploymentEntity deployment = deploymentEntityManager.findById(deploymentId); deployment.setNew(false); deploy(deployment, null); cachedProcessDefinition = processDefinitionCache.get(processDefinitionId); if (cachedProcessDefinition == null) { throw new ActivitiException("deployment '" + deploymentId + "' didn't put process definition '" + processDefinitionId + "' in the cache"); } } return cachedProcessDefinition; }
protected void restoreMessageStartEvent(ProcessDefinition previousProcessDefinition, BpmnModel bpmnModel, StartEvent startEvent, EventDefinition eventDefinition) { MessageEventDefinition messageEventDefinition = (MessageEventDefinition) eventDefinition; if (bpmnModel.containsMessageId(messageEventDefinition.getMessageRef())) { Message message = bpmnModel.getMessage(messageEventDefinition.getMessageRef()); messageEventDefinition.setMessageRef(message.getName()); } MessageEventSubscriptionEntity newSubscription = getEventSubscriptionEntityManager().createMessageEventSubscription(); newSubscription.setEventName(messageEventDefinition.getMessageRef()); newSubscription.setActivityId(startEvent.getId()); newSubscription.setConfiguration(previousProcessDefinition.getId()); newSubscription.setProcessDefinitionId(previousProcessDefinition.getId()); if (previousProcessDefinition.getTenantId() != null) { newSubscription.setTenantId(previousProcessDefinition.getTenantId()); } getEventSubscriptionEntityManager().insert(newSubscription); }
protected void restoreTimerStartEvent(ProcessDefinition previousProcessDefinition, StartEvent startEvent, EventDefinition eventDefinition) { TimerEventDefinition timerEventDefinition = (TimerEventDefinition) eventDefinition; TimerJobEntity timer = TimerUtil.createTimerEntityForTimerEventDefinition((TimerEventDefinition) eventDefinition, false, null, TimerStartEventJobHandler.TYPE, TimerEventHandler.createConfiguration(startEvent.getId(), timerEventDefinition.getEndDate(), timerEventDefinition.getCalendarName())); if (timer != null) { TimerJobEntity timerJob = getJobManager().createTimerJob((TimerEventDefinition) eventDefinition, false, null, TimerStartEventJobHandler.TYPE, TimerEventHandler.createConfiguration(startEvent.getId(), timerEventDefinition.getEndDate(), timerEventDefinition.getCalendarName())); timerJob.setProcessDefinitionId(previousProcessDefinition.getId()); if (previousProcessDefinition.getTenantId() != null) { timerJob.setTenantId(previousProcessDefinition.getTenantId()); } getJobManager().scheduleTimerJob(timerJob); } }
protected void restoreSignalStartEvent(ProcessDefinition previousProcessDefinition, BpmnModel bpmnModel, StartEvent startEvent, EventDefinition eventDefinition) { SignalEventDefinition signalEventDefinition = (SignalEventDefinition) eventDefinition; SignalEventSubscriptionEntity subscriptionEntity = getEventSubscriptionEntityManager().createSignalEventSubscription(); Signal signal = bpmnModel.getSignal(signalEventDefinition.getSignalRef()); if (signal != null) { subscriptionEntity.setEventName(signal.getName()); } else { subscriptionEntity.setEventName(signalEventDefinition.getSignalRef()); } subscriptionEntity.setActivityId(startEvent.getId()); subscriptionEntity.setProcessDefinitionId(previousProcessDefinition.getId()); if (previousProcessDefinition.getTenantId() != null) { subscriptionEntity.setTenantId(previousProcessDefinition.getTenantId()); } getEventSubscriptionEntityManager().insert(subscriptionEntity); }
@Override public void deleteDeployment(String deploymentId, boolean cascade) { List<ProcessDefinition> processDefinitions = new ProcessDefinitionQueryImpl().deploymentId(deploymentId).list(); updateRelatedModels(deploymentId); if (cascade) { deleteProcessInstancesForProcessDefinitions(processDefinitions); } for (ProcessDefinition processDefinition : processDefinitions) { deleteProcessDefinitionIdentityLinks(processDefinition); deleteEventSubscriptions(processDefinition); deleteProcessDefinitionInfo(processDefinition.getId()); removeTimerStartJobs(processDefinition); // If previous process definition version has a timer/signal/message start event, it must be added // Only if the currently deleted process definition is the latest version, // we fall back to the previous timer/signal/message start event restorePreviousStartEventsIfNeeded(processDefinition); } deleteProcessDefinitionForDeployment(deploymentId); getResourceEntityManager().deleteResourcesByDeploymentId(deploymentId); delete(findById(deploymentId), false); }