View Javadoc
1   /**
2    * 根据模块定义(@see Resource)
3    */
4   package gboat2.web.service.impl;
5   
6   import gboat2.base.core.model.Opera;
7   import gboat2.base.core.service.IOperationProvider;
8   import gboat2.base.core.service.ModuleService;
9   import gboat2.base.core.web.BaseActionSupport;
10  import gboat2.web.exception.ResourceException;
11  import gboat2.web.model.Operation;
12  import gboat2.web.model.Resource;
13  import gboat2.web.service.IResourceService;
14  
15  import java.util.LinkedList;
16  import java.util.List;
17  
18  import org.apache.commons.lang3.StringUtils;
19  import org.osgi.framework.FrameworkUtil;
20  import org.springframework.beans.factory.annotation.Autowired;
21  
22  import com.opensymphony.xwork2.ActionContext;
23  import com.opensymphony.xwork2.ActionProxy;
24  
25  /**
26   * @author <a mail="[email protected]">lysming</a>
27   * @since 3.0
28   * @date 2014年5月23日
29   */
30  public class ResourceOperationProvider implements IOperationProvider {
31  	protected final static String RESOURCE_PARAMS = "resource_id_";
32  	@Autowired
33  	private IResourceService resourceService;
34  
35  	/* (non-Javadoc)
36  	 * @see gboat2.base.core.service.IOperationProvider#getOperations(gboat2.base.core.web.BaseActionSupport)
37  	 */
38  	@Override
39  	public List<Opera> getOperations(BaseActionSupport action) {
40  		String moduleParam = action.request.getParameter(RESOURCE_PARAMS);
41  		ActionProxy proxy = ActionContext.getContext().getActionInvocation().getProxy();
42  		if(StringUtils.isNotBlank(moduleParam)){
43  			return getOperas(action, moduleParam);
44  		}else{
45  			String actionUrl = proxy.getActionName() + "!" + proxy.getMethod()+".do";
46  			List<Resource> reses = resourceService.getResourcesByUrl(actionUrl);
47  			if(reses.size()==0&&proxy.getMethod().equals("list")){
48  				actionUrl = proxy.getActionName()+".do";
49  				reses = resourceService.getResourcesByUrl(actionUrl);
50  			}else if(reses.size()==0&&proxy.getMethod().equals("execute")){
51  			    actionUrl = proxy.getActionName()+".do";
52                  reses = resourceService.getResourcesByUrl(actionUrl);
53  			}
54  			
55  			if(reses.size()==1){
56  				return getOperas(action, reses.get(0).getResId());
57  			}else if(reses.size()>1){
58  				//如果同一个Action配置了多个模块,检查其操作是否相同,如果不同系统则无法识别
59  				if(checkSame(reses)){
60  					return getOperas(action, reses.get(0).getResId());
61  				}else{
62  					throw new ResourceException("系统拥有多个同一Action不同操作的模块("+getResourceNames(reses)+"),平台无法识别");
63  				}
64  			}else{
65  				String symbolicName = FrameworkUtil.getBundle(action.getClass()).getSymbolicName();
66  				return ModuleService.getOperaForMetadata(symbolicName, action.getClass().getName());
67  			}
68  		}
69  	}
70  
71  	private String getResourceNames(List<Resource> reses) {
72  		StringBuilder string = new StringBuilder();
73  		for(Resource res : reses){
74  			if(string.length()>0){
75  				string.append(",");
76  			}
77  			string.append(res.getResName());
78  		}
79  		return string.toString();
80  	}
81  
82  	private List<Opera> getOperas(BaseActionSupport action, String resId) {
83  		List<Opera> operas = new LinkedList<Opera>();
84  		Resource resource = resourceService.getResourceById(resId);
85  		if(resource == null){
86  			throw new ResourceException("主键为"+resId+"的模块不存在,请检查参数是否正确");
87  		}
88  		List<Operation> operations = resourceService.getOperationsOfResource(resId);
89  		for(Operation op : operations){
90  			operas.add(new Opera(op.getOperationName(),op.getOperationCode(),action.getClass().getName(),"","",""));
91  		}
92  		return operas;
93  	}
94  	
95  	private boolean checkSame(List<Resource> reses){
96  		List<List<Operation>> operas = new LinkedList<List<Operation>>();
97  		for(Resource res : reses){
98  			operas.add(resourceService.getOperationsOfResource(res.getResId()));
99  		}
100 		int total = 0;
101 		List<String> operaCodes = new LinkedList<String>();
102 		for(List<Operation> ops : operas){
103 			if(total==0&&ops.size()!=0){
104 				total = ops.size();
105 				for(Operation op : ops){
106 					operaCodes.add(op.getOperationCode());
107 				}
108 			}
109 			if(total!=ops.size()){
110 				return false;
111 			}else{
112 				for(Operation op : ops){
113 					if(!operaCodes.contains(op.getOperationCode())){
114 						return false;
115 					}
116 				}
117 			}
118 		}
119 		return true;
120 	}
121 
122 }