package com.fujitsu.iflowqa.testframe;

import java.awt.Point;
import java.sql.Connection;

import com.fujitsu.iflow.model.workflow.DataItemRef;
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.WFAdminSession;
import com.fujitsu.iflow.model.workflow.WFObjectFactory;

/**
* This class implements TestSet and the purpose of this class to test
* Analytics. To run this class JTurbo driver and iFlowClients.properties file should be in classpath.
* The results of the test are recorded using
* TestRecorder class.
*/

public class ReadOnlyUDASTest implements TestSet
{

    private TestRecorder tr = null;
    private WFAdminSession wfSession = null;
    private String serverName = null;
    private String userName = null;
    private String passWord = null;

    public
    void
    runTests(TestRecorder testRecord)
        throws Exception
    {
        Connection connection = null;
        try {
            tr = testRecord;
            logIn();
            Plan testPlan = createTemplate();
            DataItemRef data  = testPlan.getDataItemRef("readonly");

            createProcess(testPlan);

        } catch (Exception e) {
           logException("Exception caught in main test level", e);
        }
        finally
        {

        }
    }

    public
    ReadOnlyUDASTest()
    {

    }

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

       try {
            String planName = "QA_AnalyticsTest";
            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( "ACTIVITY-ANALYTICS", Node.TYPE_ACTIVITY );
            firstActNode.setPosition( new Point( startX + 200, startY ) );
            firstActNode.setRole( "Role" );
            firstActNode.setDesc("ACTIVITY-ANALYTICS");
            startNode.addArrow( "START", firstActNode );
            Node exitNode = plan.addNode( "EXIT", Node.TYPE_EXIT );
            exitNode.setPosition( new Point( startX + 200, startY + 200) );
            firstActNode.addArrow( "DONE", exitNode );
           // DataItemRef uda = new DataItemRef("readonly", "STRING" ,"ganga");

            DataItemRef uda = plan.addDataItemRef("readonly", "STRING", "Ganga", false, false, "" , "");
            uda.markAsReadOnly(true);
            plan.setInitScript("setProcessAttribute(\"readonly\",\"STRING\" , \"Kumar\")");
            plan = plan.createProcessDef();

            //wfSession.publishPlan(plan.getId());
            tr.markPassed("Analytics Test -- Successfully plan created with ID :" + plan.getId());
        }catch (Exception e) {
            logException("Analytics Test -- Plan creation failed", e);
        }
        return plan;
    }

    private
    void
    createProcess(Plan plan)
        throws Exception
    {
        try{
            ProcessInstance pi = plan.createProcessInstance();
            pi.start();
            tr.markPassed("Analytics Test -- Successfully process created and started with ID :" + pi.getId());
        } catch (Exception e) {
            logException("Analytics Test -- Process creation failed", e);
        }
    }

    public
    void
    logIn()
        throws Exception
    {
        try {
            String transPortType = tr.getRequiredProperty("TWFTransportType");
            wfSession = WFObjectFactory.getWFAdminSession();
            wfSession.initForApplication(null, tr.getProps());
            serverName = tr.getRequiredProperty("servername");
            userName = tr.getRequiredProperty("username");
            passWord = tr.getRequiredProperty("password");
            wfSession.logIn(serverName+"Flow", userName, passWord);
            tr.markPassed("Analytics Test --  login successfully PASSED on Server : " + serverName + " with  User Name : " + userName + " and Password : " + passWord);
        } catch (Exception e) {
            logException("Analytics Test --  login FAILED on Server : " + serverName + " with  User Name : " + userName + " and Password : " + passWord, e);
        }
    }

    public
    void
    logOut()
        throws Exception
    {
        try {
            wfSession.logOut();
            tr.markPassed("Analytics Test --  logout  successfully PASSED on Server : " + serverName + " with  User Name : " + userName + " and Password : " + passWord);
        } catch (Exception e) {
            logException("Analytics Test --  logout FAILED on Server : " + serverName + " with  User Name : " + userName + " and Password : " + passWord,e);
        }
    }


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

