@Override
  public void stopWorkflow(String processInstanceId) {
    workflowComponent.stopWorkflow(processInstanceId);

    // Create the workflow history event
    WorkflowHistoryDAO workflowHistoryDao =
        (WorkflowHistoryDAO) Context.getInstance().getBean(WorkflowHistoryDAO.class);
    WorkflowHistory transaction = new WorkflowHistory();
    WorkflowInstance instance = getWorkflowInstanceById(processInstanceId, FETCH_TYPE.INFO);
    WorkflowPersistenceTemplateDAO workflowTemplateDao =
        (WorkflowPersistenceTemplateDAO)
            Context.getInstance().getBean(WorkflowPersistenceTemplateDAO.class);
    WorkflowPersistenceTemplate template = workflowTemplateDao.findByName(instance.getName());

    transaction.setTemplateId(template.getId());
    transaction.setInstanceId(processInstanceId);
    transaction.setDate(new Date());
    transaction.setSessionId(SessionManagement.getCurrentUserSessionId());
    transaction.setEvent(WorkflowHistory.EVENT_WORKFLOW_END);
    transaction.setComment("");
    transaction.setUser(SessionManagement.getUser());

    workflowHistoryDao.store(transaction);
  }
  public List<WorkflowTaskInstance> getWorkflowTasks(
      WorkflowInstance workflowInstance, WorkflowTaskInstance.STATE taskState, TASK_SORT sort) {

    List<WorkflowTaskInstance> workflowTaskInstances =
        this.workflowComponent.getTaskInstancesByActiveWorkflow(workflowInstance.getId());
    List<WorkflowTaskInstance> instances = new LinkedList<WorkflowTaskInstance>();

    for (WorkflowTaskInstance taskInstance : workflowTaskInstances) {

      if (taskInstance.getState().equals(WorkflowTaskInstance.STATE.ALL) == false) {
        if (taskInstance.getState().equals(taskState) == false) continue;
      }

      instances.add(taskInstance);
    }

    for (int i = 0; i < instances.size() - 1; i++) {

      for (int j = 0; j < instances.size() - 1; j++) {

        WorkflowTaskInstance currentElement = instances.get(j);
        WorkflowTaskInstance nextElement = instances.get(j + 1);

        Date currentDate = (Date) currentElement.getProperties().get(WorkflowConstants.VAR_ENDDATE);
        Date nextDate = (Date) nextElement.getProperties().get(WorkflowConstants.VAR_ENDDATE);

        if (currentDate == null || nextDate == null) continue;

        if (sort.equals(TASK_SORT.ASC)) {

          // the current date is before the second date
          if (nextDate.after(currentDate)) continue;

          instances.set(j, nextElement);
          instances.set(j + 1, currentElement);

        } else {
          if (nextDate.before(currentDate)) continue;

          instances.set(j, nextElement);
          instances.set(j + 1, currentElement);
        }
      }
    }

    return instances;
  }
  public WorkflowInstance startWorkflow(
      WorkflowDefinition workflowDefinition, Map<String, Serializable> properties) {

    WorkflowPersistenceTemplateDAO workflowTemplateDao =
        (WorkflowPersistenceTemplateDAO)
            Context.getInstance().getBean(WorkflowPersistenceTemplateDAO.class);

    try {
      WorkflowInstance workflowInstance = null;
      if (workflowDefinition != null && !workflowDefinition.getDefinitionId().isEmpty()) {
        log.info("workflowComponent: " + workflowComponent);
        log.info("workflowDefinition: " + workflowDefinition);
        log.info("workflowDefinition.getDefinitionId()" + workflowDefinition.getDefinitionId());
        log.info("properties size: " + properties.size());

        workflowInstance =
            workflowComponent.startWorkflow(workflowDefinition.getDefinitionId(), properties);

        // Create the workflow history event
        WorkflowHistoryDAO workflowHistoryDao =
            (WorkflowHistoryDAO) Context.getInstance().getBean(WorkflowHistoryDAO.class);
        WorkflowHistory transaction = new WorkflowHistory();

        WorkflowPersistenceTemplate template =
            workflowTemplateDao.findByName(workflowInstance.getName());

        transaction.setTemplateId(template.getId());
        transaction.setTemplateId(template.getId());
        transaction.setInstanceId(workflowInstance.getId());
        transaction.setDate(new Date());
        transaction.setSessionId(SessionManagement.getCurrentUserSessionId());
        transaction.setEvent(WorkflowHistory.EVENT_WORKFLOW_START);
        transaction.setComment("");
        transaction.setUser(SessionManagement.getUser());

        workflowHistoryDao.store(transaction);

        // Create a workflow history for each document associated to
        // this
        // workflow instance
        Set<Long> docIds =
            (Set<Long>) workflowInstance.getProperties().get(WorkflowConstants.VAR_DOCUMENTS);

        for (Long docId : docIds) {
          // Create the workflow history event
          WorkflowHistory docAppended = new WorkflowHistory();
          docAppended.setTemplateId(template.getId());
          docAppended.setInstanceId(workflowInstance.getId());
          docAppended.setDate(new Date());
          docAppended.setSessionId(SessionManagement.getCurrentUserSessionId());
          docAppended.setEvent(WorkflowHistory.EVENT_WORKFLOW_DOCAPPENDED);
          docAppended.setDocId(docId);
          docAppended.setComment("");
          docAppended.setUser(SessionManagement.getUser());

          workflowHistoryDao.store(docAppended);
        }
      } else {
        Messages.addLocalizedWarn("noselection");
      }

      return workflowInstance;
    } finally {
      workflowTemplateDao.fixConversionField();
    }
  }