package com.chenyang.nse.bussiness.engine.infa.powercenter961.subsetdata;

import com.chenyang.nse.bussiness.engine.infa.InfoProperty;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.ATTRIBUTE;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.CONFIGREFERENCE;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.CONNECTIONREFERENCE;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.FLATFILE;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.SCHEDULEINFO;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.SCHEDULER;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.SESSION;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.SESSIONEXTENSION;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.SESSTRANSFORMATIONINST;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.TASK;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.TASKINSTANCE;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.WORKFLOW;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.WORKFLOWLINK;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.WORKFLOWVARIABLE;
import com.chenyang.nse.bussiness.engine.infa.powercenter961.pubbean.WorkflowBean;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ChildMappletWorkflowUtil {
   public WORKFLOW getChildGroupWorkflow(String mappingName, String workflowname, String servername, String serverdomainname, List<WorkflowBean> wb, String paramfilepath, InfoProperty tengine) throws Exception {
      WORKFLOW workflow = new WORKFLOW();
      workflow.setDESCRIPTION("");
      workflow.setISENABLED("YES");
      workflow.setISRUNNABLESERVICE("NO");
      workflow.setISSERVICE("NO");
      workflow.setISVALID("YES");
      workflowname = dealWorkflowname(workflowname);
      workflow.setNAME(workflowname);
      workflow.setREUSABLESCHEDULER("NO");
      workflow.setSCHEDULERNAME("Scheduler");
      workflow.setSERVERNAME(servername);
      workflow.setSERVERDOMAINNAME(serverdomainname);
      workflow.setSUSPENDONERROR("NO");
      workflow.setTASKSMUSTRUNONSERVER("NO");
      workflow.setVERSIONNUMBER("1");
      workflow.setSCHEDULER(this.getSCHEDULER());
      workflow.getTASK().add(this.getTASK());
      WORKFLOWLINK workflowlink = new WORKFLOWLINK();
      workflowlink.setCONDITION("");
      workflowlink.setFROMTASK("Start");
      workflowlink.setTOTASK("s_M_" + mappingName);
      workflow.getWORKFLOWLINK().add(workflowlink);

      for(TASKINSTANCE taskinstance : this.getTASKINSTANCE("s_M_" + mappingName)) {
         workflow.getTASKINSTANCE().add(taskinstance);
      }

      for(WORKFLOWVARIABLE workflowvariable : this.getWORKFLOWVARIABLE(mappingName)) {
         workflow.getWORKFLOWVARIABLE().add(workflowvariable);
      }

      for(ATTRIBUTE attribute : this.getATTRIBUTE(workflowname, paramfilepath)) {
         workflow.getATTRIBUTE().add(attribute);
      }

      workflow.getSESSION().add(this.getSESSION(mappingName, workflowname, wb, tengine));
      return workflow;
   }

   public SCHEDULER getSCHEDULER() {
      SCHEDULER scheduler = new SCHEDULER();
      scheduler.setDESCRIPTION("");
      scheduler.setNAME("Scheduler");
      scheduler.setREUSABLE("NO");
      scheduler.setVERSIONNUMBER("1");
      SCHEDULEINFO scheduleinfo = new SCHEDULEINFO();
      scheduleinfo.setSCHEDULETYPE("ONDEMAND");
      scheduler.setSCHEDULEINFO(scheduleinfo);
      return scheduler;
   }

   public TASK getTASK() {
      TASK task = new TASK();
      task.setDESCRIPTION("");
      task.setNAME("Start");
      task.setREUSABLE("NO");
      task.setTYPE("Start");
      task.setVERSIONNUMBER("1");
      return task;
   }

   public SESSION getSESSION(String mappingName, String workflowname, List<WorkflowBean> wb, InfoProperty tengine) throws Exception {
      SESSION session = new SESSION();
      session.setDESCRIPTION("");
      session.setISVALID("YES");
      session.setMAPPINGNAME(mappingName);
      session.setNAME("s_M_" + mappingName);
      session.setREUSABLE("NO");
      session.setSORTORDER("Binary");
      session.setVERSIONNUMBER("1");
      if (wb != null && wb.size() > 0) {
         for(int i = 0; i < wb.size(); ++i) {
            for(SESSTRANSFORMATIONINST sesstransformationinst : this.getSESSTRANSFORMATIONINST(((WorkflowBean)wb.get(i)).getSourcename(), ((WorkflowBean)wb.get(i)).getTargetname(), ((WorkflowBean)wb.get(i)).getSqname(), ((WorkflowBean)wb.get(i)).getSourceownername(), ((WorkflowBean)wb.get(i)).getTargetownername())) {
               session.getSESSTRANSFORMATIONINST().add(sesstransformationinst);
            }
         }
      }

      for(SESSTRANSFORMATIONINST sesstransformationinst : this.getSESSTRANSFORMATIONINSTTWO(wb)) {
         session.getSESSTRANSFORMATIONINST().add(sesstransformationinst);
      }

      CONFIGREFERENCE configreference = new CONFIGREFERENCE();
      configreference.setREFOBJECTNAME("default_session_config");
      configreference.setTYPE("Session config");
      session.setCONFIGREFERENCE(configreference);
      if (wb != null && wb.size() > 0) {
         for(int i = 0; i < wb.size(); ++i) {
            for(SESSIONEXTENSION sessionextension : this.getSESSIONEXTENSIONList(((WorkflowBean)wb.get(i)).getSourcename(), ((WorkflowBean)wb.get(i)).getTargetname(), ((WorkflowBean)wb.get(i)).getSqname(), ((WorkflowBean)wb.get(i)).getTargetdbname(), ((WorkflowBean)wb.get(i)).getSourcedbname(), ((WorkflowBean)wb.get(i)).getSource_dbtype())) {
               session.getSESSIONEXTENSION().add(sessionextension);
            }
         }
      }

      for(SESSIONEXTENSION sessionextension : this.getSESSIONEXTENSIONListTWO(wb, tengine)) {
         session.getSESSIONEXTENSION().add(sessionextension);
      }

      for(SESSIONEXTENSION sessionextension : this.getSESSIONEXTENSIONListTHREE(wb, tengine)) {
         session.getSESSIONEXTENSION().add(sessionextension);
      }

      for(ATTRIBUTE attribute : this.getATTRIBUTEInfo(workflowname)) {
         session.getATTRIBUTE().add(attribute);
      }

      return session;
   }

   public List<SESSTRANSFORMATIONINST> getSESSTRANSFORMATIONINST(String sourcename, String targetname, String sqname, String source_ownername, String target_ownername) throws Exception {
      List<SESSTRANSFORMATIONINST> sesstransformationinstList = new ArrayList();
      SESSTRANSFORMATIONINST sesstransformationinst = new SESSTRANSFORMATIONINST();
      sesstransformationinst.setISREPARTITIONPOINT("YES");
      sesstransformationinst.setPARTITIONTYPE("PASS THROUGH");
      sesstransformationinst.setPIPELINE("1");
      sesstransformationinst.setSTAGE("1");
      sesstransformationinst.setSINSTANCENAME(targetname);
      sesstransformationinst.setTRANSFORMATIONNAME(targetname);
      sesstransformationinst.setTRANSFORMATIONTYPE("Target Definition");
      ATTRIBUTE a1 = new ATTRIBUTE();
      a1.setNAME("Table Name Prefix");
      a1.setVALUE(target_ownername);
      sesstransformationinst.getATTRIBUTE().add(a1);
      sesstransformationinstList.add(sesstransformationinst);
      SESSTRANSFORMATIONINST sesstransformationinst1 = new SESSTRANSFORMATIONINST();
      sesstransformationinst1.setISREPARTITIONPOINT("NO");
      sesstransformationinst1.setPIPELINE("0");
      sesstransformationinst1.setSINSTANCENAME(sourcename);
      sesstransformationinst1.setSTAGE("0");
      sesstransformationinst1.setTRANSFORMATIONNAME(sourcename);
      sesstransformationinst1.setTRANSFORMATIONTYPE("Source Definition");
      ATTRIBUTE a2 = new ATTRIBUTE();
      a2.setNAME("Owner Name");
      a2.setVALUE(source_ownername);
      sesstransformationinst1.getATTRIBUTE().add(a2);
      sesstransformationinstList.add(sesstransformationinst1);
      SESSTRANSFORMATIONINST sesstransformationinst2 = new SESSTRANSFORMATIONINST();
      sesstransformationinst2.setISREPARTITIONPOINT("YES");
      sesstransformationinst2.setPARTITIONTYPE("PASS THROUGH");
      sesstransformationinst2.setPIPELINE("1");
      sesstransformationinst2.setSINSTANCENAME(sqname);
      sesstransformationinst2.setSTAGE("2");
      sesstransformationinst2.setTRANSFORMATIONNAME(sqname);
      sesstransformationinst2.setTRANSFORMATIONTYPE("Source Qualifier");
      sesstransformationinstList.add(sesstransformationinst2);
      return sesstransformationinstList;
   }

   public List<SESSTRANSFORMATIONINST> getSESSTRANSFORMATIONINSTTWO(List<WorkflowBean> wb) throws Exception {
      List<SESSTRANSFORMATIONINST> sesstransformationinstList = new ArrayList();
      SESSTRANSFORMATIONINST sesstransformationinst = new SESSTRANSFORMATIONINST();
      sesstransformationinst.setISREPARTITIONPOINT("YES");
      sesstransformationinst.setPARTITIONTYPE("PASS THROUGH");
      sesstransformationinst.setPIPELINE("1");
      sesstransformationinst.setSTAGE("2");
      sesstransformationinst.setSINSTANCENAME("target_tab_candidate0");
      sesstransformationinst.setTRANSFORMATIONNAME("target_tab_candidate0");
      sesstransformationinst.setTRANSFORMATIONTYPE("Target Definition");
      FLATFILE flatfile = new FLATFILE();
      flatfile.setCODEPAGE("MS936");
      flatfile.setCONSECDELIMITERSASONE("NO");
      flatfile.setDELIMITED("YES");
      flatfile.setDELIMITERS(",");
      flatfile.setESCAPECHARACTER("");
      flatfile.setKEEPESCAPECHAR("NO");
      flatfile.setLINESEQUENTIAL("NO");
      flatfile.setMULTIDELIMITERSASAND("NO");
      flatfile.setNULLCHARTYPE("ASCII");
      flatfile.setNULLCHARACTER("*");
      flatfile.setPADBYTES("1");
      flatfile.setQUOTECHARACTER("NONE");
      flatfile.setREPEATABLE("NO");
      flatfile.setROWDELIMITER("0");
      flatfile.setSHIFTSENSITIVEDATA("NO");
      flatfile.setSKIPROWS("0");
      flatfile.setSTRIPTRAILINGBLANKS("NO");
      sesstransformationinst.getFLATFILE().add(flatfile);
      sesstransformationinstList.add(sesstransformationinst);

      for(int i = 1; i < wb.size(); ++i) {
         SESSTRANSFORMATIONINST sesstransformationinst1 = new SESSTRANSFORMATIONINST();
         sesstransformationinst1.setISREPARTITIONPOINT("YES");
         sesstransformationinst1.setPARTITIONTYPE("PASS THROUGH");
         sesstransformationinst1.setPIPELINE("1");
         sesstransformationinst1.setSTAGE("2");
         sesstransformationinst1.setSINSTANCENAME("target_tab_candidate" + i);
         sesstransformationinst1.setTRANSFORMATIONNAME("target_tab_candidate" + i);
         sesstransformationinst1.setTRANSFORMATIONTYPE("Target Definition");
         FLATFILE flatfile1 = new FLATFILE();
         flatfile1.setCODEPAGE("MS936");
         flatfile1.setCONSECDELIMITERSASONE("NO");
         flatfile1.setDELIMITED("YES");
         flatfile1.setDELIMITERS(",");
         flatfile1.setESCAPECHARACTER("");
         flatfile1.setKEEPESCAPECHAR("NO");
         flatfile1.setLINESEQUENTIAL("NO");
         flatfile1.setMULTIDELIMITERSASAND("NO");
         flatfile1.setNULLCHARTYPE("ASCII");
         flatfile1.setNULLCHARACTER("*");
         flatfile1.setPADBYTES("1");
         flatfile1.setQUOTECHARACTER("NONE");
         flatfile1.setREPEATABLE("NO");
         flatfile1.setROWDELIMITER("0");
         flatfile1.setSHIFTSENSITIVEDATA("NO");
         flatfile1.setSKIPROWS("0");
         flatfile1.setSTRIPTRAILINGBLANKS("NO");
         sesstransformationinst1.getFLATFILE().add(flatfile1);
         sesstransformationinstList.add(sesstransformationinst1);
      }

      for(int i = 1; i < wb.size(); ++i) {
         SESSTRANSFORMATIONINST sesstransformationinst1 = new SESSTRANSFORMATIONINST();
         sesstransformationinst1.setISREPARTITIONPOINT("YES");
         sesstransformationinst1.setPARTITIONTYPE("PASS THROUGH");
         sesstransformationinst1.setPIPELINE("1");
         sesstransformationinst1.setSTAGE("2");
         sesstransformationinst1.setSINSTANCENAME("source_tab_candidate" + i);
         sesstransformationinst1.setTRANSFORMATIONNAME("source_tab_candidate" + i);
         sesstransformationinst1.setTRANSFORMATIONTYPE("Source Definition");
         FLATFILE flatfile1 = new FLATFILE();
         flatfile1.setCODEPAGE("MS936");
         flatfile1.setCONSECDELIMITERSASONE("NO");
         flatfile1.setDELIMITED("YES");
         flatfile1.setDELIMITERS(",");
         flatfile1.setESCAPECHARACTER("");
         flatfile1.setKEEPESCAPECHAR("NO");
         flatfile1.setLINESEQUENTIAL("NO");
         flatfile1.setMULTIDELIMITERSASAND("NO");
         flatfile1.setNULLCHARTYPE("ASCII");
         flatfile1.setNULLCHARACTER("*");
         flatfile1.setPADBYTES("1");
         flatfile1.setQUOTECHARACTER("NONE");
         flatfile1.setREPEATABLE("NO");
         flatfile1.setROWDELIMITER("0");
         flatfile1.setSHIFTSENSITIVEDATA("NO");
         flatfile1.setSKIPROWS("0");
         flatfile1.setSTRIPTRAILINGBLANKS("NO");
         sesstransformationinst1.getFLATFILE().add(flatfile1);
         sesstransformationinstList.add(sesstransformationinst1);
         SESSTRANSFORMATIONINST sesstransformationinst2 = new SESSTRANSFORMATIONINST();
         sesstransformationinst2.setISREPARTITIONPOINT("YES");
         sesstransformationinst2.setPARTITIONTYPE("PASS THROUGH");
         sesstransformationinst2.setPIPELINE("5");
         sesstransformationinst2.setSTAGE("11");
         sesstransformationinst2.setSINSTANCENAME("SQ_tab_candidate" + i);
         sesstransformationinst2.setTRANSFORMATIONNAME("SQ_tab_candidate" + i);
         sesstransformationinst2.setTRANSFORMATIONTYPE("Source Qualifier");
         sesstransformationinstList.add(sesstransformationinst2);
      }

      for(int i = 1; i < wb.size() + 1; ++i) {
         SESSTRANSFORMATIONINST sesstransformationinst3 = new SESSTRANSFORMATIONINST();
         sesstransformationinst3.setISREPARTITIONPOINT("NO");
         sesstransformationinst3.setPIPELINE("2");
         sesstransformationinst3.setSTAGE("8");
         sesstransformationinst3.setSINSTANCENAME("SRTTRANS" + (i - 1));
         sesstransformationinst3.setTRANSFORMATIONNAME("SRTTRANS" + (i - 1));
         sesstransformationinst3.setTRANSFORMATIONTYPE("Sorter");
         sesstransformationinstList.add(sesstransformationinst3);
      }

      for(int i = 1; i < wb.size(); ++i) {
         SESSTRANSFORMATIONINST sesstransformationinst3 = new SESSTRANSFORMATIONINST();
         sesstransformationinst3.setISREPARTITIONPOINT("NO");
         sesstransformationinst3.setPIPELINE("4");
         sesstransformationinst3.setSTAGE("10");
         sesstransformationinst3.setSINSTANCENAME("JNRTRANS" + i);
         sesstransformationinst3.setTRANSFORMATIONNAME("JNRTRANS" + i);
         sesstransformationinst3.setTRANSFORMATIONTYPE("Joiner");
         sesstransformationinstList.add(sesstransformationinst3);
      }

      return sesstransformationinstList;
   }

   public List<SESSIONEXTENSION> getSESSIONEXTENSIONList(String sourceDbname, String tragetDbname, String sq_name, String co_target, String sourcedatabasename, String dbtype) throws Exception {
      List<SESSIONEXTENSION> sessionextensionList = new ArrayList();
      SESSIONEXTENSION sessionextension = new SESSIONEXTENSION();
      sessionextension.setNAME("Relational Writer");
      sessionextension.setSINSTANCENAME(tragetDbname);
      sessionextension.setSUBTYPE("Relational Writer");
      sessionextension.setTRANSFORMATIONTYPE("Target Definition");
      sessionextension.setTYPE("WRITER");
      CONNECTIONREFERENCE connectionreference = new CONNECTIONREFERENCE();
      connectionreference.setCNXREFNAME("DB Connection");
      connectionreference.setCONNECTIONNAME(co_target);
      connectionreference.setCONNECTIONNUMBER("1");
      if ("ORACLE".equals(dbtype)) {
         connectionreference.setCONNECTIONSUBTYPE(dbtype);
      } else if ("DB2".equals(dbtype)) {
         connectionreference.setCONNECTIONSUBTYPE(dbtype);
      } else if ("Flat File".equals(dbtype)) {
         connectionreference.setCONNECTIONSUBTYPE(dbtype);
      } else {
         connectionreference.setCONNECTIONSUBTYPE("ODBC");
      }

      connectionreference.setCONNECTIONTYPE("Relational");
      connectionreference.setVARIABLE("");
      sessionextension.getCONNECTIONREFERENCE().add(connectionreference);
      String[] attributeStr = new String[]{"Target load type@Normal", "Insert@YES", "Update as Update@YES", "Update as Insert@NO", "Update else Insert@NO", "Delete@YES", "Truncate target table option@NO", "Reject file directory@$PMBadFileDir", "Reject filename@1.bad"};
      String[] tmpAttr = null;
      ATTRIBUTE[] attribute = new ATTRIBUTE[attributeStr.length];

      for(int i = 0; i < attributeStr.length; ++i) {
         tmpAttr = attributeStr[i].split("@");
         attribute[i] = new ATTRIBUTE();
         attribute[i].setNAME(tmpAttr[0]);
         if (tmpAttr.length > 1) {
            attribute[i].setVALUE(tmpAttr[1]);
         } else {
            attribute[i].setVALUE("");
         }

         sessionextension.getATTRIBUTE().add(attribute[i]);
      }

      sessionextensionList.add(sessionextension);
      SESSIONEXTENSION sessionextension3 = new SESSIONEXTENSION();
      sessionextension3.setDSQINSTNAME(sq_name);
      sessionextension3.setDSQINSTTYPE("Source Qualifier");
      sessionextension3.setNAME("Relational Reader");
      sessionextension3.setSINSTANCENAME(sourceDbname);
      sessionextension3.setSUBTYPE("Relational Reader");
      sessionextension3.setTRANSFORMATIONTYPE("Source Definition");
      sessionextension3.setTYPE("READER");
      sessionextensionList.add(sessionextension3);
      SESSIONEXTENSION sessionextension1 = new SESSIONEXTENSION();
      sessionextension1.setNAME("Relational Reader");
      sessionextension1.setSINSTANCENAME(sq_name);
      sessionextension1.setSUBTYPE("Relational Reader");
      sessionextension1.setTRANSFORMATIONTYPE("Source Qualifier");
      sessionextension1.setTYPE("READER");
      CONNECTIONREFERENCE connectionreference1 = new CONNECTIONREFERENCE();
      connectionreference1.setCNXREFNAME("DB Connection");
      connectionreference1.setCONNECTIONNAME(sourcedatabasename);
      connectionreference1.setCONNECTIONNUMBER("1");
      if ("ORACLE".equals(dbtype)) {
         connectionreference1.setCONNECTIONSUBTYPE(dbtype);
      } else if ("DB2".equals(dbtype)) {
         connectionreference1.setCONNECTIONSUBTYPE(dbtype);
      } else if ("Flat File".equals(dbtype)) {
         connectionreference1.setCONNECTIONSUBTYPE(dbtype);
      } else {
         connectionreference1.setCONNECTIONSUBTYPE("ODBC");
      }

      connectionreference1.setCONNECTIONTYPE("Relational");
      connectionreference1.setVARIABLE("");
      sessionextension1.getCONNECTIONREFERENCE().add(connectionreference1);
      sessionextensionList.add(sessionextension1);
      return sessionextensionList;
   }

   public List<SESSIONEXTENSION> getSESSIONEXTENSIONListTWO(List<WorkflowBean> wb, InfoProperty tengine) throws Exception {
      List<SESSIONEXTENSION> sessionextensionList = new ArrayList();

      for(int j = 0; j < wb.size(); ++j) {
         SESSIONEXTENSION sessionextension = new SESSIONEXTENSION();
         sessionextension.setNAME("File Writer");
         sessionextension.setSINSTANCENAME("target_tab_candidate" + j);
         sessionextension.setSUBTYPE("File Writer");
         sessionextension.setTRANSFORMATIONTYPE("Target Definition");
         sessionextension.setTYPE("WRITER");
         CONNECTIONREFERENCE connectionreference = new CONNECTIONREFERENCE();
         connectionreference.setCNXREFNAME("Connection");
         connectionreference.setCONNECTIONNAME("");
         connectionreference.setCONNECTIONNUMBER("1");
         connectionreference.setCONNECTIONSUBTYPE("");
         connectionreference.setCONNECTIONTYPE("");
         connectionreference.setVARIABLE("");
         sessionextension.getCONNECTIONREFERENCE().add(connectionreference);
         String path = tengine.getXmldir() + File.separator + "Subset" + File.separator;
         String[] attributeStr = new String[]{"Merge Type@No Merge", "Merge File Directory@$PMTargetFileDir/", "Merge File Name@tab_candidate" + (j + 1) + ".out", "Append if Exists@NO", "Create Target Directory@NO", "Header Options@No Header", "Header Command@", "Footer Command@", "Output Type@file", "Merge Command@", "Output file directory@" + path, "Output filename@" + (j + 1) + ".out", "Reject file directory@$PMBadFileDir/", "Reject filename@tab_candidate1.bad", "Command@", "Codepage Parameter@"};
         ((WorkflowBean)wb.get(j)).setT_file_path(path);
         ((WorkflowBean)wb.get(j)).setT_file_name(String.valueOf(j + 1));
         String[] tmpAttr = null;
         ATTRIBUTE[] attribute = new ATTRIBUTE[attributeStr.length];

         for(int i = 0; i < attributeStr.length; ++i) {
            tmpAttr = attributeStr[i].split("@");
            attribute[i] = new ATTRIBUTE();
            attribute[i].setNAME(tmpAttr[0]);
            if (tmpAttr.length > 1) {
               attribute[i].setVALUE(tmpAttr[1]);
            } else {
               attribute[i].setVALUE("");
            }

            sessionextension.getATTRIBUTE().add(attribute[i]);
         }

         sessionextensionList.add(sessionextension);
      }

      return sessionextensionList;
   }

   public List<SESSIONEXTENSION> getSESSIONEXTENSIONListTHREE(List<WorkflowBean> wb, InfoProperty tengine) throws Exception {
      List<SESSIONEXTENSION> sessionextensionList = new ArrayList();

      for(int j = 1; j < wb.size(); ++j) {
         String sourceFileName = String.valueOf(j + 1);
         WorkflowBean wfb = (WorkflowBean)wb.get(j);
         if (null != wfb.getMainTable() && wfb.getMainTable().size() > 0) {
            String mainTable = (String)wfb.getMainTable().get(0);

            for(int z = 0; z < wb.size(); ++z) {
               if (null != mainTable && mainTable.equals(((WorkflowBean)wb.get(z)).getSourcename())) {
                  sourceFileName = ((WorkflowBean)wb.get(z)).getT_file_name();
                  break;
               }
            }
         }

         SESSIONEXTENSION sessionextension1 = new SESSIONEXTENSION();
         sessionextension1.setNAME("File Reader");
         sessionextension1.setSINSTANCENAME("SQ_tab_candidate" + j);
         sessionextension1.setSUBTYPE("File Reader");
         sessionextension1.setTRANSFORMATIONTYPE("Source Qualifier");
         sessionextension1.setTYPE("READER");
         SESSIONEXTENSION sessionextension = new SESSIONEXTENSION();
         sessionextension.setNAME("File Reader");
         sessionextension.setSINSTANCENAME("source_tab_candidate" + j);
         sessionextension.setSUBTYPE("File Reader");
         sessionextension.setTRANSFORMATIONTYPE("Source Definition");
         sessionextension.setTYPE("READER");
         sessionextension.setDSQINSTNAME("SQ_tab_candidate" + j);
         sessionextension.setDSQINSTTYPE("Source Qualifier");
         CONNECTIONREFERENCE connectionreference = new CONNECTIONREFERENCE();
         connectionreference.setCNXREFNAME("Connection");
         connectionreference.setCONNECTIONNAME("");
         connectionreference.setCONNECTIONNUMBER("1");
         connectionreference.setCONNECTIONSUBTYPE("");
         connectionreference.setCONNECTIONTYPE("");
         connectionreference.setVARIABLE("");
         sessionextension.getCONNECTIONREFERENCE().add(connectionreference);
         String path = tengine.getXmldir() + File.separator + "Subset" + File.separator;
         String[] attributeStr = new String[]{"Input Type@File", "Concurrent read partitioning@Optimize throughput", "Command Type@Command Generating Data", "Source filetype@Direct", "Source file directory@" + path, "Source filename@" + sourceFileName + ".out", "Command@", "File Reader Truncate String Null@NO", "Codepage Parameter@"};
         String[] tmpAttr = null;
         ATTRIBUTE[] attribute = new ATTRIBUTE[attributeStr.length];

         for(int i = 0; i < attributeStr.length; ++i) {
            tmpAttr = attributeStr[i].split("@");
            attribute[i] = new ATTRIBUTE();
            attribute[i].setNAME(tmpAttr[0]);
            if (tmpAttr.length > 1) {
               attribute[i].setVALUE(tmpAttr[1]);
            } else {
               attribute[i].setVALUE("");
            }

            sessionextension.getATTRIBUTE().add(attribute[i]);
         }

         sessionextensionList.add(sessionextension1);
         sessionextensionList.add(sessionextension);
      }

      return sessionextensionList;
   }

   public List<ATTRIBUTE> getATTRIBUTEInfo(String workflowname) {
      List<ATTRIBUTE> ATTRIBUTEList = new ArrayList();
      String[] attruInfo = new String[]{"General Options@", "Write Backward Compatible Session Log File@NO", "Session Log File Name@" + workflowname + ".log", "Session Log File directory@$PMSessionLogDir\\", "Parameter Filename@", "Enable Test Load@NO", "$Source connection value@", "$Target connection value@", "Treat source rows as@Insert", "Commit Type@Target", "Commit Interval@10000", "Commit On End Of File@YES", "Rollback Transactions on Errors@NO", "Recovery Strategy@Fail task and continue workflow", "Java Classpath@", "Performance@", "DTM buffer size@Auto", "Collect performance data@NO", "Write performance data to repository@NO", "Incremental Aggregation@NO", "Enable high precision@NO", "Session retry on deadlock@NO", "Pushdown Optimization@None", "Allow Temporary View for Pushdown@NO", "Allow Temporary Sequence for Pushdown@NO", "Allow Pushdown for User Incompatible Connections@NO"};
      String[] tmpAttr = null;
      ATTRIBUTE[] attribute1 = new ATTRIBUTE[attruInfo.length];

      for(int i = 0; i < attruInfo.length; ++i) {
         tmpAttr = attruInfo[i].split("@");
         attribute1[i] = new ATTRIBUTE();
         attribute1[i].setNAME(tmpAttr[0]);
         if (tmpAttr.length > 1) {
            attribute1[i].setVALUE(tmpAttr[1]);
         } else {
            attribute1[i].setVALUE("");
         }

         ATTRIBUTEList.add(attribute1[i]);
      }

      return ATTRIBUTEList;
   }

   public List<TASKINSTANCE> getTASKINSTANCE(String sessionname) {
      List<TASKINSTANCE> taskinstanceList = new ArrayList();
      TASKINSTANCE taskinstance = new TASKINSTANCE();
      taskinstance.setDESCRIPTION("");
      taskinstance.setISENABLED("YES");
      taskinstance.setNAME("Start");
      taskinstance.setREUSABLE("NO");
      taskinstance.setTASKNAME("Start");
      taskinstance.setTASKTYPE("Start");
      taskinstanceList.add(taskinstance);
      TASKINSTANCE taskinstance1 = new TASKINSTANCE();
      taskinstance1.setDESCRIPTION("");
      taskinstance1.setFAILPARENTIFINSTANCEDIDNOTRUN("NO");
      taskinstance1.setFAILPARENTIFINSTANCEFAILS("YES");
      taskinstance1.setISENABLED("YES");
      taskinstance1.setREUSABLE("NO");
      taskinstance1.setNAME(sessionname);
      taskinstance1.setTASKNAME(sessionname);
      taskinstance1.setTASKTYPE("Session");
      taskinstance1.setTREATINPUTLINKASAND("YES");
      taskinstanceList.add(taskinstance1);
      return taskinstanceList;
   }

   public List<WORKFLOWVARIABLE> getWORKFLOWVARIABLE(String mappingName) {
      List<WORKFLOWVARIABLE> workflowvariableList = new ArrayList();
      String name = "s_M_" + mappingName;
      String[] workflowvariableStr = new String[]{"date/time@The time this task started@NO@NO@$Start.StartTime@NO", "date/time@The time this task completed@NO@NO@$Start.EndTime@NO", "integer@Status of this task&apos;s execution@NO@NO@$Start.Status@NO", "integer@Status of the previous task that is not disabled@NO@NO@$Start.PrevTaskStatus@NO", "integer@Error code for this task&apos;s execution@NO@NO@$Start.ErrorCode@NO", "string@Error message for this task&apos;s execution@NO@NO@$Start.ErrorMsg@NO", "date/time@The time this task started@NO@NO@$" + name + ".StartTime@NO", "date/time@The time this task completed@NO@NO@$" + name + ".EndTime@NO", "integer@Status of this task&apos;s execution@NO@NO@$" + name + ".Status@NO", "integer@Status of the previous task that is not disabled@NO@NO@$" + name + ".PrevTaskStatus@NO", "integer@Error code for this task&apos;s execution@NO@NO@$" + name + ".ErrorCode@NO", "string@Error message for this task&apos;s execution@NO@NO@$" + name + ".ErrorMsg@NO", "integer@Rows successfully read@NO@NO@$" + name + ".SrcSuccessRows@NO", "integer@Rows failed to read@NO@NO@$" + name + ".SrcFailedRows@NO", "integer@Rows successfully loaded@NO@NO@$" + name + ".TgtSuccessRows@NO", "integer@Rows failed to load@NO@NO@$" + name + ".TgtFailedRows@NO", "integer@Total number of transformation errors@NO@NO@$" + name + ".TotalTransErrors@NO", "integer@First error code@NO@NO@$" + name + ".FirstErrorCode@NO", "string@First error message@NO@NO@$" + name + ".FirstErrorMsg@NO"};
      String[] splitArry = null;
      WORKFLOWVARIABLE[] workflowvariable = new WORKFLOWVARIABLE[workflowvariableStr.length];

      for(int i = 0; i < workflowvariableStr.length; ++i) {
         workflowvariable[i] = new WORKFLOWVARIABLE();
         splitArry = workflowvariableStr[i].split("@");
         workflowvariable[i].setDATATYPE(splitArry[0]);
         workflowvariable[i].setDEFAULTVALUE("");
         workflowvariable[i].setDESCRIPTION(splitArry[1]);
         workflowvariable[i].setISNULL(splitArry[2]);
         workflowvariable[i].setISPERSISTENT(splitArry[3]);
         workflowvariable[i].setNAME(splitArry[4]);
         workflowvariable[i].setUSERDEFINED(splitArry[5]);
         workflowvariableList.add(workflowvariable[i]);
      }

      return workflowvariableList;
   }

   public List<ATTRIBUTE> getATTRIBUTE(String workflowname, String paramfilepath) {
      List<ATTRIBUTE> attributeList = new ArrayList();
      String[] attributeStr = new String[]{"Parameter Filename@" + paramfilepath, "Write Backward Compatible Workflow Log File@NO", "Workflow Log File Name@" + workflowname + ".log", "Workflow Log File Directory@$PMWorkflowLogDir\\", "Save Workflow log by@By runs", "Save workflow log for these runs@0", "Service Name@", "Service Timeout@0", "Is Service Visible@NO", "Is Service Protected@NO", "Enable HA recovery@NO", "Automatically recover terminated tasks@NO", "Service Level Name@Default", "Allow concurrent run with unique run instance name@NO", "Allow concurrent run with same run instance name@NO", "Maximum number of concurrent runs@0", "Assigned Web Services Hubs@", "Maximum number of concurrent runs per Hub@1000", "Expected Service Time@1"};
      String[] splitArry = null;
      ATTRIBUTE[] attribute = new ATTRIBUTE[attributeStr.length];

      for(int i = 0; i < attributeStr.length; ++i) {
         attribute[i] = new ATTRIBUTE();
         splitArry = attributeStr[i].split("@");
         attribute[i].setNAME(splitArry[0]);
         if (splitArry.length > 1) {
            attribute[i].setVALUE(splitArry[1]);
         } else {
            attribute[i].setVALUE("");
         }

         attributeList.add(attribute[i]);
      }

      return attributeList;
   }

   public static String dealWorkflowname(String name) {
      name = name.replace("$", "_").replace(".", "_").replace("+", "_").replace("-", "_").replace("=", "_").replace("~", "_").replace("`", "_").replace("!", "_").replace("%", "_").replace("^", "_").replace("&", "_").replace("*", "_").replace("(", "_").replace(")", "_").replace("[", "_").replace("]", "_").replace("{", "_").replace("}", "_").replace("<", "_").replace(">", "_").replace("|", "_").replace(";", "_").replace(":", "_").replace(",", "_").replace("?", "_");
      return name;
   }
}
