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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package com.vci.client.uif.actions.client;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
import com.vci.client.bof.ClientBusinessObject;
import com.vci.client.bof.ClientBusinessObjectOperation;
import com.vci.client.bof.ClientLinkObject;
import com.vci.client.bof.ClientLinkObjectOperation;
import com.vci.client.ui.swing.components.VCIJOptionPane;
import com.vci.client.uif.engine.client.ChooseBusinessObjectDialog;
import com.vci.client.uif.engine.common.DefaultTableNode;
import com.vci.client.uif.engine.common.IDataNode;
import com.vci.corba.common.VCIError;
import com.vci.mw.ClientContextVariable;
 
public class ChooseReleatedObjAndSaveLinkAction extends DoseNotSelectDataHasRightCheckAction {
 
    private Map<String, String> paramsMap = null;
    /**
     * 操作方法
     */
    private UIFUtils operation = new UIFUtils();
    
    /**
     * link对象操作方法
     */
    private ClientLinkObjectOperation loOperation = new ClientLinkObjectOperation();
    
    /**
     * 业务对象操作方法
     */
    private ClientBusinessObjectOperation boOperation = new ClientBusinessObjectOperation();
    
    /**
     * 是否需要先选择数据然后执行创建操作
     */
    boolean needSelect = false;
    
    @Override
    public String getKey() {
        return "choosereleatedobjandsavelink";
    }
    
    @Override
    public boolean checkHasRight(){
        // 按LBO进行数据权限检查
        setDataRightCheckType(RightCheckConstants.RIGHT_CHECK_TYPE_FB);
        return super.checkHasRight();
    }
    
    /**
     * 判断执行Action任务之前是否需要先选择数据
     */
    public boolean beforePost() {
        
        //获得按钮参数
        paramsMap = getButtonParams();
        if(paramsMap.containsKey("needselect")){
            String value = paramsMap.get("needselect");
            if(value.equalsIgnoreCase("true")){
                needSelect = true;
            }
        }
        if(needSelect){
            if(!isSelectedObject()) return false;
            
            Object[] objs = getDataModel().getSelectObjects();
            if(objs.length > 1){
                VCIJOptionPane.showMessage(getParentComponent(), "只能选择一条数据执行创建操作!");
                return false;
            }
        }
        return true;
    }
    
    @Override
    public boolean doPost() {
        try {
            //获得按钮参数
            paramsMap = getButtonParams();
            //得到打开窗口的上下文信息
            String type = paramsMap.get("type");
            String context = paramsMap.get("context");
            String linkType = paramsMap.get("linkType");
            String directionStr = paramsMap.get("direction");
            String isCloneSelectObj = paramsMap.get("isCloneSelectObj");
            if(type == null || type.equals("") || context == null || context.equals("")){
                UIFUtils.showMessage(ClientContextVariable.getFrame(),
                        "uifmodel.plm.uif.actions.syserror.parmerror", "");
                return false;
            }
            /**
             * 判断是否是正向还是反向
             */
            boolean direction = true;
            if(directionStr != null && directionStr.trim().equalsIgnoreCase("false")){
                direction = false;
            }
            
            //构建选择窗口
            ChooseBusinessObjectDialog cbd = new ChooseBusinessObjectDialog(
                    ClientContextVariable.getFrame(), type, context, true, "");
            if(cbd.isOk()){
                //得到用户所选数据
                Object[] selectedDatas = cbd.getSelectedData();
                if(selectedDatas == null){
                    UIFUtils.showMessage(ClientContextVariable.getFrame(), 
                            "uifmodel.plm.uif.actions.choosesave.noselect");
                    return false;
                }
                //验证选择的数据类型,在选择窗口中只允许选择业务对象数据,
                //但是如果按钮配置错误有可能会选择成link类型数据
                DefaultTableNode dtn = (DefaultTableNode) selectedDatas[0];
                if(!(dtn.getMaterObject() instanceof ClientBusinessObject)){
                    UIFUtils.showMessage(ClientContextVariable.getFrame(),
                            "uifmodel.plm.uif.actions.syserror.parmerror", "");
                    return false;
                }
                IDataNode sourceData = null;
                if(needSelect){
                    sourceData = (IDataNode)getDataModel().getSelectObjects()[0];
                } else {
                    sourceData = getRegionPanel().getSourceData();
                }
                //得到主对象对象,根据主对象得到From BO
                ClientBusinessObject from = null;
                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;
                }
 
                List<ClientBusinessObject> toBoList = new ArrayList<ClientBusinessObject>();
                for(Object obj : selectedDatas){
                    //重新下载用户选择的对象
                    DefaultTableNode dtnc = (DefaultTableNode) obj;
                    //已经在选择的时候经行过数据的类型的校验,此处不再经行校验直接转换
                    ClientBusinessObject cbo = (ClientBusinessObject) dtnc.getMaterObject();
                    if(cbo != null && cbo.getBusinessObject().oid != null
                            && !cbo.getBusinessObject().oid.equals("")){
                        toBoList.add(cbo);
                    }
                }
                ClientLinkObject[] clos = new ClientLinkObject[selectedDatas.length];
                for (int i = 0; i < clos.length; i++) {
                    ClientLinkObject clo = operation.createEmptyClientLinkObject(
                            ClientContextVariable.getFrame(), linkType);
                    if (direction) {
                        clo.setFromBO(from);
                        clo.setToBO(toBoList.get(i));
                    } else {
                        clo.setFromBO(toBoList.get(i));
                        clo.setToBO(from);
                    }
                    clos[i] = clo;
                }
                
                ClientBusinessObject[] cloneObjs = new ClientBusinessObject[0];
                if (isCloneSelectObj != null && isCloneSelectObj.equalsIgnoreCase("true")) {
                    cloneObjs = cloneSelectObject(toBoList);
                }
                boolean result = boOperation.batchSaveCreateBuinessObject(cloneObjs, clos);
                
                if(!result){
                    UIFUtils.showMessage(ClientContextVariable.getFrame(),
                            "uifmodel.plm.uif.actions.createlinkerror");
                    return false;
                }
            }
            return true;
        } catch (Exception e){
            UIFUtils.showErrorMessage(ClientContextVariable.getFrame(), 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;
    }
    
    public ClientBusinessObject[] cloneSelectObject(List<ClientBusinessObject> list) throws VCIError {
        ClientBusinessObject[] cloneObjs = new ClientBusinessObject[list.size()];
        for (int i = 0; i < list.size(); i++) {
            cloneObjs[i] = new ClientBusinessObject();
            cloneObjs[i]= operation.cloneDBBusinessObject(list.get(i));
        }
        return cloneObjs;
    }
}