dangsn
2024-12-03 d0ae279ff3b83358d1c07f4481a041c4ad335026
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
package com.vci.web.service.impl;
 
import com.vci.common.qt.object.Symbol;
import com.vci.constant.IRightConstant;
import com.vci.corba.common.PLException;
import com.vci.corba.framework.data.GrandValue;
import com.vci.corba.omd.btm.BizType;
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.OsDataAuthDTO;
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.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("删除成功!");
    }
 
    //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.get(0));
            // 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(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;
 
    }
 
 
}