@Override
  public String evaluate(KaleoCondition kaleoCondition, ExecutionContext executionContext)
      throws PortalException {

    Map<String, Object> inputObjects =
        ScriptingContextBuilderUtil.buildScriptingContext(executionContext);

    Map<String, Object> results =
        _scripting.eval(
            null,
            inputObjects,
            _outputNames,
            kaleoCondition.getScriptLanguage(),
            kaleoCondition.getScript());

    Map<String, Serializable> resultsWorkflowContext =
        (Map<String, Serializable>) results.get(WorkflowContextUtil.WORKFLOW_CONTEXT_NAME);

    WorkflowContextUtil.mergeWorkflowContexts(executionContext, resultsWorkflowContext);

    String returnValue = (String) results.get(_RETURN_VALUE);

    if (returnValue != null) {
      return returnValue;
    }

    throw new IllegalStateException(
        "Conditional did not return value for script " + kaleoCondition.getScript());
  }
  @Override
  public Collection<KaleoTaskAssignment> calculateTaskAssignments(
      KaleoTaskAssignment kaleoTaskAssignment,
      ExecutionContext executionContext,
      ClassLoader... classLoaders)
      throws PortalException, SystemException {

    Map<String, Object> inputObjects =
        ScriptingContextBuilderUtil.buildScriptingContext(executionContext);

    String assigneeScript = kaleoTaskAssignment.getAssigneeScript();

    String assigneeScriptingLanguage = kaleoTaskAssignment.getAssigneeScriptLanguage();

    Map<String, Object> results =
        ScriptingUtil.eval(
            null,
            inputObjects,
            _outputNames,
            assigneeScriptingLanguage,
            assigneeScript,
            classLoaders);

    Map<String, Serializable> resultsWorkflowContext =
        (Map<String, Serializable>) results.get(WorkflowContextUtil.WORKFLOW_CONTEXT_NAME);

    WorkflowContextUtil.mergeWorkflowContexts(executionContext, resultsWorkflowContext);

    return getKaleoTaskAssignments(results);
  }
Example #3
0
  public WorkflowTaskAdapter(
      KaleoTaskInstanceToken kaleoTaskInstanceToken, Map<String, Serializable> workflowContext)
      throws PortalException, SystemException {

    setCreateDate(kaleoTaskInstanceToken.getCreateDate());
    setCompletionDate(kaleoTaskInstanceToken.getCompletionDate());
    setDescription(kaleoTaskInstanceToken.getKaleoTask().getDescription());
    setDueDate(kaleoTaskInstanceToken.getDueDate());
    setName(kaleoTaskInstanceToken.getKaleoTask().getName());

    if (workflowContext != null) {
      setOptionalAttributes(workflowContext);
    } else {
      setOptionalAttributes(
          WorkflowContextUtil.convert(kaleoTaskInstanceToken.getWorkflowContext()));
    }

    KaleoInstanceToken kaleoInstanceToken = kaleoTaskInstanceToken.getKaleoInstanceToken();
    KaleoInstance kaleoInstance = kaleoInstanceToken.getKaleoInstance();

    setWorkflowDefinitionId(kaleoInstance.getKaleoDefinitionId());
    setWorkflowDefinitionName(kaleoInstance.getKaleoDefinitionName());
    setWorkflowDefinitionVersion(kaleoInstance.getKaleoDefinitionVersion());
    setWorkflowInstanceId(kaleoInstance.getKaleoInstanceId());

    List<WorkflowTaskAssignee> workflowTaskAssignees =
        KaleoTaskAssignmentInstanceUtil.getWorkflowTaskAssignees(kaleoTaskInstanceToken);

    setWorkflowTaskAssignees(workflowTaskAssignees);

    setWorkflowTaskId(kaleoTaskInstanceToken.getKaleoTaskInstanceTokenId());
  }
  public KaleoTaskInstanceToken addKaleoTaskInstanceToken(
      long kaleoInstanceTokenId,
      long kaleoTaskId,
      String kaleoTaskName,
      Collection<KaleoTaskAssignment> kaleoTaskAssignments,
      Date dueDate,
      Map<String, Serializable> workflowContext,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    KaleoInstanceToken kaleoInstanceToken =
        kaleoInstanceTokenPersistence.findByPrimaryKey(kaleoInstanceTokenId);

    User user = userPersistence.findByPrimaryKey(serviceContext.getGuestOrUserId());
    Date now = new Date();

    long kaleoTaskInstanceTokenId = counterLocalService.increment();

    KaleoTaskInstanceToken kaleoTaskInstanceToken =
        kaleoTaskInstanceTokenPersistence.create(kaleoTaskInstanceTokenId);

    long groupId = StagingUtil.getLiveGroupId(serviceContext.getScopeGroupId());

    kaleoTaskInstanceToken.setGroupId(groupId);

    kaleoTaskInstanceToken.setCompanyId(user.getCompanyId());
    kaleoTaskInstanceToken.setUserId(user.getUserId());
    kaleoTaskInstanceToken.setUserName(user.getFullName());
    kaleoTaskInstanceToken.setCreateDate(now);
    kaleoTaskInstanceToken.setModifiedDate(now);
    kaleoTaskInstanceToken.setDueDate(dueDate);
    kaleoTaskInstanceToken.setKaleoDefinitionId(kaleoInstanceToken.getKaleoDefinitionId());
    kaleoTaskInstanceToken.setKaleoInstanceId(kaleoInstanceToken.getKaleoInstanceId());
    kaleoTaskInstanceToken.setKaleoInstanceTokenId(kaleoInstanceTokenId);

    kaleoTaskInstanceToken.setKaleoTaskId(kaleoTaskId);
    kaleoTaskInstanceToken.setKaleoTaskName(kaleoTaskName);

    if (workflowContext != null) {
      kaleoTaskInstanceToken.setClassName(
          (String) workflowContext.get(WorkflowConstants.CONTEXT_ENTRY_CLASS_NAME));

      if (workflowContext.containsKey(WorkflowConstants.CONTEXT_ENTRY_CLASS_PK)) {

        kaleoTaskInstanceToken.setClassPK(
            GetterUtil.getLong(
                (String) workflowContext.get(WorkflowConstants.CONTEXT_ENTRY_CLASS_PK)));
      }
    }

    kaleoTaskInstanceToken.setCompleted(false);
    kaleoTaskInstanceToken.setWorkflowContext(WorkflowContextUtil.convert(workflowContext));

    kaleoTaskInstanceTokenPersistence.update(kaleoTaskInstanceToken, false);

    kaleoTaskAssignmentInstanceLocalService.addTaskAssignmentInstances(
        kaleoTaskInstanceToken, kaleoTaskAssignments, workflowContext, serviceContext);

    return kaleoTaskInstanceToken;
  }
  @Override
  public KaleoInstance addKaleoInstance(
      long kaleoDefinitionId,
      String kaleoDefinitionName,
      int kaleoDefinitionVersion,
      Map<String, Serializable> workflowContext,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    User user = userPersistence.fetchByPrimaryKey(serviceContext.getUserId());

    if (user == null) {
      user = userLocalService.getDefaultUser(serviceContext.getCompanyId());
    }

    Date now = new Date();

    long kaleoInstanceId = counterLocalService.increment();

    KaleoInstance kaleoInstance = kaleoInstancePersistence.create(kaleoInstanceId);

    long groupId = StagingUtil.getLiveGroupId(serviceContext.getScopeGroupId());

    kaleoInstance.setGroupId(groupId);

    kaleoInstance.setCompanyId(user.getCompanyId());
    kaleoInstance.setUserId(user.getUserId());
    kaleoInstance.setUserName(user.getFullName());
    kaleoInstance.setCreateDate(now);
    kaleoInstance.setModifiedDate(now);
    kaleoInstance.setKaleoDefinitionId(kaleoDefinitionId);
    kaleoInstance.setKaleoDefinitionName(kaleoDefinitionName);
    kaleoInstance.setKaleoDefinitionVersion(kaleoDefinitionVersion);
    kaleoInstance.setClassName(
        (String) workflowContext.get(WorkflowConstants.CONTEXT_ENTRY_CLASS_NAME));

    if (workflowContext.containsKey(WorkflowConstants.CONTEXT_ENTRY_CLASS_PK)) {

      kaleoInstance.setClassPK(
          GetterUtil.getLong(
              (String) workflowContext.get(WorkflowConstants.CONTEXT_ENTRY_CLASS_PK)));
    }

    kaleoInstance.setCompleted(false);
    kaleoInstance.setWorkflowContext(WorkflowContextUtil.convert(workflowContext));

    kaleoInstancePersistence.update(kaleoInstance);

    return kaleoInstance;
  }
  protected List<WorkflowTask> toWorkflowTasks(List<KaleoTaskInstanceToken> kaleoTaskInstanceTokens)
      throws PortalException {

    List<WorkflowTask> workflowTasks = new ArrayList<>(kaleoTaskInstanceTokens.size());

    for (KaleoTaskInstanceToken kaleoTaskInstanceToken : kaleoTaskInstanceTokens) {

      workflowTasks.add(
          WorkflowModelUtil.toWorkflowTask(
              kaleoTaskInstanceToken,
              WorkflowContextUtil.convert(kaleoTaskInstanceToken.getWorkflowContext())));
    }

    return workflowTasks;
  }
  @Override
  public WorkflowTask getWorkflowTask(long companyId, long workflowTaskInstanceId)
      throws WorkflowException {

    try {
      KaleoTaskInstanceToken kaleoTaskInstanceToken =
          KaleoTaskInstanceTokenLocalServiceUtil.getKaleoTaskInstanceToken(workflowTaskInstanceId);

      return WorkflowModelUtil.toWorkflowTask(
          kaleoTaskInstanceToken,
          WorkflowContextUtil.convert(kaleoTaskInstanceToken.getWorkflowContext()));
    } catch (Exception e) {
      throw new WorkflowException(e);
    }
  }
  @Override
  public KaleoInstance updateKaleoInstance(
      long kaleoInstanceId,
      Map<String, Serializable> workflowContext,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    KaleoInstance kaleoInstance = kaleoInstancePersistence.findByPrimaryKey(kaleoInstanceId);

    kaleoInstance.setWorkflowContext(WorkflowContextUtil.convert(workflowContext));

    kaleoInstancePersistence.update(kaleoInstance);

    return kaleoInstance;
  }
  public KaleoTaskInstanceToken assignKaleoTaskInstanceToken(
      long kaleoTaskInstanceTokenId,
      String assigneeClassName,
      long assigneeClassPK,
      Map<String, Serializable> workflowContext,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    KaleoTaskInstanceToken kaleoTaskInstanceToken =
        kaleoTaskInstanceTokenPersistence.findByPrimaryKey(kaleoTaskInstanceTokenId);

    kaleoTaskInstanceToken.setModifiedDate(new Date());
    kaleoTaskInstanceToken.setWorkflowContext(WorkflowContextUtil.convert(workflowContext));

    kaleoTaskInstanceTokenPersistence.update(kaleoTaskInstanceToken, false);

    kaleoTaskAssignmentInstanceLocalService.assignKaleoTaskAssignmentInstance(
        kaleoTaskInstanceToken, assigneeClassName,
        assigneeClassPK, serviceContext);

    return kaleoTaskInstanceToken;
  }
  protected List<KaleoTaskAssignment> getCalculatedKaleoTaskAssignments(
      KaleoTaskInstanceToken kaleoTaskInstanceToken) throws PortalException {

    List<KaleoTaskAssignment> calculatedKaleoTaskAssignments = new ArrayList<>();

    KaleoInstanceToken kaleoInstanceToken = kaleoTaskInstanceToken.getKaleoInstanceToken();

    Map<String, Serializable> workflowContext =
        WorkflowContextUtil.convert(kaleoTaskInstanceToken.getWorkflowContext());

    ServiceContext workflowContextServiceContext =
        (ServiceContext) workflowContext.get(WorkflowConstants.CONTEXT_SERVICE_CONTEXT);

    ExecutionContext executionContext =
        new ExecutionContext(kaleoInstanceToken, workflowContext, workflowContextServiceContext);

    List<KaleoTaskAssignment> configuredKaleoTaskAssignments =
        KaleoTaskAssignmentLocalServiceUtil.getKaleoTaskAssignments(
            kaleoTaskInstanceToken.getKaleoTaskId());

    for (KaleoTaskAssignment configuredKaleoTaskAssignment : configuredKaleoTaskAssignments) {

      String[] assigneeScriptRequiredContexts =
          StringUtil.split(configuredKaleoTaskAssignment.getAssigneeScriptRequiredContexts());

      ClassLoader[] classLoaders = ClassLoaderUtil.getClassLoaders(assigneeScriptRequiredContexts);

      Collection<KaleoTaskAssignment> kaleoTaskAssignments =
          _taskAssignmentSelector.calculateTaskAssignments(
              configuredKaleoTaskAssignment, executionContext, classLoaders);

      calculatedKaleoTaskAssignments.addAll(kaleoTaskAssignments);
    }

    return calculatedKaleoTaskAssignments;
  }
  protected void doReceive(Message message) throws Exception {
    long kaleoTimerInstanceTokenId = message.getLong("kaleoTimerInstanceTokenId");

    try {
      KaleoTimerInstanceToken kaleoTimerInstanceToken = getKaleoTimerInstanceToken(message);

      Map<String, Serializable> workflowContext =
          WorkflowContextUtil.convert(kaleoTimerInstanceToken.getWorkflowContext());

      ServiceContext serviceContext =
          (ServiceContext) workflowContext.get(WorkflowConstants.CONTEXT_SERVICE_CONTEXT);

      _workflowEngine.executeTimerWorkflowInstance(
          kaleoTimerInstanceTokenId, serviceContext, workflowContext);
    } catch (Exception e) {
      if (_log.isWarnEnabled()) {
        _log.warn("Unable to execute scheduled job. Unregistering job " + message, e);
      }

      String groupName = SchedulerUtil.getGroupName(kaleoTimerInstanceTokenId);

      SchedulerEngineHelperUtil.delete(groupName, StorageType.PERSISTED);
    }
  }
  @Override
  public WorkflowTask completeWorkflowTask(
      long companyId,
      long userId,
      long workflowTaskInstanceId,
      String transitionName,
      String comment,
      Map<String, Serializable> workflowContext)
      throws WorkflowException {

    Lock lock = null;

    try {
      lock =
          LockManagerUtil.lock(
              userId,
              WorkflowTask.class.getName(),
              workflowTaskInstanceId,
              String.valueOf(userId),
              false,
              1000);
    } catch (PortalException pe) {
      if (pe instanceof DuplicateLockException) {
        throw new WorkflowException(
            "Workflow task " + workflowTaskInstanceId + " is locked by user " + userId, pe);
      }

      throw new WorkflowException("Unable to lock workflow task " + workflowTaskInstanceId, pe);
    }

    try {
      ServiceContext serviceContext = new ServiceContext();

      serviceContext.setCompanyId(companyId);
      serviceContext.setUserId(userId);

      WorkflowTask workflowTask =
          _taskManager.completeWorkflowTask(
              workflowTaskInstanceId, transitionName, comment, workflowContext, serviceContext);

      KaleoTaskInstanceToken kaleoTaskInstanceToken =
          KaleoTaskInstanceTokenLocalServiceUtil.getKaleoTaskInstanceToken(
              workflowTask.getWorkflowTaskId());

      KaleoInstanceToken kaleoInstanceToken = kaleoTaskInstanceToken.getKaleoInstanceToken();

      if (workflowContext == null) {
        workflowContext =
            WorkflowContextUtil.convert(kaleoInstanceToken.getKaleoInstance().getWorkflowContext());
      }

      workflowContext.put(WorkflowConstants.CONTEXT_TASK_COMMENTS, comment);
      workflowContext.put(WorkflowConstants.CONTEXT_TRANSITION_NAME, transitionName);

      ExecutionContext executionContext =
          new ExecutionContext(
              kaleoInstanceToken, kaleoTaskInstanceToken, workflowContext, serviceContext);

      _kaleoSignaler.signalExit(transitionName, executionContext);

      return workflowTask;
    } catch (Exception e) {
      throw new WorkflowException("Unable to complete task", e);
    } finally {
      LockManagerUtil.unlock(lock.getClassName(), lock.getKey());
    }
  }