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

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.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.util.ArrayList;
import java.util.List;

public class GroupWorkflowUtil {
   public WORKFLOW getGroupWorkflow(String mappingName, String workflowname, String servername, String serverdomainname, List<WorkflowBean> wb, String paramfilepath) throws Exception {
      WORKFLOW workflow = new WORKFLOW();
      workflow.setDESCRIPTION("");
      workflow.setISENABLED("YES");
      workflow.setISRUNNABLESERVICE("NO");
      workflow.setISSERVICE("NO");
      workflow.setISVALID("YES");
      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));
      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) 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);
            }
         }
      }

      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())) {
               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<SESSIONEXTENSION> getSESSIONEXTENSIONList(String sourceDbname, String tragetDbname, String sq_name, String co_target, String sourcedatabasename) 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");
      connectionreference.setCONNECTIONSUBTYPE("Oracle");
      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");
      connectionreference1.setCONNECTIONSUBTYPE("Oracle");
      connectionreference1.setCONNECTIONTYPE("Relational");
      connectionreference1.setVARIABLE("");
      sessionextension1.getCONNECTIONREFERENCE().add(connectionreference1);
      sessionextensionList.add(sessionextension1);
      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;
   }
}
