1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.shale.usecases.rolodex; |
19 | |
|
20 | |
import java.io.UnsupportedEncodingException; |
21 | |
import java.net.URLDecoder; |
22 | |
import java.util.List; |
23 | |
|
24 | |
import javax.faces.context.FacesContext; |
25 | |
import javax.faces.model.SelectItem; |
26 | |
|
27 | |
import org.apache.commons.logging.Log; |
28 | |
import org.apache.commons.logging.LogFactory; |
29 | |
import org.apache.shale.clay.config.beans.AttributeBean; |
30 | |
import org.apache.shale.clay.config.beans.ComponentBean; |
31 | |
import org.apache.shale.clay.config.beans.ElementBean; |
32 | |
import org.apache.shale.view.AbstractViewController; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | 5 | public class Rolodex extends AbstractViewController { |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
private static Log log; |
49 | |
static { |
50 | 2 | log = LogFactory.getLog(Rolodex.class); |
51 | 1 | } |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | 5 | private int selectedTab = 0; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
public int getSelectedTab() { |
66 | 22 | return selectedTab; |
67 | |
} |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | 5 | private Contact selectedContact = null; |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
public Contact getSelectedContact() { |
82 | 41 | return selectedContact; |
83 | |
} |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
public void setSelectedContact(Contact contact) { |
91 | 46 | selectedContact = contact; |
92 | 46 | } |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
public void setSelectedTab(int index) { |
100 | 21 | if (log.isInfoEnabled()) |
101 | 21 | log.info("Switching from tab " + selectedTab + " to tab " + index); |
102 | |
|
103 | 21 | selectedTab = index; |
104 | 21 | contacts = null; |
105 | 21 | } |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
protected ElementBean createCommandLinkMetadata(SelectItem item, |
119 | |
FacesContext context) { |
120 | |
|
121 | |
|
122 | 9 | ElementBean link = new ElementBean(); |
123 | 9 | link.setRenderId(generateId()); |
124 | 9 | link.setJsfid("commandLink"); |
125 | 9 | link.setComponentType("javax.faces.HtmlCommandLink"); |
126 | |
|
127 | |
|
128 | 9 | AttributeBean attr = new AttributeBean(); |
129 | 9 | attr.setName("value"); |
130 | 9 | attr.setValue(item.getLabel()); |
131 | 9 | link.addAttribute(attr); |
132 | |
|
133 | |
|
134 | |
|
135 | 9 | attr = new AttributeBean(); |
136 | 9 | attr.setName("immediate"); |
137 | 9 | attr.setValue("true"); |
138 | 9 | link.addAttribute(attr); |
139 | |
|
140 | |
|
141 | 9 | attr = new AttributeBean(); |
142 | 9 | attr.setName("action"); |
143 | 9 | attr.setValue("#{@managed-bean-name.changeTab}"); |
144 | 9 | link.addAttribute(attr); |
145 | |
|
146 | |
|
147 | 9 | ElementBean param = new ElementBean(); |
148 | 9 | param.setJsfid("param"); |
149 | 9 | param.setComponentType("javax.faces.Parameter"); |
150 | |
|
151 | 9 | param.setRenderId(generateId()); |
152 | |
|
153 | |
|
154 | |
|
155 | 9 | attr = new AttributeBean(); |
156 | 9 | attr.setName("name"); |
157 | 9 | attr.setValue("tabIndex"); |
158 | 9 | param.addAttribute(attr); |
159 | |
|
160 | |
|
161 | 9 | attr = new AttributeBean(); |
162 | 9 | attr.setName("value"); |
163 | 9 | attr.setValue(((Integer) item.getValue()).toString()); |
164 | 9 | param.addAttribute(attr); |
165 | |
|
166 | |
|
167 | 9 | link.addChild(param); |
168 | |
|
169 | 9 | return link; |
170 | |
} |
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | 5 | int renderId = 0; |
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
private int generateId() { |
186 | 65 | return renderId++; |
187 | |
} |
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
protected ElementBean createVerbatimMetadata(String html, String renderExp, |
206 | |
FacesContext context) { |
207 | 18 | ElementBean text = createVerbatimMetadata(html, context); |
208 | |
|
209 | |
|
210 | 18 | AttributeBean attr = new AttributeBean(); |
211 | 18 | attr.setName("rendered"); |
212 | 18 | attr.setValue(renderExp); |
213 | 18 | attr.setBindingType(AttributeBean.BINDING_TYPE_VALUE); |
214 | 18 | text.addAttribute(attr); |
215 | |
|
216 | 18 | return text; |
217 | |
|
218 | |
} |
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
protected ElementBean createVerbatimMetadata(String html, |
234 | |
FacesContext context) { |
235 | |
|
236 | |
|
237 | 47 | ElementBean text = new ElementBean(); |
238 | 47 | text.setRenderId(generateId()); |
239 | 47 | text.setJsfid("f:verbatim"); |
240 | 47 | text.setComponentType("javax.faces.HtmlOutputText"); |
241 | |
|
242 | |
|
243 | 47 | AttributeBean attr = new AttributeBean(); |
244 | 47 | attr.setName("value"); |
245 | 47 | attr.setValue(html); |
246 | 47 | text.addAttribute(attr); |
247 | |
|
248 | |
|
249 | 47 | attr = new AttributeBean(); |
250 | 47 | attr.setName("escape"); |
251 | 47 | attr.setValue(Boolean.FALSE.toString()); |
252 | 47 | text.addAttribute(attr); |
253 | |
|
254 | |
|
255 | 47 | attr = new AttributeBean(); |
256 | 47 | attr.setName("isTransient"); |
257 | 47 | attr.setValue(Boolean.TRUE.toString()); |
258 | 47 | text.addAttribute(attr); |
259 | |
|
260 | 47 | return text; |
261 | |
} |
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
public String changeTab() { |
271 | 9 | if (log.isInfoEnabled()) |
272 | 9 | log.info("changeTab()"); |
273 | |
|
274 | 9 | QueryParam paramObj = (QueryParam) getBean("queryParam"); |
275 | 9 | String tabIndex = paramObj.getTabIndex(); |
276 | |
|
277 | 9 | if (tabIndex != null) { |
278 | 9 | setSelectedTab(Integer.parseInt(tabIndex)); |
279 | |
|
280 | |
|
281 | 9 | setSelectedContact(null); |
282 | |
} |
283 | |
|
284 | 9 | return "rolodex$test"; |
285 | |
} |
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
public void createTabs(javax.faces.context.FacesContext context, |
301 | |
javax.faces.component.UIComponent component, |
302 | |
java.lang.Object displayElementRoot) { |
303 | |
|
304 | 1 | if (log.isInfoEnabled()) |
305 | 1 | log.info("createTabs()"); |
306 | |
|
307 | |
|
308 | 1 | RolodexDao dao = (RolodexDao) getBean("rolodexDao"); |
309 | |
|
310 | |
|
311 | 1 | List tabs = dao.getTabs(); |
312 | |
|
313 | 1 | ComponentBean root = (ComponentBean) displayElementRoot; |
314 | 1 | root.setComponentType("javax.faces.HtmlPanelGroup"); |
315 | 1 | root.addChild(createVerbatimMetadata("<ul id=\"menu\">", context)); |
316 | |
|
317 | 10 | for (int i = 0; i < tabs.size(); i++) { |
318 | 9 | SelectItem item = (SelectItem) tabs.get(i); |
319 | |
|
320 | 9 | root.addChild(createVerbatimMetadata("<li id=\"", context)); |
321 | |
|
322 | 9 | root.addChild(createVerbatimMetadata("nav-sel", |
323 | |
"#{@managed-bean-name.selectedTab == " + i + "}", context)); |
324 | 9 | root.addChild(createVerbatimMetadata("nav", |
325 | |
"#{@managed-bean-name.selectedTab != " + i + "}", context)); |
326 | |
|
327 | 9 | root.addChild(createVerbatimMetadata("\">", context)); |
328 | 9 | root.addChild(createCommandLinkMetadata(item, context)); |
329 | 9 | root.addChild(createVerbatimMetadata("</li>", context)); |
330 | |
} |
331 | |
|
332 | 1 | root.addChild(createVerbatimMetadata("</ul>", context)); |
333 | |
|
334 | 1 | } |
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | 5 | private List contacts = null; |
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
public List getContactsForTab() { |
348 | 12 | if (log.isInfoEnabled()) |
349 | 12 | log.info("getContactsForTab()"); |
350 | |
|
351 | 12 | if (contacts == null) { |
352 | |
|
353 | 12 | RolodexDao dao = (RolodexDao) getBean("rolodexDao"); |
354 | |
|
355 | |
|
356 | 12 | contacts = dao.findContactsForTab(getSelectedTab()); |
357 | |
} |
358 | |
|
359 | 12 | return contacts; |
360 | |
} |
361 | |
|
362 | |
|
363 | |
|
364 | |
|
365 | |
|
366 | |
|
367 | |
|
368 | |
|
369 | |
|
370 | |
public String selectContact() { |
371 | 13 | if (log.isInfoEnabled()) |
372 | 13 | log.info("selectContact()"); |
373 | |
|
374 | |
|
375 | 13 | QueryParam paramObj = (QueryParam) getBean("queryParam"); |
376 | 13 | String name = paramObj.getSelectedName(); |
377 | |
|
378 | 13 | if (name != null) { |
379 | |
|
380 | 13 | RolodexDao dao = (RolodexDao) getBean("rolodexDao"); |
381 | |
|
382 | |
|
383 | |
try { |
384 | 13 | name = URLDecoder.decode(name, "UTF-8"); |
385 | 13 | } catch (UnsupportedEncodingException e) {} |
386 | |
|
387 | |
|
388 | 13 | setSelectedContact(dao.findContact(name)); |
389 | |
} |
390 | |
|
391 | 13 | return "rolodex$test"; |
392 | |
} |
393 | |
|
394 | |
|
395 | |
|
396 | |
|
397 | |
|
398 | |
|
399 | |
public String saveContact() { |
400 | 1 | if (log.isInfoEnabled()) |
401 | 1 | log.info("saveContact()"); |
402 | |
|
403 | 1 | if (getSelectedContact() != null) { |
404 | 1 | RolodexDao dao = (RolodexDao) getBean("rolodexDao"); |
405 | |
|
406 | |
|
407 | 1 | setSelectedTab(dao.saveContact(getSelectedContact())); |
408 | |
} |
409 | |
|
410 | 1 | return "rolodex$test"; |
411 | |
} |
412 | |
|
413 | |
|
414 | |
|
415 | |
|
416 | |
|
417 | |
|
418 | |
public String deleteContact() { |
419 | 1 | if (log.isInfoEnabled()) |
420 | 1 | log.info("deleteContact()"); |
421 | |
|
422 | 1 | if (getSelectedContact() != null) { |
423 | 1 | RolodexDao dao = (RolodexDao) getBean("rolodexDao"); |
424 | 1 | dao.deleteContact(getSelectedContact()); |
425 | 1 | setSelectedContact(null); |
426 | 1 | contacts = null; |
427 | |
} |
428 | |
|
429 | 1 | return "rolodex$test"; |
430 | |
} |
431 | |
|
432 | |
|
433 | |
|
434 | |
|
435 | |
|
436 | |
|
437 | |
public String newContact() { |
438 | 1 | if (log.isInfoEnabled()) |
439 | 1 | log.info("newContact()"); |
440 | |
|
441 | 1 | setSelectedContact(new Contact()); |
442 | |
|
443 | 1 | return "rolodex$test"; |
444 | |
} |
445 | |
|
446 | |
} |