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

import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.SOURCE;
import com.chenyang.nse.bussiness.engine.infa.basic.xmlbean.SOURCEFIELD;
import com.chenyang.nse.bussiness.engine.infa.powercenter961.pubbean.SourceBean;
import java.util.ArrayList;
import java.util.List;

public class GroupSourceUtil {
   public List<SOURCE> getGroupSource(List<SourceBean> sb) {
      List<SOURCE> so_list = new ArrayList();
      if (sb != null && sb.size() > 0) {
         for(int i = 0; i < sb.size(); ++i) {
            SOURCE source = new SOURCE();
            source.setBUSINESSNAME(((SourceBean)sb.get(i)).getBusinessname());
            source.setDATABASETYPE(((SourceBean)sb.get(i)).getDatabasetype());
            source.setDBDNAME(((SourceBean)sb.get(i)).getDbdname());
            source.setDESCRIPTION(((SourceBean)sb.get(i)).getDescripion());
            source.setNAME(((SourceBean)sb.get(i)).getSo_name());
            source.setOBJECTVERSION("1");
            source.setOWNERNAME(((SourceBean)sb.get(i)).getSo_ownname());
            source.setVERSIONNUMBER("1");

            for(SOURCEFIELD sourcefield : this.getConvertTableInfoToSOURCEFIELD(((SourceBean)sb.get(i)).getCb().getKeytype(), ((SourceBean)sb.get(i)).getCb().getNameList(), ((SourceBean)sb.get(i)).getCb().getTypeList(), ((SourceBean)sb.get(i)).getCb().getLengthList(), ((SourceBean)sb.get(i)).getCb().getScalelist(), ((SourceBean)sb.get(i)).getCb().getPrecisionList())) {
               source.getSOURCEFIELD().add(sourcefield);
            }

            so_list.add(source);
         }
      }

      return so_list;
   }

   public List<SOURCEFIELD> getConvertTableInfoToSOURCEFIELD(List<String> keytype, List<String> nameList, List<String> typeList, List<String> lengthList, List<String> scalelist, List<String> precisionlist) {
      String tmpFieldName = "";
      List sourceFieldList = new ArrayList();
      int phyOffset = 0;
      int prePhyOffset = 0;
      int offset = 0;
      int preOffset = 0;
      String[] sourceFieldInfoStr = null;

      for(int i = 0; i < nameList.size(); ++i) {
         SOURCEFIELD sourceField = new SOURCEFIELD();
         sourceField.setBUSINESSNAME("");
         sourceField.setDATATYPE((String)typeList.get(i));
         sourceField.setDESCRIPTION("");
         sourceField.setFIELDNUMBER(i + 1 + "");
         sourceField.setFIELDPROPERTY("0");
         sourceField.setFIELDTYPE("ELEMITEM");
         sourceField.setHIDDEN("NO");
         sourceField.setKEYTYPE((String)keytype.get(i));
         sourceField.setLENGTH((String)lengthList.get(i));
         sourceField.setLEVEL("0");
         sourceField.setNAME((String)nameList.get(i));
         if ("PRIMARY KEY".equals(keytype.get(i))) {
            sourceField.setNULLABLE("NOTNULL");
         } else {
            sourceField.setNULLABLE("NULL");
         }

         sourceField.setOCCURS("0");
         offset += preOffset;
         sourceField.setOFFSET(offset + "");
         sourceField.setPHYSICALLENGTH((String)lengthList.get(i));
         phyOffset += prePhyOffset;
         sourceField.setPHYSICALOFFSET(phyOffset + "");
         sourceField.setPICTURETEXT("");
         sourceField.setPRECISION((String)precisionlist.get(i));
         sourceField.setSCALE((String)scalelist.get(i));
         sourceField.setUSAGEFLAGS("");
         sourceFieldList.add(sourceField);
      }

      return sourceFieldList;
   }
}
