yuxc
2024-12-31 69425e4b837598c2463662726ae3b4d25eceae66
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package com.vci.web.service.impl;
 
import com.vci.client.common.oq.OQTool;
import com.vci.common.qt.object.QueryTemplate;
import com.vci.common.qt.object.Symbol;
import com.vci.constant.IRightConstant;
import com.vci.corba.common.PLException;
import com.vci.corba.framework.data.CheckValue;
import com.vci.corba.framework.data.GrandValue;
import com.vci.corba.omd.btm.BizType;
import com.vci.corba.omd.data.AttributeValue;
import com.vci.corba.omd.data.BusinessObject;
import com.vci.corba.omd.data.LinkObject;
import com.vci.corba.omd.lcm.LifeCycle;
import com.vci.corba.omd.lcm.TransitionVO;
import com.vci.corba.omd.ltm.LinkType;
import com.vci.corba.portal.data.PLAction;
import com.vci.dto.AuthResultDTO;
import com.vci.dto.OsDataAuthDTO;
import com.vci.pagemodel.OpItemVO;
import com.vci.starter.web.exception.VciBaseException;
import com.vci.starter.web.pagemodel.BaseResult;
import com.vci.web.service.OsDataAuthServiceI;
import com.vci.web.util.PlatformClientUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.*;
 
/**
 * 数据权限的控制器
 * @author yuxc
 * @date 2024-11-25
 */
@Service
public class OsDataAuthServiceImpl implements OsDataAuthServiceI {
 
    @Autowired
    private PlatformClientUtil platformClientUtil;
 
    @Override
    public BaseResult getData(String typeName) throws PLException {
        GrandValue[] grandValues = platformClientUtil.getFrameworkService().queryGrand(typeName);
        // 设置Table列头信息
        List<String> tableHeader = new ArrayList<String>();
        //获取表头数据
        getTableHeader(grandValues, tableHeader, typeName);
        //列表数据
        List<Map> rowList = new ArrayList<>();
        //获取列表数据
        getTableData(grandValues, tableHeader, typeName, rowList);
        Map<String, String> actionsMap = getAllActionsByType2(typeName, "business");
        OsDataAuthDTO tableData = new OsDataAuthDTO();
        tableData.setTableHeader(tableHeader);
        tableData.setRowList(rowList);
        tableData.setActionMap(actionsMap);
        return BaseResult.success(tableData);
    }
    /**
     * 保存数据权限数据
     * @return 保存结果
     */
    @Override
    public BaseResult saveGrand(List<GrandValue> grandValues) throws PLException {
        boolean isTrue = false;
        isTrue = platformClientUtil.getFrameworkService()
                .deleteTypeRuleGrand(grandValues.get(0).identifier.split("_")[0], grandValues.get(0).ruleName);
        if(!isTrue){
            throw new VciBaseException("数据权限删除失败!");
        }
        // save
        boolean isTrue_add = false;
        try {
            isTrue_add = platformClientUtil.getFrameworkService()
                    .saveGrand(grandValues.toArray(new GrandValue[grandValues.size()]));
        } catch (Exception e) {
            throw new VciBaseException("数据权限保存失败!");
        }
        if (isTrue_add) {
            return BaseResult.success("保存成功!");
        }
        return BaseResult.success("保存失败!");
    }
    /**
     * 删除数据权限
     * @param typeName 业务类型
     * @param ruleName 规则名称
     * @return 删除结果
     */
    @Override
    public BaseResult deleteTypeRuleGrand(String typeName, String ruleName) throws PLException {
        // DataBase
        boolean isTrue = platformClientUtil.getFrameworkService().deleteTypeRuleGrand(typeName,
                    ruleName);
        if (!isTrue) {
            return BaseResult.success("删除失败!");
        }
        return BaseResult.success("删除成功!");
    }
    /**
     * 查询授权结果
     * @param params 查询对象
     * @return 授权结果
     */
    @Override
    public BaseResult authResult(CheckValue params) {
//        CheckValue params = new CheckValue();
        params.opname = IRightConstant.OPS;
        if(StringUtils.isBlank(params.objectoid)){
            params.objectoid = "22385E82-485C-549D-E2F4-91278E9E0A76";
        }
        List<String> tableHeaders = new ArrayList<>();
        List<Object> tableValues = new ArrayList<>();
        // 操作
        List<OpItemVO> opitems = new ArrayList<OpItemVO>();
        try {
            String where = platformClientUtil.getFrameworkService().checkRight(params);
            String[] ops = where.split(":");
            //处理列表头
            for (String s : ops) {
                if (s != null && !s.equals("")) {
                    OpItemVO item = new OpItemVO();
                    String[] op = s.split(",");
                    item.setName(op[0]);
                    item.setValue(op[1]);
                    tableHeaders.add(op[0]);
                    opitems.add(item);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new VciBaseException("鉴权出错\n"
                            + e.getLocalizedMessage());
        }
        //处理数据
        for (int n = 0; n < opitems.size(); n++) {
            tableValues.add(opitems.get(n).getValue().equals("1") ? true : false);
        }
        AuthResultDTO authResultDTO = new AuthResultDTO();
        authResultDTO.setTableValues(tableValues);
        authResultDTO.setTableHeader(tableHeaders);
        return BaseResult.success(authResultDTO);
    }
    /**
     * 查询业务对象
     * @param btmName 业务类型名称
     * @param boFlag 是否为bo类型,true为btm,false为link
     * @param btmType 业务类型
     * @return 查询出的数据
     */
    @Override
    public BaseResult queryBusiness(String btmName, boolean boFlag, String btmType) {
        QueryTemplate qt = new QueryTemplate();
        qt.setBtmType(btmName);
        List<String> clauseList = new ArrayList<String>();
        clauseList.add("*");
        qt.setClauseList(clauseList);
        qt.setType(btmType);// 链接link
        qt.setCondition(null);
        qt.setId("qt1");
        List<Object> tableValues = new ArrayList<>();
        try {
            clauseList = new ArrayList<>(Arrays.asList(platformClientUtil.getBtmService().getSysAttributeNames()));
            clauseList.addAll(Arrays.asList(platformClientUtil.getBtmService().getAttributeNames(btmName)));
 
            if (boFlag) {
                BusinessObject[] result = platformClientUtil.getQueryService().findBTMObjects(qt.getId(), OQTool.qtTOXMl(qt).asXML());
                for (int i = 0; i < result.length; i++) {
                    List<Object> lineData = new ArrayList<>();
                    BusinessObject bo = result[i];
                    AttributeValue[] attList = bo.hisAttrValList;
                    for (int j = 0; j < clauseList.size(); j++) {
                        lineData.add(attList[j].value);
//                        qrTable.setValueAt(attList[j].attrVal, i, j);
                    }
                    tableValues.add(lineData);
                }
            } else {
                List<Object> lineData = new ArrayList<>();
                LinkObject[] loResult = platformClientUtil.getQueryService().findLTObjects(qt.getId(), OQTool.qtTOXMl(qt).asXML());
                for (int i = 0; i < loResult.length; i++) {
                    LinkObject lo = loResult[i];
                    AttributeValue[] attList = lo.hisAttrValList;
                    for (int j = 0; j < attList.length; j++) {
                        lineData.add(attList[j].value);
//                        qrTable.setValueAt(attList[j], i, j);
                    }
                    tableValues.add(lineData);
                }
            }
        } catch (PLException e) {
            e.printStackTrace();
        }
        Map<String, Object> resultData = new HashMap<>();
        resultData.put("tableHeader", clauseList);
        resultData.put("tableValues", tableValues);
        return BaseResult.success(resultData);
    }
 
    //add by caill start 2015 12.18 将查询出来的action放入到map中
    private Map<String, String> getAllActionsByType2(String typeName, String type) throws PLException {
        Map<String,String> boActions2 = new HashMap<String,String>();
        boActions2.put("查询", "query");
        PLAction[] allActions = platformClientUtil.getUIService().getAllPLActionEntityByType(typeName);
        if (allActions == null || allActions.length == 0) {
            return boActions2;
        }
        for (int i = 0; i < allActions.length; i++) {
            if(allActions[i].plTypeType.equals(type)){
                boActions2.put(allActions[i].plName,allActions[i].plCode);    //将中文显示作为key,英文显示作为value放到map中
            }
        }
        return boActions2;
    }
 
    private void getTableData(GrandValue[] grandValues, List<String> tableHeader, String typeName, List<Map> rowList) throws PLException {
        // 依据规则名称,规则分类
        Map<String, List<GrandValue>> splitRuleMap = splitRuleByName(grandValues);
        // 依据规则名称
//        int row = 0;
        // 设置规则记录行数
//        ruleModel.setRowCount(splitRuleMap.size());
        // add by caill start 2016.1.11 得到btm、link、lifeCyles
        LinkType[] links = null;
        LifeCycle lifeCycle = null;
        BizType btm = null;
        try {
            btm = platformClientUtil.getBtmService().getBizTypeByName(typeName);
            links = platformClientUtil.getLinkTypeService().getLinkTypeByBtmName(btm.name, Symbol.FROM);
            LifeCycle[] lifeCyles = platformClientUtil.getLifeCycleService().getLifeCycles();
            for (int m = 0; m < lifeCyles.length; m++) {
                if (lifeCyles[m].name.equals(btm.lifeCycle)) {
                    lifeCycle = lifeCyles[m];
                    break;
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
 
        // add by caill end
//        ActionConstants act = new ActionConstants();
        PLAction[] allActions = platformClientUtil.getUIService().getAllPLActionEntityByType(typeName);
//        platformClientUtil.getUIService().getAllPLActionEntityByType(typeName);
        for (Iterator<Map.Entry<String, List<GrandValue>>> iter = splitRuleMap.entrySet().iterator(); iter.hasNext();) {
            Map.Entry<String, List<GrandValue>> entry = iter.next();
            List<GrandValue> rules = entry.getValue();
 
            Map<Object, Object> columnData = new HashMap<>();
            // 增加页面缓存
//            ruleModel.setConditionValue(row, rules);
            columnData.put("rules",rules);
            // add by caill start 2015 12.18 将查出的action放到map中,注意不要落下“查询”
            Map<String, String> actionMap = new HashMap<String, String>();
            actionMap.put("query", "查询"); // 将操作中的query放入actionMap中
            for (int i = 0; i < allActions.length; i++) {
                if (allActions[i].plTypeType.equals("business")) {
                    actionMap.put(allActions[i].plCode, allActions[i].plName); // 将操作放入actionMap中
                }
            }
            for (int j = 0; j < links.length; j++) {
                for (int i = 0; i < allActions.length; i++) {
                    if (allActions[i].plTypeType.equals("link")) {
                        actionMap.put(links[j].name + "." + allActions[i].plCode,
                                links[j].name + "." + allActions[i].plName);// 将关系放入actionMap中
                    }
                }
                actionMap.put(links[j].name + "." + "query", links[j].name + "." + "查询"); // 将关系中的query放入actionMap中
            }
 
            TransitionVO[] transitions = lifeCycle != null ? lifeCycle.routes : new TransitionVO[0];
            for (int j = 0; j < transitions.length; j++) {
                String name = lifeCycle.name + "." + transitions[j].connect;
                actionMap.put(name, name); // 将跃迁放入actionMap中
            }
            // add by caill end
            for (int j = 0; j < rules.size(); j++) {
                // 规则名和类型
//                ruleModel.setValueAt(rules.get(j).ruleName, row, 0);
                columnData.put("0", rules.get(j).ruleName);
                if (rules.get(j).ruleType.equals(IRightConstant.RULETYPE__HAS)) {
                    columnData.put("1", "允许规则");
                } else if (rules.get(j).ruleType.equals(IRightConstant.RULETYPE__NOTHAS)) {
                    columnData.put("1", "拒绝规则");
                } else if (rules.get(j).ruleType.equals(IRightConstant.RULETYPE_ALL_HAS)) {
                    columnData.put("1", "全部有权");
                } else {
                    columnData.put("1", "全部无权");
                }
                // 指定的操作赋值
//                TableColumnModel columnModel = funclet.getRightMainPanel().getRuleTable().getColumnModel();
                int start = rules.get(j).identifier.lastIndexOf("_") + 1;
                String op = rules.get(j).identifier.substring(start, rules.get(j).identifier.length());
                // add by caill start 2015.12.18 遍历map,根据英文名称找到相应的中文名称,并根据中文名称找到对应的列
                String headerName = null;
                Set<String> keys = null;
                if (actionMap.size() > 0) {
                    keys = actionMap.keySet();
                }
                if (keys.size() != 0) {
                    for (Iterator<String> iterator = keys.iterator(); iterator.hasNext();) {
                        String key = iterator.next();
                        if (op.equals(key)) {
                            headerName = actionMap.get(key);
                            break;
                        }
                    }
                }
 
                int columnIndex = -1;
                if (headerName != null) {
//                    columnIndex = columnModel.getColumnIndex(headerName);
                    columnIndex = tableHeader.indexOf(headerName);
                }
                // add by caill end
                // int columnIndex = columnModel.getColumnIndex(op);
                if (columnIndex >= 0) {
//                    ruleModel.setValueAt(rules.get(j).isGrand == '1' ? true : false, row, columnIndex);
                    columnData.put(String.valueOf(columnIndex), rules.get(j).isGrand == '1' ? true : false);
                }
 
            }
//            ++row;
            rowList.add(columnData);
        }
    }
 
    private void getTableHeader(GrandValue[] grandValues, List<String> tableHeader, String typeName) throws PLException {
        // 操作信息
        List<String> ops = new ArrayList<String>();
        for (int i = 0; i < grandValues.length; i++) {
            int start = grandValues[i].identifier.lastIndexOf("_") + 1;
            String op = grandValues[i].identifier.substring(start, grandValues[i].identifier.length());
            if (!ops.contains(op)) {
                ops.add(op);
            }
        }
        // 操作信息
        String[] operations = ops.toArray(new String[ops.size()]);
//        List<String> tablerule = new ArrayList<String>();
        tableHeader.add("规则名称");
        tableHeader.add("规则类型");
        PLAction[] allActions = platformClientUtil.getUIService().getAllPLActionEntityByType(typeName);
 
        for (int m = 0; m < operations.length; m++) {
            if (allActions != null && allActions.length != 0) {
                for (int i = 0; i < allActions.length; i++) {
                    if (allActions[i].plCode.equals(operations[m])) {
                        tableHeader.add(allActions[i].plName);
                        break;
                    } else if (operations[m].endsWith("." + allActions[i].plCode)) {
                        int index = operations[m].indexOf("." + allActions[i].plCode);
                        StringBuilder sb = new StringBuilder(operations[m]);
                        StringBuilder actionChina = sb.replace(index + 1, operations[m].length(), allActions[i].plName);
                        tableHeader.add(actionChina.toString());
                        break;
                    } else if (operations[m].equals("query")) {
                        tableHeader.add("查询"); // 注意不要落下写死的“查询”
                        break;
                    } else if (operations[m].endsWith(".query")) {
                        int index = operations[m].indexOf(".query");
                        StringBuilder sb = new StringBuilder(operations[m]);
                        StringBuilder actionChina = sb.replace(index + 1, operations[m].length(), "查询");
                        tableHeader.add(actionChina.toString());
                        break;
                    }
 
                }
            } else {
                if (operations[m].equals("query")) {
                    tableHeader.add("查询"); // 注意不要落下写死的“查询”
                }
                if (operations[m].endsWith(".query")) {
                    int index = operations[m].indexOf(".query");
                    StringBuilder sb = new StringBuilder(operations[m]);
                    StringBuilder actionChina = sb.replace(index + 1, operations[m].length(), "查询");
                    tableHeader.add(actionChina.toString());
                }
            }
 
        }
 
        LifeCycle lifeCycle = null;
        BizType btm = null;
        try {
            btm = platformClientUtil.getBtmService().getBizTypeByName(typeName);
            LifeCycle[] lifeCyles = platformClientUtil.getLifeCycleService().getLifeCycles();
            for (int j = 0; j < lifeCyles.length; j++) {
                if (lifeCyles[j].name.equals(btm.lifeCycle)) {
                    lifeCycle = lifeCyles[j];
                    break;
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        TransitionVO[] transitions = lifeCycle != null ? lifeCycle.routes : new TransitionVO[0];
        for (int j = 0; j < transitions.length; j++) {
            tableHeader.add(lifeCycle.name + "." + transitions[j].connect);
        }
    }
 
    private Map<String, List<GrandValue>> splitRuleByName(GrandValue[] values) {
        Map<String, List<GrandValue>> splitRuleMap = new HashMap<String, List<GrandValue>>();
        for (int i = 0; i < values.length; i++) {
            if (splitRuleMap.get(values[i].ruleName) == null) {
                List<GrandValue> ruleOfOneName = new ArrayList<GrandValue>();
                ruleOfOneName.add(values[i]);
                splitRuleMap.put(values[i].ruleName, ruleOfOneName);
            } else {
                splitRuleMap.get(values[i].ruleName).add(values[i]);
            }
 
        }
        return splitRuleMap;
 
    }
 
 
}