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

import com.chenyang.nse.bussiness.engine.infa.InfoProperty;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.CONFIG;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.FOLDER;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.MAPPING;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.MAPPLET;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.POWERMART;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.REPOSITORY;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.SOURCE;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.TARGET;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.WORKFLOW;
import com.chenyang.nse.bussiness.engine.infa.powercenter961.pubbean.CommonBean;
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.pubbean.SourceBean;
import com.chenyang.nse.bussiness.engine.infa.powercenter961.pubbean.TargetBean;
import com.chenyang.nse.bussiness.engine.infa.powercenter961.pubbean.WorkflowBean;
import com.chenyang.nse.bussiness.engine.infa.powercenter961.pubtool.ToObjutil;
import com.chenyang.nse.bussiness.tools.io.FileTool;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class MakeMaskingutil {
   public static void maskMasking(ParamBean par_bean, InfoProperty tengine, SourceBean sou_bean, TargetBean tar_bean, List<MappletMakeDatabean> mapplet, List<MappingPramaBean> paramList) {
      new POWERMART();
      POWERMART powermart = Powermartutil.getPowermart(par_bean);
      new REPOSITORY();
      REPOSITORY repository = Repositoryutil.getREPOSITORY(par_bean, tengine);
      new FOLDER();
      FOLDER folder = Folderutil.getFolder(par_bean);
      new SOURCE();
      SOURCE source = Sourceutil.getSource(sou_bean);
      folder.getFOLDERVERSIONOrCONFIGOrSCHEDULEROrTASKOrSESSIONOrWORKLETOrWORKFLOWOrSOURCEOrTARGETOrTRANSFORMATIONOrMAPPLETOrMAPPINGOrSHORTCUTOrEXPRMACRO().add(source);
      new TARGET();
      TARGET target = Targetutil.getTarget(tar_bean);
      folder.getFOLDERVERSIONOrCONFIGOrSCHEDULEROrTASKOrSESSIONOrWORKLETOrWORKFLOWOrSOURCEOrTARGETOrTRANSFORMATIONOrMAPPLETOrMAPPINGOrSHORTCUTOrEXPRMACRO().add(target);
      FileTool.createFolder(tengine.getXmldir() + File.separator + "mapplet");
      List<String> maplist = new ArrayList();
      List<MappletMakeDatabean> map = new ArrayList();
      if (mapplet != null && mapplet.size() > 0) {
         for(int i = 0; i < mapplet.size(); ++i) {
            if (i == 0) {
               map.add(mapplet.get(i));
               maplist.add(((MappletMakeDatabean)mapplet.get(i)).getMappletname());
            } else if (!maplist.contains(((MappletMakeDatabean)mapplet.get(i)).getMappletname())) {
               map.add(mapplet.get(i));
            }
         }
      }

      if (map != null && map.size() > 0) {
         for(int i = 0; i < map.size(); ++i) {
            try {
               InputStream is = new FileInputStream(tengine.getXmldir() + File.separator + "mapplet" + File.separator + ((MappletMakeDatabean)map.get(i)).getMappletname() + ".xml");
               System.out.println("xxxxxxxxxx" + tengine.getXmldir() + File.separator + "mapplet" + File.separator + ((MappletMakeDatabean)map.get(i)).getMappletname() + ".xml");
               ToObjutil to = new ToObjutil();
               BufferedReader tBufferedReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
               int k = 0;
               StringBuffer tStringBuffer = new StringBuffer();
               new String("");

               String mapping;
               while((mapping = tBufferedReader.readLine()) != null) {
                  ++k;
                  if (k == 2) {
                     tStringBuffer.append("");
                  } else {
                     tStringBuffer.append(mapping);
                  }
               }

               Object obj = ToObjutil.unmarshalmo("com.chenyang.nse.bussiness.xmlbean", tStringBuffer.toString());
               POWERMART power = (POWERMART)obj;
               List<REPOSITORY> repositorListWf = power.getREPOSITORY();
               new MAPPLET();

               for(int l = 0; l < repositorListWf.size(); ++l) {
                  REPOSITORY repositor = (REPOSITORY)repositorListWf.get(l);
                  List<FOLDER> folderList = repositor.getFOLDER();

                  for(int j = 0; j < folderList.size(); ++j) {
                     FOLDER folder1 = (FOLDER)folderList.get(j);

                     for(Object object1 : folder1.getFOLDERVERSIONOrCONFIGOrSCHEDULEROrTASKOrSESSIONOrWORKLETOrWORKFLOWOrSOURCEOrTARGETOrTRANSFORMATIONOrMAPPLETOrMAPPINGOrSHORTCUTOrEXPRMACRO()) {
                        if (object1.getClass().getName().equals("com.chenyang.nse.bussiness.xmlbean.MAPPLET")) {
                           MAPPLET workflow = (MAPPLET)object1;
                           folder.getFOLDERVERSIONOrCONFIGOrSCHEDULEROrTASKOrSESSIONOrWORKLETOrWORKFLOWOrSOURCEOrTARGETOrTRANSFORMATIONOrMAPPLETOrMAPPINGOrSHORTCUTOrEXPRMACRO().add(workflow);
                        }
                     }
                  }
               }
            } catch (Exception e) {
               e.printStackTrace();
            }
         }
      }

      MappingBean map_bean = new MappingBean();
      map_bean.setMapping_name("mp_" + sou_bean.getSo_name());
      map_bean.setSq_name("SQ_" + sou_bean.getSo_name());
      map_bean.setMappingsql(par_bean.getMappingsql());
      map_bean.setSource_name(sou_bean.getSo_name());
      map_bean.setTarget_name(tar_bean.getTar_name());
      map_bean.setSourcedbname(sou_bean.getDbdname());
      map_bean.setMappletbean(mapplet);
      map_bean.setParamList(paramList);
      CommonBean mapping_cd = new CommonBean();
      CommonBean cblist = sou_bean.getCb();
      List<String> map_typeList = new ArrayList();
      List<String> map_precisionList = new ArrayList();
      List<String> map_scaleList = new ArrayList();
      if ("DB2".equals(sou_bean.getDatabasetype())) {
         for(int i = 0; i < cblist.getTypeList().size(); ++i) {
            if ("character".equalsIgnoreCase((String)cblist.getTypeList().get(i))) {
               map_typeList.add("string");
               map_precisionList.add(cblist.getPrecisionList().get(i));
               map_scaleList.add(cblist.getScalelist().get(i));
            } else if ("varchar".equalsIgnoreCase((String)cblist.getTypeList().get(i))) {
               map_typeList.add("string");
               map_precisionList.add(cblist.getPrecisionList().get(i));
               map_scaleList.add(cblist.getScalelist().get(i));
            } else if ("char".equalsIgnoreCase((String)cblist.getTypeList().get(i))) {
               map_typeList.add("string");
               map_precisionList.add(cblist.getPrecisionList().get(i));
               map_scaleList.add(cblist.getScalelist().get(i));
            } else if ("date".equalsIgnoreCase((String)cblist.getTypeList().get(i))) {
               map_typeList.add("date/time");
               map_precisionList.add("29");
               map_scaleList.add("9");
            } else if ("time".equalsIgnoreCase((String)cblist.getTypeList().get(i))) {
               map_typeList.add("date/time");
               map_precisionList.add("29");
               map_scaleList.add("9");
            } else if ("timestamp".equalsIgnoreCase((String)cblist.getTypeList().get(i))) {
               map_typeList.add("date/time");
               map_precisionList.add("29");
               map_scaleList.add("9");
            } else if ("clob".equalsIgnoreCase((String)cblist.getTypeList().get(i))) {
               map_typeList.add("text");
               map_precisionList.add(cblist.getPrecisionList().get(i));
               map_scaleList.add("0");
            } else if ("blob".equalsIgnoreCase((String)cblist.getTypeList().get(i))) {
               map_typeList.add("binary");
               map_precisionList.add(cblist.getPrecisionList().get(i));
               map_scaleList.add("0");
            } else if ("smallint".equalsIgnoreCase((String)cblist.getTypeList().get(i))) {
               map_typeList.add("small integer");
               map_precisionList.add(cblist.getPrecisionList().get(i));
               map_scaleList.add("0");
            } else {
               map_typeList.add(((String)cblist.getTypeList().get(i)).toLowerCase());
               map_precisionList.add(cblist.getPrecisionList().get(i));
               map_scaleList.add(cblist.getScalelist().get(i));
            }
         }
      } else if ("ORACLE".equals(sou_bean.getDatabasetype())) {
         for(int i = 0; i < cblist.getTypeList().size(); ++i) {
            if ("TIMESTAMP".equalsIgnoreCase((String)cblist.getTypeList().get(i))) {
               map_typeList.add("date/time");
               map_precisionList.add("29");
               map_scaleList.add("9");
            } else if ("NUMBER".equalsIgnoreCase((String)cblist.getTypeList().get(i))) {
               map_typeList.add("double");
               map_precisionList.add("15");
               map_scaleList.add("15");
            } else if ("DATE".equalsIgnoreCase((String)cblist.getTypeList().get(i))) {
               map_typeList.add("date/time");
               map_precisionList.add("29");
               map_scaleList.add("9");
            } else if ("VARBINARY".equalsIgnoreCase((String)cblist.getTypeList().get(i))) {
               map_typeList.add("binary");
               map_precisionList.add(cblist.getPrecisionList().get(i));
               map_scaleList.add(cblist.getScalelist().get(i));
            } else if ("BIGINT".equalsIgnoreCase((String)cblist.getTypeList().get(i))) {
               map_typeList.add("double");
               map_precisionList.add(cblist.getPrecisionList().get(i));
               map_scaleList.add(cblist.getScalelist().get(i));
            } else if ("TIME".equalsIgnoreCase((String)cblist.getTypeList().get(i))) {
               map_typeList.add("date/time");
               map_precisionList.add("29");
               map_scaleList.add("9");
            } else if ("NUMERIC".equalsIgnoreCase((String)cblist.getTypeList().get(i))) {
               map_typeList.add("double");
               map_precisionList.add(cblist.getPrecisionList().get(i));
               map_scaleList.add(cblist.getScalelist().get(i));
            } else if ("CHAR".equalsIgnoreCase((String)cblist.getTypeList().get(i))) {
               map_typeList.add("string");
               map_precisionList.add(cblist.getPrecisionList().get(i));
               map_scaleList.add(cblist.getScalelist().get(i));
            } else if ("BIT".equalsIgnoreCase((String)cblist.getTypeList().get(i))) {
               map_typeList.add("double");
               map_precisionList.add(cblist.getPrecisionList().get(i));
               map_scaleList.add(cblist.getScalelist().get(i));
            } else if ("SMALLINT".equalsIgnoreCase((String)cblist.getTypeList().get(i))) {
               map_typeList.add("double");
               map_precisionList.add(cblist.getPrecisionList().get(i));
               map_scaleList.add(cblist.getScalelist().get(i));
            } else if ("BLOB".equalsIgnoreCase((String)cblist.getTypeList().get(i))) {
               map_typeList.add("binary");
               map_precisionList.add(cblist.getPrecisionList().get(i));
               map_scaleList.add(cblist.getScalelist().get(i));
            } else if ("VARCHAR".equalsIgnoreCase((String)cblist.getTypeList().get(i))) {
               map_typeList.add("string");
               map_precisionList.add(cblist.getPrecisionList().get(i));
               map_scaleList.add(cblist.getScalelist().get(i));
            } else if ("DECIMAL".equalsIgnoreCase((String)cblist.getTypeList().get(i))) {
               map_typeList.add("decimal");
               map_precisionList.add(cblist.getPrecisionList().get(i));
               map_scaleList.add(cblist.getScalelist().get(i));
            } else if ("CLOB".equalsIgnoreCase((String)cblist.getTypeList().get(i))) {
               map_typeList.add("text");
               map_precisionList.add(cblist.getPrecisionList().get(i));
               map_scaleList.add(cblist.getScalelist().get(i));
            } else if ("REAL".equalsIgnoreCase((String)cblist.getTypeList().get(i))) {
               map_typeList.add("double");
               map_precisionList.add(cblist.getPrecisionList().get(i));
               map_scaleList.add(cblist.getScalelist().get(i));
            } else if ("NCHAR".equalsIgnoreCase((String)cblist.getTypeList().get(i))) {
               map_typeList.add("string");
               map_precisionList.add(cblist.getPrecisionList().get(i));
               map_scaleList.add(cblist.getScalelist().get(i));
            } else if ("NCLOB".equalsIgnoreCase((String)cblist.getTypeList().get(i))) {
               map_typeList.add("string");
               map_precisionList.add(cblist.getPrecisionList().get(i));
               map_scaleList.add(cblist.getScalelist().get(i));
            } else if ("FLOAT".equalsIgnoreCase((String)cblist.getTypeList().get(i))) {
               map_typeList.add("double");
               map_precisionList.add(cblist.getPrecisionList().get(i));
               map_scaleList.add(cblist.getScalelist().get(i));
            } else if ("LONGVARCHAR".equalsIgnoreCase((String)cblist.getTypeList().get(i))) {
               map_typeList.add("text");
               map_precisionList.add(cblist.getPrecisionList().get(i));
               map_scaleList.add(cblist.getScalelist().get(i));
            } else if ("TINYINT".equalsIgnoreCase((String)cblist.getTypeList().get(i))) {
               map_typeList.add("double");
               map_precisionList.add(cblist.getPrecisionList().get(i));
               map_scaleList.add(cblist.getScalelist().get(i));
            } else if ("INTEGER".equalsIgnoreCase((String)cblist.getTypeList().get(i))) {
               map_typeList.add("double");
               map_precisionList.add(cblist.getPrecisionList().get(i));
               map_scaleList.add(cblist.getScalelist().get(i));
            } else if ("NVARCHAR".equalsIgnoreCase((String)cblist.getTypeList().get(i))) {
               map_typeList.add("string");
               map_precisionList.add(cblist.getPrecisionList().get(i));
               map_scaleList.add(cblist.getScalelist().get(i));
            } else if ("VARCHAR2".equalsIgnoreCase((String)cblist.getTypeList().get(i))) {
               map_typeList.add("string");
               map_precisionList.add(cblist.getPrecisionList().get(i));
               map_scaleList.add(cblist.getScalelist().get(i));
            } else {
               map_typeList.add(((String)cblist.getTypeList().get(i)).toLowerCase());
               map_precisionList.add(cblist.getPrecisionList().get(i));
               map_scaleList.add(cblist.getScalelist().get(i));
            }
         }
      }

      mapping_cd.setNameList(cblist.getNameList());
      mapping_cd.setMappingLengthList(map_precisionList);
      mapping_cd.setMappingTypeList(map_typeList);
      mapping_cd.setMappingScalelist(map_scaleList);
      map_bean.setCb(mapping_cd);
      new MAPPING();
      Mappingutil mu = new Mappingutil();

      try {
         MAPPING var43 = mu.getMapping(map_bean);
         folder.getFOLDERVERSIONOrCONFIGOrSCHEDULEROrTASKOrSESSIONOrWORKLETOrWORKFLOWOrSOURCEOrTARGETOrTRANSFORMATIONOrMAPPLETOrMAPPINGOrSHORTCUTOrEXPRMACRO().add(var43);
         new CONFIG();
         CONFIG var45 = Configutil.getConfig();
         folder.getFOLDERVERSIONOrCONFIGOrSCHEDULEROrTASKOrSESSIONOrWORKLETOrWORKFLOWOrSOURCEOrTARGETOrTRANSFORMATIONOrMAPPLETOrMAPPINGOrSHORTCUTOrEXPRMACRO().add(var45);
         WorkflowBean work_bean = new WorkflowBean();
         work_bean.setWorkflow_name("wf_" + sou_bean.getSo_name());
         work_bean.setMapping_name(map_bean.getMapping_name());
         work_bean.setSourcename(sou_bean.getSo_name());
         work_bean.setTargetname(tar_bean.getTar_name());
         work_bean.setSqname(map_bean.getSq_name());
         work_bean.setTargetdbname(tar_bean.getTar_name());
         work_bean.setSourcedbname(sou_bean.getSo_name());
         work_bean.setSourceownername(sou_bean.getSourceownername());
         work_bean.setTargetownername(tar_bean.getTargetownername());
         work_bean.setMappletbean(mapplet);
         work_bean.setSou_linkname(sou_bean.getLinkname());
         work_bean.setTar_linkname(tar_bean.getLinkname());
         work_bean.setParampath(par_bean.getParam_path());
         work_bean.setSource_dbtype(sou_bean.getDatabasetype());
         work_bean.setTarget_dbtype(tar_bean.getDatabasetype());
         new WORKFLOW();
         Workflowutil wu = new Workflowutil();
         WORKFLOW var47 = wu.getWorkflow(sou_bean, tar_bean, work_bean, tengine);
         folder.getFOLDERVERSIONOrCONFIGOrSCHEDULEROrTASKOrSESSIONOrWORKLETOrWORKFLOWOrSOURCEOrTARGETOrTRANSFORMATIONOrMAPPLETOrMAPPINGOrSHORTCUTOrEXPRMACRO().add(var47);
         repository.getFOLDER().add(folder);
         powermart.getREPOSITORY().add(repository);
         List<Object> alllist = new ArrayList();
         alllist.add(powermart);
         OutputStream f = null;
         FileTool.createFolder(tengine.getXmldir() + File.separator + par_bean.getTaskname());
         OutputStream var51 = new FileOutputStream(tengine.getXmldir() + File.separator + par_bean.getTaskname() + File.separator + sou_bean.getSo_name() + ".xml");
         ToObjutil.marshalmapplet("com.chenyang.nse.bussiness.xmlbean", alllist, var51, "");
      } catch (Exception e) {
         e.printStackTrace();
      }

   }
}
