1
2
3
4
5 package gboat2.web.action;
6
7 import gboat2.base.core.annotation.Module;
8 import gboat2.base.core.annotation.Operation;
9 import gboat2.base.core.model.PreferenceDefinition;
10 import gboat2.base.core.service.IModuleService;
11 import gboat2.base.core.web.BaseActionSupport;
12 import gboat2.base.core.web.JsonResultSupport;
13 import gboat2.web.model.Resource;
14
15 import java.util.List;
16
17 import net.sf.json.JSONObject;
18
19 import org.apache.struts2.convention.annotation.ResultPath;
20 import org.springframework.beans.factory.annotation.Autowired;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 @Module(name = "首选项")
36 @ResultPath("/content/preference")
37 public class PreferenceAction extends BaseActionSupport {
38
39 private static final long serialVersionUID = 1568958760674720749L;
40
41 @Autowired
42 IModuleService moduleSer;
43
44 private static int resId = 1000;
45
46
47
48
49
50 public String preference() {
51 return null;
52 }
53
54
55
56
57
58 private String generateResId() {
59 if (resId < 1000 || resId > 100000) {
60 resId = 1000;
61 }
62 return String.valueOf(++resId);
63 }
64
65 @Operation(name = "配置", code = "preferences", desc = "此配置主要是为了限制访问")
66 public String preferences() {
67
68 Resource root = new Resource();
69 root.setResName("root");
70 root.setResId("0");
71 root.setLeaf(false);
72
73
74 List<PreferenceDefinition> list = moduleSer.getPreferences();
75
76 if (null != list) {
77 for (PreferenceDefinition pre : list) {
78 Resource parent = getOrCreateParent(root, pre);
79
80 Resource current = getChildByResName(parent, pre.getShortName());
81 if (null != current && ("1" == current.getType())) {
82 current.setResName(pre.getShortName());
83 current.setActionClass(pre.getActionClass());
84 current.setBundle(pre.getBundle());
85 List<Resource> children = current.getChildren();
86 if (null != children && children.size() > 0) {
87 current.setLeaf(false);
88 } else {
89 current.setLeaf(true);
90 }
91 current.setResUrl(pre.getUri());
92 current.setType("0");
93 } else {
94 current = new Resource();
95 current.setResId(generateResId());
96 current.setResName(pre.getShortName());
97 current.setActionClass(pre.getActionClass());
98 current.setBundle(pre.getBundle());
99 current.setLeaf(true);
100 current.setParentId(parent.getResId());
101 current.setResUrl(pre.getUri());
102 current.setType("0");
103
104 parent.setLeaf(false);
105 parent.addChild(current);
106 }
107
108 }
109 }
110
111 JsonResultSupport.output(JSONObject.fromObject(root));
112 return null;
113 }
114
115
116
117
118
119
120
121 private Resource getChildByResName(Resource parent, String resName) {
122 List<Resource> children = parent.getChildren();
123 if (null != children && children.size() > 0) {
124 for (Resource child : children) {
125 if (child.getResName().equals(resName)) {
126 return child;
127 }
128 }
129 }
130 return null;
131 }
132
133
134
135
136
137
138
139 private Resource getOrCreateParent(Resource root, PreferenceDefinition pre) {
140 String[] ancestors = pre.getAncestors();
141 if (null == ancestors) {
142 return root;
143 }
144
145 Resource parent = root;
146 for (int deep = 0; deep < ancestors.length; deep++) {
147 String resName = ancestors[deep];
148 Resource child = getChildByResName(parent, resName);
149 if (null == child) {
150 child = new Resource();
151 child.setResId(generateResId());
152 child.setResName(resName);
153 child.setLeaf(true);
154 child.setParentId(parent.getResId());
155 child.setType("1");
156
157 parent.setLeaf(false);
158 parent.addChild(child);
159 }
160 parent = child;
161 }
162
163 return parent;
164 }
165 }