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

import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.ASSOCIATEDSOURCEINSTANCE;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.CONNECTOR;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.ERPINFO;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.INSTANCE;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.MAPPING;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.MAPPINGVARIABLE;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.TABLEATTRIBUTE;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.TARGETLOADORDER;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.TRANSFORMATION;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.TRANSFORMFIELD;
import com.chenyang.nse.bussiness.engine.infa.powercenter961.pubbean.MappletBean;
import com.chenyang.nse.bussiness.engine.infa.powercenter961.pubtool.StringUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MakeDataMappingUtil {
   public MAPPING getMakeDataMapping(String mappingname, String sq_name, List<String> columnList, List<String> dataTypeList, List<String> mappinglengthlist, String sourcename, String targetname, String sourcedbname, List<MappletBean> mapplet) throws Exception {
      MAPPING mapping = new MAPPING();
      mapping.setDESCRIPTION("");
      mapping.setISVALID("YES");
      mapping.setNAME(mappingname);
      mapping.setOBJECTVERSION("1");
      mapping.setVERSIONNUMBER("1");

      for(TRANSFORMATION tr : this.getTRANSFORMATIONXml(sq_name, columnList, dataTypeList, mappinglengthlist)) {
         mapping.getTRANSFORMATION().add(tr);
      }

      for(INSTANCE instance : this.getConvertTableInfoToINSTANCE(sq_name, sourcename, targetname, sourcedbname, mapplet)) {
         mapping.getINSTANCE().add(instance);
      }

      for(CONNECTOR connector : this.getConvertTableInfoToCONNECTOR(columnList, mapplet, sq_name, sourcename, targetname)) {
         mapping.getCONNECTOR().add(connector);
      }

      TARGETLOADORDER targetloadorder = new TARGETLOADORDER();
      targetloadorder.setORDER("1");
      targetloadorder.setTARGETINSTANCE("MAPPING");
      mapping.getTARGETLOADORDER().add(targetloadorder);
      MAPPINGVARIABLE mappingvariable = new MAPPINGVARIABLE();
      mappingvariable.setAGGFUNCTION("MAX");
      mappingvariable.setDATATYPE("integer");
      mappingvariable.setISEXPRESSIONVARIABLE("NO");
      mappingvariable.setISPARAM("NO");
      mappingvariable.setNAME("$$ROWNUMPARA");
      mappingvariable.setPRECISION("10");
      mappingvariable.setSCALE("0");
      mappingvariable.setUSERDEFINED("YES");
      mapping.getMAPPINGVARIABLE().add(mappingvariable);
      ERPINFO erpinfo = new ERPINFO();
      mapping.getERPINFO().add(erpinfo);
      return mapping;
   }

   public List<TRANSFORMATION> getTRANSFORMATIONXml(String sq_name, List<String> columnList, List<String> dataTypeList, List<String> mappinglengthlist) {
      List<TRANSFORMATION> tranlist = new ArrayList();
      TRANSFORMATION transformation1 = new TRANSFORMATION();
      transformation1.setDESCRIPTION("");
      transformation1.setNAME(sq_name);
      transformation1.setOBJECTVERSION("1");
      transformation1.setREUSABLE("NO");
      transformation1.setTYPE("Source Qualifier");
      transformation1.setVERSIONNUMBER("1");

      for(TRANSFORMFIELD transformfield : this.getTransformfield(columnList, dataTypeList, mappinglengthlist)) {
         transformation1.getTRANSFORMFIELD().add(transformfield);
      }

      for(TABLEATTRIBUTE tableattribute : this.getTableattribute()) {
         transformation1.getTABLEATTRIBUTE().add(tableattribute);
      }

      tranlist.add(transformation1);
      TRANSFORMATION transformation2 = new TRANSFORMATION();
      transformation2.setDESCRIPTION("");
      transformation2.setNAME("SQ_Tab_Seed");
      transformation2.setOBJECTVERSION("1");
      transformation2.setREUSABLE("NO");
      transformation2.setTYPE("Source Qualifier");
      transformation2.setVERSIONNUMBER("1");
      TRANSFORMFIELD tf2 = new TRANSFORMFIELD();
      tf2.setDATATYPE("double");
      tf2.setDEFAULTVALUE("");
      tf2.setDESCRIPTION("");
      tf2.setNAME("SEED_COL");
      tf2.setPICTURETEXT("");
      tf2.setPORTTYPE("INPUT/OUTPUT");
      tf2.setPRECISION("15");
      tf2.setSCALE("0");
      transformation2.getTRANSFORMFIELD().add(tf2);

      for(TABLEATTRIBUTE tableattribute2 : this.getTableattribute2()) {
         transformation2.getTABLEATTRIBUTE().add(tableattribute2);
      }

      tranlist.add(transformation2);
      TRANSFORMATION transformation3 = new TRANSFORMATION();
      transformation3.setDESCRIPTION("");
      transformation3.setNAME("EXPTRANS");
      transformation3.setOBJECTVERSION("1");
      transformation3.setREUSABLE("NO");
      transformation3.setTYPE("Expression");
      transformation3.setVERSIONNUMBER("1");

      for(TRANSFORMFIELD transformfield1 : this.getTransformfieldEXP(columnList, dataTypeList, mappinglengthlist)) {
         transformation3.getTRANSFORMFIELD().add(transformfield1);
      }

      TABLEATTRIBUTE trb3 = new TABLEATTRIBUTE();
      trb3.setNAME("Tracing Level");
      trb3.setVALUE("Normal");
      transformation3.getTABLEATTRIBUTE().add(trb3);
      tranlist.add(transformation3);
      TRANSFORMATION transformation4 = new TRANSFORMATION();
      transformation4.setDESCRIPTION("");
      transformation4.setNAME("JNRTRANS");
      transformation4.setOBJECTVERSION("1");
      transformation4.setREUSABLE("NO");
      transformation4.setTYPE("Joiner");
      transformation4.setVERSIONNUMBER("1");

      for(TRANSFORMFIELD transformfield2 : this.getTransformfieldJNRTRANS(columnList, dataTypeList, mappinglengthlist)) {
         transformation4.getTRANSFORMFIELD().add(transformfield2);
      }

      for(TABLEATTRIBUTE tableattribute : this.getTableattributeJNRTRANS()) {
         transformation4.getTABLEATTRIBUTE().add(tableattribute);
      }

      tranlist.add(transformation4);
      return tranlist;
   }

   public List<TRANSFORMFIELD> getTransformfield(List<String> columnList, List<String> dataTypeList, List<String> mappinglengthlist) {
      List<TRANSFORMFIELD> transformfield = new ArrayList();

      for(int i = 0; i < columnList.size(); ++i) {
         TRANSFORMFIELD tran = new TRANSFORMFIELD();
         tran.setDATATYPE((String)dataTypeList.get(i));
         tran.setDEFAULTVALUE("");
         tran.setDESCRIPTION("");
         tran.setNAME(StringUtil.null2String(columnList.get(i)));
         tran.setPICTURETEXT("");
         tran.setPORTTYPE("INPUT/OUTPUT");
         tran.setPRECISION((String)mappinglengthlist.get(i));
         if ("date/time".equals(dataTypeList.get(i))) {
            tran.setSCALE("9");
         } else {
            tran.setSCALE("0");
         }

         transformfield.add(tran);
      }

      return transformfield;
   }

   public List<TRANSFORMFIELD> getTransformfieldEXP(List<String> columnList, List<String> dataTypeList, List<String> mappinglengthlist) {
      List<TRANSFORMFIELD> transformfield = new ArrayList();

      for(int i = 0; i < columnList.size(); ++i) {
         TRANSFORMFIELD tran = new TRANSFORMFIELD();
         tran.setEXPRESSION(StringUtil.null2String(columnList.get(i)));
         tran.setEXPRESSIONTYPE("GENERAL");
         tran.setDATATYPE((String)dataTypeList.get(i));
         tran.setDEFAULTVALUE("");
         tran.setDESCRIPTION("");
         tran.setNAME(StringUtil.null2String(columnList.get(i)));
         tran.setPICTURETEXT("");
         tran.setPORTTYPE("INPUT/OUTPUT");
         tran.setPRECISION((String)mappinglengthlist.get(i));
         if ("date/time".equals(dataTypeList.get(i))) {
            tran.setSCALE("9");
         } else {
            tran.setSCALE("0");
         }

         transformfield.add(tran);
      }

      TRANSFORMFIELD tran1 = new TRANSFORMFIELD();
      tran1.setEXPRESSION("1");
      tran1.setEXPRESSIONTYPE("GENERAL");
      tran1.setDATATYPE("integer");
      tran1.setDEFAULTVALUE("ERROR('transformation error')");
      tran1.setDESCRIPTION("");
      tran1.setNAME("Dummy_col");
      tran1.setPICTURETEXT("");
      tran1.setPORTTYPE("OUTPUT");
      tran1.setPRECISION("10");
      tran1.setSCALE("0");
      transformfield.add(tran1);
      return transformfield;
   }

   public List<TRANSFORMFIELD> getTransformfieldJNRTRANS(List<String> columnList, List<String> dataTypeList, List<String> mappinglengthlist) {
      List<TRANSFORMFIELD> transformfield = new ArrayList();

      for(int i = 0; i < columnList.size(); ++i) {
         TRANSFORMFIELD tran = new TRANSFORMFIELD();
         tran.setDATATYPE((String)dataTypeList.get(i));
         tran.setDEFAULTVALUE("");
         tran.setDESCRIPTION("");
         tran.setNAME(StringUtil.null2String(columnList.get(i)));
         tran.setPICTURETEXT("");
         tran.setPORTTYPE("INPUT/OUTPUT/MASTER");
         tran.setPRECISION((String)mappinglengthlist.get(i));
         if ("date/time".equals(dataTypeList.get(i))) {
            tran.setSCALE("9");
         } else {
            tran.setSCALE("0");
         }

         transformfield.add(tran);
      }

      TRANSFORMFIELD tran1 = new TRANSFORMFIELD();
      tran1.setDATATYPE("integer");
      tran1.setDEFAULTVALUE("");
      tran1.setDESCRIPTION("");
      tran1.setNAME("Dummy_col");
      tran1.setPICTURETEXT("");
      tran1.setPORTTYPE("INPUT/OUTPUT/MASTER");
      tran1.setPRECISION("10");
      tran1.setSCALE("0");
      TRANSFORMFIELD tran2 = new TRANSFORMFIELD();
      tran2.setDATATYPE("integer");
      tran2.setDEFAULTVALUE("");
      tran2.setDESCRIPTION("");
      tran2.setNAME("SEED_COL");
      tran2.setPICTURETEXT("");
      tran2.setPORTTYPE("INPUT/OUTPUT");
      tran2.setPRECISION("10");
      tran2.setSCALE("0");
      transformfield.add(tran1);
      transformfield.add(tran2);
      return transformfield;
   }

   public List<TABLEATTRIBUTE> getTableattribute() {
      List<TABLEATTRIBUTE> tableattributeList = new ArrayList();
      String querySql = "@";
      String[] tableattributeStr = new String[]{"Sql Query@", "User Defined Join", "Source Filter@", "Number Of Sorted Ports@0", "Tracing Level@Normal", "Select Distinct@NO", "Is Partitionable@NO", "Pre SQL", "Post SQL", "Output is deterministic@NO", "Output is repeatable@Never"};

      for(int i = 0; i < tableattributeStr.length; ++i) {
         TABLEATTRIBUTE tableattribute = new TABLEATTRIBUTE();
         String[] tmpStr = tableattributeStr[i].split("@");
         tableattribute.setNAME(tmpStr[0]);
         if (tmpStr.length > 1) {
            tableattribute.setVALUE(tmpStr[1]);
         } else {
            tableattribute.setVALUE("");
         }

         tableattributeList.add(tableattribute);
      }

      return tableattributeList;
   }

   public List<TABLEATTRIBUTE> getTableattribute2() {
      List<TABLEATTRIBUTE> tableattributeList = new ArrayList();
      String querySql = "@SELECT TAB_SEED.SEED_COL FROM TAB_SEED WHERE ROWNUM<$$ROWNUMPARA";
      String[] tableattributeStr = new String[]{"Sql Query" + querySql, "User Defined Join", "Source Filter@", "Number Of Sorted Ports@0", "Tracing Level@Normal", "Select Distinct@NO", "Is Partitionable@NO", "Pre SQL", "Post SQL", "Output is deterministic@NO", "Output is repeatable@Never"};

      for(int i = 0; i < tableattributeStr.length; ++i) {
         TABLEATTRIBUTE tableattribute = new TABLEATTRIBUTE();
         String[] tmpStr = tableattributeStr[i].split("@");
         tableattribute.setNAME(tmpStr[0]);
         if (tmpStr.length > 1) {
            tableattribute.setVALUE(tmpStr[1]);
         } else {
            tableattribute.setVALUE("");
         }

         tableattributeList.add(tableattribute);
      }

      return tableattributeList;
   }

   public List<TABLEATTRIBUTE> getTableattributeJNRTRANS() {
      List<TABLEATTRIBUTE> tableattributeList = new ArrayList();
      String[] tableattributeStr = new String[]{"Case Sensitive String Comparison@YES", "Cache Directory@$PMCacheDir", "Join Condition@Dummy_col = SEED_COL", "Join Type@Normal Join", "Null ordering in master@Null Is Highest Value", "Null ordering in detail@Null Is Highest Value", "Tracing Level@Normal", "Joiner Data Cache Size@Auto", "Joiner Index Cache Size@Auto", "Sorted Input@NO", "Master Sort Order@Auto", "Transformation Scope@All Input"};

      for(int i = 0; i < tableattributeStr.length; ++i) {
         TABLEATTRIBUTE tableattribute = new TABLEATTRIBUTE();
         String[] tmpStr = tableattributeStr[i].split("@");
         tableattribute.setNAME(tmpStr[0]);
         if (tmpStr.length > 1) {
            tableattribute.setVALUE(tmpStr[1]);
         } else {
            tableattribute.setVALUE("");
         }

         tableattributeList.add(tableattribute);
      }

      return tableattributeList;
   }

   public List<INSTANCE> getConvertTableInfoToINSTANCE(String sq_name, String sourcename, String targetname, String sourcedbname, List<MappletBean> mapplet) {
      List<INSTANCE> instanceList = new ArrayList();
      INSTANCE instance_1 = new INSTANCE();
      instance_1.setDESCRIPTION("");
      instance_1.setNAME(targetname);
      instance_1.setTRANSFORMATIONNAME(targetname);
      instance_1.setTRANSFORMATIONTYPE("Target Definition");
      instance_1.setTYPE("TARGET");
      instanceList.add(instance_1);
      INSTANCE instance_2 = new INSTANCE();
      instance_2.setDBDNAME(sourcedbname);
      instance_2.setDESCRIPTION("");
      instance_2.setNAME(sourcename);
      instance_2.setTRANSFORMATIONNAME(sourcename);
      instance_2.setTRANSFORMATIONTYPE("SOURCE Definition");
      instance_2.setTYPE("SOURCE");
      instanceList.add(instance_2);
      INSTANCE instance_3 = new INSTANCE();
      instance_3.setDBDNAME("DBD_Tab_Seed");
      instance_3.setDESCRIPTION("");
      instance_3.setNAME("TAB_SEED");
      instance_3.setTRANSFORMATIONNAME("TAB_SEED");
      instance_3.setTRANSFORMATIONTYPE("SOURCE Definition");
      instance_3.setTYPE("SOURCE");
      instanceList.add(instance_3);
      if (mapplet != null && mapplet.size() > 0) {
         for(int i = 0; i < mapplet.size(); ++i) {
            INSTANCE instance = new INSTANCE();
            instance.setDESCRIPTION("");
            instance.setNAME(((MappletBean)mapplet.get(i)).getMappletname());
            instance.setREUSABLE("YES");
            instance.setTRANSFORMATIONNAME(((MappletBean)mapplet.get(i)).getMappletname());
            instance.setTRANSFORMATIONTYPE("Mapplet");
            instance.setTYPE("MAPPLET");
            instanceList.add(instance);
         }
      }

      INSTANCE instance_4 = new INSTANCE();
      instance_4.setDESCRIPTION("");
      instance_4.setNAME("EXPTRANS");
      instance_4.setTRANSFORMATIONNAME("EXPTRANS");
      instance_4.setTRANSFORMATIONTYPE("Expression");
      instance_4.setTYPE("TRANSFORMATION");
      instance_4.setREUSABLE("NO");
      instanceList.add(instance_4);
      INSTANCE instance_5 = new INSTANCE();
      instance_5.setDESCRIPTION("");
      instance_5.setNAME("JNRTRANS");
      instance_5.setTRANSFORMATIONNAME("JNRTRANS");
      instance_5.setTRANSFORMATIONTYPE("Joiner");
      instance_5.setTYPE("TRANSFORMATION");
      instance_5.setREUSABLE("NO");
      instanceList.add(instance_5);
      INSTANCE instance_6 = new INSTANCE();
      instance_6.setDESCRIPTION("");
      instance_6.setNAME(sq_name);
      instance_6.setREUSABLE("NO");
      instance_6.setTRANSFORMATIONNAME(sq_name);
      instance_6.setTRANSFORMATIONTYPE("Source Qualifier");
      instance_6.setTYPE("TRANSFORMATION");
      ASSOCIATEDSOURCEINSTANCE associatedsourceinstance = new ASSOCIATEDSOURCEINSTANCE();
      associatedsourceinstance.setNAME(sourcename);
      instance_6.getASSOCIATEDSOURCEINSTANCE().add(associatedsourceinstance);
      instanceList.add(instance_6);
      INSTANCE instance_7 = new INSTANCE();
      instance_7.setDESCRIPTION("");
      instance_7.setNAME("SQ_Tab_Seed");
      instance_7.setREUSABLE("NO");
      instance_7.setTRANSFORMATIONNAME("SQ_Tab_Seed");
      instance_7.setTRANSFORMATIONTYPE("Source Qualifier");
      instance_7.setTYPE("TRANSFORMATION");
      ASSOCIATEDSOURCEINSTANCE associatedsourceinstance1 = new ASSOCIATEDSOURCEINSTANCE();
      associatedsourceinstance1.setNAME("Tab_Seed");
      instance_7.getASSOCIATEDSOURCEINSTANCE().add(associatedsourceinstance1);
      instanceList.add(instance_7);
      return instanceList;
   }

   public List<CONNECTOR> getConvertTableInfoToCONNECTOR(List<String> columnList, List<MappletBean> mapplet, String sq_name, String sourcename, String targetname) throws Exception {
      List mappingConnectorlist = new ArrayList();
      new HashMap();

      for(int i = 0; i < columnList.size(); ++i) {
         CONNECTOR connectOr = new CONNECTOR();
         connectOr.setFROMFIELD((String)columnList.get(i));
         connectOr.setFROMINSTANCE(sourcename);
         connectOr.setFROMINSTANCETYPE("Source Definition");
         connectOr.setTOFIELD((String)columnList.get(i));
         connectOr.setTOINSTANCE(sq_name);
         connectOr.setTOINSTANCETYPE("Source Qualifier");
         mappingConnectorlist.add(connectOr);
      }

      CONNECTOR connectOr2 = new CONNECTOR();
      connectOr2.setFROMFIELD("SEED_COL");
      connectOr2.setFROMINSTANCE("TAB_SEED");
      connectOr2.setFROMINSTANCETYPE("Source Definition");
      connectOr2.setTOFIELD("SEED_COL");
      connectOr2.setTOINSTANCE("SQ_Tab_Seed");
      connectOr2.setTOINSTANCETYPE("Source Qualifier");
      mappingConnectorlist.add(connectOr2);

      for(int i = 0; i < columnList.size(); ++i) {
         CONNECTOR connectOr = new CONNECTOR();
         connectOr.setFROMFIELD((String)columnList.get(i));
         connectOr.setFROMINSTANCE(sq_name);
         connectOr.setFROMINSTANCETYPE("Source Qualifier");
         connectOr.setTOFIELD((String)columnList.get(i));
         connectOr.setTOINSTANCE("EXPTRANS");
         connectOr.setTOINSTANCETYPE("Expression");
         mappingConnectorlist.add(connectOr);
      }

      for(int i = 0; i < columnList.size(); ++i) {
         CONNECTOR connectOr = new CONNECTOR();
         connectOr.setFROMFIELD((String)columnList.get(i));
         connectOr.setFROMINSTANCE("EXPTRANS");
         connectOr.setFROMINSTANCETYPE("Expression");
         connectOr.setTOFIELD((String)columnList.get(i));
         connectOr.setTOINSTANCE("JNRTRANS");
         connectOr.setTOINSTANCETYPE("Joiner");
         mappingConnectorlist.add(connectOr);
      }

      CONNECTOR connectOr3 = new CONNECTOR();
      connectOr3.setFROMFIELD("Dummy_col");
      connectOr3.setFROMINSTANCE("EXPTRANS");
      connectOr3.setFROMINSTANCETYPE("Expression");
      connectOr3.setTOFIELD("Dummy_col");
      connectOr3.setTOINSTANCE("JNRTRANS");
      connectOr3.setTOINSTANCETYPE("Joiner");
      mappingConnectorlist.add(connectOr3);
      CONNECTOR connectOr4 = new CONNECTOR();
      connectOr4.setFROMFIELD("SEED_COL");
      connectOr4.setFROMINSTANCE("SQ_Tab_Seed");
      connectOr4.setFROMINSTANCETYPE("Source Qualifier");
      connectOr4.setTOFIELD("SEED_COL");
      connectOr4.setTOINSTANCE("JNRTRANS");
      connectOr4.setTOINSTANCETYPE("Joiner");
      mappingConnectorlist.add(connectOr4);
      List<String> list = new ArrayList();
      if (mapplet != null && mapplet.size() > 0) {
         for(int i = 0; i < mapplet.size(); ++i) {
            list.add(((MappletBean)mapplet.get(i)).getColumnname());
         }
      }

      for(int i = 0; i < columnList.size(); ++i) {
         if (!list.contains(columnList.get(i))) {
            CONNECTOR connectOr = new CONNECTOR();
            connectOr.setFROMFIELD((String)columnList.get(i));
            connectOr.setFROMINSTANCE("JNRTRANS");
            connectOr.setFROMINSTANCETYPE("Joiner");
            connectOr.setTOFIELD((String)columnList.get(i));
            connectOr.setTOINSTANCE(targetname);
            connectOr.setTOINSTANCETYPE("Target Definition");
            mappingConnectorlist.add(connectOr);
         }
      }

      if (mapplet != null && mapplet.size() > 0) {
         for(int i = 0; i < mapplet.size(); ++i) {
            if (i == 0) {
               CONNECTOR connectOr = new CONNECTOR();
               connectOr.setFROMFIELD(((MappletBean)mapplet.get(i)).getColumnname());
               connectOr.setFROMINSTANCE("JNRTRANS");
               connectOr.setFROMINSTANCETYPE("Joiner");
               connectOr.setTOFIELD("IN_PARAM");
               connectOr.setTOINSTANCE(((MappletBean)mapplet.get(i)).getMappletname());
               connectOr.setTOINSTANCETYPE("Mapplet");
               mappingConnectorlist.add(connectOr);
               CONNECTOR connectOr1 = new CONNECTOR();
               connectOr1.setFROMFIELD("out_IN_PARAM");
               connectOr1.setFROMINSTANCE(((MappletBean)mapplet.get(i)).getMappletname());
               connectOr1.setFROMINSTANCETYPE("Mapplet");
               connectOr1.setTOFIELD(((MappletBean)mapplet.get(i)).getColumnname());
               connectOr1.setTOINSTANCE(targetname);
               connectOr1.setTOINSTANCETYPE("Target Definition");
               mappingConnectorlist.add(connectOr1);
            }
         }
      }

      return mappingConnectorlist;
   }
}
