jiapeng 8 years ago
parent
commit
b3e4a89969

+ 196 - 0
src/main/java/com/ekexiu/console/service/Upload.java

@ -0,0 +1,196 @@
1
package com.ekexiu.console.service;
2

3
import java.io.File;
4
import java.io.FileOutputStream;
5
import java.io.IOException;
6
import java.io.InputStream;
7
import java.util.concurrent.atomic.AtomicReference;
8

9
import org.jfw.util.exception.JfwBaseException;
10
import org.jfw.util.io.IoUtil;
11
import org.jfw.util.web.fileupload.Item;
12
import org.jfw.util.web.fileupload.UploadItemIterator;
13

14
public class Upload {
15
	public static final int ERROR_NO_FOUND_SUFFIX = JfwBaseException.MIN_APP_ERROR_CODE + 1;
16
	public static final int ERROR_INVALID_SUFFIX = JfwBaseException.MIN_APP_ERROR_CODE + 2;
17
	public static final int ERROR_INVALID_SIZE = JfwBaseException.MIN_APP_ERROR_CODE + 3;
18
	// public static final int ERROR_NO_FOUND_SUFFIX =
19
	// JfwBaseException.MIN_APP_ERROR_CODE+1;
20

21
	private File path;
22
	private String validSuffix;
23
	private boolean includeSuffix;
24
	private long maxSize = Long.MAX_VALUE;
25
	private long rate = 24 * 60 * 60 * 1000;
26
	private int buffSize = 8092;
27
	private AtomicReference<String> lastPathWithCreate = new AtomicReference<String>(null);
28

29
	public int getBuffSize() {
30
		return buffSize;
31
	}
32

33
	public void setBuffSize(int buffSize) {
34
		this.buffSize = buffSize;
35
	}
36

37
	public long getRate() {
38
		return rate;
39
	}
40

41
	public void setRate(long rate) {
42
		this.rate = rate;
43
	}
44

45
	public File getPath() {
46
		return path;
47
	}
48

49
	public void setPath(File path) {
50
		this.path = path;
51
	}
52

53
	public String getValidSuffix() {
54
		return validSuffix;
55
	}
56

57
	public void setValidSuffix(String validSuffix) {
58
		this.validSuffix = validSuffix;
59
	}
60

61
	public boolean isIncludeSuffix() {
62
		return includeSuffix;
63
	}
64

65
	public void setIncludeSuffix(boolean includeSuffix) {
66
		this.includeSuffix = includeSuffix;
67
	}
68

69
	public long getMaxSize() {
70
		return maxSize;
71
	}
72

73
	public void setMaxSize(long maxSize) {
74
		this.maxSize = maxSize;
75
	}
76

77
	public void setUploadPath(File path) {
78
		this.path = path;
79
	}
80

81
	private void checkSuffix(String ext) throws JfwBaseException {
82
		if (ext == null) {
83
			if (this.includeSuffix) {
84
				throw new JfwBaseException(ERROR_NO_FOUND_SUFFIX, "upload file no found suffix");
85
			}
86
		} else {
87
			if (null != this.validSuffix && this.validSuffix.indexOf("," + ext + ",") < 0) {
88
				throw new JfwBaseException(ERROR_INVALID_SUFFIX, "upload file suffix  invalid");
89
			}
90
		}
91
	}
92

93
	public UploadItem upload(UploadItemIterator it) throws IOException, JfwBaseException {
94
		while (it.hasNext()) {
95
			Item item = it.next();
96
			if (!item.isFormField()) {
97
				String fn = item.getName();
98
				String ext = null;
99
				int index = fn.lastIndexOf('.');
100
				if (index >= 0) {
101
					ext = fn.substring(index + 1);
102
					if (ext.length() == 0) {
103
						ext = null;
104
					}
105
				}
106
				this.checkSuffix(ext);
107
				UploadItem ui = new UploadItem();
108
				ui.setName(fn);
109
				String uri = this.findValidUri(ext);
110
				File dest = new File(this.path, uri);
111
				while (dest.exists()) {
112
					uri = this.findValidUri(ext);
113
					dest = new File(this.path, uri);
114
				}
115
				FileOutputStream os = new FileOutputStream(dest);
116
				try {
117
					byte[] buf = new byte[this.buffSize];
118
					InputStream in = item.getInputStream();
119
					long size = 0;
120
					try {
121
						int len = 0;
122
						while ((len = in.read(buf)) >= 0) {
123
							if (len > 0) {
124
								os.write(buf, 0, len);
125
								size += len;
126
								if (size > this.maxSize) {
127
									throw new JfwBaseException(ERROR_INVALID_SIZE, "upload file size is larger");
128
								}
129
							}
130
						}
131
						ui.setSize(size);
132
						ui.setUri(uri);
133
						return ui;
134
					} finally {
135
						IoUtil.close(in);
136
					}
137
				} finally {
138
					IoUtil.close(os);
139
				}
140
			}
141
		}
142
		return null;
143
	}
144

145
	private String findValidUri(String ext) throws IOException {
146
		StringBuilder sb = new StringBuilder();
147
		sb.append(Long.toHexString(System.currentTimeMillis() / this.rate));
148
		String p = sb.toString();
149
		if (!p.equals(lastPathWithCreate.get())) {
150
			File cp = new File(this.path, p);
151
			if (!cp.exists()) {
152
				if (!cp.mkdirs()) {
153
					throw new IOException("create path[" + cp.getAbsolutePath() + "] error");
154
				}
155
			} else if (cp.isFile()) {
156
				throw new IOException("create path[" + cp.getAbsolutePath() + "] error,exists same name file");
157
			}
158
			lastPathWithCreate.set(p);
159
		}
160
		sb.append("/").append(Long.toBinaryString(System.nanoTime()));
161
		if (ext != null)
162
			sb.append(".").append(ext);
163
		return sb.toString();
164
	}
165

166
	public static class UploadItem {
167
		private String name;
168
		private String uri;
169
		private long size;
170

171
		public String getName() {
172
			return name;
173
		}
174

175
		public void setName(String name) {
176
			this.name = name;
177
		}
178

179
		public String getUri() {
180
			return uri;
181
		}
182

183
		public void setUri(String uri) {
184
			this.uri = uri;
185
		}
186

187
		public long getSize() {
188
			return size;
189
		}
190

191
		public void setSize(long size) {
192
			this.size = size;
193
		}
194

195
	}
196
}

+ 12 - 2
src/main/java/com/ekexiu/console/system/dao/UserDao.java

@ -11,19 +11,24 @@ import org.jfw.apt.orm.annotation.dao.method.Or;
11 11
import org.jfw.apt.orm.annotation.dao.method.SetSentence;
12 12
import org.jfw.apt.orm.annotation.dao.method.Where;
13 13
import org.jfw.apt.orm.annotation.dao.method.operator.Insert;
14
import org.jfw.apt.orm.annotation.dao.method.operator.PageSelect;
14 15
import org.jfw.apt.orm.annotation.dao.method.operator.SelectOne;
15 16
import org.jfw.apt.orm.annotation.dao.method.operator.Update;
16 17
import org.jfw.apt.orm.annotation.dao.method.operator.UpdateWith;
17 18
import org.jfw.apt.orm.annotation.dao.param.Alias;
19
import org.jfw.apt.orm.annotation.dao.param.GtEq;
20
import org.jfw.apt.orm.annotation.dao.param.Like;
21
import org.jfw.apt.orm.annotation.dao.param.LtEq;
18 22
import org.jfw.apt.orm.annotation.dao.param.Set;
23
import org.jfw.util.PageQueryResult;
19 24

20 25
import com.ekexiu.console.system.po.User;
21 26

22 27
@DAO
23 28
public interface UserDao {
24
	
29

25 30
	@Insert
26
	int insert(Connection con,User user)throws SQLException;
31
	int insert(Connection con, User user) throws SQLException;
27 32

28 33
	@Nullable
29 34
	@SelectOne
@ -56,4 +61,9 @@ public interface UserDao {
56 61
	@UpdateWith
57 62
	int dsiable(Connection con, String id) throws SQLException;
58 63

64
	@PageSelect
65
	public PageQueryResult<User> pageQuery(Connection con, @Nullable @Like String name, @Nullable Boolean actived, @Nullable String mobile,
66
			@Nullable String email, @Nullable @GtEq @Alias("createTime") String bt, @Nullable @Alias("createTime") @LtEq String et, int pageNo, int pageSize)
67
			throws SQLException;
68

59 69
}

+ 11 - 38
src/main/java/com/ekexiu/console/system/service/DictService.java

@ -166,15 +166,7 @@ public class DictService {
166 166
		}
167 167
		// TODO : check dictItem.system == true can't modify
168 168
		LinkedList<DictItem> ret = new LinkedList<DictItem>();
169
		if (di.isTree()) {
170
			if (transfer(items, ret) == null) {
171
				throw new JfwBaseException(50003, "not exists parentCode or loop parentCode");
172
			}
173
		} else {
174
			for (ListIterator<EditDictItem> it = items.listIterator(); it.hasNext();) {
175
				ret.add(transfer(it.next()));
176
			}
177
		}
169
		this.transfer(items,ret);
178 170
		final AtomicBoolean ab = new AtomicBoolean(false);
179 171
		final String val = JsonService.toJson(items);
180 172
		JdbcUtil.executeAutoCommit(con, new JdbcTask() {
@ -216,37 +208,18 @@ public class DictService {
216 208
	}
217 209

218 210
	public LinkedList<DictItem> transfer(List<EditDictItem> src, LinkedList<DictItem> dest) {
219
		Map<String, EditDictItem> map = new HashMap<String, EditDictItem>();
220
		for (EditDictItem eitem : src) {
221
			map.put(eitem.getCode(), eitem);
222
		}
223
		LinkedList<String> removeCodes = new LinkedList<String>();
224
		for (;;) {
225
			for (String code : removeCodes) {
226
				map.remove(code);
227
			}
228
			removeCodes.clear();
229
			for (Map.Entry<String, EditDictItem> entry : map.entrySet()) {
230
				String code = entry.getKey();
231
				EditDictItem eitem = entry.getValue();
232
				String pc = eitem.getParentCode();
233
				if (pc == null || pc.length() == 0) {
234
					removeCodes.add(code);
235
					dest.add(transfer(eitem));
236
				} else {
237
					DictItem item = query(dest, pc);
238
					if (item != null) {
239
						removeCodes.add(code);
240
						item.getChildren().add(transfer(eitem));
241
					}
242
				}
243
			}
244
			if (removeCodes.size() == map.size())
245
				return dest;
246
			if (removeCodes.isEmpty()) {
247
				return null;
211
		for(ListIterator<EditDictItem> it = src.listIterator();it.hasNext();){
212
			EditDictItem edi = it.next();
213
			DictItem id = transfer(edi);
214
			dest.add(id);
215
			LinkedList<EditDictItem> child = edi.getChildren();
216
			if(null!= child && child.size()>0){
217
				LinkedList<DictItem> ldi =new LinkedList<DictItem>();
218
				id .setChildren(ldi);
219
				transfer(child,ldi);
248 220
			}
249 221
		}
222
		return dest;
250 223
	}
251 224

252 225
	public DictItem transfer(EditDictItem eitem) {

+ 68 - 5
src/main/java/com/ekexiu/console/system/service/UserService.java

@ -1,19 +1,29 @@
1 1
package com.ekexiu.console.system.service;
2 2

3
import java.io.File;
4
import java.io.IOException;
3 5
import java.sql.Connection;
4 6
import java.sql.SQLException;
5 7
import java.util.ArrayList;
6 8

7 9
import org.jfw.apt.annotation.Autowrie;
10
import org.jfw.apt.annotation.DefaultValue;
11
import org.jfw.apt.annotation.Nullable;
8 12
import org.jfw.apt.web.annotation.Path;
9 13
import org.jfw.apt.web.annotation.method.InvalidSession;
10 14
import org.jfw.apt.web.annotation.method.SetSession;
11 15
import org.jfw.apt.web.annotation.operate.Get;
12 16
import org.jfw.apt.web.annotation.operate.Post;
17
import org.jfw.apt.web.annotation.param.FieldParam;
13 18
import org.jfw.apt.web.annotation.param.JdbcConn;
19
import org.jfw.apt.web.annotation.param.RequestParam;
20
import org.jfw.util.PageQueryResult;
14 21
import org.jfw.util.StringUtil;
15 22
import org.jfw.util.auth.AuthUtil;
23
import org.jfw.util.exception.JfwBaseException;
24
import org.jfw.util.web.fileupload.UploadItemIterator;
16 25

26
import com.ekexiu.console.service.Upload;
17 27
import com.ekexiu.console.system.dao.RoleDao;
18 28
import com.ekexiu.console.system.dao.UserDao;
19 29
import com.ekexiu.console.system.po.Role;
@ -21,13 +31,24 @@ import com.ekexiu.console.system.po.User;
21 31
import com.ekexiu.console.system.vo.ConsoleAuthUser;
22 32

23 33
@Path("/sys/user")
24
public class UserService {
34
public class UserService extends Upload {
35
	public static final String DEFAULT_AUTH_STR=AuthUtil.serialAuth(new int[]{0});
36
	public static final String DEFAULT_PW_STR=StringUtil.md5("12345678");
37
	private File headPath;
25 38

26 39
	@Autowrie
27 40
	private UserDao userDao;
28 41
	@Autowrie
29 42
	RoleDao roleDao;
30 43

44
	public File getHeadPath() {
45
		return headPath;
46
	}
47

48
	public void setHeadPath(File headPath) {
49
		this.headPath = headPath;
50
	}
51

31 52
	public RoleDao getRoleDao() {
32 53
		return roleDao;
33 54
	}
@ -56,7 +77,7 @@ public class UserService {
56 77
			cau.setName(user.getName());
57 78
			ArrayList<int[]> list = new ArrayList<int[]>(1);
58 79
			list.add(AuthUtil.deSerialAuth(user.getAuthinfo()));
59
			for(Role role:this.roleDao.queryByUser(con,user.getId())){
80
			for (Role role : this.roleDao.queryByUser(con, user.getId())) {
60 81
				list.add(AuthUtil.deSerialAuth(role.getAuthinfo()));
61 82
			}
62 83
			cau.setAuths(AuthUtil.merge(list.toArray(new int[0][0])));
@ -64,12 +85,54 @@ public class UserService {
64 85
		}
65 86
		return null;
66 87
	}
88

67 89
	@Get
68 90
	@Path("/logout")
69 91
	@InvalidSession
70
	public void logout(){		
92
	public void logout() {
93
	}
94

95
	public static void main(String[] args) throws Exception {
96
		System.out.println(AuthUtil.serialAuth(new int[] { 0 }));
97
	}
98

99
	@Get
100
	@Path("/pq")
101
	public PageQueryResult<User> pageQuery(@JdbcConn Connection con, @Nullable String name, @Nullable Boolean actived, @Nullable String mobile,
102
			@Nullable String email, @Nullable String bt, @Nullable String et, @DefaultValue("1") int pageNo, @DefaultValue("10") int pageSize)
103
			throws SQLException {
104
		if (bt != null)
105
			bt = bt + "000000";
106
		if (et != null)
107
			et = et + "235959";
108
		PageQueryResult<User> ret = this.userDao.pageQuery(con, name, actived, mobile, email, bt, et, pageNo, pageSize);
109
		for (User u : ret.getData()) {
110
			u.setLoginPassword(null);
111
			u.setAuthinfo(null);
112
		}
113
		return ret;
114
	}
115

116
	@Post
117
	@Path
118
	public void insert(@JdbcConn(true) Connection con,
119
			@RequestParam(fields = { @FieldParam(value = "name", valueClass = String.class, required = true),
120
					@FieldParam(value = "email", valueClass = String.class, required = true),
121
					@FieldParam(value = "mobile", valueClass = String.class, required = false),
122
					@FieldParam(value = "descp", valueClass = String.class, required = false),
123
					@FieldParam(value="head",valueClass=String.class,required=false)}) User user)
124
			throws SQLException {
125
		String id = StringUtil.buildUUID();
126
		user.setId(id);
127
		user.setActived(true);
128
		user.setAuthinfo(DEFAULT_AUTH_STR);
129
		user.setLoginPassword(DEFAULT_PW_STR);
130
		this.userDao.insert(con, user);
71 131
	}
72
	public static void main(String[] args)throws Exception{
73
		System.out.println(AuthUtil.serialAuth(new int[]{0}));
132

133
	@Post
134
	@Path("/head")
135
	public Upload.UploadItem head(@org.jfw.apt.web.annotation.param.Upload UploadItemIterator it) throws JfwBaseException, IOException {
136
		return this.upload(it);
74 137
	}
75 138
}

+ 9 - 8
src/main/java/com/ekexiu/console/system/vo/EditDictItem.java

@ -1,15 +1,16 @@
1 1
package com.ekexiu.console.system.vo;
2 2

3 3
import java.util.HashMap;
4
import java.util.LinkedList;
4 5

5 6
public class EditDictItem {
6 7
	private String code;
7
	private String parentCode;
8 8
	private String caption;
9 9
	private String shortCode;
10 10
	private boolean enabled;
11 11
	private HashMap<String,String> data = new HashMap<String,String>();
12 12
    private boolean system;
13
    private LinkedList<EditDictItem> children;
13 14
    private String descp ;
14 15
    
15 16
	public String getCode() {
@ -18,12 +19,7 @@ public class EditDictItem {
18 19
	public void setCode(String code) {
19 20
		this.code = code;
20 21
	}
21
	public String getParentCode() {
22
		return parentCode;
23
	}
24
	public void setParentCode(String parentCode) {
25
		this.parentCode = parentCode;
26
	}
22

27 23
	public String getCaption() {
28 24
		return caption;
29 25
	}
@ -60,5 +56,10 @@ public class EditDictItem {
60 56
	public void setDescp(String descp) {
61 57
		this.descp = descp;
62 58
	}
63
	
59
	public LinkedList<EditDictItem> getChildren() {
60
		return children;
61
	}
62
	public void setChildren(LinkedList<EditDictItem> children) {
63
		this.children = children;
64
	}
64 65
}