package com.connor.rb.plm.rb025; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMFactory; import org.apache.axiom.om.OMNamespace; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.client.ServiceClient; import com.teamcenter.rac.aif.AbstractAIFApplication; import com.teamcenter.rac.aif.AbstractAIFDialog; import com.teamcenter.rac.aifrcp.AIFUtility; import com.teamcenter.rac.kernel.TCClassService; import com.teamcenter.rac.kernel.TCClassificationService; import com.teamcenter.rac.kernel.TCComponent; import com.teamcenter.rac.kernel.TCComponentForm; import com.teamcenter.rac.kernel.TCComponentICO; import com.teamcenter.rac.kernel.TCComponentItemRevision; import com.teamcenter.rac.kernel.TCException; import com.teamcenter.rac.kernel.TCSession; import com.teamcenter.rac.kernel.ics.ICSAdminClass; import com.teamcenter.rac.kernel.ics.ICSKeyLov; import com.teamcenter.rac.kernel.ics.ICSProperty; import com.teamcenter.rac.kernel.ics.ICSPropertyDescription; import com.teamcenter.rac.util.MessageBox; import net.sf.json.JSONArray; /** * 修改分类属性 */ public class EditClassificationDialog extends AbstractAIFDialog { private static final long serialVersionUID = 1L; private AbstractAIFApplication app; private TCSession session; private TCComponent target; private Map propMap; private TCClassificationService myService; final EditClassificationDialog DIALOG = this; private TCComponentItemRevision rev; private int[] ids; private String[] values; private String[] names; private boolean finished; private ICSPropertyDescription[] desc; public EditClassificationDialog(AbstractAIFApplication app) throws TCException { this.app = app; this.session = (TCSession) app.getSession(); this.myService = session.getClassificationService(); this.target = (TCComponent) AIFUtility.getCurrentApplication().getTargetComponent(); System.out.println(target.getType()); System.out.println(target instanceof TCComponentItemRevision ? "是" : "否"); if (target instanceof TCComponentItemRevision) { try { rev = (TCComponentItemRevision) target; boolean isAccess = session.getTCAccessControlService().checkPrivilege(rev, "WRITE"); if (!isAccess) { JOptionPane.showMessageDialog(this, "没有版本修改权限,请联系系统管理员", "提示", JOptionPane.WARNING_MESSAGE); return; } initUI(); } catch (Exception e) { e.printStackTrace(); } } else { MessageBox.post("请选择版本", "错误", MessageBox.ERROR); } } /** * 获取分类属性LOV显示值 * @param icsD * @return */ public static String getClassDisplayableValue(ICSPropertyDescription icsD) { String result = ""; ICSKeyLov lov = icsD.getFormat().getKeyLov(); if (lov != null) { String[] keys = lov.getKeys(); for (String key : keys) { result = lov.getValueOfKey(key); return result; } } return result; } private void initUI() { this.setTitle("修改分类属性"); JPanel rootPanel = new JPanel(); rootPanel.setLayout(null); rootPanel.setPreferredSize(new Dimension(600, 350)); Map displayMap = new LinkedHashMap(); propMap = new HashMap(); try { if (rev.getClassificationClass() != null && !"".equals(rev.getClassificationClass())) { try { rev.refresh(); System.out.println("rev.getClassificationClass()=" + rev.getClassificationClass()); TCComponentICO ico = rev.getClassificationObjects()[0]; ICSProperty[] props = ico.getICSProperties(true); desc = ico.getICSPropertyDescriptors(); for (int i = 0; i < props.length; i++) { // System.out.println(props[i].getId() + "===>" + props[i].getValue());// 30101===> // desc.getName=Type and size series 就是分类属性的名字||| desc.getId=30101就是分类属性ID // System.out.println("desc.getName=" + desc[i].getName() + " desc.getId=" + desc[i].getId()); displayMap.put(props[i], desc[i]); } } catch (TCException e1) { e1.printStackTrace(); } } else { this.disposeDialog(); MessageBox.post("当前版本未被分类", "错误", MessageBox.ERROR); return; } } catch (TCException e) { e.printStackTrace(); } if (displayMap.size() == 0) { return; } JPanel content = new JPanel(); content.setLayout(null); content.setPreferredSize(new Dimension(600, displayMap.size() * (25 + 10))); int num = 0; // 通过对象获取所有的分类属性,然后遍历分类属性 for (Entry entry : displayMap.entrySet()) { JLabel label = new JLabel(entry.getValue().getName() + ":"); label.setBounds(10, num * 35 + 10, 100, 25); ICSKeyLov lov = entry.getValue().getFormat().getKeyLov(); // ArrayList list = new ArrayList<>(); if (lov != null) { String[] keys = lov.getKeys(); JComboBox combo = new JComboBox(); combo.addItem(new ClassPropBean("", "", "")); for (String key : keys) { combo.addItem(new ClassPropBean(key, lov.getValueOfKey(key), "")); } String v = entry.getKey().getValue(); for (int i = 0; i < combo.getItemCount(); i++) { if (v.equals(combo.getItemAt(i).getId())) { combo.setSelectedIndex(i); break; } } // combo.setSelectedItem(v); // if (!Arrays.asList(lov.getDisplayValues()).contains(v)) { // combo.addItem(v); // combo.setSelectedItem(v); // } combo.setBounds(130, num * 35 + 10, 350, 25); propMap.put(entry.getValue().getName(), combo); content.add(combo); } else { JTextField text = new JTextField(entry.getKey().getValue()); text.setBounds(130, num * 35 + 10, 350, 25); propMap.put(entry.getValue().getName(), text); content.add(text); } content.add(label); num++; } JButton okBtn = new JButton("确认"); okBtn.setBounds(40, 310, 100, 25); okBtn.setSelected(true); okBtn.addActionListener(ok()); JButton cancelBtn = new JButton("取消"); cancelBtn.setBounds(160, 310, 100, 25); cancelBtn.addActionListener(cancel()); JScrollPane jsp = new JScrollPane(content); jsp.getVerticalScrollBar().setUnitIncrement(16); jsp.setBounds(0, 0, 600, 300); jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); rootPanel.add(jsp); rootPanel.add(okBtn); rootPanel.add(cancelBtn); this.add(rootPanel); this.pack(); this.centerToScreen(); this.setAutoRequestFocus(true); // this.setResizable(false); this.setAlwaysOnTop(true); this.showDialog(); } private ActionListener ok() { return new ActionListener() { @Override public void actionPerformed(ActionEvent e) { ((JButton) e.getSource()).setEnabled(false); rev = (TCComponentItemRevision) target; new Thread(new Runnable() { @Override public void run() { int i = 1; while (!DIALOG.finished) { try { DIALOG.setTitle("正在修改分类属性 " + StringCreateUtil.createPoint(i++ % 3)); Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); EditClassificationDialog.this.setAlwaysOnTop(false); if (saveClassification(rev)) { MessageBox.post("修改分类属性成功!", "", MessageBox.ERROR); EditClassificationDialog.this.disposeDialog(); } else { MessageBox.post("修改分类属性失败!", "", MessageBox.ERROR); } } }; } private ActionListener cancel() { return new ActionListener() { @Override public void actionPerformed(ActionEvent e) { DIALOG.disposeDialog(); } }; } @SuppressWarnings("unchecked") private boolean saveClassification(TCComponentItemRevision rev) { try { TCComponentForm form = (TCComponentForm) rev.getRelatedComponents("IMAN_master_form_rev")[0]; TCComponent[] status = form.getRelatedComponents("release_status_list"); for (TCComponent comp : status) { if (comp.getProperty("object_name").contains("正式")) { JOptionPane.showMessageDialog(this, "版本表单已正式发布,不允许修改分类属性", "提示", JOptionPane.WARNING_MESSAGE); return false; } } TCComponentICO[] icoS = rev.getClassificationObjects(); System.out.println("======>获取的分类对象有:" + icoS.length); String classID = rev.getClassificationClass(); if (icoS == null || icoS.length == 0) { return false; } for (int i = 0; i < icoS.length; i++) { List propList = new ArrayList<>(); TCComponentICO ico = icoS[i]; ICSPropertyDescription[] desc = ico.getICSPropertyDescriptors(); ICSProperty[] oldICSProps = ico.getICSProperties(true); ids = new int[oldICSProps.length]; values = new String[oldICSProps.length]; names = new String[oldICSProps.length]; for (int j = 0; j < oldICSProps.length; j++) { ids[j] = oldICSProps[j].getId(); if (propMap.get(desc[j].getName()) instanceof JComboBox) { ClassPropBean bean = (ClassPropBean) ((JComboBox) propMap.get(desc[j].getName())).getSelectedItem(); String str = bean.getId(); values[j] = " ".equals(str) ? "" : str; } else { values[j] = ((JTextField) propMap.get(desc[j].getName())).getText(); } names[j] = desc[j].getName(); ClassProperty prop = new ClassProperty(ids[j], values[j]); propList.add(prop); } String json = objectToJson(propList); System.out.println("classID=" + classID); System.out.println("json=" + json); System.out.println("删除分类属性..."); try { deleteClass(getICO(rev, classID).getUid()); } catch (Exception e) { e.printStackTrace(); } System.out.println("发送分类属性..."); if (sendToClass(rev.getUid(), classID, json)) { return true; } else { return false; } } } catch (TCException e1) { e1.printStackTrace(); JOptionPane.showMessageDialog(null, e1, "错误", JOptionPane.ERROR_MESSAGE); return false; } return true; } private TCComponentICO getICO(TCComponentItemRevision rev, String classId) throws Exception { if (rev == null || classId.equals("")) { return null; } TCComponentICO[] icos = rev.getClassificationObjects(); System.out.println(rev + " -> 已存在分类:" + Arrays.toString(icos)); int len = icos.length; if (len == 0) { return null; } TCClassificationService classService = session.getClassificationService(); for (int i = 0; i < len; i++) { TCComponentICO ico = icos[i]; classService.loadICOs(new String[] { ico.getUid() }, ""); if (ico.getClassId() == null || ico.getClassId().equals("")) { throw new Exception("分类加载异常,请联系管理员处理"); } if (classId.equals(ico.getClassId())) { return ico; } } return null; } public void deleteClass(String icoPuid) { try { ServiceClient sc = new ServiceClient(); Options opts = new Options(); String url = JDMethodUtil.getPrefStr("Autocode_SendClassServer_URL", session);// "http://10.201.5.203:19090/ErpWebService.asmx?WSDL"; EndpointReference end = new EndpointReference(url); opts.setTo(end); opts.setAction("DeleteClass"); sc.setOptions(opts); OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace omNs = fac.createOMNamespace("http://tempuri.org/", ""); OMElement method = fac.createOMElement("DeleteClass", omNs); OMElement value = fac.createOMElement("icoPuid", omNs); value.setText(icoPuid); method.addChild(value); OMElement res = sc.sendReceive(method); res.getFirstElement().getText(); System.out.println(res.getFirstElement().getText()); } catch (AxisFault e) { e.printStackTrace(); } } public boolean sendToClass(String wsoPUID, String classID, String json) { try { ServiceClient sc = new ServiceClient(); Options opts = new Options(); String url = JDMethodUtil.getPrefStr("Autocode_SendClassServer_URL", session); // "http://10.201.5.203:19090/ErpWebService.asmx?WSDL"; EndpointReference end = new EndpointReference(url); opts.setTo(end); opts.setAction("SendToClass"); sc.setOptions(opts); OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace omNs = fac.createOMNamespace("http://tempuri.org/", ""); OMElement method = fac.createOMElement("SendToClass", omNs); OMElement value = fac.createOMElement("wsoPuiid", omNs); value.setText(wsoPUID); OMElement value1 = fac.createOMElement("classID", omNs); value1.setText(classID); OMElement value2 = fac.createOMElement("JsonContainer", omNs); // "[{\"value\":\"1\",\"propertyID\":-500003},{\"value\":\"1\",\"propertyID\":-500011}]" value2.setText(json); method.addChild(value); method.addChild(value1); method.addChild(value2); OMElement res = sc.sendReceive(method); res.getFirstElement().getText(); System.out.println(res.getFirstElement().getText()); } catch (AxisFault e) { // MessageBox.post("发送分类失败!", "", MessageBox.ERROR); e.printStackTrace(); return false; } return true; } public static String objectToJson(List props) { String str = null; try { JSONArray json = JSONArray.fromObject(props);// 将java对象转换为json对象 str = json.toString();// 将json对象转换为字符串 } catch (Exception e) { e.printStackTrace(); } System.out.println("JSON =>" + str); return str; } }