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

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

public class ChildSeMappingUtil {
   public MAPPING getSeChildMapping(String mappingname, List<MappingBean> mb, List<MappingPramaBean> paramList) 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().getMappingPrecisionList(), ((MappingBean)mb.get(i)).getMappingsql()));
         }
      }

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

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

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

      if (paramList != null && paramList.size() > 0) {
         for(int i = 0; i < paramList.size(); ++i) {
            MAPPINGVARIABLE mappingvariable = new MAPPINGVARIABLE();
            mappingvariable.setAGGFUNCTION("MAX");
            mappingvariable.setDATATYPE(((MappingPramaBean)paramList.get(i)).getDatatype());
            mappingvariable.setISEXPRESSIONVARIABLE("NO");
            mappingvariable.setISPARAM("NO");
            mappingvariable.setNAME(((MappingPramaBean)paramList.get(i)).getName());
            mappingvariable.setPRECISION(((MappingPramaBean)paramList.get(i)).getPrecision());
            mappingvariable.setSCALE(((MappingPramaBean)paramList.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> lengthList, 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, lengthList)) {
         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> 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<TABLEATTRIBUTE> getTableattribute(String mappingsql) {
      List<TABLEATTRIBUTE> tableattributeList = new ArrayList();
      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(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<CONNECTOR> getConvertTableInfoToCONNECTOR(List<String> columnList, String sq_name, String sourcename, String targetname) throws Exception {
      List mappingConnectorlist = new ArrayList();
      new HashMap();
      List<String> list = new ArrayList();

      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(sourcename);
            connectOr.setFROMINSTANCETYPE("Source Definition");
            connectOr.setTOFIELD((String)columnList.get(i));
            connectOr.setTOINSTANCE(sq_name);
            connectOr.setTOINSTANCETYPE("Source Qualifier");
            mappingConnectorlist.add(connectOr);
         }
      }

      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(targetname);
         connectOr.setTOINSTANCETYPE("Target Definition");
         mappingConnectorlist.add(connectOr);
      }

      return mappingConnectorlist;
   }
}
