jiapeng 8 vuotta sitten
vanhempi
commit
775b1a984b

+ 3 - 0
src/main/java/com/ekexiu/console/system/dao/DictDao.java

11
import org.jfw.apt.orm.annotation.dao.method.operator.DeleteWith;
11
import org.jfw.apt.orm.annotation.dao.method.operator.DeleteWith;
12
import org.jfw.apt.orm.annotation.dao.method.operator.QueryVal;
12
import org.jfw.apt.orm.annotation.dao.method.operator.QueryVal;
13
import org.jfw.apt.orm.annotation.dao.method.operator.SelectList;
13
import org.jfw.apt.orm.annotation.dao.method.operator.SelectList;
14
import org.jfw.apt.orm.annotation.dao.method.operator.Update;
14
import org.jfw.apt.orm.annotation.dao.method.operator.UpdateWith;
15
import org.jfw.apt.orm.annotation.dao.method.operator.UpdateWith;
15
import org.jfw.apt.orm.annotation.dao.param.Set;
16
import org.jfw.apt.orm.annotation.dao.param.Set;
16
import org.jfw.apt.orm.core.defaultImpl.StringHandler;
17
import org.jfw.apt.orm.core.defaultImpl.StringHandler;
22

23

23
	@SelectList
24
	@SelectList
24
	List<Dict> queryAlll(Connection con)throws SQLException;
25
	List<Dict> queryAlll(Connection con)throws SQLException;
26
	@Update
27
	int update(Connection con ,Dict dict)throws SQLException;
25
	
28
	
26
	@Nullable
29
	@Nullable
27
	@QueryVal
30
	@QueryVal

+ 1 - 1
src/main/java/com/ekexiu/console/system/dao/OrgDao.java

37
	Organization query(Connection con, String id) throws SQLException;
37
	Organization query(Connection con, String id) throws SQLException;
38

38

39
	@PageSelect
39
	@PageSelect
40
	@OrderBy(" ORDER BY ID")
40
	@OrderBy(" ORDER BY NAME")
41
	PageQueryResult<Organization> query(Connection con, @Nullable @Like String name, @Nullable String type, @GtEq @Nullable @Alias("createTime") String bt,
41
	PageQueryResult<Organization> query(Connection con, @Nullable @Like String name, @Nullable String type, @GtEq @Nullable @Alias("createTime") String bt,
42
			@LtEq @Nullable @Alias("createTime") String et, int pageSize, int pageNo) throws SQLException;
42
			@LtEq @Nullable @Alias("createTime") String et, int pageSize, int pageNo) throws SQLException;
43

43


+ 2 - 1
src/main/java/com/ekexiu/console/system/dao/SysResourceDao.java

6

6

7
import org.jfw.apt.orm.annotation.dao.DAO;
7
import org.jfw.apt.orm.annotation.dao.DAO;
8
import org.jfw.apt.orm.annotation.dao.method.From;
8
import org.jfw.apt.orm.annotation.dao.method.From;
9
import org.jfw.apt.orm.annotation.dao.method.OrderBy;
9
import org.jfw.apt.orm.annotation.dao.method.operator.DeleteWith;
10
import org.jfw.apt.orm.annotation.dao.method.operator.DeleteWith;
10
import org.jfw.apt.orm.annotation.dao.method.operator.Insert;
11
import org.jfw.apt.orm.annotation.dao.method.operator.Insert;
11
import org.jfw.apt.orm.annotation.dao.method.operator.SelectList;
12
import org.jfw.apt.orm.annotation.dao.method.operator.SelectList;
26
	@From(SysResource.class)
27
	@From(SysResource.class)
27
	int delete(Connection con ,String id)throws SQLException;
28
	int delete(Connection con ,String id)throws SQLException;
28
	
29
	
30
	@OrderBy("ORDER BY ID ASC")
29
	@SelectList
31
	@SelectList
30
	List<SysResource> query(Connection con)throws SQLException;	
32
	List<SysResource> query(Connection con)throws SQLException;	
31
	
32
}
33
}

+ 9 - 1
src/main/java/com/ekexiu/console/system/po/Dict.java

12
	private String code;
12
	private String code;
13
	private String caption;
13
	private String caption;
14
	private String descp;
14
	private String descp;
15
	private boolean tree;
15
	private boolean readonly;
16
	private boolean readonly;
16
	private String item;
17
	private String item;
17
	
18
	
43
	public void setReadonly(boolean readonly) {
44
	public void setReadonly(boolean readonly) {
44
		this.readonly = readonly;
45
		this.readonly = readonly;
45
	}
46
	}
46
	@Column(DE.Text_de)
47
	@Column(value=DE.Text_de,renewable=false)
47
	public String getItem() {
48
	public String getItem() {
48
		return item;
49
		return item;
49
	}
50
	}
50
	public void setItem(String item) {
51
	public void setItem(String item) {
51
		this.item = item;
52
		this.item = item;
52
	}
53
	}
54
	@Column(DE.boolean_de)
55
	public boolean isTree() {
56
		return tree;
57
	}
58
	public void setTree(boolean tree) {
59
		this.tree = tree;
60
	}
53
	
61
	
54
}
62
}

+ 9 - 0
src/main/java/com/ekexiu/console/system/po/SysResource.java

9
@Table
9
@Table
10
public class SysResource {
10
public class SysResource {
11
	private String id;
11
	private String id;
12
	private String name;
12
	private String uri;
13
	private String uri;
13
	private String css;
14
	private String css;
14
	private String script;
15
	private String script;
25
	public void setType(String type) {
26
	public void setType(String type) {
26
		this.type = type;
27
		this.type = type;
27
	}
28
	}
29
	@Column(DE.string_de)
30
	public String getName() {
31
		return name;
32
	}
33

34
	public void setName(String name) {
35
		this.name = name;
36
	}
28

37

29
	@Column(DE.string_de)
38
	@Column(DE.string_de)
30
	public String getId() {
39
	public String getId() {

+ 88 - 43
src/main/java/com/ekexiu/console/system/service/DictService.java

11
import java.util.List;
11
import java.util.List;
12
import java.util.ListIterator;
12
import java.util.ListIterator;
13
import java.util.Map;
13
import java.util.Map;
14
import java.util.concurrent.atomic.AtomicBoolean;
14
import java.util.concurrent.atomic.AtomicReference;
15
import java.util.concurrent.atomic.AtomicReference;
15

16

16
import javax.swing.event.ListSelectionEvent;
17
import javax.sql.DataSource;
17

18

18
import org.jfw.apt.annotation.Autowrie;
19
import org.jfw.apt.annotation.Autowrie;
19
import org.jfw.apt.web.annotation.Path;
20
import org.jfw.apt.web.annotation.Path;
23
import org.jfw.apt.web.annotation.param.PathVar;
24
import org.jfw.apt.web.annotation.param.PathVar;
24
import org.jfw.apt.web.annotation.param.RequestBody;
25
import org.jfw.apt.web.annotation.param.RequestBody;
25
import org.jfw.util.exception.JfwBaseException;
26
import org.jfw.util.exception.JfwBaseException;
27
import org.jfw.util.jdbc.JdbcTask;
28
import org.jfw.util.jdbc.JdbcUtil;
26
import org.jfw.util.json.JsonService;
29
import org.jfw.util.json.JsonService;
27
import org.jfw.util.reflect.TypeReference;
30
import org.jfw.util.reflect.TypeReference;
28

31

60
		ret.addAll(ds);
63
		ret.addAll(ds);
61
		return ret;
64
		return ret;
62
	}
65
	}
63
	
66

64
	@Get
67
	@Get
65
	@Path("/code/{code}")
66
	public DictInfo get(@PathVar String code)
67
	{
68
	@Path("/info/{code}")
69
	public DictInfo get(@PathVar String code) {
68
		return this.dicts.get().get(code);
70
		return this.dicts.get().get(code);
69
	}
71
	}
70
	
71
	
72

73
	@Get
74
	@Path("/item/{code}")
75
	public List<DictItem> getItems(@PathVar String code) {
76
		AtomicReference<LinkedList<DictItem>> ref = this.items.get(code);
77
		return ref != null ? ref.get() : Collections.<DictItem> emptyList();
78
	}
79

80
	@Get
81
	@Path("/editItem/{code}")
82
	public List<EditDictItem> getItems(@JdbcConn Connection con, @PathVar String code) throws SQLException {
83
		DictInfo di = this.dicts.get().get(code);
84
		if (di == null || di.isReadonly())
85
			return Collections.<EditDictItem> emptyList();
86

87
		String val = this.dictDao.queryItem(con, code);
88
		if (val == null || val.length() < 3)
89
			return Collections.<EditDictItem> emptyList();
90
		return JsonService.fromJson(val, LINKEDLIST_EDITDICTITEM_TYPE);
91
	}
72

92

73
	@Put
93
	@Put
74
	@Path
75
	public void update(@JdbcConn(true) Connection con,  @RequestBody List<EditDictItem> items) throws SQLException, JfwBaseException {
76
		LinkedList<DictItem> ret = new LinkedList<DictItem>();
77
	//	if()
78
		
94
	@Path("/info")
95
	public void update(@JdbcConn final Connection con, @RequestBody final Dict dict) throws SQLException {
96
		final AtomicBoolean ab = new AtomicBoolean(false);
97
		JdbcUtil.executeAutoCommit(con, new JdbcTask() {
98

99
			@Override
100
			public void exec() throws SQLException {
101
				ab.set(dictDao.update(con, dict) > 0);
102
			}
103
		});
79

104

105
		if (ab.get())
106
			this.update(dict);
107
	}
108

109
	@Put
110
	@Path("/item/{code}")
111
	public void update(@JdbcConn(true) final Connection con, @PathVar final String code, @RequestBody List<EditDictItem> items)
112
			throws SQLException, JfwBaseException {
113
		DictInfo di = this.dicts.get().get(code);
114
		if (di == null) {
115
			throw new JfwBaseException(50001, "invalid parameter code: not exists dict code");
116
		}
117
		if (di.isReadonly()) {
118
			throw new JfwBaseException(50002, "invalid parameter code: readonly dict code");
119
		}
120
		// TODO : check dictItem.system == true can't modify
121
		LinkedList<DictItem> ret = new LinkedList<DictItem>();
122
		if(di.isTree()){
123
		if (transfer(items, ret) == null) {
124
			throw new JfwBaseException(50003, "not exists parentCode or loop parentCode");
125
		}}else{
126
			for(ListIterator<EditDictItem> it = items.listIterator();it.hasNext();){
127
				ret.add(transfer(it.next()));
128
			}
129
		}
130
		final AtomicBoolean ab = new AtomicBoolean(false);
131
		final String val = JsonService.toJson(items);
132
		JdbcUtil.executeAutoCommit(con, new JdbcTask() {
133
			public void exec() throws SQLException {
134
				if (dictDao.update(con, code, val) > 0)
135
					ab.set(true);
136
			}
137
		});
138
		if (ab.get()) {
139
			this.items.get(code).set(ret);
140
		}
80
	}
141
	}
81

142

82
	// public static List
143
	// public static List
105
		}
166
		}
106
		return null;
167
		return null;
107
	}
168
	}
108
	
109

110

169

111
	public LinkedList<DictItem> transfer(List<EditDictItem> src, LinkedList<DictItem> dest) {
170
	public LinkedList<DictItem> transfer(List<EditDictItem> src, LinkedList<DictItem> dest) {
112
		Map<String, EditDictItem> map = new HashMap<String, EditDictItem>();
171
		Map<String, EditDictItem> map = new HashMap<String, EditDictItem>();
140
				return null;
199
				return null;
141
			}
200
			}
142
		}
201
		}
143

144
		// while(map.size()>0){
145
		// boolean added = false;
146
		// for(Map.Entry<String,EditDictItem> entry:map.entrySet()){
147
		// String code = entry.getKey();
148
		// EditDictItem eitem = entry.getValue();
149
		// String pc =eitem.getParentCode();
150
		// if(pc ==null || pc.length()==0){
151
		// added = true;
152
		// map.remove(code);
153
		// dest.add(transfer(eitem));
154
		// break;
155
		// }else{
156
		// DictItem item = query(dest,pc);
157
		// if(item!=null){
158
		// added=true;
159
		// item.getChildren().add(transfer(eitem));
160
		// map.remove(code);
161
		// break;
162
		// }
163
		// }
164
		// }
165
		// if(!added) break;
166
		// }
167
		// return dest;
168
	}
202
	}
169

203

170
	public DictItem transfer(EditDictItem eitem) {
204
	public DictItem transfer(EditDictItem eitem) {
188
			if (null == transfer(list, ret)) {
222
			if (null == transfer(list, ret)) {
189
				// TODO:: // log not exists parentCode or loop parentCode
223
				// TODO:: // log not exists parentCode or loop parentCode
190

224

191
			} 
225
			}
192
		} catch (Exception e) {
226
		} catch (Exception e) {
193
			// TODO: ///// log json parse exception
227
			// TODO: ///// log json parse exception
194

228

196
		return ret;
230
		return ret;
197
	}
231
	}
198

232

233
	public void initByDataSource(DataSource ds) throws SQLException {
234
		final Connection con = ds.getConnection();
235
		JdbcUtil.executeAutoClose(con, new JdbcTask() {
236
			@Override
237
			public void exec() throws SQLException {
238
				List<Dict> list = DictService.this.dictDao.queryAlll(con);
239
				DictService.this.init(list);
240
			}
241
		}, false);
242
	}
243

199
	private synchronized void init(List<Dict> list) {
244
	private synchronized void init(List<Dict> list) {
200
		if(null!= this.dicts.get()) return;		
245
		if (null != this.dicts.get())
246
			return;
201
		Map<String, DictInfo> map = new HashMap<String, DictInfo>();
247
		Map<String, DictInfo> map = new HashMap<String, DictInfo>();
202
		for (Dict dict : list) {
248
		for (Dict dict : list) {
203
			map.put(dict.getCode(), new DictInfo(dict));
249
			map.put(dict.getCode(), new DictInfo(dict));
214
		map.put(dict.getCode(), new DictInfo(dict));
260
		map.put(dict.getCode(), new DictInfo(dict));
215
		this.dicts.set(map);
261
		this.dicts.set(map);
216
	}
262
	}
217
	
218
	
263

219
}
264
}

+ 76 - 13
src/main/java/com/ekexiu/console/system/service/RightService.java

2

2

3
import java.sql.Connection;
3
import java.sql.Connection;
4
import java.sql.SQLException;
4
import java.sql.SQLException;
5
import java.util.ArrayList;
6
import java.util.Collection;
7
import java.util.HashMap;
5
import java.util.List;
8
import java.util.List;
9
import java.util.Map;
10
import java.util.concurrent.atomic.AtomicReference;
11

12
import javax.sql.DataSource;
6

13

7
import org.jfw.apt.annotation.Autowrie;
14
import org.jfw.apt.annotation.Autowrie;
8
import org.jfw.apt.orm.annotation.dao.method.operator.Delete;
15
import org.jfw.apt.orm.annotation.dao.method.operator.Delete;
13
import org.jfw.apt.web.annotation.param.JdbcConn;
20
import org.jfw.apt.web.annotation.param.JdbcConn;
14
import org.jfw.apt.web.annotation.param.PathVar;
21
import org.jfw.apt.web.annotation.param.PathVar;
15
import org.jfw.apt.web.annotation.param.RequestBody;
22
import org.jfw.apt.web.annotation.param.RequestBody;
23
import org.jfw.util.jdbc.JdbcTask;
24
import org.jfw.util.jdbc.JdbcUtil;
16

25

17
import com.ekexiu.console.system.dao.RightDao;
26
import com.ekexiu.console.system.dao.RightDao;
18
import com.ekexiu.console.system.po.SysRight;
27
import com.ekexiu.console.system.po.SysRight;
19

28

20
@Path("/sys/right")
29
@Path("/sys/right")
21
public class RightService {
30
public class RightService {
31
	private AtomicReference<Map<Integer, SysRight>> rights = new AtomicReference<Map<Integer, SysRight>>();
22

32

23
	@Autowrie
33
	@Autowrie
24
	private RightDao rightDao;
34
	private RightDao rightDao;
30
	public void setRightDao(RightDao rightDao) {
40
	public void setRightDao(RightDao rightDao) {
31
		this.rightDao = rightDao;
41
		this.rightDao = rightDao;
32
	}
42
	}
33
	
34
	
35

43

36
	@Get
44
	@Get
37
	@Path("/all")
45
	@Path("/all")
38
	public List<SysRight> query(@JdbcConn Connection con) throws SQLException {
39
		return this.rightDao.query(con);
46
	public Collection<SysRight> query() throws SQLException {
47
		return this.rights.get().values();
40
	}
48
	}
41

49

42
	@Get
50
	@Get
43
	@Path("/type/{type}")
51
	@Path("/type/{type}")
44
	public List<SysRight> queryByType(@JdbcConn Connection con, @PathVar String type) throws SQLException {
45
		return this.rightDao.query(con, type);
52
	public List<SysRight> queryByType(@PathVar String type) throws SQLException {
53
		ArrayList<SysRight> list = new ArrayList<SysRight>();
54
		for (SysRight right : this.query()) {
55
			if (type.equals(right.getType()))
56
				list.add(right);
57
		}
58
		return list;
46
	}
59
	}
47

60

48
	@Get
61
	@Get
49
	@Path("/id/{id}")
62
	@Path("/id/{id}")
50
	public SysRight queryById(@JdbcConn Connection con, @PathVar int id) throws SQLException {
63
	public SysRight queryById(@JdbcConn Connection con, @PathVar int id) throws SQLException {
51
		return this.rightDao.query(con, id);
64
		return this.rights.get().get(id);
52
	}
65
	}
53

66

54
	@Post
67
	@Post
55
	@Path()
68
	@Path()
56
	public void insert(@JdbcConn(true) Connection con, SysRight right) throws SQLException {
57
		this.rightDao.insert(con, right);
69
	public void insert(@JdbcConn final Connection con, final SysRight right) throws SQLException {
70
		JdbcUtil.executeAutoCommit(con, new JdbcTask() {
71
			@Override
72
			public void exec() throws SQLException {
73
				rightDao.insert(con, right);
74
			}
75
		});
76
		this.update(right);
58
	}
77
	}
59

78

60
	@Put
79
	@Put
61
	@Path()
80
	@Path()
62
	public void update(@JdbcConn(true) Connection con, @RequestBody SysRight right) throws SQLException {
63
		this.rightDao.insert(con, right);
81
	public void update(@JdbcConn final Connection con, @RequestBody final SysRight right) throws SQLException {
82
		final AtomicReference<Boolean> ab = new AtomicReference<Boolean>(false);
83
		JdbcUtil.executeAutoCommit(con, new JdbcTask() {
84
			@Override
85
			public void exec() throws SQLException {
86
				ab.set(rightDao.update(con, right)>0);
87
			}
88
		});
89
		if(ab.get())this.update(right);
64
	}
90
	}
65

91

66
	@Path("/{id}")
92
	@Path("/{id}")
67
	@Delete
93
	@Delete
68
	public void delete(@JdbcConn(true) Connection con, @PathVar int id) throws SQLException {
69
		this.rightDao.delete(con, id);
94
	public void delete(@JdbcConn final Connection con, @PathVar final int id) throws SQLException {
95
		final AtomicReference<Boolean> ab = new AtomicReference<Boolean>(false);
96
		JdbcUtil.executeAutoCommit(con, new JdbcTask() {
97
			@Override
98
			public void exec() throws SQLException {
99
				ab.set(rightDao.delete(con, id)>0);
100
			}
101
		});
102
		if(ab.get())this.remove(id);
103
	}
104

105
	public void initByDataSource(DataSource ds) throws SQLException {
106
		final Connection con = ds.getConnection();
107
		JdbcUtil.executeAutoClose(con, new JdbcTask() {
108
			@Override
109
			public void exec() throws SQLException {
110
				List<SysRight> list =rightDao. query(con);
111
				Map<Integer, SysRight> map = new HashMap<Integer, SysRight>();
112
				for (SysRight right : list) {
113
					map.put(right.getId(), right);
114
				}
115
				rights.set(map);
116
			}
117
		}, false);
118
	}
119

120
	private void update(SysRight right) {
121
		Map<Integer, SysRight> map = new HashMap<Integer, SysRight>();
122
		map.putAll(rights.get());
123
		map.remove(right.getId());
124
		map.put(right.getId(), right);
125
		rights.set(map);
126
	}
127

128
	private void remove(int rightId) {
129
		Map<Integer, SysRight> map = new HashMap<Integer, SysRight>();
130
		map.putAll(rights.get());
131
		map.remove(rightId);
132
		rights.set(map);
70
	}
133
	}
71

134

72
}
135
}

+ 108 - 50
src/main/java/com/ekexiu/console/system/service/RoleService.java

3
import java.sql.Connection;
3
import java.sql.Connection;
4
import java.sql.SQLException;
4
import java.sql.SQLException;
5
import java.util.ArrayList;
5
import java.util.ArrayList;
6
import java.util.Collection;
7
import java.util.HashMap;
6
import java.util.List;
8
import java.util.List;
7
import java.util.ListIterator;
9
import java.util.ListIterator;
10
import java.util.Map;
11
import java.util.concurrent.atomic.AtomicBoolean;
12
import java.util.concurrent.atomic.AtomicReference;
13

14
import javax.sql.DataSource;
8

15

9
import org.jfw.apt.annotation.Autowrie;
16
import org.jfw.apt.annotation.Autowrie;
10
import org.jfw.apt.web.annotation.Path;
17
import org.jfw.apt.web.annotation.Path;
17
import org.jfw.apt.web.annotation.param.RequestBody;
24
import org.jfw.apt.web.annotation.param.RequestBody;
18
import org.jfw.util.StringUtil;
25
import org.jfw.util.StringUtil;
19
import org.jfw.util.auth.AuthUtil;
26
import org.jfw.util.auth.AuthUtil;
27
import org.jfw.util.jdbc.JdbcTask;
28
import org.jfw.util.jdbc.JdbcUtil;
20

29

21
import com.ekexiu.console.system.dao.RoleDao;
30
import com.ekexiu.console.system.dao.RoleDao;
22
import com.ekexiu.console.system.dao.UserRoleDao;
31
import com.ekexiu.console.system.dao.UserRoleDao;
25

34

26
@Path("/sys/role")
35
@Path("/sys/role")
27
public class RoleService {
36
public class RoleService {
28
	
29
	public static final String EMPTY_AUTH_STR = AuthUtil.serialAuth(new int[]{0});
30
	
37

38
	public static final String EMPTY_AUTH_STR = AuthUtil.serialAuth(new int[] { 0 });
39
	private AtomicReference<Map<String, ConsoleAuthRole>> cacheRoles = new AtomicReference<Map<String, ConsoleAuthRole>>();
40

31
	@Autowrie
41
	@Autowrie
32
	private RoleDao roleDao;
42
	private RoleDao roleDao;
33
	@Autowrie
43
	@Autowrie
48
	public void setRoleDao(RoleDao roleDao) {
58
	public void setRoleDao(RoleDao roleDao) {
49
		this.roleDao = roleDao;
59
		this.roleDao = roleDao;
50
	}
60
	}
51
	
52
	
53
	
61

54
	@Post
62
	@Post
55
	@Path
63
	@Path
56
	public void insert(@JdbcConn(true) Connection con,ConsoleAuthRole role)throws SQLException{
57
		Role r = new Role();
58
		r.setId(StringUtil.buildUUID());
64
	public void insert(@JdbcConn final Connection con, ConsoleAuthRole role) throws SQLException {
65
		String id = StringUtil.buildUUID();
66
		role.setId(id);
67

68
		final Role r = new Role();
69
		r.setId(id);
59
		r.setName(role.getName());
70
		r.setName(role.getName());
60
		r.setDescp(role.getDescp());
71
		r.setDescp(role.getDescp());
61
		int[] auths = role.getAuths();
72
		int[] auths = role.getAuths();
62
		if(auths==null || auths.length==0){
73
		if (auths == null || auths.length == 0) {
63
			r.setAuthinfo(EMPTY_AUTH_STR);
74
			r.setAuthinfo(EMPTY_AUTH_STR);
64
		}else{
75
		} else {
65
			r.setAuthinfo(AuthUtil.serialAuth(AuthUtil.compress(auths)));
76
			r.setAuthinfo(AuthUtil.serialAuth(AuthUtil.compress(auths)));
66
		}
77
		}
67
		this.roleDao.insert(con, r);
78
		JdbcUtil.executeAutoCommit(con, new JdbcTask() {
79
			@Override
80
			public void exec() throws SQLException {
81

82
				roleDao.insert(con, r);
83
			}
84
		});
85
		this.update(role);
68
	}
86
	}
87

69
	@Put
88
	@Put
70
	@Path
89
	@Path
71
	public void update(@JdbcConn(true) Connection con,@RequestBody ConsoleAuthRole role)throws SQLException{
72
		Role r = new Role();
73
		r.setName(role.getName());
74
		r.setDescp(role.getDescp());
75
		int[] auths = role.getAuths();
76
		if(auths==null || auths.length==0){
77
			r.setAuthinfo(EMPTY_AUTH_STR);
78
		}else{
79
			r.setAuthinfo(AuthUtil.serialAuth(AuthUtil.compress(auths)));
80
		}
81
		this.roleDao.update(con, r);
90
	public void update(@JdbcConn final Connection con, @RequestBody final ConsoleAuthRole role) throws SQLException {
91
		final AtomicBoolean ab = new AtomicBoolean(false);
92
		JdbcUtil.executeAutoCommit(con, new JdbcTask() {
93

94
			@Override
95
			public void exec() throws SQLException {
96

97
				Role r = new Role();
98
				r.setName(role.getName());
99
				r.setDescp(role.getDescp());
100
				int[] auths = role.getAuths();
101
				if (auths == null || auths.length == 0) {
102
					r.setAuthinfo(EMPTY_AUTH_STR);
103
				} else {
104
					r.setAuthinfo(AuthUtil.serialAuth(AuthUtil.compress(auths)));
105
				}
106
				ab.set(roleDao.update(con, r) > 0);
107
			}
108
		});
109
		if (ab.get())
110
			this.update(role);
82
	}
111
	}
83
	
112

84
	@Path("/{id}")
113
	@Path("/{id}")
85
	@Delete
114
	@Delete
86
	public void delete(@JdbcConn(true) Connection con,@PathVar String id)throws SQLException{
87
		 this.userRoleDao.deleteByRoleId(con, id);		 
88
		 this.roleDao.delete(con, id);
115
	public void delete(@JdbcConn final Connection con, @PathVar final String id) throws SQLException {
116
		final AtomicBoolean ab = new AtomicBoolean(false);
117
		JdbcUtil.executeAutoCommit(con, new JdbcTask() {
118
			@Override
119
			public void exec() throws SQLException {
120
				userRoleDao.deleteByRoleId(con, id);
121
				ab.set(roleDao.delete(con, id) > 0);
122
			}
123
		});
124
		if (ab.get())
125
			this.remove(id);
89
	}
126
	}
90
	
127

91
	@Path("/all")
128
	@Path("/all")
92
	@Get
129
	@Get
93
	public List<ConsoleAuthRole> queryAll(@JdbcConn Connection con)throws SQLException{
94
		List<Role> roles= this.roleDao.query(con);
95
		ArrayList<ConsoleAuthRole> ret = new ArrayList<ConsoleAuthRole>(roles.size());
96
		for(ListIterator<Role> it = roles.listIterator(); it.hasNext();){
97
			ret.add(conver(it.next()));
98
		}
99
		return ret;
130
	public Collection<ConsoleAuthRole> queryAll() throws SQLException {
131
		return this.cacheRoles.get().values();
100
	}
132
	}
133

101
	@Path("/id/{id}")
134
	@Path("/id/{id}")
102
	@Get
135
	@Get
103
	public ConsoleAuthRole queryById(@JdbcConn Connection con ,@PathVar String id ) throws SQLException{
104
	  return conver(this.roleDao.query(con, id));
136
	public ConsoleAuthRole queryById(@PathVar String id) throws SQLException {
137
		return this.cacheRoles.get().get(id);
105
	}
138
	}
139

106
	@Path("/byUserid/{userid}")
140
	@Path("/byUserid/{userid}")
107
	@Get
141
	@Get
108
	public List<ConsoleAuthRole> queryByUserId(@JdbcConn Connection con,@PathVar String userid)throws SQLException{
109
		List<Role> roles= this.roleDao.queryByUser(con, userid);
142
	public List<ConsoleAuthRole> queryByUserId(@JdbcConn Connection con, @PathVar String userid) throws SQLException {
143
		List<Role> roles = this.roleDao.queryByUser(con, userid);
110
		ArrayList<ConsoleAuthRole> ret = new ArrayList<ConsoleAuthRole>(roles.size());
144
		ArrayList<ConsoleAuthRole> ret = new ArrayList<ConsoleAuthRole>(roles.size());
111
		for(ListIterator<Role> it = roles.listIterator(); it.hasNext();){
145
		for (ListIterator<Role> it = roles.listIterator(); it.hasNext();) {
112
			ret.add(conver(it.next()));
146
			ret.add(conver(it.next()));
113
		}
147
		}
114
		return ret;
148
		return ret;
115
	}
149
	}
116

150

117
	
118
	
119
	
120
	
121
	
122
	
123
	
124
	
125
	public static ConsoleAuthRole conver(Role role){
126
		if(null == role) return null;
151
	public static ConsoleAuthRole conver(Role role) {
152
		if (null == role)
153
			return null;
127
		ConsoleAuthRole cr = new ConsoleAuthRole();
154
		ConsoleAuthRole cr = new ConsoleAuthRole();
128
		cr.setDescp(role.getDescp());
155
		cr.setDescp(role.getDescp());
129
		cr.setId(role.getId());
156
		cr.setId(role.getId());
131
		cr.setAuths(AuthUtil.unCompress(AuthUtil.deSerialAuth(role.getAuthinfo())));
158
		cr.setAuths(AuthUtil.unCompress(AuthUtil.deSerialAuth(role.getAuthinfo())));
132
		return cr;
159
		return cr;
133
	}
160
	}
161

162
	public void initByDataSource(DataSource ds) throws SQLException {
163
		final Connection con = ds.getConnection();
164
		JdbcUtil.executeAutoClose(con, new JdbcTask() {
165
			@Override
166
			public void exec() throws SQLException {
167
				List<Role> list = roleDao.query(con);
168
				Map<String, ConsoleAuthRole> map = new HashMap<String, ConsoleAuthRole>();
169
				for (Role role : list) {
170
					ConsoleAuthRole car = conver(role);
171
					map.put(car.getId(), car);
172
				}
173
				cacheRoles.set(map);
174
			}
175
		}, false);
176
	}
177

178
	private void update(ConsoleAuthRole car) {
179
		Map<String, ConsoleAuthRole> map = new HashMap<String, ConsoleAuthRole>();
180
		map.putAll(cacheRoles.get());
181
		map.remove(car.getId());
182
		map.put(car.getId(), car);
183
		cacheRoles.set(map);
184
	}
185

186
	private void remove(String id) {
187
		Map<String, ConsoleAuthRole> map = new HashMap<String, ConsoleAuthRole>();
188
		map.putAll(cacheRoles.get());
189
		map.remove(id);
190
		cacheRoles.set(map);
191
	}
134
}
192
}

+ 80 - 18
src/main/java/com/ekexiu/console/system/service/SysResourceService.java

3
import java.sql.Connection;
3
import java.sql.Connection;
4
import java.sql.SQLException;
4
import java.sql.SQLException;
5
import java.util.ArrayList;
5
import java.util.ArrayList;
6
import java.util.Collection;
7
import java.util.HashMap;
6
import java.util.List;
8
import java.util.List;
9
import java.util.Map;
10
import java.util.concurrent.atomic.AtomicBoolean;
11
import java.util.concurrent.atomic.AtomicReference;
12

13
import javax.sql.DataSource;
7

14

8
import org.jfw.apt.annotation.Autowrie;
15
import org.jfw.apt.annotation.Autowrie;
9
import org.jfw.apt.web.annotation.LoginUser;
16
import org.jfw.apt.web.annotation.LoginUser;
14
import org.jfw.apt.web.annotation.operate.Put;
21
import org.jfw.apt.web.annotation.operate.Put;
15
import org.jfw.apt.web.annotation.param.JdbcConn;
22
import org.jfw.apt.web.annotation.param.JdbcConn;
16
import org.jfw.apt.web.annotation.param.PathVar;
23
import org.jfw.apt.web.annotation.param.PathVar;
24
import org.jfw.util.jdbc.JdbcTask;
25
import org.jfw.util.jdbc.JdbcUtil;
17

26

18
import com.ekexiu.console.system.dao.SysResourceDao;
27
import com.ekexiu.console.system.dao.SysResourceDao;
19
import com.ekexiu.console.system.po.SysResource;
28
import com.ekexiu.console.system.po.SysResource;
21

30

22
@Path("/sys/resource")
31
@Path("/sys/resource")
23
public class SysResourceService {
32
public class SysResourceService {
24
	
33
	private AtomicReference<Map<String, SysResource>> cache = new AtomicReference<Map<String, SysResource>>();
25
	@Autowrie
34
	@Autowrie
26
	private SysResourceDao sysResourceDao;
35
	private SysResourceDao sysResourceDao;
27

36

32
	public void setSysResourceDao(SysResourceDao sysResourceDao) {
41
	public void setSysResourceDao(SysResourceDao sysResourceDao) {
33
		this.sysResourceDao = sysResourceDao;
42
		this.sysResourceDao = sysResourceDao;
34
	}
43
	}
35
	
44

36
	@Get
45
	@Get
37
	@Path("/all")
46
	@Path("/all")
38
	public List<SysResource> query(@JdbcConn Connection con)throws SQLException{
39
		return this.sysResourceDao.query(con);
47
	public Collection<SysResource> query() throws SQLException {
48
		return this.cache.get().values();
40
	}
49
	}
41
	
50

42
	@Post
51
	@Post
43
	@Path
52
	@Path
44
	public void insert(@JdbcConn(true) Connection con,SysResource sres)throws SQLException{
45
		this.sysResourceDao.insert(con, sres);
53
	public void insert(@JdbcConn(true) final Connection con, final SysResource sres) throws SQLException {
54
		JdbcUtil.executeAutoCommit(con, new JdbcTask() {
55
			@Override
56
			public void exec() throws SQLException {
57
				sysResourceDao.insert(con, sres);
58
			}
59
		});
60
		this.update(sres);
46
	}
61
	}
47
	
62

48
	@Put
63
	@Put
49
	@Path
64
	@Path
50
	public void update(@JdbcConn(true) Connection con,SysResource sres) throws SQLException{
51
		this.sysResourceDao.update(con, sres);
65
	public void update(@JdbcConn(true) final Connection con, final SysResource sres) throws SQLException {
66
		final AtomicBoolean ab = new AtomicBoolean(false);
67
		JdbcUtil.executeAutoCommit(con, new JdbcTask() {
68
			@Override
69
			public void exec() throws SQLException {
70
				ab.set(sysResourceDao.update(con, sres) > 0);
71
			}
72
		});
73
		if (ab.get())
74
			this.update(sres);
52
	}
75
	}
76

53
	@Delete
77
	@Delete
54
	@Path("/{id}")
55
	public void delete(@JdbcConn(true) Connection con ,@PathVar String id)throws SQLException{
56
		this.sysResourceDao.delete(con, id);
78
	@Path("/id/{id}")
79
	public void delete(@JdbcConn(true) final Connection con, @PathVar final String id) throws SQLException {
80
		final AtomicBoolean ab = new AtomicBoolean(false);
81
		JdbcUtil.executeAutoCommit(con, new JdbcTask() {
82
			@Override
83
			public void exec() throws SQLException {
84
				ab.set(sysResourceDao.delete(con, id) > 0);
85
			}
86
		});
87
		if (ab.get())
88
			this.remove(id);
57
	}
89
	}
58
	
90

59
	@Get
91
	@Get
60
	@Path("/byLoginUser")
61
	public List<SysResource> query(@JdbcConn Connection con,@LoginUser ConsoleAuthUser user )throws SQLException{
92
	@Path("/user")
93
	public List<SysResource> query(@JdbcConn Connection con, @LoginUser ConsoleAuthUser user) throws SQLException {
62
		List<SysResource> ret = new ArrayList<SysResource>();
94
		List<SysResource> ret = new ArrayList<SysResource>();
63
		for(SysResource res:this.query(con)){
64
			if(user.hasAuthority(res.getAuthRight())){
95
		for (SysResource res :this.cache.get().values()) {
96
			if (user.hasAuthority(res.getAuthRight())) {
65
				ret.add(res);
97
				ret.add(res);
66
			}
98
			}
67
		}
99
		}
68
		return ret;
100
		return ret;
69
	}
101
	}
102

103
	public void initByDataSource(DataSource ds) throws SQLException {
104
		final Connection con = ds.getConnection();
105
		JdbcUtil.executeAutoClose(con, new JdbcTask() {
106
			@Override
107
			public void exec() throws SQLException {
108
				List<SysResource> list = sysResourceDao.query(con);
109
				Map<String, SysResource> map = new HashMap<String, SysResource>();
110
				for (SysResource res : list) {
111
					map.put(res.getId(), res);
112
				}
113
				cache.set(map);
114
			}
115
		}, false);
116
	}
117

118
	private void update(SysResource res) {
119
		Map<String, SysResource> map = new HashMap<String, SysResource>();
120
		map.putAll(cache.get());
121
		map.remove(res.getId());
122
		map.put(res.getId(), res);
123
		cache.set(map);
124
	}
125

126
	private void remove(String id) {
127
		Map<String, SysResource> map = new HashMap<String, SysResource>();
128
		map.putAll(cache.get());
129
		map.remove(id);
130
		cache.set(map);
131
	}
70
}
132
}

+ 30 - 6
src/main/java/com/ekexiu/console/system/service/UserConfigService.java

5
import java.lang.reflect.Type;
5
import java.lang.reflect.Type;
6
import java.sql.Connection;
6
import java.sql.Connection;
7
import java.sql.SQLException;
7
import java.sql.SQLException;
8
import java.util.ArrayList;
8
import java.util.List;
9
import java.util.List;
9

10

10
import org.jfw.apt.annotation.Autowrie;
11
import org.jfw.apt.annotation.Autowrie;
20
import org.jfw.util.reflect.TypeReference;
21
import org.jfw.util.reflect.TypeReference;
21

22

22
import com.ekexiu.console.system.dao.UserConfigDao;
23
import com.ekexiu.console.system.dao.UserConfigDao;
24
import com.ekexiu.console.system.po.SysResource;
23
import com.ekexiu.console.system.po.UserConfig;
25
import com.ekexiu.console.system.po.UserConfig;
24
import com.ekexiu.console.system.vo.ConsoleAuthUser;
26
import com.ekexiu.console.system.vo.ConsoleAuthUser;
25
import com.ekexiu.console.system.vo.UserMenu;
27
import com.ekexiu.console.system.vo.UserMenu;
29

31

30
	@Autowrie
32
	@Autowrie
31
	private UserConfigDao userConfigDao;
33
	private UserConfigDao userConfigDao;
34
	@Autowrie
35
	private SysResourceService sysResourceService;
36

37
	public SysResourceService getSysResourceService() {
38
		return sysResourceService;
39
	}
40

41
	public void setSysResourceService(SysResourceService sysResourceService) {
42
		this.sysResourceService = sysResourceService;
43
	}
32

44

33
	public UserConfigDao getUserConfigDao() {
45
	public UserConfigDao getUserConfigDao() {
34
		return userConfigDao;
46
		return userConfigDao;
68

80

69
	@Get
81
	@Get
70
	@Path("/menu")
82
	@Path("/menu")
71
	public List<UserMenu> menu(@JdbcConn Connection con,@LoginUser() ConsoleAuthUser user)throws SQLException{
72
		List<UserMenu> ret = this.load(con,user.getId(),"menu",MENU_TYPE);
73
		if(null==ret){
74
			//TODO: impl load default
83
	public List<UserMenu> menu(@JdbcConn Connection con, @LoginUser() ConsoleAuthUser user) throws SQLException {
84
		List<UserMenu> ret = this.load(con, user.getId(), "menu", MENU_TYPE);
85
		if (null == ret || ret.isEmpty()) {
86
			ret = new ArrayList<UserMenu>();
87
			List<SysResource> reses = this.sysResourceService.query(con, user);
88
			for (SysResource res : reses) {
89
				if (res.isMain())
90
					ret.add(transfer(res));
91
			}
75
		}
92
		}
76
		//TODO: test del trigger deploy
77
		return	 JsonService.fromJson("[{\"caption\":\"组织机构\",\"children\":[{\"caption\":\"机构管理\",\"res\":\"sys_org_index\"}]}]", MENU_TYPE);
93
		return ret;
94
	}
95

96
	public UserMenu transfer(SysResource res) {
97
		UserMenu menu = new UserMenu();
98
		menu.setCaption(res.getName());
99
		menu.setModal(false);
100
		menu.setRes(res.getId());
101
		return menu;
78
	}
102
	}
79

103

80
}
104
}

+ 8 - 0
src/main/java/com/ekexiu/console/system/vo/DictInfo.java

7
	private String caption;
7
	private String caption;
8
	private String descp;
8
	private String descp;
9
	private boolean readonly;
9
	private boolean readonly;
10
	private boolean tree;
10
	
11
	
11
	
12
	
12
	
13
	
16
		this.caption = dict.getCaption();
17
		this.caption = dict.getCaption();
17
		this.descp = dict.getDescp();
18
		this.descp = dict.getDescp();
18
		this.readonly = dict.isReadonly();
19
		this.readonly = dict.isReadonly();
20
		this.tree = dict.isTree();
19
	}
21
	}
20
	public String getCode() {
22
	public String getCode() {
21
		return code;
23
		return code;
41
	public void setReadonly(boolean readonly) {
43
	public void setReadonly(boolean readonly) {
42
		this.readonly = readonly;
44
		this.readonly = readonly;
43
	}
45
	}
46
	public boolean isTree() {
47
		return tree;
48
	}
49
	public void setTree(boolean tree) {
50
		this.tree = tree;
51
	}
44
	
52
	
45
	
53
	
46
}
54
}

+ 104 - 0
src/main/java/com/ekexiu/console/util/GlobalInitBean.java

1
package com.ekexiu.console.util;
2

3
import java.util.LinkedList;
4
import java.util.List;
5
import java.util.ListIterator;
6
import java.util.concurrent.ScheduledExecutorService;
7

8
import javax.sql.DataSource;
9

10
import org.jfw.util.bean.AfterBeanFactory;
11
import org.jfw.util.bean.BeanFactory;
12
import org.jfw.util.context.JfwAppContext;
13

14
import com.ekexiu.console.system.service.DictService;
15
import com.ekexiu.console.system.service.RightService;
16
import com.ekexiu.console.system.service.RoleService;
17
import com.ekexiu.console.system.service.SysResourceService;
18

19
public class GlobalInitBean implements AfterBeanFactory {
20
	private DataSource dataSource;
21
	private ScheduledExecutorService service;
22
	private BeanFactory beanf;
23
	
24
	
25
	public BeanFactory getBeanf() {
26
		return beanf;
27
	}
28

29

30
	public void setBeanf(BeanFactory beanf) {
31
		this.beanf = beanf;
32
	}
33

34

35
	public DataSource getDataSource() {
36
		return dataSource;
37
	}
38

39

40
	public void setDataSource(DataSource dataSource) {
41
		this.dataSource = dataSource;
42
	}
43

44

45
	public ScheduledExecutorService getService() {
46
		return service;
47
	}
48

49

50
	public void setService(ScheduledExecutorService service) {
51
		this.service = service;
52
	}
53

54

55
	@Override
56
	public void handle(BeanFactory bf) throws Throwable {
57
		this.beanf = bf;
58
		this.service = JfwAppContext.getScheduledExecutorService();
59
		this.dataSource = (DataSource) bf.getBean("dataSource");
60
		for(ListIterator<Initable> it= this.loadInitables().listIterator() ; it.hasNext();){
61
			it.next().init(this);
62
		}
63
	}
64
	
65
	public List<Initable> loadInitables()throws Exception{
66
		LinkedList<Initable> list = new LinkedList<Initable>();
67
		list.add(new Initable() {			
68
			@Override
69
			public void init(GlobalInitBean gib) throws Exception {
70
				getBean(DictService.class).initByDataSource(dataSource);
71
			}
72
		});
73
		list.add(new Initable() {			
74
			@Override
75
			public void init(GlobalInitBean gib) throws Exception {
76
				getBean(RightService.class).initByDataSource(dataSource);
77
			}
78
		});
79
		list.add(new Initable() {			
80
			@Override
81
			public void init(GlobalInitBean gib) throws Exception {
82
				getBean(RoleService.class).initByDataSource(dataSource);
83
			}
84
		});
85
		list.add(new Initable() {			
86
			@Override
87
			public void init(GlobalInitBean gib) throws Exception {
88
				getBean(SysResourceService.class).initByDataSource(dataSource);
89
			}
90
		});
91
		return list;
92
	}
93
	public <T> T getBean(Class<T> cls){
94
		return this.beanf.getBean(classNameToBeanName(cls), cls);
95
	}
96

97
	public static String classNameToBeanName(Class<?> cls){
98
		return cls.getName().replaceAll("\\.","_");
99
	}
100
	
101
	public static void main(String[] args){
102
		System.out.println(classNameToBeanName(GlobalInitBean.class));
103
	}
104
}

+ 5 - 0
src/main/java/com/ekexiu/console/util/Initable.java

1
package com.ekexiu.console.util;
2

3
public interface Initable {
4
	void init(GlobalInitBean gib)throws Exception; 
5
}

+ 3 - 3
src/main/webapp/WEB-INF/web.xml

15
      <param-name>configFileName</param-name>
15
      <param-name>configFileName</param-name>
16
      <param-value>beanConfig.properties,${main.config}</param-value>
16
      <param-value>beanConfig.properties,${main.config}</param-value>
17
    </init-param>
17
    </init-param>
18
<!--     <init-param>
18
  <init-param>
19
    	<param-name>afterBeanFactory</param-name>
19
    	<param-name>afterBeanFactory</param-name>
20
    	<param-value>com.ekexiu.portal.job.SchJob,com.ekexiu.portal.job.TaskJob</param-value>
21
    </init-param> -->
20
    	<param-value>com.ekexiu.console.util.GlobalInitBean</param-value>
21
    </init-param>
22
    <load-on-startup>1</load-on-startup>
22
    <load-on-startup>1</load-on-startup>
23
  </servlet>
23
  </servlet>
24
  
24