
package com.fujitsu.iflowqa.testframe;

import java.awt.Point;

import com.fujitsu.iflow.model.workflow.Filter;
import com.fujitsu.iflow.model.workflow.Node;
import com.fujitsu.iflow.model.workflow.Plan;
import com.fujitsu.iflow.model.workflow.ProcessInstance;
import com.fujitsu.iflow.model.workflow.WFObjectFactory;
import com.fujitsu.iflow.model.workflow.WFObjectList;
import com.fujitsu.iflow.model.workflow.WorkItem;

public class WorkItemRefresh extends BPMTestBase implements TestSet
{

/**
 * This Work Item Refresh Test program work for only NT Directory Service.
 */

    public WorkItemRefresh()
    {
    }

    public
    Plan
    createTemplate()
        throws Exception
    {
        Plan plan = null;

        String planName = "WorkItemRefresh";

        plan = WFObjectFactory.getPlan();
        plan.setWFSession(wfSession);
        plan.startEdit();

        plan.setName( planName );
        plan.setTitle( planName + " title" );
        plan.setDesc( planName + " description" );
        plan.setPlanIdentifier("Global");

        Node startNode = plan.addNode( "Start", Node.TYPE_START );
        int startX = 50;
        int startY = 100;
        startNode.setPosition( new Point( startX, startY ) );

        Node firstActNode = plan.addNode( "WorkItem-Refresh", Node.TYPE_ACTIVITY );
        firstActNode.setPosition( new Point( startX + 200, startY ) );
        firstActNode.setRole( "Role" );

        plan.addArrow( "START", startNode, firstActNode );

        Node exitNode = plan.addNode( "EXIT", Node.TYPE_EXIT );
        exitNode.setPosition( new Point( startX + 200, startY + 200) );
        plan.addArrow( "DONE", firstActNode, exitNode );
        plan = plan.createProcessDef();

        return plan;

    }

    public
    void
    refreshWorkItems(ProcessInstance pi, String user)
            throws Exception
    {
        String command  = "net localgroup role " + user + " /ADD";
        java.lang.Process p = Runtime.getRuntime().exec(command, null);
        tr.log("Waiting for command '"+command+"' to finish");
        long start = System.currentTimeMillis();
        p.waitFor();
        long waited = System.currentTimeMillis() - start;
        tr.log("Command finished. Waited " + waited + "ms");

        wfSession.resetLDAPGroupsList();
        wfSession.resetLDAPUsersList();

        waitForWorkItem("WorkItem-Refresh", wfSession.getUserName(), Filter.MyActiveWorkItems, null, pi.getId(), tr);

        wfSession.refreshWIforProcess(pi.getId(),true);

        WFObjectList wfol = WFObjectFactory.getWFObjectList(wfSession);

        wfol.addFilter(WFObjectList.LISTFIELD_PROCESSINSTANCE_ID, "=",
                        "'" + pi.getId() + "'");

        wfol.openBatchedList(Filter.AllWorkItems);
        Object[] workItemList = wfol.getNextBatch(100);
         if ((workItemList == null) || (workItemList.length == 0)) {
            tr.markFailed("WorkItemRefresh test ", "After ADD-- Test Failed could not find work items for this process");
         }
        for(int i = 0; i < workItemList.length; i++){
            WorkItem workItem = (WorkItem) workItemList[i];

            if(workItem.getAssignee().equalsIgnoreCase(user)){
                tr.markPassed("Test successfully added new member into the role and new work item created for  User : " + user);
            }
        }
        command  = "net localgroup role " +  user  + " /DEL";
        java.lang.Process p2 = Runtime.getRuntime().exec(command, null);
        tr.log("Waiting for command '"+command+"' to finish");
        start = System.currentTimeMillis();
        p2.waitFor();
        waited = System.currentTimeMillis() - start;
        tr.log("Command finished. Waited " + waited + "ms");

        wfSession.resetLDAPGroupsList();
        wfSession.resetLDAPUsersList();

        wfSession.refreshWorkItems(true);
        wfol.addFilter(WFObjectList.LISTFIELD_PROCESSINSTANCE_ID, "=",
                        "'" + pi.getId() + "'");
        wfol.openBatchedList(Filter.AllWorkItems);
        boolean found =  false;
        workItemList = wfol.getNextBatch(100);
         if ((workItemList == null) || (workItemList.length == 0)) {
             tr.markFailed("WorkItemRefresh test " ,"After DEL -- Test Failed could not find work items for this process");
             return;
         }
        for(int i = 0; i < workItemList.length; i++){
            WorkItem workItem = (WorkItem) workItemList[i];

            if(workItem.getAssignee().equalsIgnoreCase(user)){
                found = true;
            }
        }
        if(found)
        {
            tr.markFailed("WorkItemRefresh test " , "Test failed to delete work item and User : " + user);
        }else{
            tr.markPassed("Test successfully added/deleted  member from the role and  work item also for  User : " + user);
        }

    }

    public
    void
    logAndRethrow(
        String message,
        Exception e)
            throws Exception
    {
        tr.log(message);
        throw new ExceptionWrapper(message, e);
    }

    public
    void
    runTests(TestRecorder testRecord)
        throws Exception
    {
        try{
            init(testRecord);
            login();
            Plan plan = createTemplate();
            ProcessInstance pi = createProcess(plan);
            refreshWorkItems(pi, "workItemRefreshUser");
        } catch (Exception e) {
            e.printStackTrace();
           logAndRethrow("Exception caught in main Analytics Test", e);
        }finally{
            wfSession.logOut();
        }
    }

}
