 package com.connor.erp.ItemAddTransferSendToERP;
 
import com.connor.erp.ItemImport.ItemImportXMLReader;
// import com.connor.erp.ItemImport.TransferItemXMLWriter;
// import com.connor.erp.ItemImport.ValidateItemXMLWriter;
 import com.connor.erp.ProcessImport.ValidateFailDialog;
import com.connor.erp.processresource.EquipmentAcquiredFromERP.TCPreferenceUitl;
import com.connor.erp.toolkit.AimType;
 import com.connor.erp.toolkit.FileType;
import com.connor.erp.toolkit.HttpUtil;
import com.connor.erp.toolkit.UtilApi;
import com.connor.erpintegration.entity.CheckResult;
import com.connor.erpintegration.entity.DepResEntity;
import com.connor.erpintegration.entity.ERPResultEntity;
// import com.connor.tcerp.conn.PlmErpIntegration;
 import com.teamcenter.rac.aif.AbstractAIFDialog;
 import com.teamcenter.rac.aif.AbstractAIFUIApplication;
 import com.teamcenter.rac.aifrcp.AIFUtility;
import com.teamcenter.rac.common.actions.NewAddressListAction;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentForm;
import com.teamcenter.rac.kernel.TCComponentItem;
import com.teamcenter.rac.kernel.TCComponentItemRevision;
 import com.teamcenter.rac.kernel.TCException;
 import com.teamcenter.rac.kernel.TCPreferenceService;
 import com.teamcenter.rac.kernel.TCProperty;
 import com.teamcenter.rac.kernel.TCSession;
 import com.teamcenter.rac.kernel.TCUserService;
 import com.teamcenter.rac.util.MessageBox;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.awt.BorderLayout;
 import java.awt.Component;
 import java.awt.Dimension;
 import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 import java.io.File;
 import java.io.FileNotFoundException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
 import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
 import java.util.Vector;
 import javax.swing.DefaultCellEditor;
 import javax.swing.JButton;
 import javax.swing.JCheckBox;
 import javax.swing.JComboBox;
 import javax.swing.JComponent;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
 import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
 import javax.swing.UIManager;
 import javax.swing.event.TableModelEvent;
 import javax.swing.event.TableModelListener;
 import javax.swing.table.DefaultTableModel;
 import javax.swing.table.JTableHeader;
 import javax.swing.table.TableCellRenderer;
 import javax.swing.table.TableColumnModel;
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.transform.TransformerException;
 
 public class SendToERPDialog extends AbstractAIFDialog {
   private static final long serialVersionUID = 1L;
   
   private TCSession session;
   
   private AbstractAIFUIApplication application;
   
   private List<TCComponentItemRevision> ItemRevision;
   
   private ResourceBundle resource = ResourceBundle.getBundle("tcUser_erpUser");
   
	private SimpleDateFormat df = new SimpleDateFormat("yyyy-M-dd HH:mm");
	
	private String errInfo = "";
   
   public class CheckHeaderCellRenderer implements TableCellRenderer {
     SendToERPDialog.CheckTableModle tableModel;
     
     JTableHeader tableHeader;
     
     final JCheckBox selectBox0;
     
     final JCheckBox selectBox4;
     
     final JCheckBox selectBox5;
     
     final JCheckBox selectBox6;
     
     public CheckHeaderCellRenderer(JTable table) {
/*  870 */       this.tableModel = (SendToERPDialog.CheckTableModle)table.getModel();
/*  871 */       this.tableHeader = table.getTableHeader();
/*  872 */       this.selectBox0 = new JCheckBox(this.tableModel.getColumnName(0));
/*  873 */       this.selectBox0.setSelected(true);
/*  875 */       this.selectBox4 = new JCheckBox(this.tableModel.getColumnName(4));
/*  876 */       this.selectBox4.setSelected(false);
/*  878 */       this.selectBox5 = new JCheckBox(this.tableModel.getColumnName(5));
/*  879 */       this.selectBox5.setSelected(false);
/*  881 */       this.selectBox6 = new JCheckBox(this.tableModel.getColumnName(6));
/*  882 */       this.selectBox6.setSelected(false);
/*  884 */       this.tableHeader.addMouseListener(new MouseAdapter() {
             public void mouseClicked(MouseEvent e) {
/*  886 */               if (e.getClickCount() != 1)
                 return; 
/*  889 */               int selectColumn = SendToERPDialog.CheckHeaderCellRenderer.this.tableHeader.columnAtPoint(e.getPoint());
/*  890 */               if (selectColumn == 0) {
/*  891 */                  btnImportERP.setEnabled(true);
/*  892 */                 boolean value = !SendToERPDialog.CheckHeaderCellRenderer.this.selectBox0.isSelected();
/*  893 */                 SendToERPDialog.CheckHeaderCellRenderer.this.selectBox0.setSelected(value);
/*  894 */                 SendToERPDialog.CheckHeaderCellRenderer.this.tableModel.selectAllOrNull(value, 0);
/*  895 */                 SendToERPDialog.CheckHeaderCellRenderer.this.tableHeader.repaint();
/*  896 */               } else if (selectColumn == 4) {
/*  897 */                 boolean value = !SendToERPDialog.CheckHeaderCellRenderer.this.selectBox4.isSelected();
/*  898 */                 SendToERPDialog.CheckHeaderCellRenderer.this.selectBox4.setSelected(value);
/*  899 */                 SendToERPDialog.CheckHeaderCellRenderer.this.tableModel.selectAllOrNull(value, 4);
/*  900 */                 SendToERPDialog.CheckHeaderCellRenderer.this.tableHeader.repaint();
/*  901 */               } else if (selectColumn == 5) {
/*  902 */                 boolean value = !SendToERPDialog.CheckHeaderCellRenderer.this.selectBox5.isSelected();
/*  903 */                 SendToERPDialog.CheckHeaderCellRenderer.this.selectBox5.setSelected(value);
/*  904 */                 SendToERPDialog.CheckHeaderCellRenderer.this.tableModel.selectAllOrNull(value, 5);
/*  905 */                 SendToERPDialog.CheckHeaderCellRenderer.this.tableHeader.repaint();
/*  906 */               } else if (selectColumn == 6) {
/*  907 */                 boolean value = !SendToERPDialog.CheckHeaderCellRenderer.this.selectBox6.isSelected();
/*  908 */                 SendToERPDialog.CheckHeaderCellRenderer.this.selectBox6.setSelected(value);
/*  909 */                 SendToERPDialog.CheckHeaderCellRenderer.this.tableModel.selectAllOrNull(value, 6);
/*  910 */                 SendToERPDialog.CheckHeaderCellRenderer.this.tableHeader.repaint();
               } else {
/*  912 */                 SendToERPDialog.CheckHeaderCellRenderer.this.tableModel.sort(selectColumn);
/*  913 */                 SendToERPDialog.CheckHeaderCellRenderer.this.tableModel.fireTableDataChanged();
               } 
             }
           });
     }
     
     public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
/*  922 */       String valueStr = (String)value;
/*  923 */       JLabel label = new JLabel(valueStr);
/*  924 */       label.setHorizontalAlignment(0);
/*  925 */       this.selectBox0.setHorizontalAlignment(0);
/*  926 */       this.selectBox0.setBorderPainted(true);
/*  927 */       this.selectBox4.setHorizontalAlignment(0);
/*  928 */       this.selectBox4.setBorderPainted(true);
/*  929 */       this.selectBox5.setHorizontalAlignment(0);
/*  930 */       this.selectBox5.setBorderPainted(true);
/*  931 */       this.selectBox6.setHorizontalAlignment(0);
/*  932 */       this.selectBox6.setBorderPainted(true);
/*  933 */       JComponent component = label;
/*  934 */       if (column == 0) {
/*  935 */         component = this.selectBox0;
/*  936 */       } else if (column == 4) {
/*  937 */         component = this.selectBox4;
/*  938 */       } else if (column == 5) {
/*  939 */         component = this.selectBox5;
/*  940 */       } else if (column == 6) {
/*  941 */         component = this.selectBox6;
       } 
/*  943 */       component.setForeground(this.tableHeader.getForeground());
/*  944 */       component.setBackground(this.tableHeader.getBackground());
/*  945 */       component.setFont(this.tableHeader.getFont());
/*  946 */       component.setBorder(UIManager.getBorder("TableHeader.cellBorder"));
/*  948 */       return component;
     }
   }
   
   private class NHLItemERPTypeDefine {
     private String typeName;
     
     private int templateId;
     
     private ArrayList<String> TCObjectType;
     
     private ArrayList<String> TCNHLwlzt;
     
     private NHLItemERPTypeDefine() {}
     
     public void setTypeName(String typeName) {
       this.typeName = typeName;
     }
     
     public String getTypeName() {
       return this.typeName;
     }
     
     public void setTemplateId(int templateId) {
       this.templateId = templateId;
     }
     
     public int getTemplateId() {
       return this.templateId;
     }
     
     public void putTCObjectType(String TCObjectType) {
       if (this.TCObjectType == null)
         this.TCObjectType = new ArrayList<String>(); 
       this.TCObjectType.add(TCObjectType);
     }
     
     public void putTCNHLwlzt(String nhlwlzt) {
       if (this.TCNHLwlzt == null)
         this.TCNHLwlzt = new ArrayList<String>(); 
       this.TCNHLwlzt.add(nhlwlzt);
     }
     
     public boolean check(String objectType, String nhlwlzt) {
       System.out.println("objectType:" + objectType + ";nhlwlzt:" + nhlwlzt);
       boolean result = false;
       if (objectType == null || this.TCObjectType == null || objectType.trim().equals(""))
         return false; 
       if (this.TCObjectType.contains(objectType)) {
         result = true;
       } else {
         return false;
       } 
       if (this.TCNHLwlzt != null && nhlwlzt != null && !nhlwlzt.trim().equals(""))
         if (this.TCNHLwlzt.contains(nhlwlzt)) {
           result = true;
         } else {
           result = false;
         }  
       return result;
     }
   }
   
   private class NHLItemERPTypePreference {
     private ArrayList<SendToERPDialog.NHLItemERPTypeDefine> nhlItemERPTypeDefineArr;
     
     private HashMap<String, String> ItemERPTypeTemplate;
     
     private String selectTypeName;
     
     private int selectTemplateId;
     
     private NHLItemERPTypePreference() {}
     
     public ArrayList<SendToERPDialog.NHLItemERPTypeDefine> GetTypeDefineArr() {
       return this.nhlItemERPTypeDefineArr;
     }
     
     public HashMap<String, String> GetTypeTemplateMap() {
       return this.ItemERPTypeTemplate;
     }
     
     public boolean LoadFromPreference(TCSession session, String preferenceName) {
       this.nhlItemERPTypeDefineArr = new ArrayList<SendToERPDialog.NHLItemERPTypeDefine>();
       this.ItemERPTypeTemplate = new HashMap<String, String>();
       TCPreferenceService tcps = session.getPreferenceService();
       String[] pfStr = tcps.getStringArray(4, preferenceName);
       if (pfStr == null || pfStr.length == 0) {
         MessageBox.post("首选项" + preferenceName + "尚未配置，请联系系统管理员！", "提示", 4);
         return false;
       } 
       for (int index = 0; index < pfStr.length; index++) {
         String pfLine = pfStr[index].replace("：", ":").replace("，", ",");
         String[] segmentStr = pfLine.split(":");
         SendToERPDialog.NHLItemERPTypeDefine nit = new SendToERPDialog.NHLItemERPTypeDefine();
         nit.setTypeName(segmentStr[0]);
         nit.setTemplateId(Integer.parseInt(segmentStr[1]));
         this.ItemERPTypeTemplate.put(segmentStr[0], segmentStr[1]);
         String[] objType = segmentStr[2].split(",");
         for (int otIndex = 0; otIndex < objType.length; otIndex++)
           nit.putTCObjectType(objType[otIndex]); 
         if (segmentStr.length == 4 && !segmentStr[3].trim().equals("")) {
           System.out.println(segmentStr[3]);
           String[] wlzt = segmentStr[3].split(",");
           for (int wlztIndex = 0; wlztIndex < wlzt.length; wlztIndex++)
             nit.putTCNHLwlzt(wlzt[wlztIndex]); 
         } 
         this.nhlItemERPTypeDefineArr.add(nit);
       } 
       return true;
     }
     
     public boolean Select(String tcObjectType, String nhlwlzt) {
       if (this.nhlItemERPTypeDefineArr == null)
         return false; 
       for (int index = 0; index < this.nhlItemERPTypeDefineArr.size(); index++) {
         SendToERPDialog.NHLItemERPTypeDefine nit = this.nhlItemERPTypeDefineArr.get(index);
         if (nit.check(tcObjectType, nhlwlzt)) {
           this.selectTypeName = nit.getTypeName();
           this.selectTemplateId = nit.getTemplateId();
           System.out.println("selectTypeName:" + this.selectTypeName);
           System.out.println("selectTemplateId:" + this.selectTemplateId);
           return true;
         } 
       } 
       return false;
     }
     
     public String getSelectTypeName() {
       return this.selectTypeName;
     }
     
     public int getSelectTemplateId() {
       return this.selectTemplateId;
     }
   }
   
   private List<String> item_id = new ArrayList<String>();
   
   private List<String> object_name = new ArrayList<String>();
   
   private List<String> nhl8_uom_tag = new ArrayList<String>();
   
   private List<String> nhl8_wllx = new ArrayList<String>();
   
   private List<String> nhl8_templateID = new ArrayList<String>();
   
   private List<Boolean> nhl8_ProdrOrg = new ArrayList<Boolean>();
   
   private List<Boolean> nhl8_SpareOrg = new ArrayList<Boolean>();
   
   private List<Boolean> nhl8_UnprodrOrg = new ArrayList<Boolean>();
   
   private List<String> nhl8_wlzt = new ArrayList<String>();
   
   private List<String> nhl8_MaxType = new ArrayList<String>();
   
   private List<String> nhl8_MinType = new ArrayList<String>();
   
   private List<Boolean> Nhl8_IfToERP = new ArrayList<Boolean>();
   
   private List<String> Info = new ArrayList<String>();
   
   private Map<String, String> wllxToTemIdMap = new HashMap<String, String>();
   
   private Map<String, String> temIdToWllxMap = new HashMap<String, String>();
   
   private List<Integer> listNotEditableRow = new ArrayList<Integer>();
   
   private JTable table;
   
   private Boolean isCellEdit = Boolean.valueOf(false);
   
   private String xmlFilePath;
   
   private String user_id = "";
   
   private JButton btnCheck;
   
   private JButton btnCancel;
   
   private JButton btnImportERP;
   
   private JButton btnEditProp;
   
   private JButton btnSaveProp;
   
   private String[] NHL_ItemtypeMould;
   
   private Map<String, String[]> typeToProsMap;
   
   private NHLItemERPTypePreference nitp = new NHLItemERPTypePreference();
   
   private String erpPreference = "connor_tc_to_erp_url";
	private String itemAddCheckSvr = "ITEMAddCheck";
//	private String PLMImpResultSvr = "PLMImpResult";
	private String itemAddSvr = "ITEMAdd";
	private boolean canAllImport ;
   
   public SendToERPDialog(TCSession pSession, String[] NHL_ItemtypeMould, List<TCComponentItemRevision> itemRevision, Map<String, String[]> typeToProsMap) {
     super(false);
     System.out.println("进入发送erp菜单初始化");
     this.session = pSession;
     this.NHL_ItemtypeMould = NHL_ItemtypeMould;
     this.ItemRevision = itemRevision;
     this.application = AIFUtility.getCurrentApplication();
     this.typeToProsMap = typeToProsMap;
     this.nitp.LoadFromPreference(this.session, "NHL_ItemERPType");
     initUI();
   }
   
   private void initUI() {
     setTitle("基础物料传递");
     this.btnCheck = new JButton("导入ERP前检查数据");
     this.btnImportERP = new JButton("导入ERP");
     this.btnImportERP.setEnabled(false);
     this.btnEditProp = new JButton("编辑属性");
     this.btnSaveProp = new JButton("保存属性");
     this.btnCancel = new JButton("取消");
     JPanel formPanel = new JPanel(new FlowLayout());
     JPanel paneButton = new JPanel(new FlowLayout());
     paneButton.add(this.btnCheck);
     paneButton.add(this.btnImportERP);
     paneButton.add(this.btnEditProp);
     paneButton.add(this.btnSaveProp);
     paneButton.add(this.btnCancel);
     Vector<String> headerNames = new Vector<String>();
     headerNames.add("是否选择传递ERP");
     headerNames.add("件号");
     headerNames.add("名称");
     headerNames.add("计量单位");
     headerNames.add("生产组织");
     headerNames.add("备件组织");
     headerNames.add("非生产组织");
     headerNames.add("物料状态");
     headerNames.add("物料类型");
     headerNames.add("导入模板");
     headerNames.add("项目大类");
     headerNames.add("项目小类");
     int revNum = this.ItemRevision.size();
//     this.NHL_ItemtypeMould.length;
     this.wllxToTemIdMap = this.nitp.GetTypeTemplateMap();
     Set<String> ts = this.wllxToTemIdMap.keySet();
     for (Iterator<String> it = ts.iterator(); it.hasNext(); ) {
       String key_wllx = it.next();
       String val_templateId = this.wllxToTemIdMap.get(key_wllx);
       this.temIdToWllxMap.put(val_templateId, key_wllx);
     } 
     for (int i = 0; i < revNum; i++) {
       TCComponentItemRevision itemRev = this.ItemRevision.get(i);
       String revType = itemRev.getType();
       System.out.println("revType=" + revType);
       System.out.println("typeToProsMap=" + this.typeToProsMap);
       String[] formOtherProNames = this.typeToProsMap.get(revType);
       TCComponentForm form = null;
       try {
         form = (TCComponentForm)itemRev.getRelatedComponent("IMAN_master_form_rev");
         String[] revProName = { "item_id", "object_name" };
         String[] revProValue = itemRev.getProperties(revProName);
         this.item_id.add(revProValue[0]);
         StringBuffer nameSb = new StringBuffer(revProValue[1]);
         String uom_tag = itemRev.getItem().getProperty("uom_tag");
         if (uom_tag.equals("") || uom_tag.equals("每个")) {
           this.nhl8_uom_tag.add("个");
         } else {
           this.nhl8_uom_tag.add(uom_tag);
         } 
         TCProperty[] formProperties = null;
         if (formOtherProNames != null) {
           int formOtherProNameLen = formOtherProNames.length;
           String[] formProName = new String[formOtherProNameLen + 6];
           formProName[0] = "nhl8_wlzt";
           formProName[1] = "nhl8_ProdrOrg";
           formProName[2] = "nhl8_SpareOrg";
           formProName[3] = "nhl8_UnprodrOrg";
           formProName[4] = "nhl8_MaxType";
           formProName[5] = "nhl8_MinType";
           int j;
           for (j = 0; j < formOtherProNameLen; j++)
             formProName[j + 6] = formOtherProNames[j]; 
           formProperties = form.getTCProperties(formProName);
           for (j = 0; j < formOtherProNameLen; j++) {
             TCProperty tcproperty = formProperties[j + 6];
             int propertyType = tcproperty.getPropertyType();
             if (propertyType == 8) {
               nameSb.append("|").append(tcproperty.getStringValue().trim());
             } else if (propertyType == 3) {
               nameSb.append("|").append(String.valueOf(tcproperty.getDoubleValue()));
             } else if (propertyType == 6) {
               nameSb.append("|").append(String.valueOf(tcproperty.getLogicalValue()));
             } 
           } 
         } else {
           String[] formProName = new String[6];
           formProName[0] = "nhl8_wlzt";
           formProName[1] = "nhl8_ProdrOrg";
           formProName[2] = "nhl8_SpareOrg";
           formProName[3] = "nhl8_UnprodrOrg";
           formProName[4] = "nhl8_MaxType";
           formProName[5] = "nhl8_MinType";
           formProperties = form.getTCProperties(formProName);
         } 
         this.object_name.add(nameSb.toString());
         String str_nhl8_wlzt = formProperties[0].getStringValue();
         this.nhl8_wlzt.add(str_nhl8_wlzt);
         String nhl8_templateIDStr = form.getProperty("nhl8_templateID");
         if (nhl8_templateIDStr.equals("")) {
           String itemType = itemRev.getItem().getType();
           if (this.nitp.Select(itemType, str_nhl8_wlzt)) {
             this.nhl8_wllx.add(this.nitp.getSelectTypeName());
             this.nhl8_templateID.add((new StringBuilder(String.valueOf(this.nitp.getSelectTemplateId()))).toString());
           } else {
             this.nhl8_wllx.add("请选择");
             this.nhl8_templateID.add("请选择");
           } 
         } else {
           this.nhl8_templateID.add(nhl8_templateIDStr);
           this.nhl8_wllx.add(this.temIdToWllxMap.get(nhl8_templateIDStr));
         } 
         this.nhl8_ProdrOrg.add(Boolean.valueOf(formProperties[1].getLogicalValue()));
         this.nhl8_SpareOrg.add(Boolean.valueOf(formProperties[2].getLogicalValue()));
         this.nhl8_UnprodrOrg.add(Boolean.valueOf(formProperties[3].getLogicalValue()));
         this.nhl8_MaxType.add(formProperties[4].getStringValue());
         this.nhl8_MinType.add(formProperties[5].getStringValue());
         this.Nhl8_IfToERP.add(Boolean.valueOf(true));
       } catch (TCException e) {
         e.printStackTrace();
       } 
     } 
     this.table = new JTable() {
         private static final long serialVersionUID = 1L;
         
         public boolean isCellEditable(int row, int column) {
           if (column == 0 || column == 4 || column == 5 || column == 6)
             return true; 
           if (column == 8 || column == 9 || column == 10 || column == 11)
             return SendToERPDialog.this.isCellEdit.booleanValue(); 
           return false;
         }
       };
     JComboBox<String> jcom_wllx = new JComboBox();
     JComboBox<String> jcom_templateID = new JComboBox();
     Set<String> set = this.wllxToTemIdMap.keySet();
     Iterator<String> iter = set.iterator();
     jcom_wllx.addItem("");
     jcom_templateID.addItem("");
     while (iter.hasNext()) {
       String wllx = iter.next();
       jcom_wllx.addItem(wllx);
       jcom_templateID.addItem(this.wllxToTemIdMap.get(wllx));
     } 
     this.table.setModel(new CheckTableModle(getData(), headerNames));
     this.table.getTableHeader().setDefaultRenderer(new CheckHeaderCellRenderer(this.table));
     JComboBox Jcom = new JComboBox();
     TableColumnModel tcm = this.table.getColumnModel();
     tcm.getColumn(8).setCellEditor(new DefaultCellEditor(jcom_wllx));
     tcm.getColumn(9).setCellEditor(new DefaultCellEditor(jcom_templateID));
     tcm.getColumn(10).setCellEditor(new DefaultCellEditor(Jcom));
     tcm.getColumn(11).setCellEditor(new DefaultCellEditor(Jcom));
     this.table.getColumnModel().getColumn(0).setPreferredWidth(130);
     JScrollPane jsp = new JScrollPane(this.table);
     jsp.setPreferredSize(new Dimension(850, 500));
     formPanel.add(jsp);
     JPanel paneMain = new JPanel(new BorderLayout());
     paneMain.add("Center", jsp);
     paneMain.add("South", paneButton);
     getContentPane().add(paneMain);
     setPreferredSize(new Dimension(900, 580));
     this.table.getModel().addTableModelListener(new TableModelListener() {
           public void tableChanged(TableModelEvent tablemodelevent) {
             int row = SendToERPDialog.this.table.getEditingRow();
             int col = SendToERPDialog.this.table.getEditingColumn();
             if (col == 0) {
               SendToERPDialog.this.btnImportERP.setEnabled(false);
             } else if (col == 8) {
               String nhl8_wllx_value = SendToERPDialog.this.table.getValueAt(row, 8).toString();
               String nhl8_templateID_value = SendToERPDialog.this.table.getValueAt(row, 9).toString();
               String new_templateID_value = (String)SendToERPDialog.this.wllxToTemIdMap.get(nhl8_wllx_value);
               if (new_templateID_value != null && !nhl8_templateID_value.equals(new_templateID_value))
                 SendToERPDialog.this.table.setValueAt(new_templateID_value, row, 9); 
             } else if (col == 9) {
               String nhl8_wllx_value = SendToERPDialog.this.table.getValueAt(row, 8).toString();
               String nhl8_templateID_value = SendToERPDialog.this.table.getValueAt(row, 9).toString();
               String new_wllx_value = (String)SendToERPDialog.this.temIdToWllxMap.get(nhl8_templateID_value);
               if (new_wllx_value != null && !nhl8_wllx_value.equals(new_wllx_value))
                 SendToERPDialog.this.table.setValueAt(new_wllx_value, row, 8); 
             } 
           }
         });
     this.btnEditProp.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
             SendToERPDialog.this.btnCheck.setEnabled(false);
             SendToERPDialog.this.btnImportERP.setEnabled(false);
             SendToERPDialog.this.isCellEdit = Boolean.valueOf(true);
           }
         });
     this.btnSaveProp.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
             Object[] obj = new Object[1];
             obj[0] = Integer.valueOf(11);
             TCUserService service = SendToERPDialog.this.session.getUserService();
             String[] formProNames = { "nhl8_wlzt", "nhl8_templateID", "nhl8_MaxType", "nhl8_MinType", "nhl8_ProdrOrg", "nhl8_SpareOrg", "nhl8_UnprodrOrg" };
             for (int i = 0; i < SendToERPDialog.this.item_id.size(); i++) {
               String nhl8_wlzt_value = (String)SendToERPDialog.this.table.getValueAt(i, 7);
               String nhl8_templateID_value = (String)SendToERPDialog.this.table.getValueAt(i, 9);
               String nhl8_MaxType_value = (String)SendToERPDialog.this.table.getValueAt(i, 10);
               String nhl8_MinType_value = (String)SendToERPDialog.this.table.getValueAt(i, 11);
               Boolean nhl8_ProdrOrg_value = (Boolean)SendToERPDialog.this.table.getValueAt(i, 4);
               Boolean nhl8_SpareOrg_value = (Boolean)SendToERPDialog.this.table.getValueAt(i, 5);
               Boolean nhl8_UnprodrOrg_value = (Boolean)SendToERPDialog.this.table.getValueAt(i, 6);
               try {
                 service.call("openByPass", obj);
                 TCComponentForm form = (TCComponentForm)((TCComponentItemRevision)SendToERPDialog.this.ItemRevision.get(i)).getRelatedComponent("IMAN_master_form_rev");
                 TCProperty[] formProperties = form.getTCProperties(formProNames);
                 formProperties[0].setStringValue(nhl8_wlzt_value);
                 formProperties[1].setStringValue(nhl8_templateID_value);
                 formProperties[2].setStringValue(nhl8_MaxType_value);
                 formProperties[3].setStringValue(nhl8_MinType_value);
                 formProperties[4].setLogicalValue(nhl8_ProdrOrg_value.booleanValue());
                 formProperties[5].setLogicalValue(nhl8_SpareOrg_value.booleanValue());
                 formProperties[6].setLogicalValue(nhl8_UnprodrOrg_value.booleanValue());
                 form.setTCProperties(formProperties);
                 form.save();
                 service.call("closeByPass", obj);
               } catch (TCException e1) {
                 e1.printStackTrace();
               } 
             } 
             SendToERPDialog.this.btnCheck.setEnabled(true);
             SendToERPDialog.this.isCellEdit = Boolean.valueOf(false);
           }
         });
     this.btnCancel.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
             SendToERPDialog.this.dispose();
           }
         });
     this.btnCheck.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
             boolean isContinue = true;
//             List<String> item_id_list = new ArrayList<String>();
//             List<String> uom_code_list = new ArrayList<String>();
             Boolean isTypeError = Boolean.valueOf(false);
             Boolean isZuzhiError = Boolean.valueOf(false);
             ArrayList<Integer> partTranNums = new ArrayList<Integer>();
             List<Map<String, String>> itemCheckList = new ArrayList<Map<String,String>>();
             Map<String, List<Map<String, String>>> itemCheckMap = new HashMap<String, List<Map<String,String>>>();
             for (int i = 0; i < SendToERPDialog.this.item_id.size(); i++) {
               Boolean Nhl8_IfToERP_value = (Boolean)SendToERPDialog.this.table.getValueAt(i, 0);
               if (Nhl8_IfToERP_value.booleanValue()) {
                 if (SendToERPDialog.this.table.getValueAt(i, 8).equals("请选择") || SendToERPDialog.this.table.getValueAt(i, 9).equals("请选择")) {
                   isTypeError = Boolean.valueOf(true);
                   isContinue = false;
                 } else if (SendToERPDialog.this.table.getValueAt(i, 4).equals(Boolean.valueOf(false)) && SendToERPDialog.this.table.getValueAt(i, 5).equals(Boolean.valueOf(false)) && SendToERPDialog.this.table.getValueAt(i, 6).equals(Boolean.valueOf(false))) {
                   isZuzhiError = Boolean.valueOf(true);
                   isContinue = false;
                 } 
                 SendToERPDialog.CheckTableModle model = (SendToERPDialog.CheckTableModle)SendToERPDialog.this.table.getModel();
                 int rowNum = model.getRowNum(i);
                 partTranNums.add(Integer.valueOf(i));
                 Map<String, String> map = new HashMap<String,String>();
                 map.put("item", item_id.get(rowNum));
                 map.put("uom", nhl8_uom_tag.get(rowNum));
                 itemCheckList.add(map);
//                 item_id_list.add(SendToERPDialog.this.item_id.get(rowNum));
//                 uom_code_list.add(SendToERPDialog.this.nhl8_uom_tag.get(rowNum));
               } 
             } 
             itemCheckMap.put("items", itemCheckList);
             if (isTypeError.booleanValue())
               MessageBox.post(SendToERPDialog.this.application.getDesktop().getShell(), "【物料类型】【导入模板】必须选择一个有效值！", "提示", 4); 
             if (isZuzhiError.booleanValue())
               MessageBox.post(SendToERPDialog.this.application.getDesktop().getShell(), "三个组织至少有一个要勾选！", "提示", 4); 
             if (isContinue) {
               try {
                 SendToERPDialog.this.user_id = SendToERPDialog.this.session.getUser().getProperty("user_id");
               } catch (TCException ex) {
                 ex.printStackTrace();
               } 
//               String xmlFileName = System.getProperty("java.io.tmpdir") + File.separator + SendToERPDialog.this.user_id + "_" + UtilApi.getCurrentTime() + "_validate_item_add";
//               SendToERPDialog.this.xmlFilePath = xmlFileName + ".xml";
//               try {
//                 ValidateItemXMLWriter.createXML(SendToERPDialog.this.user_id, SendToERPDialog.this.xmlFilePath, FileType.item, AimType.add, item_id_list, uom_code_list);
//               } catch (FileNotFoundException e1) {
//                 e1.printStackTrace();
//               } catch (ParserConfigurationException e1) {
//                 e1.printStackTrace();
//               } catch (TransformerException e1) {
//                 e1.printStackTrace();
//               } 
               int rowCount = SendToERPDialog.this.table.getRowCount();
               Map<String, String> map = new HashMap<String, String>();
               for (Iterator<Integer> iterator = partTranNums.iterator(); iterator.hasNext(); ) {
                 int j = ((Integer)iterator.next()).intValue();
                 SendToERPDialog.CheckTableModle model = (SendToERPDialog.CheckTableModle)SendToERPDialog.this.table.getModel();
                 int row = model.getRowNum(j);
                 String value1 = SendToERPDialog.this.table.getValueAt(row, 1).toString();
                 boolean value4 = ((Boolean)SendToERPDialog.this.table.getValueAt(row, 4)).booleanValue();
                 boolean value5 = ((Boolean)SendToERPDialog.this.table.getValueAt(row, 5)).booleanValue();
                 boolean value6 = ((Boolean)SendToERPDialog.this.table.getValueAt(row, 6)).booleanValue();
                 map.put(value1, String.valueOf(value4) + "-" + value5 + "-" + value6);
               } 
               
       		List<CheckResult> checkResults = doGet(itemCheckMap,itemAddCheckSvr);
    		
//               String result = UtilApi.getErrorInfoFromCode(PlmErpIntegration.validateData(SendToERPDialog.this.xmlFilePath));
//               if (!result.equals("成功")) {
//                 MessageBox.post(SendToERPDialog.this.application.getDesktop().getShell(), result, "验证出现错误", 1);
//               } else {
                 List<String> orgExistRightItems = new ArrayList<String>();
                 List<String> orgExistWrongItems = new ArrayList<String>();
                 ItemImportXMLReader.loadElements(checkResults, SendToERPDialog.this.Info, orgExistRightItems, orgExistWrongItems, map);
                 String errorInfo = SendToERPDialog.this.Info.get(0);
//                 String errorInfo = "";
                 if (errorInfo.equals("")) {
                   int orgExistRightItemNum = orgExistRightItems.size();
                   if(canAllImport){
                       MessageBox.post(SendToERPDialog.this.application.getDesktop().getShell(), "验证成功", "成功", 2);
                       SendToERPDialog.this.btnImportERP.setEnabled(true);
                     }else if (orgExistRightItemNum > 0) {
                     if (orgExistRightItemNum == rowCount) {
                       MessageBox.post(SendToERPDialog.this.application.getDesktop().getShell(), "所要导入数据在ERP均已经存在，无需导入！", "成功", 2);
                       Object[] obj = new Object[1];
                       obj[0] = Integer.valueOf(11);
                       TCUserService service = SendToERPDialog.this.session.getUserService();
                       for (Iterator<Integer> iterator1 = partTranNums.iterator(); iterator1.hasNext(); ) {
                         int j = ((Integer)iterator1.next()).intValue();
                         SendToERPDialog.CheckTableModle model = (SendToERPDialog.CheckTableModle)SendToERPDialog.this.table.getModel();
                         int row = model.getRowNum(j);
                         try {
                           service.call("openByPass", obj);
                           ((TCComponentItemRevision)SendToERPDialog.this.ItemRevision.get(row)).getRelatedComponent("IMAN_master_form_rev").setLogicalProperty("nhl8_ItemimpSucc", true);
                           service.call("closeByPass", obj);
                         } catch (TCException e1) {
                           e1.printStackTrace();
                         } 
                       } 
                       SendToERPDialog.this.dispose();
                     } else {
                       Object[] obj = new Object[1];
                       obj[0] = Integer.valueOf(11);
                       TCUserService service = SendToERPDialog.this.session.getUserService();
                       SendToERPDialog.this.listNotEditableRow = new ArrayList();
                       for (Iterator<Integer> iterator1 = partTranNums.iterator(); iterator1.hasNext(); ) {
                         int k = ((Integer)iterator1.next()).intValue();
                         String value1 = SendToERPDialog.this.table.getValueAt(k, 1).toString();
                         SendToERPDialog.CheckTableModle model = (SendToERPDialog.CheckTableModle)SendToERPDialog.this.table.getModel();
                         int row = model.getRowNum(k);
                         if (orgExistRightItems.contains(value1)) {
                           SendToERPDialog.this.table.setValueAt(Boolean.valueOf(false), k, 0);
                           SendToERPDialog.this.listNotEditableRow.add(Integer.valueOf(row));
                           try {
                             service.call("openByPass", obj);
                             ((TCComponentItemRevision)SendToERPDialog.this.ItemRevision.get(row)).getRelatedComponent("IMAN_master_form_rev").setLogicalProperty("nhl8_ItemimpSucc", true);
                             service.call("closeByPass", obj);
                           } catch (TCException e1) {
                             e1.printStackTrace();
                           } 
                         } 
                       } 
                       StringBuffer errorInfo2 = new StringBuffer("");
                       for (int j = 0; j < orgExistRightItemNum; j++)
                         errorInfo2.append(String.valueOf(orgExistRightItems.get(j)) + "在ERP中已经存在！<br>"); 
                       SwingUtilities.invokeLater((Runnable)new ValidateFailDialog("验证成功但有忽略", "警告", errorInfo2.toString()));
//                       SendToERPDialog.this.btnImportERP.setEnabled(true);
                     } 
                   }
                 } else {
                   int orgExistWrongItemNum = orgExistWrongItems.size();
                   if (orgExistWrongItemNum > 0) {
                     if (orgExistWrongItemNum == rowCount) {
                       errorInfo.replace("但在主组织中已经存在，", "");
                       SwingUtilities.invokeLater((Runnable)new ValidateFailDialog("所要导入的数据在主组织中已经存在，请查看详细！", "警告", errorInfo));
                       SendToERPDialog.this.dispose();
                     } else {
                       SwingUtilities.invokeLater((Runnable)new ValidateFailDialog("验证成功但有忽略", "警告", errorInfo));
                       Object[] obj = new Object[1];
                       obj[0] = Integer.valueOf(11);
                       TCUserService service = SendToERPDialog.this.session.getUserService();
                       SendToERPDialog.this.listNotEditableRow = new ArrayList();
                       for (Iterator<Integer> iterator1 = partTranNums.iterator(); iterator1.hasNext(); ) {
                         int j = ((Integer)iterator1.next()).intValue();
                         String value1 = SendToERPDialog.this.table.getValueAt(j, 1).toString();
                         SendToERPDialog.CheckTableModle model = (SendToERPDialog.CheckTableModle)SendToERPDialog.this.table.getModel();
                         int row = model.getRowNum(j);
                         if (orgExistWrongItems.contains(value1)) {
                           SendToERPDialog.this.table.setValueAt(Boolean.valueOf(false), j, 0);
                           SendToERPDialog.this.listNotEditableRow.add(Integer.valueOf(row));
                           try {
                             service.call("openByPass", obj);
                             ((TCComponentItemRevision)SendToERPDialog.this.ItemRevision.get(row)).getRelatedComponent("IMAN_master_form_rev").setLogicalProperty("nhl8_ItemimpSucc", true);
                             service.call("closeByPass", obj);
                           } catch (TCException e1) {
                             e1.printStackTrace();
                           } 
                         } 
                       } 
//                       SendToERPDialog.this.btnImportERP.setEnabled(true);
                     } 
                   } else {
                     SwingUtilities.invokeLater((Runnable)new ValidateFailDialog("验证失败", "错误", errorInfo));
                   } 
                 } 
//             }
             } 
           }

		
         });
     this.btnImportERP.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
        	   SendToERPOperation operation = new SendToERPOperation(application, session, table,SendToERPDialog.this,item_id,Info,ItemRevision);
        	   session.queueOperation(operation);
           }

         });
     
     
   }
   
   
//   private String doGet2(Map<String, Object> itemCheckMap, String svr) {
//		HashMap<String, String> preferenceHashMap = TCPreferenceUitl.getPreferenceHashMap(session, TCPreferenceService.TC_preference_site, erpPreference);
//		String url = preferenceHashMap.get("url");
//		String token = preferenceHashMap.get("token");
//		JSONObject itemCheckJson = JSONObject.fromObject(itemCheckMap);
//		JSONObject jsonObject2 = null;
//		try {
//			System.out.println("开始发送请求");
//			   String args = itemCheckJson.toString();
//				token = URLEncoder.encode(token,"UTF-8");
//				svr = URLEncoder.encode(svr,"UTF-8");
//				args = URLEncoder.encode(args,"UTF-8");
//				String param = "usr="+token+"&svr="+svr+"&args="+args;
//			   System.out.println("url=============="+url);
//			   System.out.println("param==========="+param);
//			   String response = HttpUtil.doGet(url+"?"+param);
//			   System.out.println("请求结果================"+response);
//			
//			JSONObject jsonObject = JSONObject.fromObject(response);
//			System.out.println("json格式==============="+jsonObject);
//			if(jsonObject.has("cont")) {
//				jsonObject2 = jsonObject.getJSONObject("cont");
//				System.out.println("cont==============="+jsonObject2);
//			}
//			
//		} catch (Exception e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}
//		return jsonObject2.toString();
//	}
   
   private List<CheckResult> doGet(Map<String, List<Map<String, String>>> itemCheckMap, String svr) {
	   HashMap<String, String> preferenceHashMap = TCPreferenceUitl.getPreferenceHashMap(session, TCPreferenceService.TC_preference_site, erpPreference);
	   String url = preferenceHashMap.get("url");
	   String token = preferenceHashMap.get("token");
	   JSONObject itemCheckJson = JSONObject.fromObject(itemCheckMap);
	   List<CheckResult> checkResults =null;
	   try {
		   System.out.println("开始发送请求");
		   String args = itemCheckJson.toString();
			token = URLEncoder.encode(token,"UTF-8");
			svr = URLEncoder.encode(svr,"UTF-8");
			args = URLEncoder.encode(args,"UTF-8");
			String param = "usr="+token+"&svr="+svr+"&args="+args;
		   System.out.println("url=============="+url);
		   System.out.println("param==========="+param);
		   String response = HttpUtil.doPost(url, param);
		   System.out.println("请求结果================"+response);
		   
		   JSONObject jsonObject = JSONObject.fromObject(response);
		   System.out.println("json格式==============="+jsonObject);
		   JSONObject jsonObject2 = jsonObject.getJSONObject("cont");
		   System.out.println("cont==============="+jsonObject2);
		   JSONArray checkResult = jsonObject2.getJSONArray("checkResult");
//		   canAllImport = true;
		   canAllImport = jsonObject2.getBoolean("canAllImport");
		   checkResults = JSONArray.toList(checkResult,CheckResult.class);
		   
	   } catch (Exception e1) {
		   // TODO Auto-generated catch block
		   e1.printStackTrace();
	   }
	   return checkResults;
   }
   
   public class CheckTableModle extends DefaultTableModel {
     private static final long serialVersionUID = 1L;
     
     private boolean SortDirection;
     
     private Row[] Rows;
     
     private int sortColumnIndex;
     
     public CheckTableModle(Vector<Object> data, Vector<String> columnNames) {
/*  960 */       super(data,columnNames);
/*  997 */       this.SortDirection = true;
       this.Rows = new Row[getRowCount()];
       for (int i = 0; i < this.Rows.length; i++)
         this.Rows[i] = new Row(i); 
     }
     
     public Class<?> getColumnClass(int c) {
       return getValueAt(0, c).getClass();
     }
     
     public void selectAllOrNull(boolean value, int column) {
       int rowNum = getRowCount();
       for (int row = 0; row < rowNum; row++) {
         if (!SendToERPDialog.this.listNotEditableRow.contains(Integer.valueOf(getRowNum(row))))
           setValueAt(Boolean.valueOf(value), row, column); 
       } 
     }
     
     public void setValueAt(Object val, int row, int column) {
       super.setValueAt(val, (this.Rows[row]).index, column);
     }
     
     public Object getValueAt(int row, int column) {
       return super.getValueAt((this.Rows[row]).index, column);
     }
     
     public String getStringAt(int row, int column) {
       return super.getValueAt(row, column).toString();
     }
     
     public int getRowNum(int row) {
/* 1002 */       return (this.Rows[row]).index;
     }
     
     public void sort(int columnIndex) {
/* 1006 */       this.sortColumnIndex = columnIndex;
/* 1007 */       Arrays.sort((Object[])this.Rows);
/* 1008 */       this.SortDirection = !this.SortDirection;
     }
     
     private class Row implements Comparable<Row> {
       private int index;
       
       public Row(int _index) {
/* 1014 */         this.index = _index;
       }
       
       public int getIndex() {
/* 1017 */         return this.index;
       }
       
       public int compareTo(Row other) {
/* 1020 */         int n = SendToERPDialog.CheckTableModle.this.getStringAt(this.index, SendToERPDialog.CheckTableModle.this.sortColumnIndex).compareTo(SendToERPDialog.CheckTableModle.this.getStringAt(other.getIndex(), SendToERPDialog.CheckTableModle.this.sortColumnIndex));
/* 1021 */         return SendToERPDialog.CheckTableModle.this.SortDirection ? n : -n;
       }
     }
   }
   
   private Vector<Object> getData() {
/* 1031 */     Vector<Object> data = new Vector();
/* 1032 */     int vecSize = this.item_id.size();
/* 1033 */     for (int i = 0; i < vecSize; i++) {
/* 1034 */       Vector<Object> rowVector = new Vector();
/* 1035 */       rowVector.add(Boolean.valueOf(true));
/* 1036 */       rowVector.add(this.item_id.get(i));
/* 1037 */       rowVector.add(this.object_name.get(i));
/* 1038 */       rowVector.add(this.nhl8_uom_tag.get(i));
/* 1039 */       rowVector.add(this.nhl8_ProdrOrg.get(i));
/* 1040 */       rowVector.add(this.nhl8_SpareOrg.get(i));
/* 1041 */       rowVector.add(this.nhl8_UnprodrOrg.get(i));
/* 1042 */       rowVector.add(this.nhl8_wlzt.get(i));
/* 1043 */       rowVector.add(this.nhl8_wllx.get(i));
/* 1044 */       rowVector.add(this.nhl8_templateID.get(i));
/* 1045 */       rowVector.add(this.nhl8_MaxType.get(i));
/* 1046 */       rowVector.add(this.nhl8_MinType.get(i));
/* 1047 */       data.add(rowVector);
     } 
/* 1050 */     return data;
   }
   
	
   public List<Integer> getListNotEditableRow() {
/* 1057 */     return this.listNotEditableRow;
   }
   
   public void setListNotEditableRow(List<Integer> listNotEditableRow) {
/* 1064 */     this.listNotEditableRow = listNotEditableRow;
   }
   
   
   public static void main(String[] args) {
/* 1068 */     boolean is = false;
/* 1069 */     boolean is2 = true;
/* 1070 */     System.out.println((new StringBuffer(String.valueOf(is))).append("-").append(is2));
   }
 }

