public void handleEvent(
      EventSubscriptionEntity eventSubscription, Object payload, CommandContext commandContext) {

    String configuration = eventSubscription.getConfiguration();
    if (configuration == null) {
      throw new ActivitiException(
          "Compensating execution not set for compensate event subscription with id "
              + eventSubscription.getId());
    }

    ExecutionEntity compensatingExecution =
        commandContext.getExecutionEntityManager().findExecutionById(configuration);

    ActivityImpl compensationHandler = eventSubscription.getActivity();

    if ((compensationHandler.getProperty(BpmnParse.PROPERTYNAME_IS_FOR_COMPENSATION) == null
            || !(Boolean)
                compensationHandler.getProperty(BpmnParse.PROPERTYNAME_IS_FOR_COMPENSATION))
        && compensationHandler.isScope()) {

      // descend into scope:
      List<CompensateEventSubscriptionEntity> eventsForThisScope =
          compensatingExecution.getCompensateEventSubscriptions();
      ScopeUtil.throwCompensationEvent(eventsForThisScope, compensatingExecution, false);

    } else {
      try {

        if (commandContext.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
          commandContext
              .getProcessEngineConfiguration()
              .getEventDispatcher()
              .dispatchEvent(
                  ActivitiEventBuilder.createActivityEvent(
                      ActivitiEventType.ACTIVITY_COMPENSATE,
                      compensationHandler.getId(),
                      compensatingExecution.getId(),
                      compensatingExecution.getProcessInstanceId(),
                      compensatingExecution.getProcessDefinitionId()));
        }
        compensatingExecution.setActivity(compensationHandler);

        // executing the atomic operation makes sure activity start events are fired
        compensatingExecution.performOperation(AtomicOperation.ACTIVITY_START);

      } catch (Exception e) {
        throw new ActivitiException(
            "Error while handling compensation event " + eventSubscription, e);
      }
    }
  }
Beispiel #2
0
  protected boolean localizeDataObjectElements(
      List<ValuedDataObject> dataObjects, ObjectNode infoNode) {
    boolean localizationValuesChanged = false;
    CommandContext commandContext = Context.getCommandContext();
    DynamicBpmnService dynamicBpmnService =
        commandContext.getProcessEngineConfiguration().getDynamicBpmnService();

    for (ValuedDataObject dataObject : dataObjects) {
      List<ExtensionElement> localizationElements =
          dataObject.getExtensionElements().get("localization");
      if (localizationElements != null) {
        for (ExtensionElement localizationElement : localizationElements) {
          if (BpmnXMLConstants.ACTIVITI_EXTENSIONS_PREFIX.equals(
              localizationElement.getNamespacePrefix())) {
            String locale = localizationElement.getAttributeValue(null, "locale");
            String name = localizationElement.getAttributeValue(null, "name");
            String documentation = null;

            List<ExtensionElement> documentationElements =
                localizationElement.getChildElements().get("documentation");
            if (documentationElements != null) {
              for (ExtensionElement documentationElement : documentationElements) {
                documentation = StringUtils.trimToNull(documentationElement.getElementText());
                break;
              }
            }

            if (name != null
                && isEqualToCurrentLocalizationValue(
                        locale,
                        dataObject.getName(),
                        DynamicBpmnConstants.LOCALIZATION_NAME,
                        name,
                        infoNode)
                    == false) {
              dynamicBpmnService.changeLocalizationName(
                  locale, dataObject.getName(), name, infoNode);
              localizationValuesChanged = true;
            }

            if (documentation != null
                && isEqualToCurrentLocalizationValue(
                        locale,
                        dataObject.getName(),
                        DynamicBpmnConstants.LOCALIZATION_DESCRIPTION,
                        documentation,
                        infoNode)
                    == false) {

              dynamicBpmnService.changeLocalizationDescription(
                  locale, dataObject.getName(), documentation, infoNode);
              localizationValuesChanged = true;
            }
          }
        }
      }
    }

    return localizationValuesChanged;
  }
Beispiel #3
0
  protected void createLocalizationValues(String processDefinitionId, Process process) {
    if (process == null) return;

    CommandContext commandContext = Context.getCommandContext();
    DynamicBpmnService dynamicBpmnService =
        commandContext.getProcessEngineConfiguration().getDynamicBpmnService();
    ObjectNode infoNode = dynamicBpmnService.getProcessDefinitionInfo(processDefinitionId);

    boolean localizationValuesChanged = false;
    List<ExtensionElement> localizationElements =
        process.getExtensionElements().get("localization");
    if (localizationElements != null) {
      for (ExtensionElement localizationElement : localizationElements) {
        if (BpmnXMLConstants.ACTIVITI_EXTENSIONS_PREFIX.equals(
            localizationElement.getNamespacePrefix())) {
          String locale = localizationElement.getAttributeValue(null, "locale");
          String name = localizationElement.getAttributeValue(null, "name");
          String documentation = null;
          List<ExtensionElement> documentationElements =
              localizationElement.getChildElements().get("documentation");
          if (documentationElements != null) {
            for (ExtensionElement documentationElement : documentationElements) {
              documentation = StringUtils.trimToNull(documentationElement.getElementText());
              break;
            }
          }

          String processId = process.getId();
          if (isEqualToCurrentLocalizationValue(locale, processId, "name", name, infoNode)
              == false) {
            dynamicBpmnService.changeLocalizationName(locale, processId, name, infoNode);
            localizationValuesChanged = true;
          }

          if (documentation != null
              && isEqualToCurrentLocalizationValue(
                      locale, processId, "description", documentation, infoNode)
                  == false) {
            dynamicBpmnService.changeLocalizationDescription(
                locale, processId, documentation, infoNode);
            localizationValuesChanged = true;
          }

          break;
        }
      }
    }

    boolean isFlowElementLocalizationChanged =
        localizeFlowElements(process.getFlowElements(), infoNode);
    boolean isDataObjectLocalizationChanged =
        localizeDataObjectElements(process.getDataObjects(), infoNode);
    if (isFlowElementLocalizationChanged || isDataObjectLocalizationChanged) {
      localizationValuesChanged = true;
    }

    if (localizationValuesChanged) {
      dynamicBpmnService.saveProcessDefinitionInfo(processDefinitionId, infoNode);
    }
  }
Beispiel #4
0
  protected void addDefinitionInfoToCache(
      ProcessDefinitionEntity processDefinition,
      ProcessEngineConfigurationImpl processEngineConfiguration,
      CommandContext commandContext) {

    if (processEngineConfiguration.isEnableProcessDefinitionInfoCache() == false) {
      return;
    }

    DeploymentManager deploymentManager = processEngineConfiguration.getDeploymentManager();
    ProcessDefinitionInfoEntityManager definitionInfoEntityManager =
        commandContext.getProcessDefinitionInfoEntityManager();
    ObjectMapper objectMapper = commandContext.getProcessEngineConfiguration().getObjectMapper();
    ProcessDefinitionInfoEntity definitionInfoEntity =
        definitionInfoEntityManager.findProcessDefinitionInfoByProcessDefinitionId(
            processDefinition.getId());

    ObjectNode infoNode = null;
    if (definitionInfoEntity != null && definitionInfoEntity.getInfoJsonId() != null) {
      byte[] infoBytes =
          definitionInfoEntityManager.findInfoJsonById(definitionInfoEntity.getInfoJsonId());
      if (infoBytes != null) {
        try {
          infoNode = (ObjectNode) objectMapper.readTree(infoBytes);
        } catch (Exception e) {
          throw new ActivitiException(
              "Error deserializing json info for process definition " + processDefinition.getId());
        }
      }
    }

    ProcessDefinitionInfoCacheObject definitionCacheObject = new ProcessDefinitionInfoCacheObject();
    if (definitionInfoEntity == null) {
      definitionCacheObject.setRevision(0);
    } else {
      definitionCacheObject.setId(definitionInfoEntity.getId());
      definitionCacheObject.setRevision(definitionInfoEntity.getRevision());
    }

    if (infoNode == null) {
      infoNode = objectMapper.createObjectNode();
    }
    definitionCacheObject.setInfoNode(infoNode);

    deploymentManager
        .getProcessDefinitionInfoCache()
        .add(processDefinition.getId(), definitionCacheObject);
  }
Beispiel #5
0
 public Task execute(CommandContext commandContext) {
   Date currentTime = commandContext.getProcessEngineConfiguration().getClock().getCurrentTime();
   TaskEntity task = TaskEntity.create(currentTime);
   task.setId(taskId);
   return task;
 }
Beispiel #6
0
  public void deploy(DeploymentEntity deployment, Map<String, Object> deploymentSettings) {
    log.debug("Processing deployment {}", deployment.getName());

    List<ProcessDefinitionEntity> processDefinitions = new ArrayList<ProcessDefinitionEntity>();
    Map<String, ResourceEntity> resources = deployment.getResources();
    Map<String, BpmnModel> bpmnModelMap = new HashMap<String, BpmnModel>();

    final ProcessEngineConfigurationImpl processEngineConfiguration =
        Context.getProcessEngineConfiguration();
    for (String resourceName : resources.keySet()) {

      log.info("Processing resource {}", resourceName);
      if (isBpmnResource(resourceName)) {
        ResourceEntity resource = resources.get(resourceName);
        byte[] bytes = resource.getBytes();
        ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);

        BpmnParse bpmnParse =
            bpmnParser
                .createParse()
                .sourceInputStream(inputStream)
                .setSourceSystemId(resourceName)
                .deployment(deployment)
                .name(resourceName);

        if (deploymentSettings != null) {

          // Schema validation if needed
          if (deploymentSettings.containsKey(DeploymentSettings.IS_BPMN20_XSD_VALIDATION_ENABLED)) {
            bpmnParse.setValidateSchema(
                (Boolean)
                    deploymentSettings.get(DeploymentSettings.IS_BPMN20_XSD_VALIDATION_ENABLED));
          }

          // Process validation if needed
          if (deploymentSettings.containsKey(DeploymentSettings.IS_PROCESS_VALIDATION_ENABLED)) {
            bpmnParse.setValidateProcess(
                (Boolean) deploymentSettings.get(DeploymentSettings.IS_PROCESS_VALIDATION_ENABLED));
          }

        } else {
          // On redeploy, we assume it is validated at the first deploy
          bpmnParse.setValidateSchema(false);
          bpmnParse.setValidateProcess(false);
        }

        bpmnParse.execute();

        for (ProcessDefinitionEntity processDefinition : bpmnParse.getProcessDefinitions()) {
          processDefinition.setResourceName(resourceName);

          if (deployment.getTenantId() != null) {
            processDefinition.setTenantId(
                deployment.getTenantId()); // process definition inherits the tenant id
          }

          String diagramResourceName =
              getDiagramResourceForProcess(resourceName, processDefinition.getKey(), resources);

          // Only generate the resource when deployment is new to prevent modification of deployment
          // resources
          // after the process-definition is actually deployed. Also to prevent resource-generation
          // failure every
          // time the process definition is added to the deployment-cache when diagram-generation
          // has failed the first time.
          if (deployment.isNew()) {
            if (processEngineConfiguration.isCreateDiagramOnDeploy()
                && diagramResourceName == null
                && processDefinition.isGraphicalNotationDefined()) {
              try {
                byte[] diagramBytes =
                    IoUtil.readInputStream(
                        processEngineConfiguration
                            .getProcessDiagramGenerator()
                            .generateDiagram(
                                bpmnParse.getBpmnModel(),
                                "png",
                                processEngineConfiguration.getActivityFontName(),
                                processEngineConfiguration.getLabelFontName(),
                                processEngineConfiguration.getClassLoader()),
                        null);
                diagramResourceName =
                    getProcessImageResourceName(resourceName, processDefinition.getKey(), "png");
                createResource(diagramResourceName, diagramBytes, deployment);
              } catch (
                  Throwable
                      t) { // if anything goes wrong, we don't store the image (the process will
                           // still be executable).
                log.warn(
                    "Error while generating process diagram, image will not be stored in repository",
                    t);
              }
            }
          }

          processDefinition.setDiagramResourceName(diagramResourceName);
          processDefinitions.add(processDefinition);
          bpmnModelMap.put(processDefinition.getKey(), bpmnParse.getBpmnModel());
        }
      }
    }

    // check if there are process definitions with the same process key to prevent database unique
    // index violation
    List<String> keyList = new ArrayList<String>();
    for (ProcessDefinitionEntity processDefinition : processDefinitions) {
      if (keyList.contains(processDefinition.getKey())) {
        throw new ActivitiException(
            "The deployment contains process definitions with the same key (process id atrribute), this is not allowed");
      }
      keyList.add(processDefinition.getKey());
    }

    CommandContext commandContext = Context.getCommandContext();
    ProcessDefinitionEntityManager processDefinitionManager =
        commandContext.getProcessDefinitionEntityManager();
    DbSqlSession dbSqlSession = commandContext.getSession(DbSqlSession.class);
    for (ProcessDefinitionEntity processDefinition : processDefinitions) {
      List<TimerEntity> timers = new ArrayList<TimerEntity>();
      if (deployment.isNew()) {
        int processDefinitionVersion;

        ProcessDefinitionEntity latestProcessDefinition = null;
        if (processDefinition.getTenantId() != null
            && !ProcessEngineConfiguration.NO_TENANT_ID.equals(processDefinition.getTenantId())) {
          latestProcessDefinition =
              processDefinitionManager.findLatestProcessDefinitionByKeyAndTenantId(
                  processDefinition.getKey(), processDefinition.getTenantId());
        } else {
          latestProcessDefinition =
              processDefinitionManager.findLatestProcessDefinitionByKey(processDefinition.getKey());
        }

        if (latestProcessDefinition != null) {
          processDefinitionVersion = latestProcessDefinition.getVersion() + 1;
        } else {
          processDefinitionVersion = 1;
        }

        processDefinition.setVersion(processDefinitionVersion);
        processDefinition.setDeploymentId(deployment.getId());

        String nextId = idGenerator.getNextId();
        String processDefinitionId =
            processDefinition.getKey()
                + ":"
                + processDefinition.getVersion()
                + ":"
                + nextId; // ACT-505

        // ACT-115: maximum id length is 64 charcaters
        if (processDefinitionId.length() > 64) {
          processDefinitionId = nextId;
        }
        processDefinition.setId(processDefinitionId);

        if (commandContext.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
          commandContext
              .getProcessEngineConfiguration()
              .getEventDispatcher()
              .dispatchEvent(
                  ActivitiEventBuilder.createEntityEvent(
                      ActivitiEventType.ENTITY_CREATED, processDefinition));
        }

        removeObsoleteTimers(processDefinition);
        addTimerDeclarations(processDefinition, timers);

        removeExistingMessageEventSubscriptions(processDefinition, latestProcessDefinition);
        addMessageEventSubscriptions(processDefinition);

        removeExistingSignalEventSubScription(processDefinition, latestProcessDefinition);
        addSignalEventSubscriptions(processDefinition);

        dbSqlSession.insert(processDefinition);
        addAuthorizations(processDefinition);

        if (commandContext.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
          commandContext
              .getProcessEngineConfiguration()
              .getEventDispatcher()
              .dispatchEvent(
                  ActivitiEventBuilder.createEntityEvent(
                      ActivitiEventType.ENTITY_INITIALIZED, processDefinition));
        }

        scheduleTimers(timers);

      } else {
        String deploymentId = deployment.getId();
        processDefinition.setDeploymentId(deploymentId);

        ProcessDefinitionEntity persistedProcessDefinition = null;
        if (processDefinition.getTenantId() == null
            || ProcessEngineConfiguration.NO_TENANT_ID.equals(processDefinition.getTenantId())) {
          persistedProcessDefinition =
              processDefinitionManager.findProcessDefinitionByDeploymentAndKey(
                  deploymentId, processDefinition.getKey());
        } else {
          persistedProcessDefinition =
              processDefinitionManager.findProcessDefinitionByDeploymentAndKeyAndTenantId(
                  deploymentId, processDefinition.getKey(), processDefinition.getTenantId());
        }

        if (persistedProcessDefinition != null) {
          processDefinition.setId(persistedProcessDefinition.getId());
          processDefinition.setVersion(persistedProcessDefinition.getVersion());
          processDefinition.setSuspensionState(persistedProcessDefinition.getSuspensionState());
        }
      }

      // Add to cache
      DeploymentManager deploymentManager = processEngineConfiguration.getDeploymentManager();
      deploymentManager
          .getProcessDefinitionCache()
          .add(processDefinition.getId(), processDefinition);
      addDefinitionInfoToCache(processDefinition, processEngineConfiguration, commandContext);

      // Add to deployment for further usage
      deployment.addDeployedArtifact(processDefinition);

      createLocalizationValues(
          processDefinition.getId(),
          bpmnModelMap.get(processDefinition.getKey()).getProcessById(processDefinition.getKey()));
    }
  }
Beispiel #7
0
  protected boolean localizeFlowElements(
      Collection<FlowElement> flowElements, ObjectNode infoNode) {
    boolean localizationValuesChanged = false;

    if (flowElements == null) return localizationValuesChanged;

    CommandContext commandContext = Context.getCommandContext();
    DynamicBpmnService dynamicBpmnService =
        commandContext.getProcessEngineConfiguration().getDynamicBpmnService();

    for (FlowElement flowElement : flowElements) {
      if (flowElement instanceof UserTask || flowElement instanceof SubProcess) {
        List<ExtensionElement> localizationElements =
            flowElement.getExtensionElements().get("localization");
        if (localizationElements != null) {
          for (ExtensionElement localizationElement : localizationElements) {
            if (BpmnXMLConstants.ACTIVITI_EXTENSIONS_PREFIX.equals(
                localizationElement.getNamespacePrefix())) {
              String locale = localizationElement.getAttributeValue(null, "locale");
              String name = localizationElement.getAttributeValue(null, "name");
              String documentation = null;
              List<ExtensionElement> documentationElements =
                  localizationElement.getChildElements().get("documentation");
              if (documentationElements != null) {
                for (ExtensionElement documentationElement : documentationElements) {
                  documentation = StringUtils.trimToNull(documentationElement.getElementText());
                  break;
                }
              }

              String flowElementId = flowElement.getId();
              if (isEqualToCurrentLocalizationValue(locale, flowElementId, "name", name, infoNode)
                  == false) {
                dynamicBpmnService.changeLocalizationName(locale, flowElementId, name, infoNode);
                localizationValuesChanged = true;
              }

              if (documentation != null
                  && isEqualToCurrentLocalizationValue(
                          locale, flowElementId, "description", documentation, infoNode)
                      == false) {
                dynamicBpmnService.changeLocalizationDescription(
                    locale, flowElementId, documentation, infoNode);
                localizationValuesChanged = true;
              }

              break;
            }
          }
        }

        if (flowElement instanceof SubProcess) {
          SubProcess subprocess = (SubProcess) flowElement;
          boolean isFlowElementLocalizationChanged =
              localizeFlowElements(subprocess.getFlowElements(), infoNode);
          boolean isDataObjectLocalizationChanged =
              localizeDataObjectElements(subprocess.getDataObjects(), infoNode);
          if (isFlowElementLocalizationChanged || isDataObjectLocalizationChanged) {
            localizationValuesChanged = true;
          }
        }
      }
    }

    return localizationValuesChanged;
  }