MenuView.java
4.78 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
package fi.codecrew.moya.web.cdiview.menu;
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.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.SitePageBeanLocal;
import fi.codecrew.moya.model.MenuNavigation;
import fi.codecrew.moya.model.PageContent;
import fi.codecrew.moya.web.cdiview.GenericCDIView;
import fi.codecrew.moya.web.helper.LayoutView;
@Named
@RequestScoped
public class MenuView extends GenericCDIView {
private static final long serialVersionUID = -5720164797157054213L;
private String pagename;
@Inject
private transient LayoutView layoutview;
@Inject
private transient FacesContext context;
@EJB
private transient MenuBeanLocal menubean;
private LinkedList<List<JsfMenuitem>> menus;
private HashSet<MenuNavigation> navis;
private Map<String, List<PageContent>> contents = new HashMap<String, List<PageContent>>();
@EJB
private transient SitePageBeanLocal pagebean;
@SuppressWarnings("unused")
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 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 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 && !super.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 && !super.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;
}
}