package com.fujitsu.iflowqa.testframe;

import java.awt.Point;

import org.w3c.dom.Document;

import com.fujitsu.iflow.common.DOMUtils;
import com.fujitsu.iflow.model.workflow.Arrow;
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.WFObjectFactory;
/**
 * This Class tests the get and set methods of extended attributes on Process definition, Node, Arrow and UDA.
 * The process definition has start, exit and activity nodes. In the class set and get extended attribute methods
 * calling on following IBPM elements:
 *  1.  Process definition which is "ExtendedAttributesTest"
 *  2.  Activity which is "ExtendedAttributesNode"
 *  3.  Arrow which is "START"
 *  4.  UDA which is "test1".
 */

public class ExtendedAttributesTest extends BPMTestBase
{
    String planExtendedAttributes =  null;
    String nodeExtendedAttributes =  null;
    String arrowExtendedAttributes =  null;
    String dataItemRefExtendedAttributes =  null;

    public ExtendedAttributesTest(){
    }

  /**
     * This method creates the Process definition with start->Activity->Exit nodes
     * @return
     * @throws Exception
     */
    public
    Plan
    createTemplate() throws Exception
    {
        Plan plan = null;

        String planName = "ExtendedAttributesTest";

        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( "ExtendedAttributesNode", Node.TYPE_ACTIVITY );
        firstActNode.setPosition( new Point( startX + 200, startY ) );
        firstActNode.setRole( "Role" );
        nodeExtendedAttributes =    "<ExtendedAttributes>"
                                        + "<ExtendedAttribute Name=\"Node-ParticipantID-1\" Value=\"ID_CSE\"/>"
                                        + "<ExtendedAttribute Name=\"XOffset\" Value=\"400\"/>"
                                        + "<ExtendedAttribute Name=\"YOffset\" Value=\"140\"/>"
                                + "</ExtendedAttributes>";
        Document doc = DOMUtils.convertStringToDocument(nodeExtendedAttributes);
        firstActNode.setExtendedAttributes(doc);

        Arrow arrow = plan.addArrow( "START", startNode, firstActNode );
        arrowExtendedAttributes =    "<ExtendedAttributes>"
                                        + "<ExtendedAttribute Name=\"Arrow-ParticipantID-1\" Value=\"ID_CSE\"/>"
                                        + "<ExtendedAttribute Name=\"XOffset\" Value=\"400\"/>"
                                        + "<ExtendedAttribute Name=\"YOffset\" Value=\"140\"/>"
                                + "</ExtendedAttributes>";
        doc = DOMUtils.convertStringToDocument(arrowExtendedAttributes);
        arrow.setExtendedAttributes(doc);

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

        DataItemRef data1  = plan.addDataItemRef("test1", "STRING", "TEST");
        dataItemRefExtendedAttributes =    "<ExtendedAttributes>"
                                            + "<ExtendedAttribute Name=\"DataItemRef-ParticipantID-1\" Value=\"ID_CSE\"/>"
                                            + "<ExtendedAttribute Name=\"XOffset\" Value=\"400\"/>"
                                            + "<ExtendedAttribute Name=\"YOffset\" Value=\"140\"/>"
                                        + "</ExtendedAttributes>";
        doc = DOMUtils.convertStringToDocument(dataItemRefExtendedAttributes);
        data1.setExtendedAttributes(doc);

        planExtendedAttributes =    "<ExtendedAttributes>"
                                        + "<ExtendedAttribute Name=\"Plan-ParticipantID-1\" Value=\"ID_CSE\"/>"
                                        + "<ExtendedAttribute Name=\"XOffset\" Value=\"400\"/>"
                                        + "<ExtendedAttribute Name=\"YOffset\" Value=\"140\"/>"
                                + "</ExtendedAttributes>" ;
        doc = DOMUtils.convertStringToDocument(planExtendedAttributes);
        plan.setExtendedAttributes(doc);

        plan = plan.createProcessDef();
        return plan;

    }
    /*
    * This method runs the rest.
    */
    public void runTests(TestRecorder ntr) throws Exception
    {
        try {
            tr = ntr;
            init(tr) ;
            login() ;
            Plan plan = createTemplate();
            long id = plan.getId();
            plan = null;
            logOut();
            login();
            plan =  WFObjectFactory.getPlan(id,wfSession);
            Document doc = plan.getExtendedAttributes();
            String xml = DOMUtils.convertDomToString(doc);
            if(xml.indexOf(planExtendedAttributes) != -1){
                tr.markPassed("Plan ExtendedAttributes test passed");
            }
            Node[] nodes = plan.getNodes();
            for(int i = 0; i < nodes.length; i++){
                if(nodes[i].getName().equals("ExtendedAttributesNode") ){
                       doc = nodes[i].getExtendedAttributes();
                       xml = DOMUtils.convertDomToString(doc);
                       if(xml.indexOf(nodeExtendedAttributes) != -1){
                            tr.markPassed("Node ExtendedAttributes test passed");
                        }
                        break;
                }
            }
            Arrow[] arrows = plan.getArrows();
            for(int i = 0; i < arrows.length; i++){
                if(arrows[i].getName().equals("START") ){
                       doc = arrows[i].getExtendedAttributes();
                       xml = DOMUtils.convertDomToString(doc);
                       if(xml.indexOf(arrowExtendedAttributes) != -1){
                            tr.markPassed("Arrow ExtendedAttributes test passed");
                        }
                        break;
                }
            }

            DataItemRef dataItem = plan.getDataItemRef("test1");
            doc = dataItem.getExtendedAttributes();
            xml = DOMUtils.convertDomToString(doc);
            if(xml.indexOf(dataItemRefExtendedAttributes) != -1){
                tr.markPassed("DataItemRef ExtendedAttributes test passed");
            }

        }
        catch (Exception e) {
            tr.log("ExtendedAttributesTest caught exception: ") ;
            throw e ;
        }
        finally {
            logOut();
        }
    }
}
