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

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.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.maskdata.ParamBean;
import com.chenyang.nse.bussiness.engine.infa.powercenter961.pubbean.MappingBean;
import com.chenyang.nse.bussiness.engine.infa.powercenter961.pubbean.MappletMakeDatabean;
import com.chenyang.nse.bussiness.engine.infa.powercenter961.pubtool.StringUtil;
import java.util.ArrayList;
import java.util.List;

public class ChildMappletMappingUtil {
   public MAPPING getChildMapping(String mappingname, List<MappingBean> mb, ParamBean par_bean) throws Exception {
      MAPPING mapping = new MAPPING();
      mapping.setDESCRIPTION("");
      mapping.setISVALID("YES");
      mapping.setNAME(mappingname);
      mapping.setOBJECTVERSION("1");
      mapping.setVERSIONNUMBER("1");
      if (mb != null && mb.size() > 0) {
         for(int i = 0; i < mb.size(); ++i) {
            mapping.getTRANSFORMATION().add(this.getTRANSFORMATIONXml(((MappingBean)mb.get(i)).getSq_name(), ((MappingBean)mb.get(i)).getCb().getNameList(), ((MappingBean)mb.get(i)).getCb().getMappingTypeList(), ((MappingBean)mb.get(i)).getCb().getMappingLengthList(), (String)par_bean.getQuerymap().get(((MappingBean)mb.get(i)).getSource_name())));
         }

         for(int i = 0; i < mb.size(); ++i) {
            mapping.getTRANSFORMATION().add(this.getTRANSFORMATIONSRTXml((MappingBean)mb.get(i), i));
         }

         List<TRANSFORMATION> getlist = this.getTRANSFORMATIONFix(mb);

         for(int i = 0; i < getlist.size(); ++i) {
            mapping.getTRANSFORMATION().add(getlist.get(i));
         }

         List<MappingBean> mbt = new ArrayList();

         for(int i = 0; i < mb.size(); ++i) {
            if (i > 0) {
               mbt.add(mb.get(i));
            }
         }

         for(int i = 0; i < mbt.size(); ++i) {
            mapping.getTRANSFORMATION().add(this.getTRANSFORMATIONJNRXml(((MappingBean)mbt.get(i)).getCb().getNameList(), ((MappingBean)mbt.get(i)).getCb().getMappingTypeList(), ((MappingBean)mbt.get(i)).getCb().getKeytype(), ((MappingBean)mbt.get(i)).getCb().getMappingLengthList(), i));
         }
      }

      if (mb != null && mb.size() > 0) {
         for(int i = 0; i < mb.size(); ++i) {
            for(INSTANCE instance : this.getConvertTableInfoToINSTANCE(((MappingBean)mb.get(i)).getSq_name(), ((MappingBean)mb.get(i)).getSource_name(), ((MappingBean)mb.get(i)).getTarget_name(), ((MappingBean)mb.get(i)).getSourcedbname())) {
               mapping.getINSTANCE().add(instance);
            }
         }
      }

      for(INSTANCE instance : this.getSourceINSTANCE(mb)) {
         mapping.getINSTANCE().add(instance);
      }

      for(INSTANCE instance : this.getTargetINSTANCE(mb)) {
         mapping.getINSTANCE().add(instance);
      }

      for(INSTANCE instance : this.getOtherINSTANCE(mb)) {
         mapping.getINSTANCE().add(instance);
      }

      if (mb != null && mb.size() > 0) {
         for(int i = 0; i < mb.size(); ++i) {
            for(CONNECTOR connector : this.getConvertTableInfoToCONNECTOR(((MappingBean)mb.get(i)).getCb().getNameList(), ((MappingBean)mb.get(i)).getSq_name(), ((MappingBean)mb.get(i)).getSource_name(), ((MappingBean)mb.get(i)).getTarget_name())) {
               mapping.getCONNECTOR().add(connector);
            }
         }
      }

      for(CONNECTOR connector : this.getOtherCONNECTOR(mb)) {
         mapping.getCONNECTOR().add(connector);
      }

      if (mb != null && mb.size() > 0) {
         for(int i = 0; i < mb.size(); ++i) {
            TARGETLOADORDER targetloadorder = new TARGETLOADORDER();
            targetloadorder.setORDER(i + 1 + "");
            targetloadorder.setTARGETINSTANCE(((MappingBean)mb.get(i)).getTarget_name());
            mapping.getTARGETLOADORDER().add(targetloadorder);
            TARGETLOADORDER targetloadorder1 = new TARGETLOADORDER();
            targetloadorder1.setORDER(i + 1 + "");
            targetloadorder1.setTARGETINSTANCE("target_tab_candidate" + i);
            mapping.getTARGETLOADORDER().add(targetloadorder1);
         }
      }

      ERPINFO erpinfo = new ERPINFO();
      mapping.getERPINFO().add(erpinfo);
      return mapping;
   }

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

      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);
      }

      return mappingConnectorlist;
   }

   public List<CONNECTOR> getOtherCONNECTOR(List<MappingBean> mb) {
      List mappingConnectorlist = new ArrayList();
      List<MappingBean> mtb = new ArrayList();

      for(int i = 0; i < mb.size(); ++i) {
         if (i > 0) {
            mtb.add(mb.get(i));
         }
      }

      for(int i = 1; i < mtb.size() + 1; ++i) {
         List<String> columnlist = new ArrayList();
         if (((MappingBean)mtb.get(i - 1)).getMappletbean() != null) {
            for(int k = 0; k < ((MappingBean)mtb.get(i - 1)).getMappletbean().size(); ++k) {
               columnlist.add(((MappletMakeDatabean)((MappingBean)mtb.get(i - 1)).getMappletbean().get(k)).getColumnname());
            }
         }

         for(int j = 1; j < ((MappingBean)mtb.get(i - 1)).getCb().getTypeList().size() + 1; ++j) {
            if (!columnlist.contains(((MappingBean)mtb.get(i - 1)).getCb().getNameList().get(j - 1))) {
               CONNECTOR connectOr5 = new CONNECTOR();
               connectOr5.setFROMFIELD((String)((MappingBean)mtb.get(i - 1)).getCb().getNameList().get(j - 1));
               connectOr5.setFROMINSTANCE("JNRTRANS" + i);
               connectOr5.setFROMINSTANCETYPE("Joiner");
               connectOr5.setTOFIELD((String)((MappingBean)mtb.get(i - 1)).getCb().getNameList().get(j - 1));
               connectOr5.setTOINSTANCE(((MappingBean)mtb.get(i - 1)).getTarget_name());
               connectOr5.setTOINSTANCETYPE("Target Definition");
               mappingConnectorlist.add(connectOr5);
            }
         }

         columnlist.clear();
      }

      List<String> mappletnamelist = new ArrayList();

      for(int i = 0; i < mb.size(); ++i) {
         if (((MappingBean)mb.get(i)).getMappletbean() != null && ((MappingBean)mb.get(i)).getMappletbean().size() > 0) {
            for(int j = 0; j < ((MappingBean)mb.get(i)).getMappletbean().size(); ++j) {
               if (i == 0 && j == 0) {
                  CONNECTOR connectOr = new CONNECTOR();
                  connectOr.setFROMFIELD(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getColumnname());
                  connectOr.setTOFIELD("in_name");
                  connectOr.setFROMINSTANCE(((MappingBean)mb.get(i)).getSq_name());
                  connectOr.setFROMINSTANCETYPE("Source Qualifier");
                  connectOr.setTOINSTANCE(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getMappletname());
                  connectOr.setTOINSTANCETYPE("Mapplet");
                  mappingConnectorlist.add(connectOr);
                  CONNECTOR connectOr1 = new CONNECTOR();
                  connectOr1.setFROMFIELD("out_name");
                  connectOr1.setTOFIELD(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getColumnname());
                  connectOr1.setFROMINSTANCE(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getMappletname());
                  connectOr1.setFROMINSTANCETYPE("Mapplet");
                  connectOr1.setTOINSTANCE(((MappingBean)mb.get(i)).getTarget_name());
                  connectOr1.setTOINSTANCETYPE("Target Definition");
                  mappingConnectorlist.add(connectOr1);
                  mappletnamelist.add(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getMappletname());
               } else if (i == 0 && mappletnamelist.contains(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getMappletname())) {
                  CONNECTOR connectOr = new CONNECTOR();
                  connectOr.setFROMFIELD(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getColumnname());
                  connectOr.setTOFIELD("in_name");
                  connectOr.setFROMINSTANCE(((MappingBean)mb.get(i)).getSq_name());
                  connectOr.setFROMINSTANCETYPE("Source Qualifier");
                  connectOr.setTOINSTANCE(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getMappletname());
                  connectOr.setTOINSTANCETYPE("Mapplet");
                  mappingConnectorlist.add(connectOr);
                  CONNECTOR connectOr1 = new CONNECTOR();
                  connectOr1.setFROMFIELD("out_name");
                  connectOr1.setTOFIELD(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getColumnname());
                  connectOr1.setFROMINSTANCE(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getMappletname());
                  connectOr1.setFROMINSTANCETYPE("Mapplet");
                  connectOr1.setTOINSTANCE(((MappingBean)mb.get(0)).getTarget_name());
                  connectOr1.setTOINSTANCETYPE("Target Definition");
                  mappingConnectorlist.add(connectOr1);
               } else if (i == 0 && !mappletnamelist.contains(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getMappletname())) {
                  CONNECTOR connectOr = new CONNECTOR();
                  connectOr.setFROMFIELD(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getColumnname());
                  connectOr.setTOFIELD("in_name");
                  connectOr.setFROMINSTANCE(((MappingBean)mb.get(i)).getSq_name());
                  connectOr.setFROMINSTANCETYPE("Source Qualifier");
                  connectOr.setTOINSTANCE(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getMappletname());
                  connectOr.setTOINSTANCETYPE("Mapplet");
                  mappingConnectorlist.add(connectOr);
                  CONNECTOR connectOr1 = new CONNECTOR();
                  connectOr1.setFROMFIELD("out_name");
                  connectOr1.setTOFIELD(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getColumnname());
                  connectOr1.setFROMINSTANCE(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getMappletname());
                  connectOr1.setFROMINSTANCETYPE("Mapplet");
                  connectOr1.setTOINSTANCE(((MappingBean)mb.get(0)).getTarget_name());
                  connectOr1.setTOINSTANCETYPE("Target Definition");
                  mappingConnectorlist.add(connectOr1);
                  mappletnamelist.add(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getMappletname());
               } else if (mappletnamelist.contains(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getMappletname())) {
                  CONNECTOR connectOr = new CONNECTOR();
                  connectOr.setFROMFIELD(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getColumnname());
                  connectOr.setTOFIELD("in_name");
                  connectOr.setFROMINSTANCE("JNRTRANS" + i);
                  connectOr.setFROMINSTANCETYPE("Joiner");
                  connectOr.setTOINSTANCE(((MappingBean)mb.get(i)).getSource_name() + "_" + ((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getColumnname());
                  connectOr.setTOINSTANCETYPE("Mapplet");
                  mappingConnectorlist.add(connectOr);
                  CONNECTOR connectOr1 = new CONNECTOR();
                  connectOr1.setFROMFIELD("out_name");
                  connectOr1.setTOFIELD(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getColumnname());
                  connectOr1.setFROMINSTANCE(((MappingBean)mb.get(i)).getSource_name() + "_" + ((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getColumnname());
                  connectOr1.setFROMINSTANCETYPE("Mapplet");
                  connectOr1.setTOINSTANCE(((MappingBean)mb.get(i)).getTarget_name());
                  connectOr1.setTOINSTANCETYPE("Target Definition");
                  mappingConnectorlist.add(connectOr1);
               } else {
                  CONNECTOR connectOr = new CONNECTOR();
                  connectOr.setFROMFIELD(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getColumnname());
                  connectOr.setTOFIELD("in_name");
                  connectOr.setFROMINSTANCE("JNRTRANS" + i);
                  connectOr.setFROMINSTANCETYPE("Joiner");
                  connectOr.setTOINSTANCE(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getMappletname());
                  connectOr.setTOINSTANCETYPE("Mapplet");
                  mappingConnectorlist.add(connectOr);
                  CONNECTOR connectOr1 = new CONNECTOR();
                  connectOr1.setFROMFIELD("out_name");
                  connectOr1.setTOFIELD(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getColumnname());
                  connectOr1.setFROMINSTANCE(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getMappletname());
                  connectOr1.setFROMINSTANCETYPE("Mapplet");
                  connectOr1.setTOINSTANCE(((MappingBean)mb.get(i)).getTarget_name());
                  connectOr1.setTOINSTANCETYPE("Target Definition");
                  mappingConnectorlist.add(connectOr1);
                  mappletnamelist.add(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getMappletname());
               }
            }
         }
      }

      for(int i = 1; i < mtb.size() + 1; ++i) {
         for(int j = 1; j < ((MappingBean)mtb.get(i - 1)).getCb().getTypeList().size() + 1; ++j) {
            CONNECTOR connectOr2 = new CONNECTOR();
            connectOr2.setFROMFIELD((String)((MappingBean)mtb.get(i - 1)).getCb().getNameList().get(j - 1));
            connectOr2.setFROMINSTANCE(((MappingBean)mtb.get(i - 1)).getSq_name());
            connectOr2.setFROMINSTANCETYPE("Source Qualifier");
            connectOr2.setTOFIELD((String)((MappingBean)mtb.get(i - 1)).getCb().getNameList().get(j - 1));
            connectOr2.setTOINSTANCE("JNRTRANS" + i);
            connectOr2.setTOINSTANCETYPE("Joiner");
            mappingConnectorlist.add(connectOr2);
            if (((String)((MappingBean)mtb.get(i - 1)).getCb().getKeytype().get(j - 1)).equals("FOREIGN KEY")) {
               CONNECTOR connectOr1 = new CONNECTOR();
               connectOr1.setFROMINSTANCE("SQ_tab_candidate" + i);
               connectOr1.setFROMINSTANCETYPE("Source Qualifier");
               connectOr1.setTOINSTANCE("JNRTRANS" + i);
               connectOr1.setTOINSTANCETYPE("Joiner");
               connectOr1.setFROMFIELD((String)((MappingBean)mtb.get(i - 1)).getCb().getNameList().get(j - 1) + "_sou");
               connectOr1.setTOFIELD((String)((MappingBean)mtb.get(i - 1)).getCb().getNameList().get(j - 1) + "jnr");
               mappingConnectorlist.add(connectOr1);
               CONNECTOR connectOr21 = new CONNECTOR();
               connectOr21.setFROMINSTANCE("source_tab_candidate" + i);
               connectOr21.setFROMINSTANCETYPE("Source Definition");
               connectOr21.setTOINSTANCE("SQ_tab_candidate" + i);
               connectOr21.setTOINSTANCETYPE("Source Qualifier");
               connectOr21.setFROMFIELD((String)((MappingBean)mtb.get(i - 1)).getCb().getNameList().get(j - 1) + "_sou");
               connectOr21.setTOFIELD((String)((MappingBean)mtb.get(i - 1)).getCb().getNameList().get(j - 1) + "_sou");
               mappingConnectorlist.add(connectOr21);
            }

            if (((String)((MappingBean)mtb.get(i - 1)).getCb().getKeytype().get(j - 1)).equals("PRIMARY KEY")) {
               CONNECTOR connectOr3 = new CONNECTOR();
               connectOr3.setFROMINSTANCE("JNRTRANS" + i);
               connectOr3.setFROMINSTANCETYPE("Joiner");
               connectOr3.setTOINSTANCE("SRTTRANS" + i);
               connectOr3.setTOINSTANCETYPE("Sorter");
               connectOr3.setFROMFIELD((String)((MappingBean)mtb.get(i - 1)).getCb().getNameList().get(j - 1));
               connectOr3.setTOFIELD((String)((MappingBean)mtb.get(i - 1)).getCb().getNameList().get(j - 1));
               mappingConnectorlist.add(connectOr3);
               CONNECTOR connectOr4 = new CONNECTOR();
               connectOr4.setFROMINSTANCE("SRTTRANS" + i);
               connectOr4.setFROMINSTANCETYPE("Sorter");
               connectOr4.setTOINSTANCE("target_tab_candidate" + i);
               connectOr4.setTOINSTANCETYPE("Target Definition");
               connectOr4.setFROMFIELD((String)((MappingBean)mtb.get(i - 1)).getCb().getNameList().get(j - 1));
               connectOr4.setTOFIELD((String)((MappingBean)mtb.get(i - 1)).getCb().getNameList().get(j - 1) + "_tar");
               mappingConnectorlist.add(connectOr4);
            }
         }
      }

      List<String> mp_list = new ArrayList();
      if (((MappingBean)mb.get(0)).getMappletbean() != null && ((MappingBean)mb.get(0)).getMappletbean().size() > 0) {
         for(int i = 0; i < ((MappingBean)mb.get(0)).getMappletbean().size(); ++i) {
            mp_list.add(((MappletMakeDatabean)((MappingBean)mb.get(0)).getMappletbean().get(i)).getColumnname());
         }
      }

      for(int i = 0; i < ((MappingBean)mb.get(0)).getCb().getNameList().size(); ++i) {
         if (!mp_list.contains(((MappingBean)mb.get(0)).getCb().getNameList().get(i))) {
            CONNECTOR connectOr = new CONNECTOR();
            connectOr.setFROMFIELD((String)((MappingBean)mb.get(0)).getCb().getNameList().get(i));
            connectOr.setFROMINSTANCE(((MappingBean)mb.get(0)).getSq_name());
            connectOr.setFROMINSTANCETYPE("Source Qualifier");
            connectOr.setTOFIELD((String)((MappingBean)mb.get(0)).getCb().getNameList().get(i));
            connectOr.setTOINSTANCE(((MappingBean)mb.get(0)).getTarget_name());
            connectOr.setTOINSTANCETYPE("Target Definition");
            mappingConnectorlist.add(connectOr);
         }
      }

      for(int j = 0; j < ((MappingBean)mb.get(0)).getCb().getTypeList().size(); ++j) {
         if (((String)((MappingBean)mb.get(0)).getCb().getKeytype().get(j)).equals("PRIMARY KEY")) {
            CONNECTOR connectOr1 = new CONNECTOR();
            connectOr1.setFROMINSTANCE("SRTTRANS0");
            connectOr1.setFROMINSTANCETYPE("Sorter");
            connectOr1.setTOINSTANCE("target_tab_candidate0");
            connectOr1.setTOINSTANCETYPE("Target Definition");
            connectOr1.setFROMFIELD((String)((MappingBean)mb.get(0)).getCb().getNameList().get(j));
            connectOr1.setTOFIELD((String)((MappingBean)mb.get(0)).getCb().getNameList().get(j) + "_tar");
            mappingConnectorlist.add(connectOr1);
            CONNECTOR connectOr2 = new CONNECTOR();
            connectOr2.setFROMINSTANCE(((MappingBean)mb.get(0)).getSq_name());
            connectOr2.setFROMINSTANCETYPE("Source Qualifier");
            connectOr2.setTOINSTANCE("SRTTRANS0");
            connectOr2.setTOINSTANCETYPE("Sorter");
            connectOr2.setFROMFIELD((String)((MappingBean)mb.get(0)).getCb().getNameList().get(j));
            connectOr2.setTOFIELD((String)((MappingBean)mb.get(0)).getCb().getNameList().get(j));
            mappingConnectorlist.add(connectOr2);
         }
      }

      return mappingConnectorlist;
   }

   public List<INSTANCE> getConvertTableInfoToINSTANCE(String sq_name, String sourcename, String targetname, String sourcedbname) {
      List<INSTANCE> instanceList = new ArrayList();
      INSTANCE first_instance = new INSTANCE();
      first_instance.setDESCRIPTION("");
      first_instance.setNAME(targetname);
      first_instance.setTRANSFORMATIONNAME(targetname);
      first_instance.setTRANSFORMATIONTYPE("Target Definition");
      first_instance.setTYPE("TARGET");
      instanceList.add(first_instance);
      INSTANCE second_instance = new INSTANCE();
      second_instance.setDBDNAME(sourcedbname);
      second_instance.setDESCRIPTION("");
      second_instance.setNAME(sourcename);
      second_instance.setTRANSFORMATIONNAME(sourcename);
      second_instance.setTRANSFORMATIONTYPE("Source Definition");
      second_instance.setTYPE("SOURCE");
      instanceList.add(second_instance);
      INSTANCE fou_instance = new INSTANCE();
      fou_instance.setDESCRIPTION("");
      fou_instance.setNAME(sq_name);
      fou_instance.setREUSABLE("NO");
      fou_instance.setTRANSFORMATIONNAME(sq_name);
      fou_instance.setTRANSFORMATIONTYPE("Source Qualifier");
      fou_instance.setTYPE("TRANSFORMATION");
      ASSOCIATEDSOURCEINSTANCE associatedsourceinstance = new ASSOCIATEDSOURCEINSTANCE();
      associatedsourceinstance.setNAME(sourcename);
      fou_instance.getASSOCIATEDSOURCEINSTANCE().add(associatedsourceinstance);
      instanceList.add(fou_instance);
      return instanceList;
   }

   public List<INSTANCE> getSourceINSTANCE(List<MappingBean> mb) {
      List<INSTANCE> instanceList = new ArrayList();

      for(int i = 1; i < mb.size(); ++i) {
         INSTANCE instance = new INSTANCE();
         instance.setDESCRIPTION("");
         instance.setNAME("source_tab_candidate" + i);
         instance.setDBDNAME("Flat_File");
         instance.setTRANSFORMATIONNAME("source_tab_candidate" + i);
         instance.setTRANSFORMATIONTYPE("Source Definition");
         instance.setTYPE("SOURCE");
         instanceList.add(instance);
      }

      for(int i = 1; i < mb.size(); ++i) {
         INSTANCE instance = new INSTANCE();
         instance.setDESCRIPTION("");
         instance.setNAME("SQ_tab_candidate" + i);
         instance.setDBDNAME("Flat_File");
         instance.setTRANSFORMATIONNAME("SQ_tab_candidate" + i);
         instance.setTRANSFORMATIONTYPE("Source Qualifier");
         instance.setTYPE("TRANSFORMATION");
         ASSOCIATEDSOURCEINSTANCE associatedsourceinstance = new ASSOCIATEDSOURCEINSTANCE();
         associatedsourceinstance.setNAME("source_tab_candidate" + i);
         instance.getASSOCIATEDSOURCEINSTANCE().add(associatedsourceinstance);
         instanceList.add(instance);
      }

      return instanceList;
   }

   public List<INSTANCE> getTargetINSTANCE(List<MappingBean> mb) {
      List<INSTANCE> instanceList = new ArrayList();
      INSTANCE first_instance = new INSTANCE();
      first_instance.setDESCRIPTION("");
      first_instance.setNAME("target_tab_candidate0");
      first_instance.setTRANSFORMATIONNAME("target_tab_candidate0");
      first_instance.setTRANSFORMATIONTYPE("Target Definition");
      first_instance.setTYPE("TARGET");
      instanceList.add(first_instance);

      for(int i = 1; i < mb.size(); ++i) {
         INSTANCE instance = new INSTANCE();
         instance.setDESCRIPTION("");
         instance.setNAME("target_tab_candidate" + i);
         instance.setTRANSFORMATIONNAME("target_tab_candidate" + i);
         instance.setTRANSFORMATIONTYPE("Target Definition");
         instance.setTYPE("TARGET");
         instanceList.add(instance);
      }

      return instanceList;
   }

   public List<INSTANCE> getOtherINSTANCE(List<MappingBean> mb) {
      List<INSTANCE> instanceList = new ArrayList();

      for(int i = 1; i < mb.size(); ++i) {
         INSTANCE instance = new INSTANCE();
         instance.setDESCRIPTION("");
         instance.setNAME("JNRTRANS" + i);
         instance.setTRANSFORMATIONNAME("JNRTRANS" + i);
         instance.setTRANSFORMATIONTYPE("Joiner");
         instance.setTYPE("TRANSFORMATION");
         instanceList.add(instance);
      }

      for(int i = 0; i < mb.size(); ++i) {
         INSTANCE instance = new INSTANCE();
         instance.setDESCRIPTION("");
         instance.setNAME("SRTTRANS" + i);
         instance.setTRANSFORMATIONNAME("SRTTRANS" + i);
         instance.setTRANSFORMATIONTYPE("Sorter");
         instance.setTYPE("TRANSFORMATION");
         instanceList.add(instance);
      }

      List<String> mappletnamelist = new ArrayList();

      for(int i = 0; i < mb.size(); ++i) {
         if (((MappingBean)mb.get(i)).getMappletbean() != null && ((MappingBean)mb.get(i)).getMappletbean().size() > 0) {
            for(int j = 0; j < ((MappingBean)mb.get(i)).getMappletbean().size(); ++j) {
               if (i == 0 && j == 0) {
                  INSTANCE instance = new INSTANCE();
                  instance.setDESCRIPTION("");
                  instance.setNAME(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getMappletname());
                  instance.setTRANSFORMATIONNAME(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getMappletname());
                  instance.setTRANSFORMATIONTYPE("Mapplet");
                  instance.setTYPE("MAPPLET");
                  instance.setREUSABLE("YES");
                  instanceList.add(instance);
                  mappletnamelist.add(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getMappletname());
               } else if (i == 0 && mappletnamelist.contains(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getMappletname())) {
                  INSTANCE instance = new INSTANCE();
                  instance.setDESCRIPTION("");
                  instance.setNAME(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getMappletname());
                  instance.setTRANSFORMATIONNAME(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getMappletname());
                  instance.setTRANSFORMATIONTYPE("Mapplet");
                  instance.setTYPE("MAPPLET");
                  instance.setREUSABLE("YES");
                  instanceList.add(instance);
               } else if (i == 0 && !mappletnamelist.contains(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getMappletname())) {
                  INSTANCE instance = new INSTANCE();
                  instance.setDESCRIPTION("");
                  instance.setNAME(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getMappletname());
                  instance.setTRANSFORMATIONNAME(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getMappletname());
                  instance.setTRANSFORMATIONTYPE("Mapplet");
                  instance.setTYPE("MAPPLET");
                  instance.setREUSABLE("YES");
                  instanceList.add(instance);
                  mappletnamelist.add(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getMappletname());
               } else if (mappletnamelist.contains(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getMappletname())) {
                  INSTANCE instance1 = new INSTANCE();
                  instance1.setDESCRIPTION("");
                  instance1.setNAME(((MappingBean)mb.get(i)).getSource_name() + "_" + ((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getColumnname());
                  instance1.setTRANSFORMATIONNAME(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getMappletname());
                  instance1.setTRANSFORMATIONTYPE("Mapplet");
                  instance1.setTYPE("MAPPLET");
                  instance1.setREUSABLE("YES");
                  instanceList.add(instance1);
               } else {
                  INSTANCE instance1 = new INSTANCE();
                  instance1.setDESCRIPTION("");
                  instance1.setNAME(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getMappletname());
                  instance1.setTRANSFORMATIONNAME(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getMappletname());
                  instance1.setTRANSFORMATIONTYPE("Mapplet");
                  instance1.setTYPE("MAPPLET");
                  instance1.setREUSABLE("YES");
                  instanceList.add(instance1);
                  mappletnamelist.add(((MappletMakeDatabean)((MappingBean)mb.get(i)).getMappletbean().get(j)).getMappletname());
               }
            }
         }
      }

      return instanceList;
   }

   public TRANSFORMATION getTRANSFORMATIONJNRXml(List<String> columnList, List<String> dataTypeList, List<String> keyTypeList, List<String> lengthList, int fix) {
      TRANSFORMATION transformation = new TRANSFORMATION();
      transformation.setDESCRIPTION("");
      transformation.setNAME("JNRTRANS" + (fix + 1));
      transformation.setOBJECTVERSION("1");
      transformation.setREUSABLE("NO");
      transformation.setTYPE("Joiner");
      transformation.setVERSIONNUMBER("1");

      for(TRANSFORMFIELD transformfield : this.getTransformfieldjnr(columnList, dataTypeList, keyTypeList, lengthList)) {
         transformation.getTRANSFORMFIELD().add(transformfield);
      }

      for(TABLEATTRIBUTE tableattribute : this.getTableattributejnr(columnList, keyTypeList, lengthList)) {
         transformation.getTABLEATTRIBUTE().add(tableattribute);
      }

      return transformation;
   }

   public List<TRANSFORMFIELD> getTransformfieldjnr(List<String> columnList, List<String> dataTypeList, List<String> keyTypeList, List<String> lengthList) {
      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)lengthList.get(i));
         if ("date/time".equals(dataTypeList.get(i))) {
            tran.setSCALE("9");
         } else {
            tran.setSCALE("0");
         }

         transformfield.add(tran);
      }

      for(int i = 0; i < keyTypeList.size(); ++i) {
         if (((String)keyTypeList.get(i)).equals("FOREIGN KEY")) {
            TRANSFORMFIELD tran = new TRANSFORMFIELD();
            tran.setDATATYPE((String)dataTypeList.get(i));
            tran.setDEFAULTVALUE("");
            tran.setDESCRIPTION("");
            tran.setNAME((String)columnList.get(i) + "jnr");
            tran.setPICTURETEXT("");
            tran.setPORTTYPE("INPUT/OUTPUT/MASTER");
            tran.setPRECISION((String)lengthList.get(i));
            tran.setSCALE("0");
            transformfield.add(tran);
         }
      }

      return transformfield;
   }

   public TRANSFORMATION getTRANSFORMATIONSRTXml(MappingBean mb, int fix) {
      TRANSFORMATION transformation = new TRANSFORMATION();
      transformation.setDESCRIPTION("");
      transformation.setNAME("SRTTRANS" + fix);
      transformation.setOBJECTVERSION("1");
      transformation.setREUSABLE("NO");
      transformation.setTYPE("Sorter");
      transformation.setVERSIONNUMBER("1");

      for(TRANSFORMFIELD transformfield : this.getTransformsrtfield(mb)) {
         transformation.getTRANSFORMFIELD().add(transformfield);
      }

      for(TABLEATTRIBUTE tableattribute : this.getTableattributesrt()) {
         transformation.getTABLEATTRIBUTE().add(tableattribute);
      }

      return transformation;
   }

   public List<TRANSFORMATION> getTRANSFORMATIONFix(List<MappingBean> mb) {
      List<TRANSFORMATION> list = new ArrayList();
      List<MappingBean> mbt = new ArrayList();
      if (mb != null && mb.size() > 0) {
         for(int i = 0; i < mb.size(); ++i) {
            if (i > 0) {
               mbt.add(mb.get(i));
            }
         }
      }

      for(int i = 0; i < mbt.size(); ++i) {
         TRANSFORMATION transformation = new TRANSFORMATION();
         transformation.setDESCRIPTION("");
         transformation.setNAME("SQ_tab_candidate" + (i + 1));
         transformation.setOBJECTVERSION("1");
         transformation.setREUSABLE("NO");
         transformation.setTYPE("Source Qualifier");
         transformation.setVERSIONNUMBER("1");

         for(TRANSFORMFIELD transformfield : this.getTransformsrtfixfield((MappingBean)mbt.get(i))) {
            transformation.getTRANSFORMFIELD().add(transformfield);
         }

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

         list.add(transformation);
      }

      return list;
   }

   public List<TRANSFORMFIELD> getTransformsrtfixfield(MappingBean mb) {
      List<TRANSFORMFIELD> transformfield = new ArrayList();

      for(int i = 0; i < mb.getCb().getKeytype().size(); ++i) {
         if (((String)mb.getCb().getKeytype().get(i)).equals("FOREIGN KEY")) {
            TRANSFORMFIELD tran = new TRANSFORMFIELD();
            if (((String)mb.getCb().getMappingTypeList().get(i)).equals("decimal")) {
               tran.setDATATYPE("integer");
            } else {
               tran.setDATATYPE("string");
            }

            tran.setDEFAULTVALUE("");
            tran.setDESCRIPTION("");
            tran.setNAME((String)mb.getCb().getNameList().get(i) + "_sou");
            tran.setPICTURETEXT("");
            tran.setPORTTYPE("INPUT/OUTPUT");
            tran.setPRECISION((String)mb.getCb().getMappingLengthList().get(i));
            tran.setSCALE("0");
            transformfield.add(tran);
         }
      }

      return transformfield;
   }

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

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

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

      return transformation;
   }

   public List<TRANSFORMFIELD> getTransformfield(List<String> columnList, List<String> dataTypeList, List<String> lengthList) {
      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)lengthList.get(i));
         if ("date/time".equals(dataTypeList.get(i))) {
            tran.setSCALE("9");
         } else {
            tran.setSCALE("0");
         }

         transformfield.add(tran);
      }

      return transformfield;
   }

   public List<TRANSFORMFIELD> getTransformsrtfield(MappingBean mb) {
      List<TRANSFORMFIELD> transformfield = new ArrayList();

      for(int j = 0; j < mb.getCb().getMappingTypeList().size(); ++j) {
         if (((String)mb.getCb().getKeytype().get(j)).equals("PRIMARY KEY")) {
            TRANSFORMFIELD tran = new TRANSFORMFIELD();
            tran.setDATATYPE((String)mb.getCb().getMappingTypeList().get(j));
            tran.setDEFAULTVALUE("");
            tran.setDESCRIPTION("");
            tran.setNAME((String)mb.getCb().getNameList().get(j));
            tran.setPICTURETEXT("");
            tran.setPORTTYPE("INPUT/OUTPUT");
            tran.setPRECISION((String)mb.getCb().getMappingLengthList().get(j));
            tran.setSORTDIRECTION("ASCENDING");
            tran.setISSORTKEY("YES");
            transformfield.add(tran);
         }
      }

      return transformfield;
   }

   public List<TABLEATTRIBUTE> getTableattributesrt() {
      List<TABLEATTRIBUTE> tableattributeList = new ArrayList();
      String[] tableattributeStr = new String[]{"Sorter Cache Size@Auto", "Case Sensitive@YES", "Work Directory@$PMTempDir", "Distinct@YES", "Tracing Level@Normal", "Null Treated Low@NO", "Merge Only@NO", "Partitioning@Order records for individual partitions", "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<TABLEATTRIBUTE> getTableattribute(String zbsql) {
      List<TABLEATTRIBUTE> tableattributeList = new ArrayList();
      String[] tableattributeStr = new String[]{"Sql Query@", "User Defined Join", "Source Filter@" + zbsql, "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> getTableattributejnr(List<String> columnList, List<String> dataTypeList, List<String> lengthList) {
      StringBuffer order = new StringBuffer();
      List<String> clist = new ArrayList();

      for(int i = 0; i < dataTypeList.size(); ++i) {
         if (((String)dataTypeList.get(i)).equals("FOREIGN KEY")) {
            clist.add(columnList.get(i));
         }
      }

      if (clist != null && clist.size() > 0) {
         for(int i = 0; i < clist.size(); ++i) {
            if (i == clist.size() - 1) {
               order.append((String)clist.get(i) + "jnr=" + (String)clist.get(i));
            } else {
               order.append((String)clist.get(i) + "jnr=" + (String)clist.get(i) + " AND ");
            }
         }
      }

      List<TABLEATTRIBUTE> tableattributeList = new ArrayList();
      String[] tableattributeStr = new String[]{"Case Sensitive String Comparison@YES", "Cache Directory@$PMCacheDir", "Join Condition@" + order.toString(), "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;
   }
}
