1
2
3
4
5
6
7 package org.asyrinx.joey.tapestry.components.entity;
8
9 import org.apache.commons.lang.StringUtils;
10 import org.apache.tapestry.ApplicationRuntimeException;
11 import org.apache.tapestry.IPage;
12 import org.apache.tapestry.IRequestCycle;
13 import org.asyrinx.joey.tapestry.page.DetailView;
14 import org.asyrinx.joey.tapestry.page.EntityPageDictionary;
15
16 /***
17 * @author takeshi
18 */
19 public class EntityPagingStrategy {
20
21 /***
22 *
23 */
24 public EntityPagingStrategy(EntityPagingAction action) {
25 super();
26 this.action = action;
27 }
28
29 private final EntityPagingAction action;
30
31 private EntityPageDictionary pageDictionary = null;
32
33 protected EntityPageDictionary getPageDictionary() {
34 EntityPageDictionary result = action.getDetailPageDictionary();
35 if (result != null)
36 return result;
37 if (this.pageDictionary != null)
38 return this.pageDictionary;
39 if (!StringUtils.isEmpty(action.getDetailPageDictionaryName())) {
40 result = (EntityPageDictionary) action.getS2Container().getComponent(action.getDetailPageDictionaryName());
41 if (result != null)
42 return result;
43 }
44 result = (EntityPageDictionary) action.getS2Container().getComponent(EntityPageDictionary.class);
45 if (result == null)
46 throw new ApplicationRuntimeException("Component was not found. '" + action.getDetailPageDictionaryName() + "'.");
47 return result;
48 }
49
50 /***
51 * @param entity
52 * @return
53 */
54 protected String getDetailPageName(final Object entity) {
55 if (!StringUtils.isEmpty(action.getDetailPageName()))
56 return action.getDetailPageName();
57 final String pageName = getPageDictionary().getPageName(entity.getClass());
58 if (StringUtils.isEmpty(pageName))
59 throw new ApplicationRuntimeException("Page was not found for entity '" + entity.getClass().getName()
60 + "'.");
61 return pageName;
62 }
63
64 /***
65 * @param cycle
66 * @param entity
67 */
68 public void showEntity(IRequestCycle cycle, final Object entity) {
69 final String pageName = getDetailPageName(entity);
70
71 final IPage page = cycle.getPage(pageName);
72 if (page == null)
73 throw new ApplicationRuntimeException("Page was not found. '" + pageName + "'.");
74 if (page instanceof DetailView) {
75 final DetailView detailView = (DetailView) page;
76 detailView.setTargetObject(entity);
77 }
78 cycle.activate(page);
79 }
80 }