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 import java.lang.reflect.ParameterizedType;
19 import java.lang.reflect.Type;
20
21 import org.apache.tapestry5.Block;
22 import org.apache.tapestry5.ComponentResources;
23 import org.apache.tapestry5.PersistenceConstants;
24 import org.apache.tapestry5.PrimaryKeyEncoder;
25 import org.apache.tapestry5.ValueEncoder;
26 import org.apache.tapestry5.annotations.Persist;
27 import org.apache.tapestry5.annotations.Retain;
28 import org.apache.tapestry5.corelib.components.Zone;
29 import org.apache.tapestry5.ioc.Messages;
30 import org.apache.tapestry5.ioc.annotations.Inject;
31 import org.apache.tapestry5.services.ValueEncoderSource;
32
33 import br.com.arsmachina.controller.Controller;
34 import br.com.arsmachina.tapestrycrud.Constants;
35 import br.com.arsmachina.tapestrycrud.CrudPage;
36 import br.com.arsmachina.tapestrycrud.encoder.ActivationContextEncoder;
37 import br.com.arsmachina.tapestrycrud.encoder.LabelEncoder;
38 import br.com.arsmachina.tapestrycrud.selectmodel.SelectModelFactory;
39 import br.com.arsmachina.tapestrycrud.services.ActivationContextEncoderSource;
40 import br.com.arsmachina.module.service.ControllerSource;
41 import br.com.arsmachina.tapestrycrud.services.LabelEncoderSource;
42 import br.com.arsmachina.tapestrycrud.services.PrimaryKeyEncoderSource;
43 import br.com.arsmachina.tapestrycrud.services.PrimaryKeyTypeService;
44
45
46
47
48
49
50
51
52 public abstract class BasePage<T, K extends Serializable> implements CrudPage<T, K> {
53
54 @Inject
55 private ActivationContextEncoderSource activationContextEncoderSource;
56
57 @Inject
58 private ControllerSource controllerSource;
59
60 @Inject
61 private LabelEncoderSource labelEncoderSource;
62
63 @Inject
64 private ValueEncoderSource valueEncoderSource;
65
66 @Inject
67 private SelectModelFactory selectModelFactory;
68
69 @Inject
70 private PrimaryKeyEncoderSource primaryKeyEncoderSource;
71
72 @Inject
73 private PrimaryKeyTypeService primaryKeyTypeService;
74
75 @Retain
76 private Class<T> entityClass;
77
78 @Retain
79 private Controller<T, K> controller;
80
81 @Persist(PersistenceConstants.FLASH)
82 private String message;
83
84 @Inject
85 private ComponentResources componentResources;
86
87 @Inject
88 private Messages messages;
89
90 @Retain
91 private Class<K> primaryKeyClass;
92
93
94
95
96 @SuppressWarnings("unchecked")
97 public BasePage() {
98
99 final Type genericSuperclass = getClass().getGenericSuperclass();
100 final ParameterizedType parameterizedType = ((ParameterizedType) genericSuperclass);
101 entityClass = (Class<T>) parameterizedType.getActualTypeArguments()[0];
102 primaryKeyClass = primaryKeyTypeService.getPrimaryKeyType(entityClass);
103
104 controller = controllerSource.get(entityClass);
105
106 assert entityClass != null;
107 assert primaryKeyClass != null;
108 assert controller != null;
109
110 }
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130 protected boolean filterReadOnlyComponentsInBeanModel() {
131 return true;
132 }
133
134
135
136
137
138
139
140
141 protected <V> ValueEncoder<V> getValueEncoder(Class<V> clasz) {
142 return valueEncoderSource.getValueEncoder(clasz);
143 }
144
145
146
147
148
149
150
151
152 protected <V> LabelEncoder<V> getLabelEncoder(Class<V> clasz) {
153 return labelEncoderSource.get(clasz);
154 }
155
156
157
158
159
160
161
162
163 protected <V, X extends Serializable> ActivationContextEncoder<V> getActivationContextEncoder(
164 Class<V> clasz) {
165 return activationContextEncoderSource.get(clasz);
166 }
167
168
169
170
171
172
173
174
175
176 protected <V, X extends Serializable> PrimaryKeyEncoder<X, V> getPrimaryKeyEncoder(
177 Class<V> clasz) {
178 return primaryKeyEncoderSource.get(clasz);
179 }
180
181
182
183
184 public String getMessage() {
185 return message;
186 }
187
188
189
190
191 public void setMessage(String message) {
192 this.message = message;
193 }
194
195
196
197
198
199
200 public final Controller<T, K> getController() {
201 return controller;
202 }
203
204 public final Class<T> getEntityClass() {
205 return entityClass;
206 }
207
208 public final Class<K> getPrimaryKeyClass() {
209 return primaryKeyClass;
210 }
211
212
213
214
215
216
217 public final Messages getMessages() {
218 return messages;
219 }
220
221
222
223
224
225
226 public Zone getFormZone() {
227 return (Zone) componentResources.getEmbeddedComponent(getFormZoneId());
228 }
229
230
231
232
233
234
235
236 String getFormBlockId() {
237 return Constants.DEFAULT_FORM_BLOCK_ID;
238 }
239
240
241
242
243
244
245
246 String getFormZoneId() {
247 return Constants.DEFAULT_FORM_ZONE_ID;
248 }
249
250
251
252
253
254
255
256 protected boolean returnZoneOnXHR() {
257 return true;
258 }
259
260
261
262
263
264
265 final protected SelectModelFactory getSelectModelFactory() {
266 return selectModelFactory;
267 }
268
269 }