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.bridge.exception.DefaultBusinessNestedException;
9   import gboat2.base.bridge.model.UserSession;
10  import gboat2.base.core.GBoatClassLoader;
11  import gboat2.base.core.annotation.Domain;
12  import gboat2.base.core.annotation.ListDomain;
13  import gboat2.base.core.annotation.Module;
14  import gboat2.base.core.annotation.Operation;
15  import gboat2.base.core.annotation.Operations;
16  import gboat2.base.core.annotation.Preference;
17  import gboat2.base.core.dao.QuerySupport;
18  import gboat2.base.core.logging.IBusinessLogService;
19  import gboat2.base.core.logging.Level;
20  import gboat2.base.core.web.BaseActionSupport;
21  import gboat2.base.core.web.JsonResult;
22  import gboat2.web.Activator;
23  import gboat2.web.Constants;
24  import gboat2.web.business.IAuthorityBusiness;
25  import gboat2.web.business.IGroupBusiness;
26  import gboat2.web.business.IResourceBusiness;
27  import gboat2.web.business.IRoleBusiness;
28  import gboat2.web.business.IShortcutBusiness;
29  import gboat2.web.model.Group;
30  import gboat2.web.model.Resource;
31  import gboat2.web.model.Role;
32  import gboat2.web.model.Shortcut;
33  import gboat2.web.model.ShortcutResourceVO;
34  
35  import java.io.IOException;
36  import java.util.ArrayList;
37  import java.util.Enumeration;
38  import java.util.List;
39  import java.util.Map;
40  
41  import org.apache.commons.lang3.StringUtils;
42  import org.apache.struts2.convention.annotation.ResultPath;
43  import org.osgi.framework.Bundle;
44  import org.slf4j.Logger;
45  import org.slf4j.LoggerFactory;
46  import org.springframework.beans.factory.annotation.Autowired;
47  
48  /**
49   * 
50   * 桌面快捷图标
51   * @author tanxw
52   * @since jdk1.6
53   * @date 2012-3-9
54   *  
55   */
56  @Preference("/系统配置/快捷方式配置")
57  @Module(name = "快捷方式管理")
58  @Operations(value = { @Operation(name = "新增快捷方式", code = "add"), @Operation(name = "编辑", code = "edit"),
59          @Operation(name = "删除", code = "delete") })
60  @ResultPath("/content/shortcut")
61  @Domain(value = Shortcut.class)
62  @ListDomain(value = ShortcutResourceVO.class)
63  public class ShortcutAction extends BaseActionSupport {
64  	
65  	/**
66  	 * 
67  	 */
68  	private static final long serialVersionUID = 1L;
69  	
70  	private final static String SUPER_PAHT = "/static/gboat2.web/data/super-shortcuts.json";
71  	
72  	private final static Logger logger = LoggerFactory.getLogger(ShortcutAction.class.getName());
73  	
74  	@Autowired
75  	private IShortcutBusiness shortcutBusi;
76  	
77  	@Autowired
78  	private IBusinessLogService loggingService;
79  	
80  	@Autowired
81  	private IAuthorityBusiness authorityBusi;
82  	
83  	@Autowired
84  	private IGroupBusiness gropBusi;
85  
86  	@Autowired
87  	private IRoleBusiness roleBusi;
88  	
89  	@Autowired
90  	private IResourceBusiness resourceBusi;
91  	
92  	private String resId;
93  	
94  	private String systemId;
95  	
96  	protected void initList(Map<String, Object> params) {
97  		params.put(QuerySupport.PARAM_PAGESIZE, -1);
98  		String currLoginId = GboatAppContext.getUserSession().getLoginId();
99  		if (Constants.SUPER.equals(currLoginId)) {
100 			params.put("_userId_null", "");
101 		} else {
102 			params.put("_userId", GboatAppContext.getUserSession().getUserId());
103 		}
104 	}
105 	
106 	protected void initSave() {
107 		UserSession session = GboatAppContext.getUserSession();
108 		Shortcut shortcut = (Shortcut) getModel();
109 		String currLoginId = session.getLoginId();
110 		if (!Constants.SUPER.equals(currLoginId)) {
111 			Group group = gropBusi.findGroupByGroupCode(session.getGroupCode());
112 			Role role = roleBusi.findRoleByRoleCode(session.getRoleCode());
113 			shortcut.setGroupId(group.getGroupId());
114 			shortcut.setRoleId(role.getRoleId());
115 			shortcut.setUserId(session.getUserId());
116 		}
117 		
118 		String type = StringUtils.isEmpty(shortcut.getShortcutId()) ? "新增" : "编辑";
119 		loggingService.log(Level.INFO, type, type + "快捷方式:" + shortcut.getName());
120 	}
121 	
122 	//读取所有快捷图标文件夹中的快捷图标
123 	private List<Shortcut> getAllShortcutIcons() throws IOException, DefaultBusinessNestedException {
124 		String theme = Constants.DEFAULT_THEME; //扩展以后支持主题
125 		String pathPrefix = "static/gboat2.web/themes/" + theme + "/images/shortcuts";
126 		List<Shortcut> list = new ArrayList<Shortcut>();
127 		
128 		Enumeration<String> paths = Activator.LOCAL_BUNDLE.getEntryPaths(pathPrefix);
129 		String path = "";
130 		Shortcut cut;
131 		while (paths.hasMoreElements()) {
132 			path = (String) paths.nextElement();
133 			if (path.matches(".*\\.(((?i)gif)|((?i)png))$")) {
134 				cut = new Shortcut();
135 				cut.setIconUrl(path);
136 				list.add(cut);
137 			}
138 		}
139 		//		//考虑更优的解决方案
140 		//		String jarFile = BundleUtil.getBundle(Constants.GBOAT2_WEB).getLocation().replace("file:/", "");
141 		//		JarFile file = new JarFile(jarFile);
142 		//		Enumeration<JarEntry> entries = file.entries();
143 		//		ZipEntry entry = null;
144 		//		String name;
145 		//		Shortcut cut;
146 		//		boolean isParsed = false;
147 		//		while (entries.hasMoreElements()) {
148 		//			entry = entries.nextElement();
149 		//			if (!entry.isDirectory()) {
150 		//				name = entry.getName();
151 		//				if (name.startsWith(pathPrefix) //只支持gif、png两种可以透明的图片格式
152 		//				        && (name.endsWith("gif") || name.endsWith("GIF") || name.endsWith("PNG") || name
153 		//				                .endsWith("png"))) {
154 		//					cut = new Shortcut();
155 		//					cut.setIconUrl(name);
156 		//					list.add(cut);
157 		//				}
158 		//			} else {
159 		//				if (isParsed) {
160 		//					break;
161 		//				}
162 		//				if (entry.getName().equals(pathPrefix)) {
163 		//					isParsed = true;
164 		//				}
165 		//			}
166 		//		}
167 		return list;
168 	}
169 	
170 	public String getAllIcons() throws IOException, DefaultBusinessNestedException {
171 		GboatAppContext.output(JsonResult.createSuccess(this.getAllShortcutIcons()));
172 		return null;
173 	}
174 	
175 	public String addModule() {
176 		return null;
177 	}
178 	
179 	protected void postDelete() {
180 		Shortcut shortcut = (Shortcut) getModel();
181 		loggingService.log(Level.INFO, "删除", "删除快捷方式:" + shortcut.getName());
182 	}
183 	
184 	/**
185 	 * 菜单树
186 	 * @author wangsr
187 	 */
188 	public void menuTree() {
189 		Resource root = null;
190 		UserSession userSession = GboatAppContext.getUserSession();
191 		String loginId = userSession.getLoginId();
192 		if (loginId != null && "super".equals(loginId)) {
193 			// 读取文件,构造resource
194 			Bundle bundle = GBoatClassLoader.getInstance().getBundle(ShortcutAction.class.getPackage().getName());
195 			//Bundle bundle = BundleUtil.getBundle(context, Constants.GBOAT2_WEB);
196 			try {
197 				root = shortcutBusi.getSuperTree(bundle.getResource(SUPER_PAHT));
198 			} catch (Exception e) {
199 				logger.error(e.getMessage(), e.getCause());
200 			}
201 		} else {
202 			Group group = gropBusi.findGroupByGroupCode(userSession.getGroupCode());
203 			Role role = roleBusi.findRoleByRoleCode(userSession.getRoleCode());
204 			String authForId = group.getGroupId() + "." + role.getRoleId();
205 			if (StringUtils.isEmpty(this.resId)) {// 如果resId为空,则得到这个角色的所有模块
206 				//root = this.authorityBusi.findResourceTreeOfRoleOfGroupOfSystem(authForId, userSession.getSystemId(), true);
207 				if(authorityBusi.isAuthorityCustomize(authForId + "."+userSession.getUserId(),userSession.getSystemId())){//用户定制
208 					authForId = authForId + "."+userSession.getUserId();
209 				}
210 				root = this.shortcutBusi.findResourceTreeForRoleOfGroup(authForId,userSession.getSystemId());
211 				
212 			} else {// 否则,取得这个角色这个模块的子模块
213 				//root = this.authorityBusi.findResourceTreeOfRoleOfGroupForRes(authForId, resId, true);
214 				if(authorityBusi.isAuthorityCustomize(authForId + "."+userSession.getUserId(),userSession.getSystemId())){//用户定制
215 					authForId = authForId + "."+userSession.getUserId();
216 					root = this.shortcutBusi.findResourceTreeForRoleOfGroupForRes(authForId, authForId, false);
217 				}else{
218 					root = this.shortcutBusi.findResourceTreeForRoleOfGroupForRes(authForId, resId,true);
219 				}
220 				
221 			}
222 		}
223 		GboatAppContext.output(root);
224 	}
225 	
226 	/**
227 	 * 快捷方式菜单树
228 	 * @author wangsr
229 	 */
230 	public void shortcutMenuTree() {
231 		Resource root = null;
232 		UserSession userSession = GboatAppContext.getUserSession();
233 		String loginId = userSession.getLoginId();
234 		if (loginId != null && Constants.SUPER.equals(loginId)) {
235 			root = this.resourceBusi.getResourcesTree(systemId);
236 		} else {
237 			// 获取当前角色所有的模块
238 			Group group = gropBusi.findGroupByGroupCode(userSession.getGroupCode());
239 			Role role = roleBusi.findRoleByRoleCode(userSession.getRoleCode());
240 			String authForId = group.getGroupId() + "." + role.getRoleId();
241 			//root = this.authorityBusi.findResourceTreeOfRoleOfGroupOfSystem(authForId, userSession.getSystemId(), true);
242 			root = this.shortcutBusi.findResourceTreeForRoleOfGroup(authForId, userSession.getSystemId());
243 		}
244 		GboatAppContext.output(root, response);
245 	}
246 	
247 	//	private Bundle getCurrentBundle(String bundleName) {
248 	//		Bundle curr = null;
249 	//		Bundle[] bs = context.getBundles();
250 	//		for (Bundle b : bs) {
251 	//			if (b.getSymbolicName().equals(bundleName)) {
252 	//				context = b.getBundleContext();
253 	//				break;
254 	//			}
255 	//		}
256 	//		curr = context.getBundle();
257 	//		return curr;
258 	//	}
259 	
260 	public String getResId() {
261 		return resId;
262 	}
263 	
264 	public void setResId(String resId) {
265 		this.resId = resId;
266 	}
267 	
268 	public String getSystemId() {
269 		return systemId;
270 	}
271 	
272 	public void setSystemId(String systemId) {
273 		this.systemId = systemId;
274 	}
275 	
276 }