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.core.annotation.Domain;
8   import gboat2.base.core.annotation.ListDomain;
9   import gboat2.base.core.annotation.Preference;
10  import gboat2.base.core.dao.Page;
11  import gboat2.base.core.web.BaseActionSupport;
12  import gboat2.web.business.IRoleBusiness;
13  import gboat2.web.business.ISystemConfigBusiness;
14  import gboat2.web.model.Role;
15  import gboat2.web.model.SystemConfig;
16  import gboat2.web.model.Widget;
17  import gboat2.web.model.WidgetAuthortyConfig;
18  import gboat2.web.model.WidgetAuthortyConfigVO;
19  import gboat2.web.service.IWidgetService;
20  import gboat2.web.util.WidgetServiceTracker;
21  
22  import java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.Collections;
25  import java.util.HashMap;
26  import java.util.Iterator;
27  import java.util.List;
28  import java.util.Map;
29  
30  import org.apache.commons.lang3.StringUtils;
31  import org.apache.struts2.convention.annotation.ResultPath;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  import org.springframework.beans.factory.annotation.Autowired;
35  
36  /**
37   * 
38   * widget权限配置页面
39   * @author zhangxj-a
40   * @since jdk1.6
41   * @date 2013-3-15
42   *  
43   */
44  @Preference("/系统配置/widget权限设置")
45  @ResultPath("/content/widget")
46  @ListDomain(WidgetAuthortyConfigVO.class)
47  @Domain(WidgetAuthortyConfig.class)
48  public class WidgetAuthorityConfigAction extends BaseActionSupport {
49  	
50  	private static final long serialVersionUID = 1L;
51  	
52  	protected final static Logger LOGGER = LoggerFactory.getLogger(WidgetAuthorityConfigAction.class.getName());
53  	
54  	@Autowired
55  	private ISystemConfigBusiness syscfgBusi;
56  	
57  	@Autowired
58  	private IRoleBusiness roleBusiness;
59  	
60  	/**获取g2中所有的system */
61  	private List<SystemConfig> systemList ;
62  	
63  	/**获取g2中所有的role */
64  	private List<Role> roleList;
65  	
66  	private List<Widget> widgetList ;
67  	
68  	private List<String> authorityWidgetList;
69  	
70  	protected void initEdit() {
71  		LOGGER.debug("find all systems in G2.");
72  		systemList = syscfgBusi.getAllSystemConfig();
73  		LOGGER.debug("find all role in G2.");
74  		roleList = roleBusiness.findAllRole();
75  		//获取所有widget
76  		createWidgetList();
77  		//获取当前系统的当前角色配置的widget 权限
78  		WidgetAuthortyConfig wac = (WidgetAuthortyConfig)get_M();
79  		
80  		String widgetIds = wac.getWidgetIds();
81  		if(StringUtils.isNotEmpty(widgetIds)){
82  			authorityWidgetList = getList(widgetIds);
83  		}
84  			
85  	}
86  	
87  	/** 
88  	  * 处理widget字段
89  	  */
90  	@SuppressWarnings("unchecked")
91  	protected void postList(Page pageinfo) {
92  		//取出所有的widget
93  		createWidgetList();
94  		//构建widget键值对,根据id取title
95  		Map<String, String> map = new HashMap<String, String>();
96  		for (Iterator iterator = widgetList.iterator(); iterator.hasNext();) {
97  			Widget widget = (Widget) iterator.next();
98  			map.put(widget.getId(), widget.getTitle());
99  		}
100 		
101 		//获取列表list,把widget 的name拼装成字符串放入widgetNames字段中
102 		List<?> result = pageinfo.getResult();
103 		for (Iterator iterator = result.iterator(); iterator.hasNext();) {
104 			//拼装widget names
105 			String titles = "";
106 			List list;
107 			WidgetAuthortyConfigVO object = (WidgetAuthortyConfigVO) iterator.next();
108 			String widgetIds = object.getWidgetIds();
109 			if(StringUtils.isEmpty(object.getWidgetIds()))continue;
110 			
111 			//把widgetIds转换为list
112 			list = getList(widgetIds);
113 			
114 			//组装titles
115 			for (int i = 0; i < list.size(); i++) {
116 				String  id = (String) list.get(i);
117 				String title = map.get(id);
118 				titles = titles + title;
119 				
120 				if(i != list.size() - 1)
121 					titles = titles + "、";
122 			}
123 			//把拼装的widget放入vo中 供列表展示
124 			object.setWidgetNames(titles);
125 			
126 		}
127 	}
128 	
129 	private List<String> getList(String widgetIds){
130 		widgetIds = widgetIds.replace(", ", ",");
131 		return Arrays.asList(widgetIds.split(","));
132 	}
133 	
134 	/**
135 	 * 读取所有widget服务
136 	 */
137 	private void createWidgetList(){
138 		
139 		IWidgetService[] widgetServices = WidgetServiceTracker.getWidgetServices();
140 		
141 		if(widgetServices == null)widgetList = Collections.emptyList();
142 		
143 		widgetList = new ArrayList<Widget>();
144 		
145 		for (int i = 0; i < widgetServices.length; i++) {
146 			Widget widget = new Widget();
147 			widgetList.add(widget);
148 			widget.setId(widgetServices[i].getId());
149 			widget.setTitle(widgetServices[i].getTitle());
150 		}
151 		
152 	}
153 	
154 
155 	public List<SystemConfig> getSystemList() {
156 		return systemList;
157 	}
158 
159 	public void setSystemList(List<SystemConfig> systemList) {
160 		this.systemList = systemList;
161 	}
162 
163 	public List<Role> getRoleList() {
164 		return roleList;
165 	}
166 
167 	public void setRoleList(List<Role> roleList) {
168 		this.roleList = roleList;
169 	}
170 
171 	public List<Widget> getWidgetList() {
172 		return widgetList;
173 	}
174 
175 	public void setWidgetList(List<Widget> widgetList) {
176 		this.widgetList = widgetList;
177 	}
178 
179 	public List<String> getAuthorityWidgetList() {
180 		return authorityWidgetList;
181 	}
182 
183 	public void setAuthorityWidgetList(List<String> authorityWidgetList) {
184 		this.authorityWidgetList = authorityWidgetList;
185 	}
186 }