View Javadoc
1   /**
2    * Copyright By Grandsoft Company Limited.  
3    * 2012-2-15 上午10:52:51
4    */
5   package gboat2.web.action;
6   
7   import gboat2.base.bridge.GboatAppContext;
8   import gboat2.base.bridge.model.UserSession;
9   import gboat2.base.bridge.util.json.JsonUtil;
10  import gboat2.base.core.web.BaseActionSupport;
11  import gboat2.base.core.web.JsonResult;
12  import gboat2.base.core.web.JsonResultSupport;
13  import gboat2.web.Constants;
14  import gboat2.web.business.IProfileBusiness;
15  import gboat2.web.business.IRoleBusiness;
16  import gboat2.web.business.ISystemConfigBusiness;
17  import gboat2.web.business.IUserBusiness;
18  import gboat2.web.business.IVersionMgrBusiness;
19  import gboat2.web.model.Profile;
20  import gboat2.web.model.Role;
21  import gboat2.web.model.RoleNavMode;
22  import gboat2.web.model.SystemAuthorityResourceVO;
23  import gboat2.web.model.SystemConfig;
24  import gboat2.web.model.SystemTheme;
25  import gboat2.web.model.User;
26  import gboat2.web.model.UserRoleGroupOrganVO;
27  import gboat2.web.util.BundleUtil;
28  
29  import java.io.FileInputStream;
30  import java.io.IOException;
31  import java.net.URL;
32  import java.util.HashMap;
33  import java.util.List;
34  import java.util.Map;
35  import java.util.Properties;
36  
37  import net.sf.json.JSONObject;
38  
39  import org.apache.commons.lang3.StringUtils;
40  import org.apache.struts2.convention.annotation.ParentPackage;
41  import org.apache.struts2.convention.annotation.Result;
42  import org.apache.struts2.convention.annotation.Results;
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   * gboat.web的首页面Action
50   * @author lysming
51   * @since jdk1.6
52   * @date 2012-2-15
53   */
54  @Results(value = {
55          @Result(name = "index", location = "index.jsp"),
56          @Result(name = "desktop", location = "desktop.vm"),
57  		@Result(name = "test", location = "test.vm") })
58  @ParentPackage(value = "desktop")
59  public class GIndexAction extends BaseActionSupport {
60  
61  	private static final long serialVersionUID = -5592431830054284953L;
62  
63  	private Logger logger = LoggerFactory.getLogger(GIndexAction.class.getName());
64  
65  	private static Boolean DEV_MODE_FLAG = null;
66  
67  	private Boolean isDesktopMode = true;
68  
69  	private boolean devMode = false;
70  
71  	/**
72  	 * 版本
73  	 */
74  	private String v = "";
75  
76  	@Autowired
77  	private IProfileBusiness profileService;
78  
79  	private String systemCount = "1";
80  
81  	private String roleCount = "1";
82  
83  	private String currentTheme;
84  
85  	private String currentStretch;
86  
87  	private String currentWallpaper;
88  
89  	private String passwordModify;
90  
91  	private String username;
92  
93  	private UserSession userSession;
94  
95  	// temp:ExtJS 版本 addby:wangsr
96  	private String extJSVer = "4.1";
97  
98  	private Map<String, String> jsEntry;
99  
100 	private SystemConfig sysCfg;
101 
102 	@Autowired
103 	private ISystemConfigBusiness sysCfgSer;
104 
105 	@Autowired
106 	private IRoleBusiness roleBusi;
107 
108 	@Autowired
109 	private IVersionMgrBusiness versionBus;
110 
111 	private String userNavMod = "2";
112 
113 	private String ip;
114 
115 	@Autowired
116 	private IUserBusiness userService;
117 
118 	private String extjsFilenameSuffix;
119 
120 	public String execute() {
121 		return "index";
122 	}
123 
124 	private void initJsEntryMap() {
125 		jsEntry = new HashMap<String, String>();
126 		Bundle[] bundles = context.getBundles();
127 		for (Bundle bundle : bundles) {
128 			String seTag = (String) bundle.getHeaders().get("Struts2-Enabled");
129 			if (seTag != null && seTag.equals("true") && !"gboat2.web".equals(bundle.getSymbolicName())) {
130 				String value = bundle.getSymbolicName().toLowerCase();//ExtJS命名规范中,所有包名(第一个字母除外)都需小写
131 				String key = value.substring(0, 1).toUpperCase() + value.substring(1);
132 				jsEntry.put(key, value);
133 			}
134 		}
135 	}
136 
137 	private void saveModeChange(String userId, String mode) {
138 
139 		UserSession session = GboatAppContext.getUserSession();
140 		if (!mode.equals(session.getAttribute(Constants.CURRENT_PAGE_APP_MODE_KEY))) {
141 			Profile profile = profileService.getProfileByUserId(userId);
142 			if (null == profile) {
143 				profile = new Profile();
144 			}
145 			profile.setPageAppMode(mode);
146 			profile.setUserId(userId);
147 			profileService.updateProfile(profile);
148 			session.setAttribute(Constants.CURRENT_PAGE_APP_MODE_KEY, mode);
149 		}
150 	}
151 
152 	public String desktop() {
153 		isDesktopMode = true;
154 		initPageApp(Constants.PAGE_APP_MODE_DESKTOP);
155 		return "desktop";
156 	}
157 
158 	public String normal() {
159 		isDesktopMode = false;
160 		initPageApp(Constants.PAGE_APP_MODE_NORMAL);
161 		return "desktop";
162 	}
163 
164 	/**登陆的公共部分*/
165 	private void initPageApp(String appMode) {
166 		userSession = GboatAppContext.getUserSession();
167 		String userId = userSession.getUserId();
168 		//用户习惯
169 		saveModeChange(userId, appMode); //记住模式		
170 		//是否修改密码
171 		User user = (User) profileService.get(User.class, userId);
172 		if (null != user) {
173 			passwordModify = user.getPasswordModify();
174 		}
175 		//系统配置,用户习惯
176 		initUserInfo();
177 		initJsEntryMap();
178 		initDevMode();
179 		initVersion();
180 	}
181 
182 	//初始化版本信息
183 	private void initVersion() {
184 		v = versionBus.getVersionCode();
185 		if (null == v) {
186 			v = "";
187 		}
188 	}
189 
190 	private void initDevMode() {
191 		//如果已经加载过文件,则不再重复加载
192 		if (null == DEV_MODE_FLAG) {
193 			Properties prop = new Properties();
194 			try {
195 				prop.load(new FileInputStream(GboatAppContext.getWebRootPath() + "WEB-INF/config/debug.properties"));
196 				DEV_MODE_FLAG = "true".equals(prop.getProperty("page.app.devMode").trim());
197 			} catch (Exception e) {
198 				logger.warn("调试配置文件WEB-INF/config/debug.properties读取出错", e);
199 			}
200 		}
201 
202 		devMode = DEV_MODE_FLAG;
203 
204 		//如果是开发模式,且从本机访问,则使用ext-all-dev.js文件,否则使用ext-all.js文件
205 		if (devMode && "localhost".equalsIgnoreCase(request.getServerName())) {
206 			extjsFilenameSuffix = "-dev";
207 		} else {
208 			extjsFilenameSuffix = "";
209 		}
210 	}
211 
212 	/**
213 	 * 初始化系统配置
214 	 * 优先级:用户定制>系统定制>默认
215 	 * @author wangsr
216 	 */
217 	private void initUserInfo() {
218 		userSession = GboatAppContext.getUserSession();
219 		String userId = userSession.getUserId();
220 
221 		currentTheme = Constants.DEFAULT_THEME; //优先级:用户定制>系统定制>默认
222 		currentStretch = "0";
223 		currentWallpaper = Constants.DEFAULT_THEME;
224 
225 		// 用户定制
226 		Profile profile = profileService.getProfileByUserId(userId);
227 		if (profile != null) { //如果用户自定义了配置
228 			//主题设置
229 			if (StringUtils.isNotEmpty(profile.getTheme())) {
230 				currentTheme = profile.getTheme();
231 			}
232 			//壁纸拉伸模式设置
233 			if (StringUtils.isNotEmpty(profile.getStretch())) {
234 				currentStretch = profile.getStretch();
235 			}
236 			//壁纸设置
237 			if (StringUtils.isNotEmpty(profile.getWallPaper())) {
238 				currentWallpaper = profile.getWallPaper();
239 			}
240 		}
241 
242 		// 系统定制
243 		String systemId = userSession.getSystemId(); //如果systemId为空,则走默认的
244 		if (StringUtils.isNotBlank(systemId)) {
245 			sysCfg = this.sysCfgSer.findSysCfgById(systemId);
246 			if (null != sysCfg) {
247 				userNavMod = sysCfg.getNavMode();
248 			}
249 			//读取角色的导航模式
250 			if (userSession.getRoleCode() != null) {
251 				RoleNavMode mode = sysCfgSer.getNavMode(systemId, userSession.getRoleCode());
252 				if (mode != null) {
253 					userNavMod = mode.getNavMode();
254 				}
255 			}
256 		} else {
257 			sysCfg = new SystemConfig();
258 			//desktop.vm需要使用者两个变量
259 			sysCfg.setSystemName(Constants.SYS_CFG_DEFAULT_SYSTEMNAME);
260 			sysCfg.setPageAppMode(Constants.SYS_CFG_DEFAULT_NAVMODE);
261 		}
262 		if (profile == null) {
263 			if (null != sysCfg) {//如果管理员作了系统定制
264 				try {
265 					Map<String, SystemTheme> themeMap = this.sysCfgSer.findThemeCfg();
266 					String themeId = sysCfg.getDefaultTheme();
267 					SystemTheme theme = themeMap.get(themeId);
268 					if (theme != null) {
269 						currentTheme = theme.getTheme();
270 						currentWallpaper = theme.getImg();
271 					}
272 				} catch (IOException e) {
273 					logger.equals("读取主题配置失败!");
274 				}
275 			}
276 		}
277 
278 		//用户当前的拥有的系统数量---------------------------------------------
279 		List<SystemAuthorityResourceVO> sysList = null;
280 		if (StringUtils.isNotEmpty(userSession.getRoleCode())) {
281 			Role role = roleBusi.findRoleByRoleCode(userSession.getRoleCode());
282 			sysList = (List<SystemAuthorityResourceVO>) sysCfgSer.getSystemsOfRole(role.getRoleId());
283 		}
284 		if (null == sysList) {
285 			systemCount = "0";
286 		} else {
287 			systemCount = String.valueOf(sysList.size());
288 		}
289 
290 		//取得用户ip地址----------------------------------------------------
291 		ip = GboatAppContext.getRemoteAddr();
292 
293 		//当前用户角色数量--------------------------------------------------
294 		List<UserRoleGroupOrganVO> userStatus = userService.getMapperVOByUserId(userSession.getUserId());
295 		if (null == userStatus) {
296 			roleCount = "0";
297 		} else {
298 			roleCount = String.valueOf(userStatus.size());
299 		}
300 	}
301 
302 	//请求头部数据
303 	public String headerData() {
304 		JSONObject obj = JsonResultSupport.wrap(true);
305 		UserSession session = GboatAppContext.getUserSession();
306 		String userName = session.getUserNameZh();
307 		String loginId = session.getLoginId();
308 		obj.accumulate("userName", userName);
309 		obj.accumulate("loginId", loginId);
310 		if (!"super".equals(loginId)) {
311 			Role role = roleBusi.findRoleByRoleCode(userSession.getRoleCode());
312 			obj.accumulate("roleName", role.getRoleName());
313 		}
314 		JsonResultSupport.output(obj);
315 		return null;
316 	}
317 
318 	//请求头部元数据
319 	public String headerMetadata() {
320 		UserSession session = GboatAppContext.getUserSession();
321 		String bundleName = Constants.GBOAT2_WEB; //默认的bunlde名称,即平台bundle
322 
323 		String sysId = session.getSystemId();
324 		if (!StringUtils.isEmpty(sysId) && !(Constants.GBOAT2_SYSTEM_ID.equals(sysId))) {
325 			bundleName = sysCfgSer.findSysCfgById(sysId).getLoginBundleName();
326 		}
327 		String metaFilePath = "static/" + bundleName + "/data/header-metadata.json";
328 		URL url = BundleUtil.getBundle(bundleName).getResource(metaFilePath);
329 		Object result = (url == null ? JsonResult.createFailure("获取页头的数据定义失败:文件 [" + metaFilePath + "] 不存在") : JsonUtil.fromURL(url));
330         GboatAppContext.output(result);
331 		return null;
332 	}
333 
334 	/**
335 	 * 生成ssoSessionID,设置session
336 	 * @param userSession userSession
337 	 */
338 	protected void setSession(UserSession userSession) {
339 		if (null != userSession) {
340 			request.getSession().setAttribute(UserSession.USER_SESSION_KEY, userSession);
341 			String sessionId = request.getSession().getId();
342 			double rdv = (double) (Math.random() * 10000000);
343 			String key = sessionId + "_" + rdv;
344 			userSession.setSsoSessionID(key);
345 		}
346 	}
347 	
348 	public Map<String, String> getJsEntry() {
349 		return jsEntry;
350 	}
351 
352 	public String getCurrentTheme() {
353 		return currentTheme;
354 	}
355 
356 	public String getCurrentStretch() {
357 		return currentStretch;
358 	}
359 
360 	public Boolean getIsDesktopMode() {
361 		return isDesktopMode;
362 	}
363 
364 	public String getExtJSVer() {
365 		return extJSVer;
366 	}
367 
368 	public String getPasswordModify() {
369 		return passwordModify;
370 	}
371 
372 	public String getCurrentWallpaper() {
373 		return currentWallpaper;
374 	}
375 
376 	public SystemConfig getSysCfg() {
377 		return sysCfg;
378 	}
379 
380 	public UserSession getUserSession() {
381 		return userSession;
382 	}
383 
384 	public String getSystemCount() {
385 		return systemCount;
386 	}
387 
388 	public String getIp() {
389 		return ip;
390 	}
391 
392 	public String getRoleCount() {
393 		return roleCount;
394 	}
395 
396 	public String getUsername() {
397 		return username;
398 	}
399 
400 	public void setUsername(String username) {
401 		this.username = username;
402 	}
403 
404 	public boolean getDevMode() {
405 		return devMode;
406 	}
407 
408 	public String getUserNavMod() {
409 		return userNavMod;
410 	}
411 
412 	public String getV() {
413 		return v;
414 	}
415 
416 	public void setV(String v) {
417 		this.v = v;
418 	}
419 
420 	public String getExtjsFilenameSuffix() {
421 		return extjsFilenameSuffix;
422 	}
423 }