package com.nanometer.smartlab.controller;
|
|
import com.nanometer.smartlab.model.MenuModel;
|
import com.nanometer.smartlab.util.FacesUtils;
|
import org.apache.commons.collections4.ListUtils;
|
import org.apache.commons.lang.StringUtils;
|
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.annotation.Scope;
|
import org.springframework.stereotype.Controller;
|
import org.springframework.web.context.support.WebApplicationContextUtils;
|
|
import javax.annotation.PostConstruct;
|
import javax.annotation.Resource;
|
import javax.faces.context.FacesContext;
|
import javax.servlet.ServletContext;
|
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.Method;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* Created by johnny on 15/12/24.
|
*/
|
@Controller
|
@Scope("session")
|
public class MenuController extends BaseController {
|
|
@Resource
|
private List<MenuModel> menuList;
|
private Map<String, MenuModel> menuMap;
|
private String currentPage;
|
private String fromPage;
|
|
@PostConstruct
|
public void init() {
|
this.initMenuMap();
|
}
|
|
public void initPage() {
|
if (this.menuList != null && this.menuList.size() > 0) {
|
for (MenuModel menuModel : this.menuList) {
|
if (StringUtils.isNotBlank(menuModel.getPage())) {
|
this.goToPage(menuModel.getPage());
|
return;
|
} else {
|
if (menuModel.getSubMenu() != null && menuModel.getSubMenu().size() > 0) {
|
for (MenuModel subMenuModel : menuModel.getSubMenu()) {
|
if (StringUtils.isNotBlank(subMenuModel.getPage())) {
|
this.goToPage(menuModel.getSubMenu().get(0).getPage());
|
return;
|
}
|
}
|
}
|
}
|
}
|
}
|
}
|
|
private void initMenuMap() {
|
this.menuMap = new HashMap<String, MenuModel>();
|
if (this.menuList != null && this.menuList.size() > 0) {
|
for (MenuModel menuModel : this.menuList) {
|
if (StringUtils.isNotBlank(menuModel.getPage())) {
|
this.menuMap.put(menuModel.getId(), menuModel);
|
}
|
|
if (menuModel.getSubMenu() != null && menuModel.getSubMenu().size() > 0) {
|
for (MenuModel subMenuModel : menuModel.getSubMenu()) {
|
if (StringUtils.isNotBlank(subMenuModel.getPage())) {
|
this.menuMap.put(subMenuModel.getId(), subMenuModel);
|
}
|
}
|
}
|
}
|
/**
|
* 姑苏实验室 价格管理需要放在审批管理后面
|
*/
|
if(this.getActiveEnv().equalsIgnoreCase("gslab")){
|
MenuModel menuModel=this.menuList.get(0);
|
List<MenuModel> subMenuModels=menuModel.getSubMenu();
|
if(!subMenuModels.get(1).getId().equalsIgnoreCase("approval_mng")){
|
swap(subMenuModels,1,2);
|
}
|
}
|
}
|
|
}
|
|
public void swap(List<MenuModel> a, int i, int j) {
|
MenuModel t = a.get(i);
|
a.set(i, a.get(j));
|
a.set(j, t);
|
}
|
|
private void executeMethod(String clazzName, String methodName, Object... params) {
|
if (StringUtils.isBlank(clazzName) || StringUtils.isBlank(methodName)) {
|
return;
|
}
|
ServletContext context = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
|
ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context);
|
Object object = applicationContext.getBean(clazzName);
|
if (object != null) {
|
Class clazz = object.getClass();
|
try {
|
Method method = null;
|
if (params != null && params.length > 0) {
|
Class[] paramsClazzs = new Class[params.length];
|
for (int i = 0; i < params.length; i ++) {
|
paramsClazzs[i] = params[i].getClass();
|
}
|
|
method = clazz.getDeclaredMethod(methodName, paramsClazzs);
|
method.invoke(object, params);
|
} else {
|
method = clazz.getDeclaredMethod(methodName);
|
method.invoke(object);
|
}
|
} catch (NoSuchMethodException e) {
|
e.printStackTrace();
|
} catch (InvocationTargetException e) {
|
e.printStackTrace();
|
} catch (IllegalAccessException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
|
public void goToPage(MenuModel menuModel) {
|
if (menuModel != null) {
|
this.fromPage = null;
|
this.executeMethod(menuModel.getInitClazz(), menuModel.getInitMethod());
|
this.setCurrentPage(menuModel.getPage());
|
}
|
}
|
|
public void goToPage(String id) {
|
if (StringUtils.isBlank(id)) {
|
return;
|
}
|
this.fromPage = null;
|
MenuModel menuModel = this.menuMap.get(id);
|
this.goToPage(menuModel);
|
}
|
|
public void goToPage(String id, String fromPage) {
|
this.goToPage(id);
|
this.fromPage = fromPage;
|
}
|
|
public void goToPage(String id, String fromPage, String initClazz, String initMethod, Object... params) {
|
MenuModel menuModel = this.menuMap.get(id);
|
if (menuModel != null) {
|
this.executeMethod(initClazz, initMethod, params);
|
this.setCurrentPage(menuModel.getPage());
|
this.fromPage = fromPage;
|
}
|
}
|
|
public void backToPage() {
|
if (StringUtils.isNotBlank(this.fromPage)) {
|
MenuModel menuModel = this.menuMap.get(this.fromPage);
|
if (menuModel != null) {
|
this.setCurrentPage(menuModel.getPage());
|
this.fromPage = null;
|
}
|
}
|
}
|
|
public boolean isBackBtnDisp() {
|
if (StringUtils.isNotBlank(this.fromPage)) {
|
return true;
|
}
|
return false;
|
}
|
|
public void derectURL(String url){
|
FacesUtils.js("openwin('" + url + "')");
|
}
|
|
public List<MenuModel> getMenuList() {
|
return menuList;
|
}
|
|
public String getCurrentPage() {
|
if (StringUtils.isBlank(currentPage)) {
|
this.initPage();
|
}
|
return currentPage;
|
}
|
|
public void setCurrentPage(String currentPage) {
|
this.currentPage = currentPage;
|
}
|
|
public boolean isDispRootMenu(MenuModel menuModel) {
|
if (menuModel.getSubMenu() != null && menuModel.getSubMenu().size() > 0) {
|
for (MenuModel model : menuModel.getSubMenu()) {
|
if (this.isPermitted(model.getPrivilegeCode())) {
|
return true;
|
}
|
}
|
}
|
|
return false;
|
}
|
}
|