1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.xulux.dataprovider.bean;
17
18 import java.lang.reflect.Method;
19 import java.lang.reflect.Modifier;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.xulux.api.dataprovider.IDataProvider;
24 import org.xulux.api.dataprovider.IField;
25 import org.xulux.api.dataprovider.IMapping;
26 import org.xulux.utils.BooleanUtils;
27 import org.xulux.utils.ClassLoaderUtils;
28
29
30
31
32
33
34
35
36
37
38
39
40
41 public class BeanMapping implements IMapping
42 {
43
44
45
46
47 private String name;
48
49
50
51 private Class bean;
52
53
54
55 private boolean discovery;
56
57
58
59 private boolean isDiscovered;
60
61
62
63 private FieldList fields;
64
65
66
67
68 private BeanDataProvider dataProvider;
69
70
71
72 public BeanMapping() {
73 }
74
75
76
77
78
79 public BeanMapping(String name)
80 {
81 setName(name);
82 }
83
84
85
86
87
88 public String getName()
89 {
90 return name;
91 }
92
93
94
95
96
97
98 public void setName(String name)
99 {
100 this.name = name;
101 }
102
103
104
105
106
107
108 public Class getBean()
109 {
110 return bean;
111 }
112
113
114
115
116
117
118 public void setBean(Class bean)
119 {
120 this.bean = bean;
121 }
122
123
124
125
126
127
128 public boolean isDiscovery()
129 {
130 return discovery;
131 }
132
133
134
135
136
137
138 public void setDiscovery(boolean discovery)
139 {
140 this.discovery = discovery;
141 }
142
143
144
145
146
147
148
149
150
151 public IField createField(Object object)
152 {
153 if (!(object instanceof String)) {
154 return null;
155 }
156 String name =(String) object;
157 Method method = findMethod(name, false);
158 Method setMethod = findMethod(name, true);
159 if (method != null) {
160 BeanField field = new BeanField(method);
161 if (setMethod != null) {
162 field.setChangeMethod(setMethod);
163 }
164 return field;
165 }
166 return null;
167 }
168
169
170
171
172
173
174
175
176 private Method findMethod(String name, boolean setMethod)
177 {
178 if (getBean() == null) {
179
180
181
182 return null;
183 }
184 Method[] methods = getBean().getMethods();
185 String pre = null;
186 if (setMethod) {
187 pre = "set";
188 } else {
189 pre = "get";
190 }
191 Method result = null;
192 for (int i = 0; i < methods.length; i++) {
193 Method method = methods[i];
194 if (method.getName().equalsIgnoreCase(pre + name) ||
195 (!setMethod && method.getName().equalsIgnoreCase("is" + name))) {
196 if (result != null && result.getParameterTypes().length != 0) {
197 continue;
198 }
199 result = method;
200 if (result.getParameterTypes().length == 0) {
201
202 result = method;
203 break;
204 }
205 }
206 }
207 return result;
208 }
209
210
211
212
213
214
215 public void addField(IField f)
216 {
217 if (fields == null)
218 {
219 fields = new FieldList();
220 }
221
222 if (f instanceof BeanField)
223 {
224 BeanField field = (BeanField) f;
225 Class clazz = field.getMethod().getReturnType();
226 Class baseClass = getProvider().getBaseClass();
227 boolean discoverNestedBean = false;
228 boolean isBaseTypeField = false;
229 if (baseClass != null)
230 {
231 if (baseClass.isInterface())
232 {
233 Class[] ifaces = clazz.getInterfaces();
234 for (int i = 0; i < ifaces.length; i++)
235 {
236 if (ifaces[i] == baseClass)
237 {
238 discoverNestedBean = true;
239 isBaseTypeField = true;
240 }
241 }
242 }
243 else
244 {
245 Class tmpClass = clazz;
246
247 if (baseClass == tmpClass)
248 {
249 discoverNestedBean = false;
250 }
251 else
252 {
253 while (tmpClass != null && tmpClass != Object.class)
254 {
255 if (baseClass == tmpClass.getSuperclass())
256 {
257 discoverNestedBean = true;
258 isBaseTypeField = true;
259 break;
260 }
261 tmpClass = tmpClass.getSuperclass();
262 }
263 }
264 }
265 }
266 if (discoverNestedBean)
267 {
268 if (dataProvider.getMapping(dataProvider.getPossibleMappingName(clazz)) == null
269 && dataProvider.getMapping(dataProvider.getPlainBeanName(clazz)) == null
270 && field.getMethod().getDeclaringClass() != clazz)
271 {
272 if (!dataProvider.isInCache(clazz))
273 {
274 dataProvider.getMapping(clazz);
275 }
276 }
277 }
278 field.setBaseType(isBaseTypeField);
279 }
280 else
281 {
282
283 return;
284 }
285 fields.add(f);
286 }
287
288
289
290
291 public List getFields()
292 {
293 return fields;
294 }
295
296
297
298
299
300
301 public IField getField(Object f)
302 {
303 if (fields == null || f == null) {
304 return null;
305 }
306 String name = f.toString();
307
308 if (name.startsWith("?")) {
309 int dotIndex = name.indexOf('.');
310 if (dotIndex != -1) {
311 name = name.substring(dotIndex + 1);
312 }
313 }
314
315 int dotIndex = name.indexOf(".");
316 String realField = null;
317 if (dotIndex != -1) {
318 String field = name.substring(0, dotIndex);
319 realField = name.substring(dotIndex + 1);
320 name = field;
321 }
322 int index = fields.indexOf(name);
323 if (index == -1 && realField != null) {
324 name = name+"."+realField;
325 dotIndex = name.lastIndexOf(".");
326 if (dotIndex != -1) {
327 String field = name.substring(0, dotIndex);
328 realField = name.substring(dotIndex + 1);
329 name = field;
330 }
331
332 index = fields.indexOf(name);
333 }
334 if (index != -1)
335 {
336 BeanField bf = (BeanField) fields.get(index);
337
338 if (realField != null) {
339 bf.setTempRealField(realField);
340 } else if (bf.hasTempRealField()) {
341
342
343 bf.removeTempRealField();
344 }
345
346 return bf;
347 }
348 return null;
349 }
350
351
352
353
354
355
356
357
358
359
360
361 public void discover()
362 {
363 if (!isDiscovered && isDiscovery())
364 {
365 Method[] methods = bean.getMethods();
366 for (int i = 0; i < methods.length; i++)
367 {
368 Method method = methods[i];
369 if ((!method.getName().equals("getClass")
370 && method.getModifiers() != Modifier.PRIVATE
371 && method.getModifiers() != Modifier.PROTECTED))
372 {
373 if (!method.getName().startsWith("get") && !method.getName().startsWith("is")) {
374 continue;
375 }
376 BeanField field = new BeanField(method);
377
378 String fieldName = field.getName();
379 if (fieldName.length() > 0) {
380 fieldName = field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1);
381 }
382 Method setMethod = findMethod(fieldName, true);
383 field.setChangeMethod(setMethod);
384 BeanField previousField = (BeanField) getField(fieldName);
385 if (previousField != null) {
386 if (previousField.getMethod().getParameterTypes().length == 0) {
387 continue;
388 } else {
389 if (field.getMethod().getParameterTypes().length == 0) {
390 fields.remove(previousField);
391 }
392
393 }
394 }
395 addField(field);
396 }
397 }
398 isDiscovered = true;
399 }
400 }
401
402
403
404
405
406
407 public void discover(String field) {
408 }
409
410
411
412 public boolean equals(Object object)
413 {
414 if (object instanceof BeanMapping) {
415 String mappingName = ((BeanMapping) object).getName();
416 if (mappingName != null) {
417 return mappingName.equals(this.getName());
418 }
419 }
420 return false;
421 }
422
423
424
425
426
427 public String toString() {
428 return getName();
429 }
430
431
432
433
434
435
436
437
438 public static class FieldList extends ArrayList
439 {
440
441
442
443 public FieldList() {
444 super();
445 }
446
447
448
449
450
451
452
453
454
455
456 public int indexOf(Object elem) {
457 if (elem == null) {
458 return -1;
459 }
460 for (int i = 0; i < size(); i++) {
461 Object data = get(i);
462 if (data != null && data.equals(elem)) {
463 return i;
464 }
465 }
466 return -1;
467 }
468 }
469
470
471
472
473 public Object setValue(String field, Object object, Object value) {
474 IField iField = getField(field);
475 if (iField != null) {
476 return iField.setValue(object, value);
477 }
478 return null;
479 }
480
481
482
483 public Object getValue(String field, Object object) {
484 IField iField = getField(field);
485 if (iField != null) {
486 return iField.getValue(object);
487 }
488 return null;
489 }
490
491
492
493
494
495
496 public void setDataProvider(BeanDataProvider dataProvider) {
497 this.dataProvider = dataProvider;
498 }
499
500
501
502
503
504 BeanDataProvider getProvider() {
505 return this.dataProvider;
506 }
507
508
509
510
511 public IDataProvider getDataProvider() {
512 return this.dataProvider;
513 }
514
515
516
517
518 public void setProperty(String name, String value) {
519 if (name == null) {
520 return;
521 }
522 if (name.equalsIgnoreCase("bean")) {
523 setBean(ClassLoaderUtils.getClass(value));
524 } else if (name.equalsIgnoreCase("discovery")) {
525 setDiscovery(BooleanUtils.toBoolean(value));
526 }
527
528 }
529
530 }