wangting
2024-12-30 306a9c4fde94c54d91aee5a69d59b993c7c707a1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package com.vci.client.workflow.editor;
 
import java.io.IOException;
import java.io.StringWriter;
 
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
 
import com.mxgraph.model.mxCell;
import com.mxgraph.model.mxICell;
import com.mxgraph.view.mxGraph;
import com.vci.client.LogonApplication;
import com.vci.client.ui.locale.LocaleDisplay;
import com.vci.client.workflow.editor.ui.FlowEditor;
import com.vci.client.workflow.editor.ui.IProcessProperty;
import com.vci.corba.common.VCIError;
 
public class EditorCreateXmlToJbpm {
 
    private FlowEditor editor = null;
    BasicGraphEditor designer = null;
    private mxGraph graph = null;
    private String sXml = "";
    Document document = null;
    private Object[] ocjectCells = null;
    private String[] elements = { FlowConstants.XMLSTART, FlowConstants.XMLEND,
            FlowConstants.XMLTASK, FlowConstants.TRANSITION,
            FlowConstants.XMLDECISION, FlowConstants.XMLFORK,
            FlowConstants.XMLJOIN, FlowConstants.DECISION, FlowConstants.MAIL,FlowConstants.SUBPROCESS };
 
    public EditorCreateXmlToJbpm(BasicGraphEditor designer) {
        super();
        this.graph = designer.getGraphComponent().getGraph();
        this.editor = designer.getEditor();
        this.designer = designer;
        //createXml();
    }
 
    private void createXml() {
        try {
            // dom4j创建xml
            Object parent = graph.getDefaultParent();
            this.ocjectCells = graph.getChildCells(parent);
            getXmlNodeFromFlowNode(ocjectCells);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return;
    }
 
    private void getXmlNodeFromFlowNode(Object[] ocjectCells)
            throws IOException, VCIError {
        // 实例化XMLDOC
        document = DocumentHelper.createDocument();
        // 创建根节点
        Element root = document.addElement(FlowConstants.BODYSNAME,
                FlowConstants.XMLNS);
        // 获取配置信息
        getFlowInfo(root);
 
        // 创建流程节点
        for (Object objectCell : ocjectCells) {
            if (objectCell instanceof mxCell) {
                mxCell cell = (mxCell) objectCell;
                if (cell.getValue() instanceof IProcessProperty
                        && !cell.isEdge()) {
                    IProcessProperty object = (IProcessProperty) cell
                            .getValue();
                    String nodeType = object.getNodeType();
                    if (checkArray(elements, nodeType)) {
                        // 创建流程节点
                        Element flowType = root.addElement(nodeType);
                        object.createAttribute(flowType);
                        // 节点坐标
                        getCoordinate(cell, flowType);
                        // 创建连接线
                        getTransition(cell, flowType);
                    }
                }
            }
        }
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("utf-8");
        StringWriter out = new StringWriter();
        XMLWriter writer = new XMLWriter(out, format);
        writer.write(document);
        writer.flush();
 
        this.sXml = out.toString();
    }
 
    public Document getDocument() {
        return document;
    }
 
    /**
     * 获取流程配置信息
     * 
     * @return
     * @throws VCIError 
     */
    private void getFlowInfo(Element owner) throws VCIError {
        mxCell cell = (mxCell) graph.getModel().getRoot();
        IProcessProperty flowInfo = (IProcessProperty) cell.getValue();
        flowInfo.createAttribute(owner);
    }
 
    /**
     * 获取链接线DOM4J
     * 
     * @param cell
     * @throws VCIError 
     */
    private void getTransition(mxCell sourceCell, Element owner) throws VCIError {
        int edgeCount = sourceCell.getEdgeCount();
        for (int i = 0; i < edgeCount; i++) {
            mxCell edge = (mxCell) sourceCell.getEdgeAt(i);
            if (edge.getSource() == sourceCell) {
                String edgeValue = "";
                if(edge.getValue() !=null ){
                    edgeValue =  edge.getValue().toString();
                }
                mxICell target = edge.getTarget();
                String target2 = target.getValue().toString();
                if (owner.getQName().getName().equals("decision")) {
                    if (edgeValue.trim().length() == 0) {
                        edgeValue = target2;
                    }
                }
 
                Element elementLine = owner
                        .addElement(FlowConstants.TRANSITION);
                if (edgeValue.trim().length() > 0) {
                    Attribute attributeKey = DocumentHelper.createAttribute(
                            elementLine, FlowConstants.XMLNAME,
                            edgeValue.toString());
                    elementLine.add(attributeKey);
                }
                if (edge.getValue() instanceof IProcessProperty) {
                    IProcessProperty object = (IProcessProperty) edge
                            .getValue();
                    object.createAttribute(elementLine);
                }
//                PropertyInfo[] transmTrainsitionInfo_start = TransitionPanel.transmTrainsitionInfo_start;
//                if (transmTrainsitionInfo_start != null
//                        && transmTrainsitionInfo_start.length > 0) {
//                        createTransitionEventXml(elementLine,
//                                transmTrainsitionInfo_start,
//                                FlowConstants.TRANSITION_EVENT);
//                        
//                        TransitionPanel.transmTrainsitionInfo_start = new PropertyInfo[] {};
//                }
                String targetName = target.getValue().toString();
                Attribute toAttribute = DocumentHelper.createAttribute(
                        elementLine, FlowConstants.XMLTARGET, targetName);
                elementLine.add(toAttribute);
            }
        }
    }
    
    
    /**
     * 获取坐标DOM4J
     * 
     * @return
     */
    private void getCoordinate(mxCell cell, Element owner) {
        String sCoordinate = "";
 
        // x
        sCoordinate = sCoordinate + (int) cell.getGeometry().getX() + ",";
        // y
        sCoordinate = sCoordinate + (int) cell.getGeometry().getY() + ",";
        // w
        sCoordinate = sCoordinate + (int) cell.getGeometry().getWidth() + ",";
        // h
        sCoordinate = sCoordinate + (int) cell.getGeometry().getHeight();
 
        Attribute flowAttribute = DocumentHelper.createAttribute(owner,
                FlowConstants.XMLCOORDINATEKEY, sCoordinate);
        owner.add(flowAttribute);
    }
 
    /**
     * 校验数组是否存在
     */
    private boolean checkArray(String[] arrString, String sElement) {
        for (int i = 0; i < arrString.length; i++) {
            if (arrString[i].equalsIgnoreCase(sElement)) {
                return true;
            }
        }
        return false;
    }
 
    public FlowEditor getEditor() {
        return editor;
    }
 
    /**
     * 得到XML
     * 
     * @return
     */
    public String getsXml() {
        createXml();
        return sXml;
    }
 
    private String getI18nForEvent(String key) {
        return LocaleDisplay.getI18nString(key, "RMIPWorkflow",
                LogonApplication.frame.getLocale());
    }
}