package com.fujitsu.iflowqa.testframe;



import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Properties;

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

public class CreateProcessAndCommit {

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




        /*
         * Login
         */
        private
        WFAdminSession
        logIn()
        {
            try {
                Properties props = new Properties();
                BufferedReader request = new BufferedReader(new InputStreamReader(
                        System.in));
                String server = "";
                String serverport="";
                System.out.println("Enter the Server Name:");
                server = request.readLine();
                System.out.println("Enter RMI server port:");
                serverport = request.readLine();
                System.out.println("Enter the User Name:");
                String user = request.readLine(); //Read the username
                System.out.println("Enter Password");
                String password = request.readLine(); //Read the password
                props.put("TWFTransportType", "RMI");
                props.put("java.naming.provider.url", "rmi://" + server + ":"+serverport);
                //String transPortType = tr.getRequiredProperty("TWFTransportType");
                wfSession = WFObjectFactory.getWFAdminSession();
                wfSession.initForApplication(null,props);
                System.out.println("Trying to login Server-->"+server+"User:"+user+"Password:"+password+"at RMI port"+serverport);
                wfSession.logIn(server + "Flow", user, password);
                //this.username = user;


            }
            catch (Exception e) {
                System.out.println("Failed to Login"+e);
            }
            return wfSession;
        }

        /*
         * This method commits all the workitems for a given user(the user is specified in test.conf file).
         */
        private
        void
        makeProcess(WFAdminSession session) throws Exception
        {

             System.out.println("Make IBPM Process\n");


            try{
               WFObjectList wfol = WFObjectFactory.getWFObjectList(session);
               wfol.openList(Filter.AllPlans);
               Object[] plan= wfol.getAllObjects();
               ProcessInstance pi;
               Plan plann;
               System.out.println("Total Plan Presenttt :"+plan.length);
               if ( (plan == null) || (plan.length == 0) )
                  return;
               for(int i=0; i < plan.length; i++)
               {
                   try{
                   System.out.println("Trying to load plan");
                   plann = (Plan) plan[i];
                   //System.out.println("Making Session");
                   System.out.println("Trying to make a process");
                   if(plann.getState()!= plann.STATE_PRIVATE)
                   {
                    pi = plann.createProcessInstance();
                    pi.start();
                    System.out.println("Plan" + plann.getName());
                   System.out.println("Process started! -->" + pi.getId());
                   }
                   else
                   {
                       System.out.println("Could not create process Plan is private");
                       System.out.println("Plan" + plann.getName());
                   }
               }
               catch(Exception e)
               {
                   System.out.println("Error Occured"+e);
               }


                   Thread.sleep(200);
            }
        }
        catch(Exception e)
        {
            throw new Exception("Error Occured");

        }
    }

        /*
         * LogOut
         */
        private
        void
        logOut()
        {
            try {
                System.out.println("logOut()\n");
                wfSession.logOut();
            }
            catch(Exception e) {
                System.out.println("Failed to Logout"+e);
            }
        }


        /*
         * Main Method
         */
        public static void main(String args[]) throws Exception
        {

          try
          {
          CreateProcessAndCommit process = new CreateProcessAndCommit();
          WFAdminSession session = process.logIn();
          process.makeProcess(session);
          process.logOut();
      }
      catch(Exception e)
      {
          throw new Exception("Error Occured"+e);
      }

        }
}
