import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

import javax.naming.Context;
import javax.rmi.PortableRemoteObject;

import com.bea.wli.worklist.api.TaskInfo;
import com.bea.wli.worklist.api.TaskInfoCursor;
import com.bea.wli.worklist.api.TaskSelector;
import com.bea.wli.worklist.api.WorklistScrollableResultManager;
import com.bea.wli.worklist.beans.session.RemoteWorklistManager;
import com.bea.wli.worklist.beans.session.RemoteWorklistManagerHome;

/**
 * @author samratr
 *
 */
public class WorklistScrollableManagerClient
{
  private static final int MAX_BATCH_SIZE = 1000;
  private WorklistScrollableResultManager worklistManager;
  private TaskSelector selector;
  private int batchSize;
  private String mode;

  public void init(HashMap properties)
  throws Exception
  {
    mode = (String) properties.get("mode");
    String strBatch = (String) properties.get("batch.size");

    if("Interactive".equalsIgnoreCase(mode) && strBatch!=null)
      batchSize = Integer.parseInt(strBatch);
    else
      batchSize = MAX_BATCH_SIZE;

    JndiUtil jndiUtil = new JndiUtil(properties);
    Context ctx = jndiUtil.getInitialContext();

    RemoteWorklistManagerHome home =
      (RemoteWorklistManagerHome) PortableRemoteObject.narrow(
          ctx.lookup("RemoteWorklistManagerBean"), RemoteWorklistManagerHome.class);
    worklistManager =
    (WorklistScrollableResultManager)PortableRemoteObject.narrow(
        home.create(), RemoteWorklistManager.class);
	System.out.println("using the task selector file :"+properties.get("selector.property.file"));
    selector = TaskSelectorFactory.getTaskSelector(
        new File((String)properties.get("selector.property.file")));
  }

  public void execute()
  throws Exception
  {
    TaskInfoCursor cursor = worklistManager.getTaskInfoCursor(selector, batchSize);

    while(!cursor.isAfterLast())
    {
      cursor = worklistManager.moveCursorForward(cursor);
      TaskInfo[] arrTasks = cursor.getTaskInfos();
      print(arrTasks);
      if(!continueScrolling()) break;
    }
  }

  private void print(TaskInfo[] arrTasks)
  {
    System.out.println();
    System.out.println("Number of tasks retrieved : " + arrTasks.length);
    System.out.println();

    System.out.println("TaskId | Task Name | Assignees | State");

    for(int i=0; i<arrTasks.length; i++)
    {
      String users = "";
      String[] arrUsers = arrTasks[i].getAssignee().getUsers();
      for(int j=0; j<arrUsers.length; j++)
        users += arrUsers[j];

      if(arrTasks[i]!=null)
        System.out.println(
          arrTasks[i].getTaskId() + " | " +
          arrTasks[i].getName() + " | " +
          users + " | " +
          arrTasks[i].getStateType());
      else
        System.out.println("No task info for this element");
    }
  }

  private boolean continueScrolling()
  throws IOException
  {
    if(!"interactive".equalsIgnoreCase(mode)) return true;

    String response = "y";
    System.out.println();
    System.out.println("Do you wish to continue? (Y/N)");
    BufferedReader rdr = new BufferedReader(new InputStreamReader(System.in));
    response = rdr.readLine();

    if(response==null) return false;
    if("n".equalsIgnoreCase(response))
      return false;

    return true;
  }

  public static void main(String[] args)
  {
    int count = 0;
    HashMap props = new HashMap();
    props.put("selector.property.file", System.getProperty("selector.property.file"));
    props.put("server.url", System.getProperty("server.url"));
    props.put("user", System.getProperty("user"));
    props.put("password", System.getProperty("password"));
    props.put("batch.size", System.getProperty("batch.size"));
    props.put("mode", System.getProperty("mode"));

    try
    {
      WorklistScrollableManagerClient client = new WorklistScrollableManagerClient();
      client.init(props);
      client.execute();
    }
    catch (Exception ex)
    {
      ex.printStackTrace();
    }
  }

}
