import java.io.File;
import java.io.FileInputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Properties;
import java.util.StringTokenizer;

import com.bea.wli.worklist.api.Assignee;
import com.bea.wli.worklist.api.StateType;
import com.bea.wli.worklist.api.TaskSelector;

public class TaskSelectorFactory
{
  private static final DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy H:mm:ss");
  
  public static TaskSelector getTaskSelector(File selectorPropertyFile)
  throws Exception
  {
    Properties props = new Properties();
    props.load(new FileInputStream(selectorPropertyFile));
    return getTaskSelector(props);
  }
  
  public static TaskSelector getTaskSelector(Properties props)
  throws Exception
  {
    TaskSelector selector = new TaskSelector();
    populateTaskSelector(selector, props);
    return selector;
  }
  
  /**
   * Date Format expected "MM/dd/yyyy H:mm:ss"
   */  
  private static void populateTaskSelector(TaskSelector selector, Properties props)
  throws ParseException
  {
    // -- STRING --
      if(props.containsKey("Comment"))
        selector.setComment(props.getProperty("Comment"), false);
      if(props.containsKey("Description"))
        selector.setDescription(props.getProperty("Description"), false);
      if(props.containsKey("ListeningProcessIds"))
        selector.setListeningProcessIds(getStringArray(props.getProperty("ListeningProcessIds")));
      if(props.containsKey("ListeningProcessUri"))
        selector.setListeningProcessUri(props.getProperty("ListeningProcessUri"), false);
      if(props.containsKey("ParentProcessUri"))
        selector.setParentProcessUri(props.getProperty("ParentProcessUri"), false);
      if(props.containsKey("TaskName"))
        selector.setTaskName(props.getProperty("TaskName"), false);
      
      // -- BOOLEAN --    
      if(props.containsKey("CanBeAborted"))
        selector.setCanBeAborted(Boolean.valueOf(props.getProperty("CanBeAborted")));
      if(props.containsKey("CanBeReassigned"))
        selector.setCanBeReassigned(Boolean.valueOf(props.getProperty("CanBeReassigned")));
      if(props.containsKey("CanBeReturned"))
        selector.setCanBeReturned(Boolean.valueOf(props.getProperty("CanBeReturned")));
  
      // -- STRING ARRAY --
      if(props.containsKey("Claimants"))
        selector.setClaimants(getStringArray(props.getProperty("Claimants")));
      if(props.containsKey("Owners"))
        selector.setOwners(getStringArray(props.getProperty("Owners")));
      if(props.containsKey("ParentProcessIds"))
        selector.setParentProcessIds(getStringArray(props.getProperty("ParentProcessIds")));
      if(props.containsKey("TaskIds"))
        selector.setTaskIds(getStringArray(props.getProperty("TaskIds")));
  
      // -- INTEGER --
      if(props.containsKey("MaxPriority"))
        selector.setMaxPriority(Integer.valueOf(props.getProperty("MaxPriority")));
      if(props.containsKey("MinPriority"))
        selector.setMinPriority(Integer.valueOf(props.getProperty("MinPriority")));
      
      // -- DATE --
      if(props.containsKey("ClaimDueDateAfter"))
        selector.setClaimDueDateAfter(dateFormat.parse(props.getProperty("ClaimDueDateAfter")));
      if(props.containsKey("ClaimDueDateBefore"))
        selector.setClaimDueDateBefore(dateFormat.parse(props.getProperty("ClaimDueDateBefore")));
      if(props.containsKey("CompletionDueDateAfter"))
        selector.setCompletionDueDateAfter(dateFormat.parse(props.getProperty("CompletionDueDateAfter")));
      if(props.containsKey("CompletionDueDateBefore"))
        selector.setCompletionDueDateBefore(dateFormat.parse(props.getProperty("CompletionDueDateBefore")));
      if(props.containsKey("CreationDateAfter"))
        selector.setCreationDateAfter(dateFormat.parse(props.getProperty("CreationDateAfter")));
      if(props.containsKey("CreationDateBefore"))
        selector.setCreationDateBefore(dateFormat.parse(props.getProperty("CreationDateBefore")));
  
      
      // -- OTHERS --
      Assignee assignee = new Assignee();
      
      if(props.containsKey("Assignee.Users"))
      {
        assignee.setUsers(getStringArray(props.getProperty("Assignee.Users")));
        selector.setAssignee(assignee);
      }
  
      if(props.containsKey("Assignee.Groups"))
      {
        assignee.setGroups(getStringArray(props.getProperty("Assignee.Groups")));
        selector.setAssignee(assignee);
      }
      
      if(props.containsKey("StateTypes"))
      {
        String[] stypes = getStringArray(props.getProperty("StateTypes"));
        StateType[] arrStateType = new StateType[stypes.length];
        for(int i=0; i<stypes.length; i++)
          arrStateType[i] = StateType.getStateType(stypes[i]);
        selector.setStateTypes(arrStateType);
      }
      
      // SORTING ORDER
      if(props.containsKey("SortByAssignee"))
        selector.setSortByAssignee(Short.parseShort(props.getProperty("SortByAssignee")));
      if(props.containsKey("SortByCanBeAborted"))
        selector.setSortByCanBeAborted(Short.parseShort(props.getProperty("SortByCanBeAborted")));
      if(props.containsKey("SortByCanBeReassigned"))
        selector.setSortByCanBeReassigned(Short.parseShort(props.getProperty("SortByCanBeReassigned")));
      if(props.containsKey("SortByCanBeReturned"))
        selector.setSortByCanBeReturned(Short.parseShort(props.getProperty("SortByCanBeReturned")));
      if(props.containsKey("SortByClaimant"))
        selector.setSortByClaimant(Short.parseShort(props.getProperty("SortByClaimant")));
      if(props.containsKey("SortByClaimDueDate"))
        selector.setSortByClaimDueDate(Short.parseShort(props.getProperty("SortByClaimDueDate")));
      if(props.containsKey("SortByComment"))
        selector.setSortByComment(Short.parseShort(props.getProperty("SortByComment")));
      if(props.containsKey("SortByCompletionDueDate"))
        selector.setSortByCompletionDueDate(Short.parseShort(props.getProperty("SortByCompletionDueDate")));
      if(props.containsKey("SortByCreationDate"))
        selector.setSortByCreationDate(Short.parseShort(props.getProperty("SortByCreationDate")));
      if(props.containsKey("SortByDescription"))
        selector.setSortByDescription(Short.parseShort(props.getProperty("SortByDescription")));
      if(props.containsKey("SortByListeningProcessId"))
        selector.setSortByListeningProcessId(Short.parseShort(props.getProperty("SortByListeningProcessId")));
      if(props.containsKey("SortByListeningProcessUri"))
        selector.setSortByListeningProcessUri(Short.parseShort(props.getProperty("SortByListeningProcessUri")));
      if(props.containsKey("SortByOwner"))
        selector.setSortByOwner(Short.parseShort(props.getProperty("SortByOwner")));
      if(props.containsKey("SortByParentProcessId"))
        selector.setSortByParentProcessId(Short.parseShort(props.getProperty("SortByParentProcessId")));
      if(props.containsKey("SortByParentProcessUri"))
        selector.setSortByParentProcessUri(Short.parseShort(props.getProperty("SortByParentProcessUri")));
      if(props.containsKey("SortByPriority"))
        selector.setSortByPriority(Short.parseShort(props.getProperty("SortByPriority")));
      if(props.containsKey("SortByStateType"))
        selector.setSortByStateType(Short.parseShort(props.getProperty("SortByStateType")));
      if(props.containsKey("SortByTaskId"))
        selector.setSortByTaskId(Short.parseShort(props.getProperty("SortByTaskId")));
      if(props.containsKey("SortByTaskName"))
        selector.setSortByTaskName(Short.parseShort(props.getProperty("SortByTaskName")));
  }

  private static String[] getStringArray(String str)
  {
    if( str!=null && str.indexOf(",")>=0 )
    {
      ArrayList list = new ArrayList();
      StringTokenizer st = new StringTokenizer(str, ", ");
      while(st.hasMoreElements())
        list.add(st.nextElement());
      return (String[])list.toArray(new String[0]);
    }
    else
    {
      return new String[] {str};
    }
  }
  

  ////////////////////////////////
  // WHITE BOX TESTING 
  ////////////////////////////////
  
  public static void main(String[] args)
  {
    try
    {
      TaskSelector selector = TaskSelectorFactory.getTaskSelector( new File(
        "D:/weblogic/dev/8.1.x/wli/perf/microbenchmarks/" +
        "resources/TaskSelectorFilter.properties"));
      System.out.println(selector);
    }
    catch (Exception ex)
    {
      ex.printStackTrace();
    }
    
  }
}
