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

import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.CONNECTOR;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.GROUP;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.INITPROP;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.INSTANCE;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.MAPPLET;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.METADATAEXTENSION;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.TABLEATTRIBUTE;
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.basic.xmlbean.TRANSFORMFIELDATTR;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.TRANSFORMFIELDATTRDEF;
import com.chenyang.nse.bussiness.engine.infa.powercenter961.pubbean.MaskPrepertyBean;
import com.chenyang.nse.bussiness.engine.infa.powercenter961.pubbean.MaskPrepertyReplaceStringbean;
import com.chenyang.nse.bussiness.engine.infa.powercenter961.pubbean.MaskPrepertyStringBean;
import java.util.ArrayList;
import java.util.List;

public class MappletMakingUtil {
   public MAPPLET getMapplet(String mplt_name, String DataType, String precision, String scale, MaskPrepertyBean mb, MaskPrepertyStringBean msb, MaskPrepertyReplaceStringbean mspb) {
      MAPPLET mapplet = new MAPPLET();
      mapplet.setCRCVALUE("");
      mapplet.setDESCRIPTION("");
      mapplet.setISVALID("YES");
      mapplet.setNAME(mplt_name);
      mapplet.setOBJECTVERSION("1");
      mapplet.setVERSIONNUMBER("1");

      for(TRANSFORMATION tranlist : this.getTransformation(DataType, precision, scale, mb, mplt_name, msb, mspb)) {
         mapplet.getTRANSFORMATION().add(tranlist);
      }

      for(INSTANCE in : this.getIns()) {
         mapplet.getINSTANCE().add(in);
      }

      for(CONNECTOR connect : this.getConnector()) {
         mapplet.getCONNECTOR().add(connect);
      }

      return mapplet;
   }

   private List<TRANSFORMATION> getTransformation(String DataType, String precision, String scale, MaskPrepertyBean mb, String mplt_name, MaskPrepertyStringBean msb, MaskPrepertyReplaceStringbean mspb) {
      List<TRANSFORMATION> tran = new ArrayList();
      TRANSFORMATION t1 = new TRANSFORMATION();
      TRANSFORMATION t2 = new TRANSFORMATION();
      TRANSFORMATION t3 = new TRANSFORMATION();
      TRANSFORMATION t4 = new TRANSFORMATION();
      t1.setCOMPONENTVERSION("5000000");
      t1.setNAME("Data_Masking");
      t1.setDESCRIPTION("");
      t1.setOBJECTVERSION("1");
      t1.setREUSABLE("NO");
      t1.setTEMPLATEID("402300");
      t1.setTEMPLATENAME("Data Masking");
      t1.setTYPE("Custom Transformation");
      t1.setVERSIONNUMBER("1");

      for(GROUP g : this.getGroup()) {
         t1.getGROUP().add(g);
      }

      TRANSFORMFIELD t1tf1 = new TRANSFORMFIELD();
      t1tf1.setDATATYPE(DataType);
      t1tf1.setDEFAULTVALUE("");
      t1tf1.setDESCRIPTION("");
      t1tf1.setGROUP("DM_INPUT_GROUP");
      t1tf1.setNAME("IN_PARAM");
      t1tf1.setOUTPUTGROUP("DM_INPUT_GROUP");
      t1tf1.setPICTURETEXT("");
      t1tf1.setPORTTYPE("INPUT");
      t1tf1.setPRECISION(precision);
      t1tf1.setSCALE(scale);
      if (mb.getExp() != null && !"".equals(mb.getExp())) {
         TRANSFORMFIELDATTR transfor = new TRANSFORMFIELDATTR();
         transfor.setNAME("DataMaskingT_PORT_EXPRESSION_ATTRIBUTE");
         transfor.setVALUE(mb.getExp());
         t1tf1.getTRANSFORMFIELDATTR().add(transfor);
      }

      TRANSFORMFIELD t1tf2 = new TRANSFORMFIELD();
      t1tf2.setDATATYPE(DataType);
      t1tf2.setDEFAULTVALUE("");
      t1tf2.setDESCRIPTION("");
      t1tf2.setGROUP("DM_OUTPUT_GROUP");
      t1tf2.setNAME("out_IN_PARAM");
      t1tf2.setOUTPUTGROUP("DM_OUTPUT_GROUP");
      t1tf2.setPICTURETEXT("");
      t1tf2.setPORTTYPE("OUTPUT");
      t1tf2.setPRECISION(precision);
      t1tf2.setSCALE(scale);
      t1tf2.setDATATYPE(DataType);
      t1.getTRANSFORMFIELD().add(t1tf1);
      t1.getTRANSFORMFIELD().add(t1tf2);
      String[] attruInfo1 = new String[]{"Language@C++", "Module Identifier@pmdatamasking", "Class Name@IDMCTPluginDriver", "Function Identifier@", "Runtime Location@", "Tracing Level@Normal", "Is Partitionable@Across Grid", "Inputs Must Block@NO", "Is Active@NO", "Update Strategy Transformation@NO", "Transformation Scope@Row", "Generate Transaction@NO", "Output Is Repeatable@Based On Input Order", "Requires Single Thread Per Partition@YES", "Output Is Deterministic@YES", "Preserves Data Set Boundary@No"};
      String[] tmpAttr = null;
      TABLEATTRIBUTE[] attribute1 = new TABLEATTRIBUTE[attruInfo1.length];

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

         t1.getTABLEATTRIBUTE().add(attribute1[i]);
      }

      INITPROP i1 = new INITPROP();
      i1.setDESCRIPTION("");
      i1.setNAME("Programmatic Identifier for Class Factory");
      i1.setUSERDEFINED("NO");
      i1.setVALUE("");
      INITPROP i2 = new INITPROP();
      i2.setDESCRIPTION("");
      i2.setNAME("Constructor");
      i2.setUSERDEFINED("NO");
      i2.setVALUE("");
      t1.getINITPROP().add(i1);
      t1.getINITPROP().add(i2);

      for(METADATAEXTENSION mt : this.getMetadataextension(mb, msb, mspb)) {
         t1.getMETADATAEXTENSION().add(mt);
      }

      List<TRANSFORMFIELDATTRDEF> trlist = new ArrayList();
      TRANSFORMFIELDATTRDEF tr1 = new TRANSFORMFIELDATTRDEF();
      tr1.setDATATYPE("STRING");
      tr1.setDESCRIPTION("");
      tr1.setNAME("DataMaskingT_PORT_EXPRESSION_ATTRIBUTE");
      tr1.setORDER("0");
      tr1.setTYPE("STRING");
      TRANSFORMFIELDATTRDEF tr2 = new TRANSFORMFIELDATTRDEF();
      tr2.setDATATYPE("STRING");
      tr2.setDESCRIPTION("");
      tr2.setDEFAULTVALUE("0");
      tr2.setNAME("DataMaskingT_PORT_EXPRESSION_ISKEYMASKED_ATTRIBUTE");
      tr2.setORDER("1");
      tr2.setTYPE("NUMERIC");
      TRANSFORMFIELDATTRDEF tr3 = new TRANSFORMFIELDATTRDEF();
      tr3.setDATATYPE("STRING");
      tr3.setDESCRIPTION("");
      tr3.setNAME("DataMaskingT_PORT_EXPRESSION_STRSEEDVALUE_ATTRIBUTE");
      tr3.setORDER("2");
      tr3.setTYPE("STRING");
      trlist.add(tr1);
      trlist.add(tr2);
      trlist.add(tr3);

      for(TRANSFORMFIELDATTRDEF trans : trlist) {
         t1.getTRANSFORMFIELDATTRDEF().add(trans);
      }

      t2.setDESCRIPTION("");
      t2.setNAME("INPUT");
      t2.setOBJECTVERSION("1");
      t2.setREUSABLE("NO");
      t2.setTYPE("INPUT Transformation");
      t2.setVERSIONNUMBER("1");
      TRANSFORMFIELD t2tf1 = new TRANSFORMFIELD();
      t2tf1.setDATATYPE("string");
      t2tf1.setDEFAULTVALUE("");
      t2tf1.setDESCRIPTION("");
      t2tf1.setNAME("IN_PARAM");
      t2tf1.setPICTURETEXT("");
      t2tf1.setPORTTYPE("OUTPUT");
      t2tf1.setPRECISION(precision);
      t2tf1.setSCALE(scale);
      t2.getTRANSFORMFIELD().add(t2tf1);
      t3.setDESCRIPTION("");
      t3.setNAME("OUTPUT");
      t3.setOBJECTVERSION("1");
      t3.setREUSABLE("NO");
      t3.setTYPE("Output Transformation");
      t3.setVERSIONNUMBER("1");
      TRANSFORMFIELD t3tf1 = new TRANSFORMFIELD();
      t3tf1.setDATATYPE("string");
      t3tf1.setDEFAULTVALUE("");
      t3tf1.setDESCRIPTION("");
      t3tf1.setNAME("out_IN_PARAM");
      t3tf1.setPICTURETEXT("");
      t3tf1.setPORTTYPE("INPUT");
      t3tf1.setPRECISION(precision);
      t3tf1.setSCALE(scale);
      t3.getTRANSFORMFIELD().add(t3tf1);
      t4.setDESCRIPTION("");
      t4.setNAME(mplt_name);
      t4.setOBJECTVERSION("1");
      t4.setREUSABLE("YES");
      t4.setTYPE("Mapplet");
      t4.setVERSIONNUMBER("1");
      List<TRANSFORMFIELD> tr2list = new ArrayList();
      TRANSFORMFIELD t4tf1 = new TRANSFORMFIELD();
      t4tf1.setDATATYPE(DataType);
      t4tf1.setDEFAULTVALUE("");
      t4tf1.setDESCRIPTION("");
      t4tf1.setNAME("IN_PARAM");
      t4tf1.setPICTURETEXT("");
      t4tf1.setPORTTYPE("INPUT");
      t4tf1.setPRECISION(precision);
      t4tf1.setSCALE(scale);
      t4tf1.setMAPPLETGROUP("INPUT");
      t4tf1.setREFFIELD("IN_PARAM");
      t4tf1.setREFINSTANCETYPE("Input Transformation");
      TRANSFORMFIELD t4tf2 = new TRANSFORMFIELD();
      t4tf2.setDATATYPE(DataType);
      t4tf2.setDEFAULTVALUE("");
      t4tf2.setDESCRIPTION("");
      t4tf2.setNAME("out_IN_PARAM");
      t4tf2.setPICTURETEXT("");
      t4tf2.setPORTTYPE("OUTPUT");
      t4tf2.setPRECISION(precision);
      t4tf2.setSCALE(scale);
      t4tf2.setMAPPLETGROUP("OUTPUT");
      t4tf2.setREFFIELD("out_IN_PARAM");
      t4tf2.setREFINSTANCETYPE("Output Transformation");
      tr2list.add(t4tf1);
      tr2list.add(t4tf2);

      for(TRANSFORMFIELD trans4 : tr2list) {
         t4.getTRANSFORMFIELD().add(trans4);
      }

      String[] attruInfo4 = new String[]{"Is Active@NO", "Is Partitionable@NO", "Form Name@"};
      String[] tmpAttr4 = null;
      TABLEATTRIBUTE[] attribute4 = new TABLEATTRIBUTE[attruInfo4.length];

      for(int i = 0; i < attruInfo4.length; ++i) {
         tmpAttr4 = attruInfo4[i].split("@");
         attribute4[i] = new TABLEATTRIBUTE();
         attribute4[i].setNAME(tmpAttr4[0]);
         if (tmpAttr4.length > 1) {
            attribute4[i].setVALUE(tmpAttr4[1]);
         } else {
            attribute4[i].setVALUE("");
         }

         t4.getTABLEATTRIBUTE().add(attribute4[i]);
      }

      tran.add(t1);
      tran.add(t2);
      tran.add(t3);
      tran.add(t4);
      return tran;
   }

   public List<INSTANCE> getIns() {
      List<INSTANCE> inlist = new ArrayList();
      INSTANCE i1 = new INSTANCE();
      i1.setNAME("Data_Masking");
      i1.setDESCRIPTION("");
      i1.setREUSABLE("NO");
      i1.setTRANSFORMATIONNAME("Data_Masking");
      i1.setTRANSFORMATIONTYPE("Custom Transformation");
      i1.setTYPE("TRANSFORMATION");
      INSTANCE i2 = new INSTANCE();
      i2.setNAME("INPUT");
      i2.setDESCRIPTION("");
      i2.setREUSABLE("NO");
      i2.setTRANSFORMATIONNAME("INPUT");
      i2.setTRANSFORMATIONTYPE("Input Transformation");
      i2.setTYPE("TRANSFORMATION");
      INSTANCE i3 = new INSTANCE();
      i3.setNAME("OUTPUT");
      i3.setDESCRIPTION("");
      i3.setREUSABLE("NO");
      i3.setTRANSFORMATIONNAME("OUTPUT");
      i3.setTRANSFORMATIONTYPE("Output Transformation");
      i3.setTYPE("TRANSFORMATION");
      inlist.add(i1);
      inlist.add(i2);
      inlist.add(i3);
      return inlist;
   }

   public List<CONNECTOR> getConnector() {
      List<CONNECTOR> conn = new ArrayList();
      CONNECTOR c1 = new CONNECTOR();
      c1.setFROMFIELD("out_IN_PARAM");
      c1.setFROMINSTANCE("Data_Masking");
      c1.setFROMINSTANCETYPE("Custom Transformation");
      c1.setTOFIELD("out_IN_PARAM");
      c1.setTOINSTANCE("OUTPUT");
      c1.setTOINSTANCETYPE("Output Transformation");
      CONNECTOR c2 = new CONNECTOR();
      c2.setFROMFIELD("IN_PARAM");
      c2.setFROMINSTANCE("INPUT");
      c2.setFROMINSTANCETYPE("Input Transformation");
      c2.setTOFIELD("IN_PARAM");
      c2.setTOINSTANCE("Data_Masking");
      c2.setTOINSTANCETYPE("Custom Transformation");
      conn.add(c1);
      conn.add(c2);
      return conn;
   }

   private List<GROUP> getGroup() {
      List<GROUP> group = new ArrayList();
      GROUP g1 = new GROUP();
      g1.setDESCRIPTION("");
      g1.setNAME("DM_INPUT_GROUP");
      g1.setORDER("1");
      g1.setTYPE("INPUT");
      GROUP g2 = new GROUP();
      g2.setDESCRIPTION("");
      g2.setNAME("DM_OUTPUT_GROUP");
      g2.setORDER("2");
      g2.setTYPE("OUTPUT");
      group.add(g1);
      group.add(g2);
      return group;
   }

   private List<METADATAEXTENSION> getMetadataextension(MaskPrepertyBean mb, MaskPrepertyStringBean msb, MaskPrepertyReplaceStringbean mspb) {
      List<METADATAEXTENSION> list = new ArrayList();
      METADATAEXTENSION m1 = new METADATAEXTENSION();
      m1.setCOMPONENTVERSION("5000000");
      m1.setDATATYPE("STRING");
      m1.setDESCRIPTION("");
      m1.setDOMAINNAME("Data Masking Domain");
      m1.setISCLIENTEDITABLE("NO");
      m1.setISREUSABLE("YES");
      m1.setISSHAREREAD("YES");
      m1.setISSHAREWRITE("YES");
      m1.setISCLIENTVISIBLE("YES");
      m1.setMAXLENGTH("5000");
      m1.setNAME("Code Pages");
      String value = "Test code page, Shift-JIS with European characters|9999|Hong Kong Supplementary Character Set|9200|IBM EBCDIC US English|2028|IBM EBCDIC German|2030|IBM EBCDIC Italian|2035|IBM EBCDIC UK English|2038|IBM EBCDIC French|2040|IBM EBCDIC International Latin-1|2044|IBM EBCDIC Korean CP833|833|IBM EBCDIC Korean CP834|834|IBM 850|850|IBM EBCDIC Japanese|930|IBM EBCDIC Korean CP933|933|IBM EBCDIC Simplified Chinese|935|IBM EBCDIC Traditional Chinese|937|IBM EBCDIC Japanese CP939|939|ISO 8859-10 Latin 6 (Nordic)|13|ISO 8859-15 Latin 9 (Western European)|201|ISO 8859-2 Eastern European|5|ISO 8859-3 Southeast European|6|ISO 8859-4 Baltic|7|ISO 8859-5 Cyrillic|8|ISO 8859-6 Arabic|9|ISO 8859-7 Greek|10|ISO 8859-8 Hebrew|11|ISO 8859-9 Latin 5 (Turkish)|12|Japanese Extended UNIX Code (including JIS X 0212)|18|Japanese EBCDIC Fujitsu|9000|Japanese EBCDIC-Kana Fujitsu|9005|NEC ACOS JIPSE Japanese|9002|NEC ACOS JIPSE-Kana Japanese|9007|HITACHI KEIS Japanese|9001|HITACHI KEIS-Kana Japanese|9006|ISO 8859-1 Western European|4|MITSUBISHI MELCOM Japanese|9004|MITSUBISHI MELCOM-Kana Japanese|9009|MS Windows Latin 2 (Central Europe)|2250|MS Windows Cyrillic (Slavic)|2251|MS Windows Latin1 (ANSI), superset of Latin1|2252|MS Windows Greek|2253|MS Windows Latin 5 (Turkish), superset of ISO 8859-9|2254|MS Windows Hebrew|2255|MS Windows Arabic|2256|MS Windows Baltic Rim|2257|MS Windows Vietnamese|2258|MS Windows Korean (Johab)|1361|MS-DOS Thai, superset of TIS 620|874|MS Windows Japanese, Shift-JIS|2024|MS Windows Simplified Chinese, superset of GB 2312-80| EUC encoding,936|MS Windows Korean, superset of KS C 5601-1992|949|MS Windows Traditional Chinese, superset of Big 5|950|UNISYS Japanese|9003|UNISYS-Kana Japanese|9008|7-bit ASCII|1|Unicode Transformation Format, multibyte|106";
      m1.setVALUE(value);
      m1.setVENDORNAME("INFORMATICA");
      METADATAEXTENSION m2 = new METADATAEXTENSION();
      m2.setCOMPONENTVERSION("5000000");
      m2.setDATATYPE("XML");
      m2.setDESCRIPTION("");
      m2.setDOMAINNAME("Data Masking Domain");
      m2.setISCLIENTEDITABLE("NO");
      m2.setISREUSABLE("YES");
      m2.setISSHAREREAD("YES");
      m2.setISSHAREWRITE("YES");
      m2.setISCLIENTVISIBLE("NO");
      m2.setMAXLENGTH("50000000");
      m2.setNAME("Masking Rules");
      m2.setVALUE(this.getM2Value(mb, msb, mspb));
      m2.setVENDORNAME("INFORMATICA");
      METADATAEXTENSION m3 = new METADATAEXTENSION();
      m3.setCOMPONENTVERSION("5000000");
      m3.setDATATYPE("STRING");
      m3.setDESCRIPTION("");
      m3.setDOMAINNAME("Data Masking Domain");
      m3.setISCLIENTEDITABLE("NO");
      m3.setISREUSABLE("YES");
      m3.setISSHAREREAD("YES");
      m3.setISSHAREWRITE("YES");
      m3.setISCLIENTVISIBLE("YES");
      m3.setMAXLENGTH("512");
      m3.setNAME("SSN High Group file");
      m3.setVALUE("$PMSourceFileDir/highgroup.txt");
      m3.setVENDORNAME("INFORMATICA");
      list.add(m1);
      list.add(m2);
      list.add(m3);
      return list;
   }

   private String getM2Value(MaskPrepertyBean mb, MaskPrepertyStringBean msb, MaskPrepertyReplaceStringbean mspb) {
      MaskPropertyUtil mu = new MaskPropertyUtil();
      if ("intergerno".equals(mb.getFlag())) {
         return mu.integerRandomNoMasking();
      } else if ("integerrandom".equals(mb.getFlag())) {
         return mu.getIntegerRandomString(mb);
      } else if ("integerexp".equals(mb.getFlag())) {
         return mu.getIntegerExpString();
      } else if ("doublerandom".equals(mb.getFlag())) {
         return mu.getDoubleRandomString(mb);
      } else {
         return "stringrandom".equals(mb.getFlag()) ? mu.getStringRandomString(msb) : mu.getStringReplaceString(mspb);
      }
   }
}
