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

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.MappingBean;
import com.chenyang.nse.bussiness.engine.infa.powercenter961.pubbean.MappingPramaBean;
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 MappingUtil {
   public MAPPING getMapping(MappingBean map_bean) throws Exception {
      MAPPING mapping = new MAPPING();
      mapping.setDESCRIPTION("");
      mapping.setISVALID("YES");
      mapping.setNAME(map_bean.getMapping_name());
      mapping.setOBJECTVERSION("1");
      mapping.setVERSIONNUMBER("1");
      mapping.getTRANSFORMATION().add(this.getTRANSFORMATIONXml(map_bean.getSq_name(), map_bean.getCb().getNameList(), map_bean.getCb().getMappingTypeList(), map_bean.getCb().getMappingLengthList(), map_bean.getMappingsql()));

      for(INSTANCE instance : this.getConvertTableInfoToINSTANCE(map_bean, map_bean.getSq_name(), map_bean.getSource_name(), map_bean.getTarget_name(), map_bean.getSourcedbname(), map_bean.getSourcedbtype(), map_bean.getMappletbean())) {
         mapping.getINSTANCE().add(instance);
      }

      for(CONNECTOR connector : this.getConvertTableInfoToCONNECTOR(map_bean, map_bean.getCb().getNameList(), map_bean.getMappletbean(), map_bean.getSq_name(), map_bean.getSource_name(), map_bean.getTarget_name())) {
         mapping.getCONNECTOR().add(connector);
      }

      TARGETLOADORDER targetloadorder = new TARGETLOADORDER();
      targetloadorder.setORDER("1");
      targetloadorder.setTARGETINSTANCE("MAPPING");
      mapping.getTARGETLOADORDER().add(targetloadorder);
      if (map_bean.getParamList() != null && map_bean.getParamList().size() > 0) {
         for(int i = 0; i < map_bean.getParamList().size(); ++i) {
            MAPPINGVARIABLE mappingvariable = new MAPPINGVARIABLE();
            mappingvariable.setAGGFUNCTION("MAX");
            mappingvariable.setDATATYPE(((MappingPramaBean)map_bean.getParamList().get(i)).getDatatype());
            mappingvariable.setISEXPRESSIONVARIABLE("NO");
            mappingvariable.setISPARAM("NO");
            mappingvariable.setNAME(((MappingPramaBean)map_bean.getParamList().get(i)).getName());
            mappingvariable.setPRECISION(((MappingPramaBean)map_bean.getParamList().get(i)).getPrecision());
            mappingvariable.setSCALE(((MappingPramaBean)map_bean.getParamList().get(i)).getScale());
            mappingvariable.setUSERDEFINED("YES");
            mapping.getMAPPINGVARIABLE().add(mappingvariable);
         }
      }

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

   public TRANSFORMATION getTRANSFORMATIONXml(String sq_name, List<String> columnList, List<String> dataTypeList, List<String> mappinglengthlist, String mappingsql) {
      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, mappinglengthlist)) {
         transformation.getTRANSFORMFIELD().add(transformfield);
      }

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

      return transformation;
   }

   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<TABLEATTRIBUTE> getTableattribute(String mappingsql) {
      List<TABLEATTRIBUTE> tableattributeList = new ArrayList();
      if (mappingsql == null) {
         mappingsql = "";
      }

      String[] tableattributeStr = new String[]{"Sql Query@" + mappingsql, "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<INSTANCE> getConvertTableInfoToINSTANCE(MappingBean map_bean, String sq_name, String sourcename, String targetname, String sourcedbname, String sourcedbtype, List<MappletMakeDatabean> mapplet) {
      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("Flat_File");
      second_instance.setDESCRIPTION("");
      second_instance.setNAME(sourcename);
      second_instance.setTRANSFORMATIONNAME(sourcename);
      second_instance.setTRANSFORMATIONTYPE("Source Definition");
      second_instance.setTYPE("SOURCE");
      instanceList.add(second_instance);
      List<String> mpt = new ArrayList();
      if (mapplet != null && mapplet.size() > 0) {
         for(int i = 0; i < mapplet.size(); ++i) {
            mpt.add(((MappletMakeDatabean)mapplet.get(i)).getColumnname());
         }
      }

      for(int i = 0; i < map_bean.getTar_bean().getCb().getNameList().size(); ++i) {
         if (map_bean.getTar_bean().getCb().getJudgelist() != null && map_bean.getTar_bean().getCb().getJudgelist().size() > 0 && "NO".equals(map_bean.getTar_bean().getCb().getJudgelist().get(i)) && !mpt.contains(map_bean.getTar_bean().getCb().getNameList().get(i))) {
            INSTANCE instance = new INSTANCE();
            instance.setDESCRIPTION("");
            instance.setNAME((String)map_bean.getTar_bean().getCb().getNameList().get(i) + "_judge");
            instance.setREUSABLE("YES");
            instance.setTRANSFORMATIONNAME("mapplet_judge");
            instance.setTRANSFORMATIONTYPE("Mapplet");
            instance.setTYPE("MAPPLET");
            instanceList.add(instance);
         }
      }

      if (mapplet != null && mapplet.size() > 0) {
         List<String> values = new ArrayList();

         for(int i = 0; i < mapplet.size(); ++i) {
            if (i == 0) {
               INSTANCE instance = new INSTANCE();
               instance.setDESCRIPTION("");
               instance.setNAME(((MappletMakeDatabean)mapplet.get(i)).getMappletname());
               instance.setREUSABLE("YES");
               instance.setTRANSFORMATIONNAME(((MappletMakeDatabean)mapplet.get(i)).getMappletname());
               instance.setTRANSFORMATIONTYPE("Mapplet");
               instance.setTYPE("MAPPLET");
               instanceList.add(instance);
               values.add(((MappletMakeDatabean)mapplet.get(i)).getMappletname());
            } else if (values.contains(((MappletMakeDatabean)mapplet.get(i)).getMappletname())) {
               INSTANCE instance = new INSTANCE();
               instance.setDESCRIPTION("");
               instance.setNAME(((MappletMakeDatabean)mapplet.get(i)).getColumnname() + 1);
               instance.setREUSABLE("YES");
               instance.setTRANSFORMATIONNAME(((MappletMakeDatabean)mapplet.get(i)).getMappletname());
               instance.setTRANSFORMATIONTYPE("Mapplet");
               instance.setTYPE("MAPPLET");
               instanceList.add(instance);
               values.add(instance.getNAME());
            } else {
               INSTANCE instance = new INSTANCE();
               instance.setDESCRIPTION("");
               instance.setNAME(((MappletMakeDatabean)mapplet.get(i)).getMappletname());
               instance.setREUSABLE("YES");
               instance.setTRANSFORMATIONNAME(((MappletMakeDatabean)mapplet.get(i)).getMappletname());
               instance.setTRANSFORMATIONTYPE("Mapplet");
               instance.setTYPE("MAPPLET");
               instanceList.add(instance);
               values.add(instance.getNAME());
            }
         }
      }

      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<CONNECTOR> getConvertTableInfoToCONNECTOR(MappingBean map_bean, List<String> columnList, List<MappletMakeDatabean> mapplet, String sq_name, String sourcename, String targetname) throws Exception {
      List<String> snamelist = map_bean.getSou_bean().getCb().getNameList();
      List<String> tnamelist = map_bean.getTar_bean().getCb().getNameList();
      List<CONNECTOR> mappingConnectorlist = new ArrayList();
      List<String> list = new ArrayList();
      if (mapplet != null && mapplet.size() > 0) {
         for(int i = 0; i < mapplet.size(); ++i) {
            list.add(((MappletMakeDatabean)mapplet.get(i)).getColumnname());
         }
      }

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

      List<String> notnulllist = new ArrayList();
      List<String> nulllist = new ArrayList();
      if (map_bean.getTar_bean().getCb().getJudgelist() != null && map_bean.getTar_bean().getCb().getJudgelist().size() > 0) {
         for(int i = 0; i < map_bean.getTar_bean().getCb().getJudgelist().size(); ++i) {
            if ("YES".equals(map_bean.getTar_bean().getCb().getJudgelist().get(i))) {
               notnulllist.add(map_bean.getTar_bean().getCb().getNameList().get(i));
            } else {
               nulllist.add(map_bean.getTar_bean().getCb().getNameList().get(i));
            }
         }
      }

      for(int i = 0; i < snamelist.size(); ++i) {
         if (!list.contains(snamelist.get(i)) && !nulllist.contains(snamelist.get(i))) {
            CONNECTOR connectOr = new CONNECTOR();
            connectOr.setFROMFIELD((String)snamelist.get(i));
            connectOr.setFROMINSTANCE(sq_name);
            connectOr.setFROMINSTANCETYPE("Source Qualifier");
            connectOr.setTOFIELD((String)tnamelist.get(i));
            connectOr.setTOINSTANCE(targetname);
            connectOr.setTOINSTANCETYPE("Target Definition");
            mappingConnectorlist.add(connectOr);
         }
      }

      for(int i = 0; i < snamelist.size(); ++i) {
         if (!list.contains(snamelist.get(i)) && nulllist.contains(snamelist.get(i))) {
            CONNECTOR connectOr = new CONNECTOR();
            connectOr.setFROMFIELD((String)snamelist.get(i));
            connectOr.setTOFIELD("in_name");
            connectOr.setFROMINSTANCE(sq_name);
            connectOr.setFROMINSTANCETYPE("Source Qualifier");
            connectOr.setTOINSTANCE((String)tnamelist.get(i) + "_judge");
            connectOr.setTOINSTANCETYPE("Mapplet");
            mappingConnectorlist.add(connectOr);
            CONNECTOR connectOr1 = new CONNECTOR();
            connectOr1.setFROMFIELD("out_name");
            connectOr1.setTOFIELD((String)tnamelist.get(i));
            connectOr1.setFROMINSTANCE((String)tnamelist.get(i) + "_judge");
            connectOr1.setFROMINSTANCETYPE("Mapplet");
            connectOr1.setTOINSTANCE(targetname);
            connectOr1.setTOINSTANCETYPE("Target Definition");
            mappingConnectorlist.add(connectOr1);
         }
      }

      if (mapplet != null && mapplet.size() > 0) {
         List<String> values = new ArrayList();

         for(int i = 0; i < mapplet.size(); ++i) {
            if (i == 0) {
               CONNECTOR connectOr = new CONNECTOR();
               connectOr.setFROMFIELD(((MappletMakeDatabean)mapplet.get(i)).getColumnname());
               connectOr.setTOFIELD("in_name");
               connectOr.setFROMINSTANCE(sq_name);
               connectOr.setFROMINSTANCETYPE("Source Qualifier");
               connectOr.setTOINSTANCE(((MappletMakeDatabean)mapplet.get(i)).getMappletname());
               connectOr.setTOINSTANCETYPE("Mapplet");
               mappingConnectorlist.add(connectOr);
               CONNECTOR connectOr1 = new CONNECTOR();
               connectOr1.setFROMFIELD("out_name");
               connectOr1.setTOFIELD(((MappletMakeDatabean)mapplet.get(i)).getTocolumnname());
               connectOr1.setFROMINSTANCE(((MappletMakeDatabean)mapplet.get(i)).getMappletname());
               connectOr1.setFROMINSTANCETYPE("Mapplet");
               connectOr1.setTOINSTANCE(targetname);
               connectOr1.setTOINSTANCETYPE("Target Definition");
               mappingConnectorlist.add(connectOr1);
               values.add(((MappletMakeDatabean)mapplet.get(i)).getMappletname());
            } else if (values.contains(((MappletMakeDatabean)mapplet.get(i)).getMappletname())) {
               CONNECTOR connectOr = new CONNECTOR();
               connectOr.setFROMFIELD(((MappletMakeDatabean)mapplet.get(i)).getColumnname());
               connectOr.setTOFIELD("in_name");
               connectOr.setFROMINSTANCE(sq_name);
               connectOr.setFROMINSTANCETYPE("Source Qualifier");
               connectOr.setTOINSTANCE(((MappletMakeDatabean)mapplet.get(i)).getColumnname() + 1);
               connectOr.setTOINSTANCETYPE("Mapplet");
               mappingConnectorlist.add(connectOr);
               CONNECTOR connectOr1 = new CONNECTOR();
               connectOr1.setFROMFIELD("out_name");
               connectOr1.setTOFIELD(((MappletMakeDatabean)mapplet.get(i)).getTocolumnname());
               connectOr1.setFROMINSTANCE(((MappletMakeDatabean)mapplet.get(i)).getColumnname() + 1);
               connectOr1.setFROMINSTANCETYPE("Mapplet");
               connectOr1.setTOINSTANCE(targetname);
               connectOr1.setTOINSTANCETYPE("Target Definition");
               mappingConnectorlist.add(connectOr1);
               values.add(((MappletMakeDatabean)mapplet.get(i)).getMappletname());
            } else {
               CONNECTOR connectOr = new CONNECTOR();
               connectOr.setFROMFIELD(((MappletMakeDatabean)mapplet.get(i)).getColumnname());
               connectOr.setTOFIELD("in_name");
               connectOr.setFROMINSTANCE(sq_name);
               connectOr.setFROMINSTANCETYPE("Source Qualifier");
               connectOr.setTOINSTANCE(((MappletMakeDatabean)mapplet.get(i)).getMappletname());
               connectOr.setTOINSTANCETYPE("Mapplet");
               mappingConnectorlist.add(connectOr);
               CONNECTOR connectOr1 = new CONNECTOR();
               connectOr1.setFROMFIELD("out_name");
               connectOr1.setTOFIELD(((MappletMakeDatabean)mapplet.get(i)).getTocolumnname());
               connectOr1.setFROMINSTANCE(((MappletMakeDatabean)mapplet.get(i)).getMappletname());
               connectOr1.setFROMINSTANCETYPE("Mapplet");
               connectOr1.setTOINSTANCE(targetname);
               connectOr1.setTOINSTANCETYPE("Target Definition");
               mappingConnectorlist.add(connectOr1);
               values.add(((MappletMakeDatabean)mapplet.get(i)).getMappletname());
            }
         }
      }

      return mappingConnectorlist;
   }
}
