View Javadoc
1   /**
2    * Copyright By Grandsoft Company Limited.  
3    * 2012-3-2 下午08:47:53
4    */
5   package gboat2.web.business.impl;
6   
7   import gboat2.base.bridge.exception.DefaultGboatNestedException;
8   import gboat2.base.bridge.model.UserSession;
9   import gboat2.base.core.cache.ICacheService;
10  import gboat2.base.core.dao.QuerySupport;
11  import gboat2.base.core.service.BaseService;
12  import gboat2.web.business.IAuthorityBusiness;
13  import gboat2.web.business.IGroupBusiness;
14  import gboat2.web.business.IRoleBusiness;
15  import gboat2.web.business.IShortcutBusiness;
16  import gboat2.web.model.AuthorityResourceVO;
17  import gboat2.web.model.Group;
18  import gboat2.web.model.Resource;
19  import gboat2.web.model.Role;
20  import gboat2.web.model.Shortcut;
21  import gboat2.web.model.ShortcutResourceVO;
22  
23  import java.io.BufferedReader;
24  import java.io.File;
25  import java.io.InputStreamReader;
26  import java.net.URL;
27  import java.nio.charset.Charset;
28  import java.util.ArrayList;
29  import java.util.HashMap;
30  import java.util.LinkedList;
31  import java.util.List;
32  import java.util.Map;
33  
34  import net.sf.json.JSONArray;
35  import net.sf.json.JSONObject;
36  
37  import org.apache.commons.lang3.RandomStringUtils;
38  import org.springframework.beans.factory.annotation.Autowired;
39  import org.springframework.stereotype.Service;
40  import org.springframework.transaction.annotation.Transactional;
41  
42  /**
43   * 
44   * @author tanxw
45   * @since jdk1.6
46   * @date 2012-3-2
47   *  
48   */
49  @Transactional
50  @Service
51  public class ShortcutBusinessImpl extends BaseService implements IShortcutBusiness {
52  
53  	@Autowired
54  	private IAuthorityBusiness authorityBusi;
55  
56  	@Autowired
57  	private IRoleBusiness roleBusi;
58  
59  	@Autowired
60  	private IGroupBusiness gropBusi;
61  
62  	@Autowired
63  	private ICacheService cacheSer;
64  
65  	@Override
66  	public Shortcut getShortcutByResId(String resId) {
67  		Map<String, Object> params = new HashMap<String, Object>();
68  		params.put(QuerySupport.PARAM_TABLENAME, Shortcut.class);
69  		params.put("_resId", resId);
70  		params.put("_userId_null", "is null");
71  		List<Shortcut> list = (List<Shortcut>) super.query(params);
72  		if (list.size() == 0) {
73  			return null;
74  		} else {
75  			return list.get(0);
76  		}
77  	}
78  
79  	@Override
80  	public List<Shortcut> getShortcutByUserId(String userId) {
81  		Map<String, Object> params = new HashMap<String, Object>();
82  		params.put(QuerySupport.PARAM_TABLENAME, Shortcut.class);
83  		params.put("_userId", userId);
84  		List<Shortcut> list = (List<Shortcut>) super.query(params);
85  		return list;
86  	}
87  
88  	/*@Override
89  	public List<AuthorityShortcutResourceVO> getShortcutByAuthForId(String authForId) {
90  		Map<String, Object> params = new HashMap<String, Object>();
91  		params.put(QuerySupport.PARAM_TABLENAME, AuthorityShortcutResourceVO.class);
92  		params.put("_authForId", authForId);
93  		params.put("_userId_null", "is null");
94  		List<AuthorityShortcutResourceVO> list = (List<AuthorityShortcutResourceVO>) super.query(params);
95  		return list;
96  	}*/
97  
98  	public List<Shortcut> getAllShortcutImgs(File imgDir) throws DefaultGboatNestedException {
99  		//		String[] images = null;
100 		//		if (!imgDir.isDirectory()) {
101 		//			throw new DefaultBusinessNestedException("读取快捷图标路径不是目录!");
102 		//		}
103 		//		
104 		//		images = imgDir.list(new FilenameFilter() {
105 		//			
106 		//			@Override
107 		//			public boolean accept(File dir, String name) {
108 		//				//				if (name.endsWith("png")) {
109 		//				//					return true;
110 		//				//				}
111 		//				return true;
112 		//			}
113 		//		});
114 		return null;
115 	}
116 
117 	@Override
118 	public List<ShortcutResourceVO> getShortcutResourceByUserId(String userId) {
119 		Map<String, Object> params = new HashMap<String, Object>();
120 		params.put(QuerySupport.PARAM_TABLENAME, ShortcutResourceVO.class);
121 		params.put("_userId", userId);
122 		List<ShortcutResourceVO> list = (List<ShortcutResourceVO>) super.query(params);
123 		return list;
124 	}
125 
126 	@Override
127 	public ShortcutResourceVO getShortcutResourceByResId(String resId) {
128 		Map<String, Object> params = new HashMap<String, Object>();
129 		params.put(QuerySupport.PARAM_TABLENAME, ShortcutResourceVO.class);
130 		params.put("_resId", resId);
131 		params.put("_userId_null", "is null");
132 		List<ShortcutResourceVO> list = (List<ShortcutResourceVO>) super.query(params);
133 		if (list != null && list.size() != 0) {
134 			return list.get(0);
135 		} else {
136 			return null;
137 		}
138 	}
139 
140 	@Override
141 	public List<ShortcutResourceVO> getShortcutResourceByUser(UserSession session, List<AuthorityResourceVO> authoritys) {
142 		Map<String, Object> params = new HashMap<String, Object>();
143 		StringBuffer resIds = new StringBuffer();
144 		List<String> settedIds = new LinkedList<String>();
145 		if (authoritys != null && authoritys.size() > 0) {
146 			for (AuthorityResourceVO auth : authoritys) {
147 				if (!settedIds.contains(auth.getResId())) {
148 					if (resIds.length() != 0) {
149 						resIds.append(",");
150 					}
151 					resIds.append("'").append(auth.getResId()).append("'");
152 					settedIds.add(auth.getResId());
153 				}
154 			}
155 		}
156 		params.put(QuerySupport.PARAM_TABLENAME, ShortcutResourceVO.class);
157 		params.put("_userId", session.getUserId());
158 
159 		Group group = gropBusi.findGroupByGroupCode(session.getGroupCode());
160 		params.put("_groupId", group.getGroupId());
161 		Role role = roleBusi.findRoleByRoleCode(session.getRoleCode());
162 		params.put("_roleId", role.getRoleId());
163 		params.put("_resId_in", resIds.toString());
164 		List<ShortcutResourceVO> list = (List<ShortcutResourceVO>) super.query(params);
165 		return list;
166 	}
167 
168 	@Override
169 	public AuthorityResourceVO findResourceTreeForRoleOfGroup(String authForId, String systemId) {
170 		AuthorityResourceVO ret = this.authorityBusi.findResourceTreeForRoleOfGroupOfSystem(authForId, systemId, true);
171 		return ret;
172 	}
173 
174 	@Override
175 	public AuthorityResourceVO findResourceTreeForRoleOfGroupForRes(String authForId, String resId, boolean useCache) {
176 		AuthorityResourceVO ret = this.authorityBusi.findResourceTreeForRoleOfGroupForRes(authForId, resId, useCache);
177 		return ret;
178 	}
179 
180 	@Override
181 	public Resource getSuperTree(URL url) throws Exception {
182 		Resource resource = new Resource();
183 		BufferedReader buf = new BufferedReader(new InputStreamReader(url.openStream(), Charset.forName("UTF-8")));
184 		StringBuffer sbf = new StringBuffer();
185 		String tmp = null;
186 		try {
187 			while ((tmp = buf.readLine()) != null) {
188 				sbf.append(tmp);
189 			}
190 		} catch (Exception e) {
191 			throw e;
192 		} finally {
193 			if (buf != null) {
194 				buf.close();
195 			}
196 		}
197 		JSONArray original = JSONArray.fromObject(sbf.toString());
198 		List<Resource> list = new ArrayList<Resource>();
199 		if (original != null && original.size() > 0) {
200 			for (int i = 0; i < original.size(); i++) {
201 				JSONObject obj = JSONObject.fromObject(original.get(i));
202 				Resource res = new Resource();
203 				if (obj.containsKey("name")) {
204 					res.setResName(obj.getString("name"));
205 				}
206 				if (obj.containsKey("resUrl")) {
207 					res.setResUrl(obj.getString("resUrl"));
208 				}
209 				if (obj.containsKey("singleton")) {
210 					res.setSingleton(obj.getString("singleton"));
211 				}
212 				if (obj.containsKey("type")) {
213 					res.setType(obj.getString("type"));
214 				}
215 				res.setResId(RandomStringUtils.randomNumeric(20));
216 				list.add(res);
217 			}
218 			resource.setChildren(list);
219 		}
220 		return resource;
221 	}
222 
223 	//	@SuppressWarnings("unchecked")
224 	//	@Override
225 	//	public List<AuthorityShortcutResourceVO> getASRVO(String authForId) {
226 	//		Map<String, Object> params = new HashMap<String, Object>();
227 	//		params.put(QuerySupport.PARAM_TABLENAME, AuthorityShortcutResourceVO.class);
228 	//		params.put("_authForId", authForId);
229 	//		params.put("_useStatus", "1");
230 	//		List<AuthorityShortcutResourceVO> list = (List<AuthorityShortcutResourceVO>) super.query(params);
231 	//		return list;
232 	//	}
233 
234 }