
package com.fujitsu.iflowqa.testframe;

// iflow classes.
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;

import com.fujitsu.iflow.model.workflow.Filter;
import com.fujitsu.iflow.model.workflow.WFAdminSession;
import com.fujitsu.iflow.model.workflow.WFObjectFactory;
import com.fujitsu.iflow.model.workflow.WFObjectList;
import com.fujitsu.iflow.model.workflow.WorkItem;


/*
 * This class is used to commit all the activities of a particular user(regardless
 * of the process to which the activities belong)
 */
public class CommitActivities {

    private WFAdminSession wfSession;
    private String username = null;
    private TestRecorder tr;
    private Writer writer;
    private PrintWriter printWriter;


    /*
     * Zero argument constructor.
     */
    public CommitActivities() {
     try {
      String args[] = {""};
      writer = new OutputStreamWriter(System.out, "UTF-8");
      tr = new TestRecorderText(writer, true, args, "./");
      printWriter = new PrintWriter(writer);
      print("Commmit Activities process started\n\n",null);
     }
     catch(Exception e) {
      print("Exception while initializing",e);
     }
    }

    /*
     * This method commits all the workitems for a given user(the user is specified in test.conf file).
     */
    private
    void
    commitAllWorkItems()
    {
         print("commitAllWorkItems()\n",null);
         while(true)
         {
          try {
           WFObjectList wfol = WFObjectFactory.getWFObjectList(wfSession);
           wfol.openList(Filter.MyActiveWorkItems);
           Object[] workItemList = wfol.getAllObjects();
           if ( (workItemList == null) || (workItemList.length == 0) )
              return;
           WorkItem workItem = (WorkItem) workItemList[0];
           print("Activity Id:  "+workItem.getId() + "\nActivity Name:  "+ workItem.getName()+"\n\n",null);
           String[] choices = workItem.getChoices();
           workItem.makeChoice(choices[0]); // always select the first choice.
           Thread.sleep(200);
           }
           catch (Exception e) {
            print("Exception while handling workitems",e);
            continue;
           }
         }
    }


    /*
     * Login
     */
    private
    void
    logIn()
    {
        try {
            print("logIn()\n",null);
            String transPortType = tr.getRequiredProperty("TWFTransportType");
            wfSession = WFObjectFactory.getWFAdminSession();
            wfSession.initForApplication(null, tr.getProps());
            wfSession.logIn(tr.getRequiredProperty("servername") + "Flow", tr.getRequiredProperty("username"), tr.getRequiredProperty("password"));
            this.username = username;

        }
        catch (Exception e) {
            print("Failed to Login",e);
        }
    }

    /*
     * LogOut
     */
    private
    void
    logOut()
    {
        try {
            print("logOut()\n",null);
            wfSession.logOut();
        }
        catch(Exception e) {
            print("Failed to Logout",e);
        }
    }

    /*
     * Prints messages and exceptions in log file(file name specified in FlowPath.bat file)
     */
    private
    void
    print(String text, Exception e){
        try{
            if(text != null)
                printWriter.write(text);
            if(e != null)
            {
                printWriter.write("\n");
                e.printStackTrace(printWriter);
                printWriter.write("\n");
            }
            printWriter.flush();
        }
        catch(Exception e1){
            System.out.println("Failed to write in log File:  \n Result: "
                        + text);
            e1.printStackTrace();
        }
    }



    /*
     * Main Method
     */
    public static void main(String args[])
    {
      CommitActivities commitActivities = new CommitActivities();
      commitActivities.logIn();
      commitActivities.commitAllWorkItems();
      commitActivities.logOut();
    }

}
