cancel
Showing results for 
Search instead for 
Did you mean: 

Query tasks between two dates in Java

joaotpd
Champ on-the-rise
Champ on-the-rise
Dear All,

It's possible to build a WorkflowTaskQuery to get tasks between two dates?

The date is "WorkflowModel.PROP_START_DATE".

Thanks,

João
6 REPLIES 6

vinaxwater
Champ in-the-making
Champ in-the-making
Dear joaotpd,

I build feature show tasks between 2 dates choosed join with tasks in works completed, can i help you with this?

Regards.

Hi vinaxwater,

I'm trying to buil a "CustomWorkflowTaskQuery" to get all the tasks (completed) between two dates:


<java>


CustomWorkflowTaskQuery query = new CustomWorkflowTaskQuery();

query.setTaskState(WorkflowTaskState.COMPLETED);

//SOMETHING HERE TO FILTER TASKS BETWEEN TO DATES

List<WorkflowTask> tasks = workflowService.queryTasks(query);


</java>

Did you find a solution for this? … How did you do it in your functionality? …

Many thanks,

João

vinaxwater
Champ in-the-making
Champ in-the-making
Dear joaotpd,

I did:

in file task-completed-dashlet.jsp (path: /project/web-client/source/jsp/workflows)
add -> show link Filter WORK COMPLETION read. in work complete panel


<f:verbatim>
   <table width="100%">
      <tr>
         <td colspan="7" align="right"> </f:verbatim>
            <a:actionLink id="col-history" style="font-weight:bold;font-family:Verdana;color:red;"
               value="Filter \"WORK COMPLETION\" read." action="dialog:historyTask" rendered="#{!NavigationBean.currentUser.admin}">
            </a:actionLink>
<f:verbatim>
         </td>
      </tr>
   </table>
</f:verbatim>


Define dialog history.
in file web-client-config-dialogs.xml add:


<dialog name="historyTask" page="/jsp/workflow/history-task-dialog.jsp"
               managed-bean="HistoryTaskDialog" icon="/images/icons/manage_workflow_task_large.gif"
               description-id="manage_task_desc" />


Create screen choose From date and To date (create file history-task-dialog.jsp in path /project/web-client/source/web/jsp/workflow):

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="/WEB-INF/alfresco.tld" prefix="a"%>
<%@ taglib uri="/WEB-INF/repo.tld" prefix="r"%>

<%@ page buffer="32kb" contentType="text/html;charset=UTF-8"%>
<%@ page isELIgnored="false"%>

<h:outputText
   value="Please input date for execute (Note: toDate <= toDay - 30 )."
   styleClass="mainSubTitle" />
<f:verbatim>
   <br />
</f:verbatim>
<h:outputText
   value="Click Yes for filter the tasks is WORK COMPLETION, click No or Logout to reset the results. " />
<f:verbatim>
   <p />
</f:verbatim>
<h:outputText value="From date:" styleClass="mainSubTitle" />
 
<h:inputText id="from-date" size="25" value="#{HistoryTaskDialog.fromDate}" maxlength="20"></h:inputText>
   
<h:outputText value="To date:" styleClass="mainSubTitle" />
 
<h:inputText id="to-date" size="29" value="#{HistoryTaskDialog.toDate}" maxlength="20"></h:inputText>
<f:verbatim>
   <p />
</f:verbatim>


Then create Bean for it (/project/web-client/source/java/org/alfresco/bean/workflow/HistoryTaskDialog.java):


package org.alfresco.web.bean.workflow;

import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.faces.component.UIForm;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.alfresco.repo.workflow.jbpm.JBPMEngine;
import org.alfresco.service.cmr.workflow.WorkflowException;
import org.alfresco.service.cmr.workflow.WorkflowInstance;
import org.alfresco.service.cmr.workflow.WorkflowService;
import org.alfresco.service.cmr.workflow.WorkflowTask;
import org.alfresco.service.cmr.workflow.WorkflowTaskState;
import org.alfresco.web.app.Application;
import org.alfresco.web.bean.dialog.BaseDialogBean;
import org.alfresco.web.bean.repository.User;
import org.alfresco.web.ui.common.Utils;
import org.alfresco.web.ui.common.component.UIActionLink;
import org.alfresco.web.ui.repo.component.property.UIPropertySheet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.access.BeanFactoryLocator;
import org.springframework.beans.factory.access.BeanFactoryReference;
import org.springmodules.workflow.jbpm31.JbpmFactoryLocator;

import com.ibm.icu.util.Calendar;


public class HistoryTaskDialog extends BaseDialogBean {

   /** Alfresco JBPM Engine */
   private static JBPMEngine jbpmEngine = null;
   private String fromDate;
   private String toDate;

   private JBPMEngine getJBPMEngine() {
      if (jbpmEngine == null) {
         BeanFactoryLocator factoryLocator = new JbpmFactoryLocator();
         BeanFactoryReference factory = factoryLocator.useBeanFactory(null);
         jbpmEngine = (JBPMEngine) factory.getFactory().getBean(
               "jbpm_engine");
         if (jbpmEngine == null) {
            throw new WorkflowException(
                  "Failed to retrieve JBPMEngine component");
         }
      }
      return jbpmEngine;
   }
   
   // ——————————————————————————
   // Dialog implementation
   @Override
   public void init(Map<String, String> parameters) {
      super.init(parameters);
      this.isFinished = false;
   }

   @Override
   protected String finishImpl(FacesContext context, String outcome) throws Exception {
         try {
            Date frDate = new Date(this.getFromDate());
            Date tDate = new Date(this.getToDate());
            
            if(frDate.before(tDate) && tDate.before(new Date())) {
               getJBPMEngine().setFromDate(this.getFromDate());
               getJBPMEngine().setToDate(this.getToDate());
               if(checkDate(toDate)) {
                  this.isFinished = true;
               } else {
                  Utils.addErrorMessage("[WARNING]: toDate <= toDay - 30");
                  isFinished = false;
                  outcome = null;
               }
            } else {
               Utils.addErrorMessage("[ERROR]: fromDate > toDate Or toDate < toDay");
               isFinished = false;
               outcome = null;
            }
            
         } catch (Throwable ex) {
            Utils.addErrorMessage(MessageFormat.format(
                  "Date input uncorrect please try again.", ex));
            isFinished = false;
            outcome = null;
         }
      return outcome;
   }
   
   // vinaxwater added for check distance 60 day.
   private boolean checkDate(String toDate) {
      Date dt = new Date();
      SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
      String[] arrFrom = sdf.format(dt).split("-");
      Date tD = new Date(toDate);
      String[] arrTo = sdf.format(tD).split("-");
      double date = Integer.valueOf(arrFrom[0]).intValue() - Integer.valueOf(arrTo[0]).intValue();
      double month = Integer.valueOf(arrFrom[1]).intValue() - Integer.valueOf(arrTo[1]).intValue();
      double year = Integer.valueOf(arrFrom[2]).intValue() - Integer.valueOf(arrTo[2]).intValue();
      if(year > 0) return true;
      else
         if(month >= 2) return true;
      return ((date + 30) > 30);
   }

   @Override
   public boolean getFinishButtonDisabled() {
      return false;
   }

   @Override
   public String getCancelButtonLabel() {
      return Application.getMessage(FacesContext.getCurrentInstance(), "no");
   }

   @Override
   public String getFinishButtonLabel() {
      return Application.getMessage(FacesContext.getCurrentInstance(), "yes");
   }

   // ——————————————————————————
   // Bean Getters and Setters
   public String getFromDate() {
      return fromDate;
   }

   public void setFromDate(String fromDate) {
      this.fromDate = fromDate;
   }

   public String getToDate() {
      return toDate;
   }

   public void setToDate(String toDate) {
      this.toDate = toDate;
   }
}


in file /project/repository/source/java/org/alfresco/repo/workflow/jbpm/JBPMEngine.java
create 2 param fromDate and toDate have method set for that params.

then in method findCompletedTaskInstances and edit (please look at code in remark as /* code added */ :

         List<TaskInstance> result = new ArrayList<TaskInstance>();
         /* code added */
         List<TaskInstance> union = new ArrayList<TaskInstance>();
         /* code added */
                    try
                    {
                       Session session = jbpmContext.getSession();
             Query query = session.createQuery(COMPLETED_TASKS_QUERY);
             query.setString("actorId", actorId);
             result = query.list();
                       /* code added */
                  if (fromDate != null && toDate != null)//20-OCT-2010
                  {
                   String COMPLETED_TASKS_QUERY_ADVANDCE = COMPLETED_TASKS_QUERY_USER + " and trunc(ti.end) >= '" + fromDate + "' " + "and trunc(ti.end) <= '" + toDate + "' ";
                      //"and sysdate-to_date('"+toDate+"','DD-MON-RRRR')>60";
                   query = session.createQuery(COMPLETED_TASKS_QUERY_ADVANDCE);
                   query.setString("actorId", actorId);
                   union = query.list();
                   result.addAll(union);
                  }
                       /* code added */
                    }
                    catch (Exception e)
                    {
                        throw new JbpmException("Couldn't get completed task instances list for actor '" + actorId + "'", e);
                    }
                    return result;


Complete I have list task complete in fromDate and toDate union with workflow complete.
If want return only list task complete fromDate toDate return union change return  result.

Hi vinaxwater,

Thank you for sharing your solution.

I'm trying to put things together… but, I'm getting "The method setFromDate(Date) is undefined for the type JBPMEngine". Do you have an extension of "JBPMEngine"? …

Thanks again!

João

vinaxwater
Champ in-the-making
Champ in-the-making
Dear joaotpd,

Please add params fromDate and toDate and set method Set for its in JBPMEngine.java before then edit file HistoryTaskDialog.java

pylady
Champ in-the-making
Champ in-the-making
Hello,
Does anyone know how to get completed tasks between two dates, but using Activiti workflows?
Thanks in advance,

Jana