1
2
3 package org.andromda.presentation.jsf;
4
5 import java.io.Serializable;
6 import java.util.List;
7 import javax.faces.context.ExternalContext;
8 import javax.faces.context.FacesContext;
9 import org.apache.commons.beanutils.MethodUtils;
10
11
12
13
14 public class NavigationItem
15 implements Serializable
16 {
17
18 private static final long serialVersionUID = 7916439134289358617L;
19 private String label = null;
20 private String outcome = null;
21 private String viewId = null;
22 private String icon = null;
23 private List children = null;
24 private Object controllerBean = null;
25 private String controllerAction = null;
26 private String roles = null;
27 private String[] _roles = null;
28
29
30
31
32 public NavigationItem()
33 {
34
35 }
36
37
38
39
40 public String getLabel()
41 {
42 return this.label;
43 }
44
45
46
47
48 public void setLabel(String labelIn)
49 {
50 this.label = labelIn;
51 }
52
53
54
55
56 public String getOutcome()
57 {
58 return this.outcome;
59 }
60
61
62
63
64 public void setOutcome(String outcomeIn)
65 {
66 this.outcome = outcomeIn;
67 }
68
69
70
71
72 public String getViewId()
73 {
74 return this.viewId;
75 }
76
77
78
79
80 public void setViewId(String viewIdIn)
81 {
82 this.viewId = viewIdIn;
83 }
84
85
86
87
88 public List getChildren()
89 {
90 return this.children;
91 }
92
93
94
95
96 public void setChildren(List childrenIn)
97 {
98 this.children = childrenIn;
99 }
100
101
102
103
104 public String getIco()
105 {
106 return this.icon;
107 }
108
109
110
111
112 public void setIco(String iconIn)
113 {
114 this.icon = iconIn;
115 }
116
117
118
119
120 public String getRoles()
121 {
122 return this.roles;
123 }
124
125 private void _updateRoles()
126 {
127 if (this.roles != null && this.roles.length() > 0)
128 {
129 this._roles = this.roles.split(",");
130 }
131 else
132 {
133 this._roles = null;
134 }
135 }
136
137
138
139
140 public void setRoles(String rolesIn)
141 {
142 this.roles = rolesIn;
143 _updateRoles();
144 }
145
146
147
148
149 public String[] getAssociatedRoles()
150 {
151 return this._roles;
152 }
153
154
155
156
157 public boolean isRendered()
158 {
159 return this.isUserInItemRoles();
160 }
161
162
163
164
165 public boolean isUserInItemRoles()
166 {
167 String[] associatedRoles = this.getAssociatedRoles();
168
169 if (associatedRoles == null || associatedRoles.length == 0)
170 {
171
172 return true;
173 }
174
175 ExternalContext ctx =
176 FacesContext.getCurrentInstance().getExternalContext();
177
178 if (ctx.getUserPrincipal() == null)
179 {
180
181 return false;
182 }
183
184 for (String role : associatedRoles)
185 {
186 if (ctx.isUserInRole(role))
187 {
188 return true;
189 }
190 }
191 return false;
192 }
193
194
195
196
197 public String getAction()
198 {
199 try
200 {
201 return (String)MethodUtils.invokeMethod(
202 getControllerBean(),
203 getControllerAction(),
204 null);
205 }
206 catch (Exception ex)
207 {
208 ex.printStackTrace();
209 return null;
210 }
211 }
212
213
214
215
216 public void setControllerBean(Object controllerBeanIn)
217 {
218 this.controllerBean = controllerBeanIn;
219 }
220
221
222
223
224 public Object getControllerBean()
225 {
226 return this.controllerBean;
227 }
228
229
230
231
232 public void setControllerAction(String controllerActionIn)
233 {
234 this.controllerAction = controllerActionIn;
235 }
236
237
238
239
240 public String getControllerAction()
241 {
242 return this.controllerAction;
243 }
244
245
246
247
248 @Override
249 public String toString()
250 {
251 StringBuilder buf = new StringBuilder();
252 buf.append(this.getClass().getName() + "[");
253 buf.append("label=").append(this.label);
254 buf.append(",outcome=").append(this.outcome);
255 buf.append(",viewId=").append(this.viewId);
256 buf.append(",roles=").append(this.roles);
257 if (this.children != null)
258 buf.append(",children=").append(this.children);
259 buf.append("]");
260 return buf.toString();
261 }
262 }