View Javadoc
1   /**
2    * Copyright By Grandsoft Company Limited.  
3    * 2013-1-5 下午02:20:49
4    */
5   package gboat2.web.action;
6   
7   import gboat2.base.core.annotation.Module;
8   import gboat2.base.core.annotation.Operation;
9   import gboat2.base.core.model.PreferenceDefinition;
10  import gboat2.base.core.service.IModuleService;
11  import gboat2.base.core.web.BaseActionSupport;
12  import gboat2.base.core.web.JsonResultSupport;
13  import gboat2.web.model.Resource;
14  
15  import java.util.List;
16  
17  import net.sf.json.JSONObject;
18  
19  import org.apache.struts2.convention.annotation.ResultPath;
20  import org.springframework.beans.factory.annotation.Autowired;
21  
22  /**
23   * 首选项配置中心
24   * @author tanxw
25   * @since jdk1.6
26   * @date 2013-1-5
27   *  
28   */
29  
30  //@Modules( { 
31  //	@Module(name = "首选项1", code = "CODE1"), 
32  //	@Module(name = "首选项2", code = "CODE2", entryMethod = "prefer2"),
33  //    @Module(name = "首选项3", code = "CODE3", entryMethod = "prefer3", params = { "a", "1" }),
34  //    @Module(name = "首选项4", code = "CODE4", entryMethod = "prefer3", params = { "a", "2" }) })
35  @Module(name = "首选项")
36  @ResultPath("/content/preference")
37  public class PreferenceAction extends BaseActionSupport {
38  	
39  	private static final long serialVersionUID = 1568958760674720749L;
40  	
41  	@Autowired
42  	IModuleService moduleSer;
43  	
44  	private static int resId = 1000;
45  	
46  	/**
47  	 * 获取元数据
48  	 * @return
49  	 */
50  	public String preference() {
51  		return null;
52  	}
53  	
54  	/**
55  	 * 生成resId
56  	 * @return
57  	 */
58  	private String generateResId() {
59  		if (resId < 1000 || resId > 100000) {
60  			resId = 1000;
61  		}
62  		return String.valueOf(++resId);
63  	}
64  	
65  	@Operation(name = "配置", code = "preferences", desc = "此配置主要是为了限制访问")
66  	public String preferences() {
67  		//生成根节点
68  		Resource root = new Resource();
69  		root.setResName("root");
70  		root.setResId("0");
71  		root.setLeaf(false);
72  		
73  		//取得所有首选项配置
74  		List<PreferenceDefinition> list = moduleSer.getPreferences();
75  		//组织成树型结构
76  		if (null != list) {
77  			for (PreferenceDefinition pre : list) {
78  				Resource parent = getOrCreateParent(root, pre);
79  				
80  				Resource current = getChildByResName(parent, pre.getShortName());
81  				if (null != current && ("1" == current.getType())) {//如果父节点下已经挂载了一个同名虚节点,则用当前节点覆盖该节点
82  					current.setResName(pre.getShortName());
83  					current.setActionClass(pre.getActionClass());
84  					current.setBundle(pre.getBundle());
85  					List<Resource> children = current.getChildren();
86  					if (null != children && children.size() > 0) {
87  						current.setLeaf(false);
88  					} else {
89  						current.setLeaf(true);
90  					}
91  					current.setResUrl(pre.getUri());
92  					current.setType("0");//设置为实节点
93  				} else {
94  					current = new Resource();
95  					current.setResId(generateResId());
96  					current.setResName(pre.getShortName());
97  					current.setActionClass(pre.getActionClass());
98  					current.setBundle(pre.getBundle());
99  					current.setLeaf(true);
100 					current.setParentId(parent.getResId());
101 					current.setResUrl(pre.getUri());
102 					current.setType("0");//设置为实节点
103 					
104 					parent.setLeaf(false); //将父节点设置为非叶子节点
105 					parent.addChild(current);
106 				}
107 				
108 			}
109 		}
110 		
111 		JsonResultSupport.output(JSONObject.fromObject(root));
112 		return null;
113 	}
114 	
115 	/**
116 	 * 查找指定节点下指定名称的孩子节点
117 	 * @param parent
118 	 * @param resName
119 	 * @return 第一个与resName匹配的孩子节点
120 	 */
121 	private Resource getChildByResName(Resource parent, String resName) {
122 		List<Resource> children = parent.getChildren();
123 		if (null != children && children.size() > 0) {
124 			for (Resource child : children) {
125 				if (child.getResName().equals(resName)) {
126 					return child;
127 				}
128 			}
129 		}
130 		return null;
131 	}
132 	
133 	/**
134 	 * 取得一个首选项的父节点,如果父节点不存在,则生成一个虚节点作为其父节点
135 	 * @param root
136 	 * @param pre
137 	 * @return 已存在的或者新生成的父节点
138 	 */
139 	private Resource getOrCreateParent(Resource root, PreferenceDefinition pre) {
140 		String[] ancestors = pre.getAncestors();
141 		if (null == ancestors) {//没有祖先,证明是第一层节点,其父节点为跟节点
142 			return root;
143 		}
144 		
145 		Resource parent = root;
146 		for (int deep = 0; deep < ancestors.length; deep++) {
147 			String resName = ancestors[deep];
148 			Resource child = getChildByResName(parent, resName);
149 			if (null == child) {
150 				child = new Resource();
151 				child.setResId(generateResId());
152 				child.setResName(resName);
153 				child.setLeaf(true);
154 				child.setParentId(parent.getResId());
155 				child.setType("1");//设置为虚节点
156 				
157 				parent.setLeaf(false); //将父节点设置为非叶子节点
158 				parent.addChild(child);
159 			}
160 			parent = child; //往下走一层
161 		}
162 		
163 		return parent;
164 	}
165 }