View Javadoc
1   /**
2    * Copyright By Grandsoft Company Limited.  
3    * 2012-2-16 下午03:34:12
4    */
5   package gboat2.base.core.web;
6   
7   import gboat2.base.core.validate.PrivilegeCheckServiceFactory;
8   
9   import java.util.Iterator;
10  import java.util.Map;
11  
12  import net.sf.json.JSONArray;
13  import net.sf.json.JSONObject;
14  
15  import org.apache.commons.lang3.StringUtils;
16  import org.slf4j.Logger;
17  import org.slf4j.LoggerFactory;
18  
19  
20  /**
21   * 
22   * 拆分元数据
23   * @author zhangxj-a
24   * @since jdk1.6
25   * @date 2013-5-21
26   *  
27   */
28  public abstract class SplitMetadataStrategy {
29  	
30  	//获得logger
31  	Logger logger = LoggerFactory.getLogger(MetadataSupportStrategy.class);
32  	
33  	public abstract  JSONObject decorateMetadata(JSONObject returned, String invokeMethod, BaseActionSupport action);
34  	
35  	
36  	/*******************************以下方法可以抽取公共方法:第一版重构,先放到抽象类中************************************/
37  	
38  	//根据每个bundle注册的系统按钮提供器来完成按组装系统按钮
39  	public  JSONArray getSystemButtonsJSON(BaseActionSupport action, String invokeMethod) {
40  		JSONArray jsonArr = new JSONArray();
41  		SystemButtonProvider[] providers = SystemButtonProviderTracker.getProviders();
42  		if (null != providers) {
43  			for (SystemButtonProvider proivder : providers) {
44  				jsonArr.addAll(proivder.getSystemButtons(action, invokeMethod));
45  			}
46  		}
47  		return jsonArr;
48  	}
49  	
50  
51  	
52  	
53  	
54  	/**
55  	 * 判断操作的权限
56  	 * @param jsonObject -- 操作
57  	 * @param action
58  	 * @return
59  	 */
60  	public  boolean checkOperaAuthorityByMethodCode(JSONObject jsonObject, BaseActionSupport action) {
61  		String operCode = (String) jsonObject.get("code");
62  		String actionName = (String) jsonObject.get("actionName");
63  		actionName = StringUtils.isEmpty(actionName) == true ? action.getClass().getName() : actionName;
64  		if (StringUtils.isNotEmpty(operCode)) {
65  			//调用权限判断规则
66  			Map<Object, Object> params = action.getPriCheckParams();
67  			params.put("ACTION_NAME", actionName);
68  			params.put("METHOD_CODE", operCode);
69  			params.put("REQUEST", action.request);
70  			return PrivilegeCheckServiceFactory.operationCheckServiceByMethodCode(params);
71  		}
72  		return true;
73  	}
74  	
75  	
76  	/**
77  	 * 过滤数据的操作权限
78  	 * @param preJSONArray
79  	 * @param action
80  	 * @return
81  	 */
82  	public  JSONArray filtMetaOperas(JSONArray preJSONArray, BaseActionSupport action) {
83  		Iterator<?> jsonIter = preJSONArray.iterator();
84  		JSONArray returnJSONArray = new JSONArray();
85  		Object itObj;
86  		JSONObject jsonObj;
87  		while (jsonIter.hasNext()) {
88  			itObj = jsonIter.next();
89  			if (!(itObj instanceof JSONObject)) {
90  				logger.warn(itObj + " is not JSONObject, skipped.");
91  				continue;
92  			}
93  
94  			jsonObj = (JSONObject) itObj;
95  			Boolean isHandle = (Boolean) ((Boolean) jsonObj.get("global") == null ? false : jsonObj.get("global"));
96  			//全局的操作不在每条数据上验证
97  			if (isHandle) {
98  				if (checkOperaAuthorityByMethodCode(jsonObj, action)) {
99  					returnJSONArray.add(jsonObj);
100 				}
101 			} else {
102 				returnJSONArray.add(jsonObj);
103 			}
104 		}
105 		return returnJSONArray;
106 	}
107 	
108 }