View Javadoc
1   /**
2    * Copyright By Grandsoft Company Limited.  
3    * 2012-3-2 下午08:32:20
4    */
5   package gboat2.web.action;
6   
7   import gboat2.base.bridge.GboatAppContext;
8   import gboat2.base.core.annotation.Domain;
9   import gboat2.base.core.annotation.Module;
10  import gboat2.base.core.annotation.Operations;
11  import gboat2.base.core.dao.Page;
12  import gboat2.base.core.dao.QuerySupport;
13  import gboat2.base.core.logging.IBusinessLogService;
14  import gboat2.base.core.logging.Level;
15  import gboat2.base.core.web.BaseActionSupport;
16  import gboat2.base.core.web.JsonResult;
17  import gboat2.web.business.ICacheAssistBusiness;
18  import gboat2.web.business.IOperationBusiness;
19  import gboat2.web.business.IResourceBusiness;
20  import gboat2.web.business.IRoleBusiness;
21  import gboat2.web.business.ISystemConfigBusiness;
22  import gboat2.web.model.Operation;
23  import gboat2.web.model.Resource;
24  import gboat2.web.model.Role;
25  import gboat2.web.model.SystemConfig;
26  
27  import java.util.ArrayList;
28  import java.util.HashMap;
29  import java.util.LinkedHashMap;
30  import java.util.List;
31  import java.util.Map;
32  
33  import org.apache.commons.lang3.StringUtils;
34  import org.apache.struts2.convention.annotation.ResultPath;
35  import org.springframework.beans.factory.annotation.Autowired;
36  
37  /**
38   * 模块管理
39   * @author tanxw
40   * @since jdk1.6
41   * @date 2012-3-9
42   *  
43   */
44  @ResultPath("/content/resource")
45  @Domain(value = Resource.class)
46  @Module(name = "模块管理")
47  @Operations(value = {
48          @gboat2.base.core.annotation.Operation(name = "新增模块", code = "add"),
49          @gboat2.base.core.annotation.Operation(name = "编辑模块", code = "edit")
50  })
51  public class ResourceAction extends BaseActionSupport {
52  	
53  	private static final long serialVersionUID = -1088485441177798733L;
54  
55  	@Autowired
56  	private IResourceBusiness resBusi;
57  	
58  	@Autowired
59  	private IOperationBusiness operaBusi;
60  	
61  	@Autowired
62  	private IBusinessLogService loggingService;
63  	
64  	@Autowired
65  	private ICacheAssistBusiness cacheAssistBusi;
66  	
67  	@Autowired
68  	private IRoleBusiness roleBusi;
69  	
70  	@Autowired
71  	private ISystemConfigBusiness systemCfgBusi;
72  	
73  	private String[] operation;
74  	
75  	private String resFilter;
76  	
77  	private String bundle;
78  	
79  	private String actionClass;
80  	
81  	private String systemId;
82  	
83  	private String operationId;
84  	
85  	private String targetParentId;
86  	
87  	private String targetIndex;
88  	
89  	private String moveId;
90  	
91  	private String copyedId;
92  	
93  	private String resCode;
94  	
95  	public String getResFilter() {
96  		return resFilter;
97  	}
98  	
99  	public void setResFilter(String resFilter) {
100 		this.resFilter = resFilter;
101 	}
102 	
103 	/**
104 	 * 当模块变化时,删除所有权限的缓存和对应模块导航树缓存
105 	 * @author wangsr
106 	 */
107 	private void removeAllCacheOfPriority() {
108 		// 取得所有的系统
109 		List<SystemConfig> allSys = systemCfgBusi.getAllSysCfg();
110 		List<Role> allRole = this.roleBusi.findAllRole();
111 		if (null != allRole && allRole.size() > 0) {
112 			for (Role r : allRole) {
113 				if (allSys != null && allSys.size() > 0) {
114 					for (SystemConfig cfg : allSys) {
115 						this.cacheAssistBusi.removeCacheOfPriority(r.getRoleId(), cfg.getSystemId());
116 						this.cacheAssistBusi.removeCacheOfMenuTree(r.getRoleId(), cfg.getSystemId());
117 					}
118 				}
119 			}
120 		}
121 	}
122 	
123 	public void initSave() {
124 		Resource res = (Resource) getModel();
125 		if (StringUtils.isEmpty(request.getParameter("_M.singleton"))) { //如果前端取消勾选时,该字段不会提交,则视为非单例
126 			res.setSingleton("0");
127 		}
128 		if (StringUtils.isEmpty(res.getResId())) {
129 			// 设置排序值
130 			double order = resBusi.getMaxOrderWithParent(res.getParentId());
131 			res.setDispOrder(order + 1);
132 		}
133 		
134 		String type = StringUtils.isEmpty(res.getResId()) ? "新增" : "编辑";
135 		loggingService.log(Level.INFO, type, type + "模块:" + res.getResName());
136 	}
137 	
138 	/**
139 	 * 检查指定的opreaCode是否在,给定的列表中,如果存在,则删除列表中该条记录,并返回true,否则返回false
140 	 * @param opreaCode operation code
141 	 * @param oldOpreas 当前resource的已经配置在数据库中的operations
142 	 * @return true/false
143 	 */
144 	private boolean removeIfExists(String opreaCode, List<Operation> oldOpreas) {
145 		for (Operation oprea : oldOpreas) {
146 			if (oprea.getOperationCode().equals(opreaCode)) {
147 				oldOpreas.remove(oprea);
148 				return true;
149 			}
150 		}
151 		
152 		return false;
153 	}
154 	
155 	private Operation getAnnotatedOperaByCode(String opreaCode, List<Operation> annotatedOperas) {
156 		for (Operation oprea : annotatedOperas) {
157 			if (oprea.getOperationCode().equals(opreaCode)) {
158 				return oprea;
159 			}
160 		}
161 		return null;
162 	}
163 	
164 	public void postSave() {
165 		Resource res = (Resource) getModel();
166 		
167 		// 清空缓存
168 		this.removeAllCacheOfPriority();
169 		
170 		List<Operation> oldOpreas = operaBusi.getOperationsByResourceId(res.getResId());
171 		List<String> newOpreaCodes = new ArrayList<String>(); //存放需要新插入的操作
172 		if (null != operation) {
173 			for (String opreaCode : operation) {//计算需要删除的记录及需要插入的记录
174 				if (!removeIfExists(opreaCode, oldOpreas)) {//opreaCode数据库中不存在,放入待插入列表中
175 					newOpreaCodes.add(opreaCode);
176 				}
177 			}
178 		}
179 		
180 		//删除未勾选且已存在数据库中的数据
181 		operaBusi.deleteOperations(oldOpreas);
182 		
183 		List<Operation> annotatedOperas = operaBusi.getOperationsByActionClass(res.getBundle(), res.getActionClass());
184 		if (null != annotatedOperas) {
185 			for (String opreaCode : newOpreaCodes) {//插入新增操作
186 				Operation oprea = getAnnotatedOperaByCode(opreaCode, annotatedOperas);
187 				if (null != oprea) {
188 					oprea.setResId(res.getResId());
189 					operaBusi.save(oprea);
190 				}
191 			}
192 		}
193 		GboatAppContext.output(JsonResult.createSuccess(res));
194 	}
195 	
196 	/**
197 	 * 移动某个节点到新的父节点下
198 	 * @return null
199 	 */
200 	@gboat2.base.core.annotation.Operation(name = "移动模块", code = "moveResource")
201 	public String moveResource() {
202 		// 清除权限和导航树缓存
203 		this.removeAllCacheOfPriority();
204 		int targetIndex = Integer.parseInt(this.targetIndex.trim());
205 		boolean success = resBusi.moveResource(moveId, targetParentId, targetIndex);
206 		if (success) {
207 			loggingService.log(Level.INFO, "编辑", "移动模块:"
208 			        + ((Resource) resBusi.get(Resource.class, moveId)).getResName());
209 		}
210 		GboatAppContext.output(new JsonResult(success));
211 		return null;
212 	}
213 	
214 	/**
215 	 * 删除节点,其子节点被挂在被删节点的父节点下
216 	 *@return null
217 	 */
218 	@gboat2.base.core.annotation.Operation(name = "删除模块", code = "deleteResource")
219 	public String deleteResource() {
220 		Resource r = (Resource) this.getModel();
221 		// 清空缓存
222 		this.removeAllCacheOfPriority();
223 		boolean success = resBusi.deleteResource(r);
224 		if (success) {
225 			loggingService.log(Level.INFO, "删除", "删除模块:" + ((Resource) get_M()).getResName());
226 		}
227 		GboatAppContext.output(new JsonResult(success));
228 		return null;
229 	}
230 	
231 	@gboat2.base.core.annotation.Operation(name = "复制模块", code = "copyResource")
232 	public String copyResource() {
233 		// 清除权限和导航树缓存
234 		this.removeAllCacheOfPriority();
235 		boolean success = resBusi.copyResource(copyedId, targetParentId);
236 		if (success) {
237 			loggingService.log(Level.INFO, "编辑", "复制模块:"
238 			        + ((Resource) resBusi.get(Resource.class, copyedId)).getResName());
239 		}
240 		GboatAppContext.output(JsonResult.SUCCESS);
241 		return null;
242 	}
243 	
244 	/**
245 	 * 取得所有节点组成的树
246 	 * @return null
247 	 */
248 	public String tree() {
249 		Resource tree = resBusi.getResourcesTree(systemId);
250 		GboatAppContext.output(tree);
251 		return null;
252 	}
253 	
254 	/**
255 	 * 取得指定action中,所有被注解的operation
256 	 * @return null
257 	 */
258 	public String getOperasByActionClass() {
259 		List<Operation> opres = operaBusi.getOperationsByActionClass(bundle, actionClass);
260 		GboatAppContext.output(JsonResult.createSuccess(opres));
261 		return null;
262 	}
263 	
264 	/**
265 	 * 获取指定节点的所有操作
266 	 * @return null
267 	 */
268 	public String getOperasByResourceId() {
269 		List<Operation> opres = operaBusi.getOperationsByResourceId(getSID());
270 	   GboatAppContext.output(JsonResult.createSuccess(opres));
271 		return null;
272 	}
273 	
274 	/**
275 	 * 删除指定节点中指定的操作
276 	 * @return null
277 	 */
278 	public String deleteOperation() {
279 		Operation op = (Operation) operaBusi.get(Operation.class, operationId);
280 		// 清空缓存
281 		this.removeAllCacheOfPriority();
282 		if (null != op && op.getResId().equals(getSID())) {
283 			operaBusi.deleteOperation(op);
284 			loggingService.log(Level.INFO, "删除", "删除操作:" + op.getOperationName());
285 			 GboatAppContext.output(JsonResult.SUCCESS);
286 		} else {
287 		    GboatAppContext.output(JsonResult.createFailure("删除失败,未找到该操作项!"));
288 		}
289 		return null;
290 	}
291 	
292 	/**
293 	 * 根据过滤条件获取所有被注解的action 
294 	 * @return null
295 	 */
296 	public String getAnnotatedResources() {
297 		String page = request.getParameter(QuerySupport.PARAM_PAGE);
298 		String pagesize = request.getParameter(QuerySupport.PARAM_PAGESIZE);
299 		Page<Resource> pageList = (Page<Resource>) resBusi.getAnnotatedResources(resFilter, page, pagesize);
300 		//		ResultUtil.outputResult(ResultUtil.wrap(resList, 100l), response);
301 		
302 		Map<String, Object> result = new LinkedHashMap<String, Object>();
303 		result.put("data", pageList.getResult());
304 		result.put("results", pageList.getPageBean().getCount());
305 		result.put("success", true);
306 		GboatAppContext.output(result);
307 		return null;
308 	}
309 	
310 	/**
311 	 * 验证模块编码是否已存在
312 	 */
313 	public void checkResCode() {
314 		boolean exist = Boolean.TRUE;
315 		this.resCode = ((Resource) this.getModel()).getResCode();
316 		Resource resource = this.resBusi.getResourceByCode(this.resCode);
317 		if (resource == null) {
318 			exist = Boolean.FALSE;
319 		}
320 		String reason = "该模块编码已经存在!!";
321 		if (!exist) {
322 			reason = "该模块编码可以使用!!";
323 		}
324 		Map<String, Object> map = new HashMap<String, Object>();
325 		map.put("valid", !exist);
326 		map.put("reason", reason);
327 		GboatAppContext.output(map);
328 	}
329 	
330 	public String[] getOperation() {
331 		return operation;
332 	}
333 	
334 	public void setOperation(String[] operation) {
335 		this.operation = operation;
336 	}
337 	
338 	public String getActionClass() {
339 		return actionClass;
340 	}
341 	
342 	public void setActionClass(String actionClass) {
343 		this.actionClass = actionClass;
344 	}
345 	
346 	public String getBundle() {
347 		return bundle;
348 	}
349 	
350 	public void setBundle(String bundle) {
351 		this.bundle = bundle;
352 	}
353 	
354 	public String getOperationId() {
355 		return operationId;
356 	}
357 	
358 	public void setOperationId(String operationId) {
359 		this.operationId = operationId;
360 	}
361 	
362 	public String getSystemId() {
363 		return systemId;
364 	}
365 	
366 	public void setSystemId(String systemId) {
367 		this.systemId = systemId;
368 	}
369 	
370 	public String getTargetParentId() {
371 		return targetParentId;
372 	}
373 	
374 	public void setTargetParentId(String targetParentId) {
375 		this.targetParentId = targetParentId;
376 	}
377 	
378 	public String getMoveId() {
379 		return moveId;
380 	}
381 	
382 	public void setMoveId(String moveId) {
383 		this.moveId = moveId;
384 	}
385 	
386 	public String getCopyedId() {
387 		return copyedId;
388 	}
389 	
390 	public void setCopyedId(String copyedId) {
391 		this.copyedId = copyedId;
392 	}
393 
394 	
395     public String getTargetIndex() {
396     	return targetIndex;
397     }
398 
399 	
400     public void setTargetIndex(String targetIndex) {
401     	this.targetIndex = targetIndex;
402     }
403 }