1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package br.com.arsmachina.tapestrycrud.base;
16
17 import java.io.Serializable;
18
19 import org.apache.tapestry5.ComponentResources;
20 import org.apache.tapestry5.EventContext;
21 import org.apache.tapestry5.PrimaryKeyEncoder;
22 import org.apache.tapestry5.annotations.Cached;
23 import org.apache.tapestry5.annotations.OnEvent;
24 import org.apache.tapestry5.annotations.PageDetached;
25 import org.apache.tapestry5.annotations.Retain;
26 import org.apache.tapestry5.beaneditor.BeanModel;
27 import org.apache.tapestry5.corelib.components.Grid;
28 import org.apache.tapestry5.ioc.annotations.Inject;
29 import org.apache.tapestry5.services.BeanModelSource;
30 import org.apache.tapestry5.services.Request;
31
32 import br.com.arsmachina.tapestrycrud.Constants;
33 import br.com.arsmachina.tapestrycrud.grid.ControllerGridDataSource;
34 import br.com.arsmachina.tapestrycrud.services.PrimaryKeyEncoderSource;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 public abstract class BaseListPage<T, K extends Serializable> extends BasePage<T, K> {
53
54 @Inject
55 private ComponentResources componentResources;
56
57 @Inject
58 private Request request;
59
60 @Inject
61 private PrimaryKeyEncoderSource primaryKeyEncoderSource;
62
63 @Retain
64 private PrimaryKeyEncoder<K, T> primaryKeyEncoder;
65
66 @Inject
67 private BeanModelSource beanModelSource;
68
69 private T object;
70
71
72
73
74 @SuppressWarnings("unchecked")
75 public BaseListPage() {
76
77 super();
78
79 primaryKeyEncoder = (PrimaryKeyEncoder<K, T>) primaryKeyEncoderSource.get(getEntityClass());
80
81 }
82
83
84
85
86
87
88
89 @SuppressWarnings("unchecked")
90 @Cached
91 public Object getObjects() {
92 return new ControllerGridDataSource(getEntityClass(), getController());
93 }
94
95
96
97
98
99
100 public T getObject() {
101 return object;
102 }
103
104
105
106
107
108
109 public void setObject(T object) {
110 this.object = object;
111 }
112
113
114
115
116
117
118 @SuppressWarnings("unchecked")
119 public BeanModel<T> getBeanModel() {
120
121 final BeanModel<T> beanModel = beanModelSource.createDisplayModel(getEntityClass(),
122 getMessages());
123 beanModel.add(Constants.ACTION_PROPERTY_NAME, null);
124
125 return beanModel;
126
127 }
128
129
130
131
132
133
134
135 protected final Object remove(T object) {
136
137 if (object == null) {
138 setRemoveErrorNotFoundMessage();
139 }
140
141 else if (canRemove(object)) {
142
143 getController().delete(object);
144 setRemoveSuccessMessage();
145
146 }
147 else {
148 setRemoveErrorNotAllowedMessage();
149 }
150
151 return returnFromDoRemove();
152
153 }
154
155
156
157
158
159
160 protected Object returnFromDoRemove() {
161
162 Object returnValue = null;
163
164 if (request.isXHR()) {
165
166 if (returnZoneOnXHR()) {
167 returnValue = getFormZone();
168 }
169 else {
170 returnValue = componentResources.getBlock(getFormBlockId());
171 }
172
173 }
174
175 return returnValue;
176
177 }
178
179
180
181
182 protected void setRemoveSuccessMessage() {
183 setMessage(getMessages().get(Constants.MESSAGE_SUCCESS_REMOVE));
184 }
185
186
187
188
189 protected void setRemoveErrorNotAllowedMessage() {
190 setMessage(getMessages().get(Constants.MESSAGE_ERROR_REMOVE_NOT_ALLOWED));
191 }
192
193
194
195
196 protected void setRemoveErrorNotFoundMessage() {
197 setMessage(getMessages().get(Constants.MESSAGE_ERROR_REMOVE_NOT_FOUND));
198 }
199
200
201
202
203
204
205
206
207 protected boolean canRemove(T object) {
208 return true;
209 }
210
211
212
213
214
215 @PageDetached
216 void clearMessage() {
217
218 if (request.isXHR()) {
219 setMessage(null);
220 }
221
222 }
223
224
225
226
227
228
229
230 @OnEvent(Constants.REMOVE_OBJECT_ACTION)
231 protected Object remove(EventContext context) {
232
233 K id = context.get(getPrimaryKeyClass(), 0);
234 final T toBeRemoved = primaryKeyEncoder.toValue(id);
235 return remove(toBeRemoved);
236
237 }
238
239
240
241
242
243
244
245
246
247 protected <X, Y extends Serializable> PrimaryKeyEncoder<Y, X> getPrimaryKeyEncoder(Class<X> clasz) {
248 return primaryKeyEncoderSource.get(clasz);
249 }
250
251 }