MenuView.java
6.62 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package fi.codecrew.moya.web.cdiview.menu;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.inject.Inject;
import javax.inject.Named;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fi.codecrew.moya.beans.MenuBeanLocal;
import fi.codecrew.moya.beans.PermissionBeanLocal;
import fi.codecrew.moya.beans.SitePageBeanLocal;
import fi.codecrew.moya.handler.NavigationHandler;
import fi.codecrew.moya.model.MenuNavigation;
import fi.codecrew.moya.model.PageContent;
import fi.codecrew.moya.web.helper.LayoutView;
@Named
@RequestScoped
public class MenuView {
private static final long serialVersionUID = -5720164797157054213L;
private String pagename;
@Inject
protected NavigationHandler navihandler;
@Inject
private transient LayoutView layoutview;
@Inject
private transient FacesContext context;
@EJB
private transient MenuBeanLocal menubean;
private transient LinkedList<List<JsfMenuitem>> menus;
private HashSet<MenuNavigation> navis;
private Map<String, List<PageContent>> contents = new HashMap<String, List<PageContent>>();
@EJB
private transient SitePageBeanLocal pagebean;
private String menuChange;
private static final Logger logger = LoggerFactory.getLogger(MenuView.class);
public List<PageContent> getPagecontent(String pagekey)
{
String key = new StringBuilder(layoutview.getPagepath()).append(":").append(pagekey).toString();
logger.debug("Getting pagecontent for key {}. Matches: {}", key, contents.containsKey(key));
if (!contents.containsKey(key)) {
contents.put(key, pagebean.findContentsForUser(key));
}
return contents.get(key);
}
public boolean isRenderViewChange()
{
return viewchangeTopmenu.size() > 1;
}
public List<JsfMenuitem> getMenu(Integer level)
{
getMenus();
if (level == null || menus.isEmpty() || level < 0 || level >= menus.size())
{
return null;
}
List<JsfMenuitem> ret = menus.get(level);
return ret;
}
public void menuChangeEvent(ValueChangeEvent e) {
// logger.info("Executed menuchange-eventlistener for menuChange {} newval {}", menuChange, e.getNewValue());
menuChange = e.getNewValue().toString();
layoutview.setPageName(menuChange);
navihandler.forward(menuChange);
// super.navihandler.forward(menuChange);
}
public boolean isRenderTopmenuChanger()
{
return getViewChangeTopmenu().size() > 1;
}
private transient List<JsfMenuitem> viewchangeTopmenu;
@EJB
private PermissionBeanLocal permbean;
public List<JsfMenuitem> getViewChangeTopmenu()
{
if (viewchangeTopmenu == null) {
viewchangeTopmenu = new ArrayList<JsfMenuitem>();
for (MenuNavigation topmenu : menubean.getTopmenus()) {
String out = null;
if (topmenu.getItem() != null) {
out = topmenu.getItem().getUrl();
}
if (out == null) {
out = getOutcome(topmenu);
}
if (out != null) {
logger.info("Adding topmenu {}", out);
viewchangeTopmenu.add(new JsfMenuitem(topmenu.getKey(), out));
}
}
// viewchangeTopmenu.add(new JsfMenuitem("topmenu.user", "/index"));
// if (super.hasPermission(UserPermission.VIEW_ALL)) {
// viewchangeTopmenu.add(new JsfMenuitem("topmenu.helpdesk", "/admin/info/index"));
// viewchangeTopmenu.add(new JsfMenuitem("topmenu.admin", "/useradmin/list"));
// }
}
return viewchangeTopmenu;
}
public LinkedList<List<JsfMenuitem>> getMenus()
{
if (menus == null)
{
menus = new LinkedList<List<JsfMenuitem>>();
navis = new HashSet<MenuNavigation>();
MenuNavigation pathIter = menubean.findNavigation(layoutview.getPagepath());
while (pathIter != null) {
if (pathIter.isVisible()) {
navis.add(pathIter);
menus.add(0, addMenu(pathIter.getChildren()));
}
pathIter = pathIter.getParent();
}
menus.add(0, addMenu(menubean.getTopmenus()));
}
return menus;
}
private LinkedList<JsfMenuitem> addMenu(List<MenuNavigation> children) {
LinkedList<JsfMenuitem> ret = new LinkedList<JsfMenuitem>();
// Iterate through children
for (MenuNavigation child : children) {
// if is visible, permission is null or has permission, continue
if (!child.isVisible() || (child.getPermission() != null && !permbean.hasPermission(child.getPermission()))) {
continue;
}
JsfMenuitem jsfItem = new JsfMenuitem(child);
// if menuitem does not have any content, it should get it from the
// first child.
if (child.getItem() == null && child.getSitepage() == null)
{
// if menulist does not have entries this is the lowest level we
// have menus, else we can use the outcome from the latest
// entry in menuarray.
if (!navis.contains(child) || menus.isEmpty() || menus.get(0).isEmpty()) {
String outcome = getOutcome(child);
// if we don't get outcome, we don't have permission to any
// subitems and this item should be skipped...
if (outcome == null) {
continue;
}
jsfItem.setOutcome(outcome);
} else {
jsfItem.setOutcome(menus.get(0).get(0).getOutcome());
}
}
if (navis.contains(child)) {
jsfItem.setSelected();
}
ret.add(jsfItem);
}
return ret;
}
private String getOutcome(MenuNavigation navi) {
String ret = null;
// try to get the outcome from the first child we have permission to.
for (MenuNavigation child : navi.getChildren())
{
if (!child.isVisible() || (child.getPermission() != null && !permbean.hasPermission(child.getPermission()))) {
continue;
}
// try to get item from this child.
if (child.getItem() != null) {
ret = child.getItem().getUrl();
}
// try to get sitepage url.
else if (child.getSitepage() != null) {
// ret = child.getSitepage().getUrl();
ret = new StringBuilder("/pages/index?id=").append(navi.getSitepage().getId()).toString();
}
// if navigation does not have menuitem or sitepage we go deeper in
// to the tree..
else {
ret = getOutcome(child);
}
// If we found outcome, brek from iterator.
if (ret != null) {
break;
}
}
return ret;
}
public void setContext(FacesContext context) {
this.context = context;
}
public LayoutView getLayoutview() {
return layoutview;
}
public void setLayoutview(LayoutView layoutview) {
this.layoutview = layoutview;
}
public String getMenuChange() {
if (menuChange == null || menuChange.isEmpty()) {
menuChange = layoutview.getPagepath();
}
return menuChange;
}
public void setMenuChange(String menuChange) {
this.menuChange = menuChange;
}
}