You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

434 lines
14 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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<String, JComponent> 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<ICSProperty, ICSPropertyDescription> displayMap = new LinkedHashMap<ICSProperty, ICSPropertyDescription>();
propMap = new HashMap<String, JComponent>();
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<ICSProperty, ICSPropertyDescription> 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<Object> list = new ArrayList<>();
if (lov != null) {
String[] keys = lov.getKeys();
JComboBox<ClassPropBean> combo = new JComboBox<ClassPropBean>();
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<ClassProperty> 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<ClassPropBean>) 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<ClassProperty> 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;
}
}