wangting
2024-09-27 a3e87f78ee262ca9bb7d9b0c997639d5f3295890
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
package com.vci.client.uif.actions.client;
 
import java.util.List;
import java.util.Map;
 
import com.vci.client.bof.ClientBusinessObject;
import com.vci.client.bof.ClientLinkObject;
import com.vci.client.bof.ClientLinkObjectOperation;
import com.vci.client.ui.swing.components.VCIJDialog;
import com.vci.client.ui.swing.components.VCIJDialog.DialogResult;
import com.vci.client.uif.engine.client.objopt.ObjectAddEditDialog;
import com.vci.client.uif.engine.common.ClientLOAndBOData;
import com.vci.client.uif.engine.common.IDataNode;
import com.vci.mw.ClientContextVariable;
 
/**
 * 创建link保存事件,应该配置在创建窗口上,
 * 对于选择保存也使用此事件
 * 
 * 现在调整BO或LO对象的设置属性值在编辑窗口中进行
 * 在Action中不再执行校验吗,设置等操作
 * @author VCI-STGK006
 *
 */
public class AddLinkSaveAction extends DoseNotSelectDataAction {
 
    /**
     * 操作方法
     */
    private UIFUtils operation =
            new UIFUtils();
    
    /**
     * 
     */
    private ClientLinkObjectOperation loOperation = new ClientLinkObjectOperation();
    
    /**
     * 判断是创建有link属性的BO,还是创建没有link属性的BO
     * 0:无link属性(业务类型),1:有link属性(有链接属性)
     */
    private int operationType = 0;
    
    /**
     * 按钮参数信息
     */
    private Map<String, String> paramsMap;
    
    /**
     * 按钮所属窗口
     */
    private ObjectAddEditDialog oaed = null;
    
    @Override
    public String getKey() {
        return "addlinksave";
    }
    
    @Override
    public boolean checkHasRight(){
        return true;
    }
 
    @Override
    public boolean doPost() {
        try {
            
            //获得按钮参数
            paramsMap = getButtonParams();
            String linkType = paramsMap.get("linktype");
            
            //用户输入属性 期中以t_oid开始的为业务对象属性,其它为link对象属性
            //获得按钮所属窗口,此窗口一定要是一个弹出窗口
            //并且窗口的类型一定是ObjectAddEditDialog
            VCIJDialog dialog = getOwnerDialog();
            if(dialog instanceof ObjectAddEditDialog){
                oaed = (ObjectAddEditDialog) dialog;
            }
            if(oaed == null){
                return false;
            }
            //得到主对象对象,根据主对象得到From BO
            ClientBusinessObject from = null;
            IDataNode sourceData = oaed.getSourceDataNode();
            Object fromObj = sourceData.getMaterObject();
            if(fromObj instanceof ClientBusinessObject){
                from = (ClientBusinessObject) fromObj;
            } else if(fromObj instanceof ClientLinkObject) {
                //如果是LO对象,使用LO对应TO端BO对象作为创建的From对象
                ClientLinkObject fromLO = (ClientLinkObject) fromObj;
                from = getToBusinessObject(fromLO);
            }
            //验证from对象
            if(from == null 
                    || from.getBusinessObject().oid == null
                    || from.getBusinessObject().oid.equals("")){
                UIFUtils.showMessage(ClientContextVariable.getFrame(), 
                        "uifmodel.plm.uif.actions.createlinkerror.fromdelete");
                return false;
            }
            
            //获得使用的表单类型
            operationType = oaed.getTypeFlag();
            //获得用户设置数据是 IDataNode类型
            Object obj = getDataModel().getRootObject();
            if(obj == null){
                return false;
            }
            IDataNode idn = (IDataNode) obj;
            //根据表单类型判断要执行的操作
            ClientLinkObject clo = null;
            ClientBusinessObject to = null;
            boolean result = false;
            if(this.operationType == 0){
                //按钮参数linktype放在此处校验,因为当使用LO的from创建时,linktype直接使用了Form对应的类型
                if(linkType == null || linkType.equals("")){
                    UIFUtils.showMessage(ClientContextVariable.getFrame(),
                            "uifmodel.plm.uif.actions.syserror.configerror.nolinktype");
                    return false;
                }
                //如果表单类型是0,表示创建的是无属性的link
 
                //创建一个空的link对象
                clo = operation.createEmptyClientLinkObject(
                        ClientContextVariable.getFrame(), linkType);
                //获得to端对象
                to = (ClientBusinessObject) idn.getMaterObject();            
            } else if (this.operationType == 1){
                //如果表单类型是1,可以再Form中直接获得要创建的LO和BO对象
                ClientLOAndBOData clbo = (ClientLOAndBOData) idn.getMaterObject();
                List<ClientLinkObject> loList = clbo.getLoDatas();
                if(loList.isEmpty()){
                    UIFUtils.showErrorMessage(ClientContextVariable.getFrame(),
                            "uifmodel.plm.uif.actions.createloerror", new Exception("系统异常:没有找到link对象"));
                    return false;
                }
                //在创建保存link中只会有一个LO对象
                clo = loList.get(0);
                to = clbo.getBOData(clo.getLinkObject().oid);
            }
            //保存link对象 如果link保存失败则终止执行
            if(oaed.isDirection()){
                clo.setFromBO(from);
                clo.setToBO(to);
            } else {
                clo.setFromBO(to);
                clo.setToBO(from);
            }
            
            ClientBusinessObject[] cbos = new ClientBusinessObject[1];
            cbos[0] = to;
            result = loOperation.saveLinkObject(cbos, clo);
            
            if(!result){
                UIFUtils.showMessage(ClientContextVariable.getFrame(),
                        "uifmodel.plm.uif.actions.createlinkerror");
                return false;
            }
            
            //上传文件
            if(!UploadFile(this.oaed, to)){
                //operation.showMessage(ClientContextVariable.getFrame(),
                //        "uifmodel.plm.uif.actions.createlinkerror");
                //return false;
            }
            
            UIFUtils.showMessage(ClientContextVariable.getFrame(), 
                    "uifmodel.plm.uif.actions.createsuccessmsg", to.getName());
            
            closeOwnerDailog(getOwnerDialog(), DialogResult.OK);
 
            // 调用此按钮所在Dialog注册的回调函数
            super.invokeOwnedDialogCallback();
            return true;
        } catch (Exception e){
            UIFUtils.showErrorMessage(ClientContextVariable.getFrame(),
                    "uifmodel.plm.uif.actions.createloerror", e);
            return false;
        }
    }
    
    /**
     * 查询LO对象TO端的BO对象
     * @param clo
     * @return
     * @throws Exception
     */
    public ClientBusinessObject getToBusinessObject(ClientLinkObject clo) throws Exception{
        ClientBusinessObject cbo = null;
        String btmName = clo.getLinkObject().toBTName;
        String oid = clo.getLinkObject().toOid;
        cbo = operation.reloadClientBusinessObject(
                ClientContextVariable.getFrame(), oid, btmName);
        return cbo;
    }
}