kongzy
2023-10-11 374fb9d9369271b9b858e11a8612850ad3560936
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package com.nanometer.smartlab.controller;
 
import com.nanometer.smartlab.model.MenuModel;
import com.nanometer.smartlab.util.FacesUtils;
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.awt.*;
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);
                        }
                    }
                }
            }
        }
    }
 
    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;
    }
}