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.InvocationTargetException;
19 import java.lang.reflect.Method;
20 import java.lang.reflect.Modifier;
21 import java.util.ArrayList;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.StringTokenizer;
25
26 import org.xulux.api.dataprovider.IConverter;
27 import org.xulux.api.dataprovider.IField;
28 import org.xulux.api.dataprovider.IMapping;
29 import org.xulux.utils.ClassLoaderUtils;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public class BeanField implements IField
46 {
47
48
49
50
51 private String name;
52
53
54
55
56 private Method method;
57
58
59
60
61
62 private String alias;
63
64
65
66
67
68 private Method changeMethod;
69
70
71
72
73
74 private boolean baseType;
75
76
77
78
79 private List parameterList;
80
81
82
83
84
85 private List setParameterList;
86
87
88
89
90
91
92 private Object[] args;
93
94
95
96
97 private String realField;
98
99
100
101
102 private boolean tempRealField;
103
104 private BeanMapping mapping;
105
106
107
108
109
110 public BeanField()
111 {
112 }
113
114
115
116
117
118
119 public BeanField(Method method)
120 {
121 setMethod(method);
122 }
123
124
125
126
127
128 public boolean isReadOnly() {
129 if (changeMethod == null) {
130 return true;
131 }
132 return false;
133 }
134
135
136
137
138
139
140
141
142
143 public Object setValue(Object bean, Object value)
144 {
145 if (isReadOnly())
146 {
147
148
149
150 return bean;
151 }
152 try
153 {
154 if (getRealField() != null) {
155 Class retType = getType();
156 BeanMapping mapping = (BeanMapping) getMapping().getDataProvider().getMapping(retType);
157
158
159 Object childObject = getMethod().invoke(bean, getArgs());
160
161 if (childObject == null) {
162
163
164
165 childObject = ClassLoaderUtils.getObjectFromClass(retType, getBeanParameterValues(new ArrayList()));
166
167 getChangeMethod().invoke(bean, getSetMethodArgs(childObject));
168
169
170 if (childObject == null) {
171
172
173
174
175 return bean;
176 }
177 }
178 BeanMapping childMapping = (BeanMapping) getMapping().getDataProvider().getMapping(childObject);
179
180 IField field = childMapping.getField(getRealField());
181
182 return field.setValue(childObject, value);
183 }
184 try {
185
186
187 this.changeMethod.invoke(bean, getSetMethodArgs(value));
188 realField = null;
189 } catch (IllegalArgumentException iae) {
190
191
192
193 }
194 }
195 catch (IllegalAccessException e)
196 {
197
198
199
200 }
201 catch (InvocationTargetException e)
202 {
203
204
205
206 }
207 catch (Exception e) {
208
209
210
211 }
212 return bean;
213 }
214
215
216
217
218
219 private List getBeanParameterValues(List list) {
220 if (list == null || list.size() == 0) {
221 return null;
222 }
223 ArrayList valueList = new ArrayList();
224 for (int i = 0; i < list.size(); i++) {
225 Object object = list.get(i);
226 if (object instanceof BeanParameter) {
227 valueList.add(((BeanParameter) object).getObject());
228 }
229 }
230
231 return valueList;
232 }
233
234
235
236
237
238
239
240
241
242
243 public Object getValue(Object bean)
244 {
245 try
246 {
247 if (this.realField != null) {
248
249
250 Class retType = getType();
251 if (retType == Object.class) {
252
253 Object retBean = getMethod().invoke(bean, getArgs());
254 retType = retBean.getClass();
255 }
256 BeanMapping mapping = (BeanMapping) getMapping().getDataProvider().getMapping(retType);
257 IField field = mapping.getField(realField);
258 if (field != null) {
259 Object fieldValue = field.getValue(getMethod().invoke(bean, getArgs()));
260 return fieldValue;
261 }
262 }
263 else {
264 if (bean != null) {
265
266
267
268 return this.method.invoke(bean, getArgs());
269 } else {
270
271
272 if (Modifier.isStatic(getMethod().getModifiers())) {
273 return getMethod().invoke(null, getArgs());
274 }
275
276
277
278
279 }
280 }
281 }
282 catch (IllegalAccessException e)
283 {
284
285
286
287
288 }
289 catch (InvocationTargetException e)
290 {
291
292
293
294
295 }
296 return null;
297 }
298
299
300
301
302
303
304 protected Object[] getArgs() {
305 if (this.args != null) {
306 return this.args;
307 }
308 if (parameterList == null) {
309 return null;
310 }
311 int listCounter = parameterList.size();
312 this.args = new Object[listCounter];
313 Iterator it = parameterList.iterator();
314 listCounter = 0;
315 while (it.hasNext()) {
316 BeanParameter parm = (BeanParameter) it.next();
317 this.args[listCounter] = parm.getObject();
318 listCounter++;
319 }
320 return this.args;
321 }
322
323
324
325
326
327
328
329
330
331
332
333 protected Object[] getSetMethodArgs(Object value) {
334 ArrayList parms = new ArrayList();
335 Class[] clz = this.changeMethod.getParameterTypes();
336 int parmSize = 0;
337 if (parameterList != null) {
338 parmSize = parameterList.size();
339 }
340 int clzSize = clz.length;
341
342 if (clzSize == 1) {
343 Class parmType = clz[0];
344 if (value != null) {
345 if (parmType == value.getClass()) {
346 return new Object[] {value};
347 }
348 }
349 }
350 if (parmSize <= clzSize && clzSize != 0) {
351 if (parmSize == 0 && clzSize == 1) {
352 if (clz[0] == String.class) {
353 if (value == null) {
354 return new Object[] {
355 value
356 };
357 } else {
358 return new Object[] {
359 value.toString()
360 };
361 }
362 } else {
363 IConverter converter = getMapping().getDataProvider().getConverter(clz[0]);
364 if (converter != null) {
365 Object newValue = converter.getBeanValue(value);
366 if (newValue != null) {
367 value = newValue;
368 }
369 }
370 return new Object[] {
371 value
372 };
373 }
374 }
375
376
377
378
379
380
381 if (parmSize == 1 && clzSize == 2) {
382 Object[] retValue = new Object[clzSize];
383 int currentParm = 1;
384 retValue[0] = ((BeanParameter) parameterList.get(0)).getObject();
385 if (clz[0] != retValue[0].getClass()) {
386 retValue[1] = retValue[0];
387 currentParm = 0;
388 }
389 if (clz[currentParm] == String.class) {
390 retValue[currentParm] = value.toString();
391 } else {
392
393
394
395
396
397
398 if (value != null && clz[currentParm] == value.getClass()) {
399 retValue[currentParm] = value;
400 } else {
401
402
403
404
405 retValue[currentParm] = value;
406 }
407 }
408 return retValue;
409 }
410
411 }
412 return parms.toArray();
413 }
414
415
416
417
418
419
420
421 public String getName()
422 {
423 return this.name;
424 }
425
426
427
428
429
430
431 public Method getMethod()
432 {
433 return method;
434 }
435
436
437
438
439
440
441 public void setMethod(Method method)
442 {
443 this.method = method;
444 if (method == null) {
445 return;
446 }
447 this.name = method.getName().toLowerCase();
448 if (this.name.startsWith("get")) {
449 this.name = this.name.substring("get".length());
450 } else if (this.name.startsWith("is")) {
451 this.name = this.name.substring("is".length());
452 }
453 }
454
455
456
457
458
459 public String toString()
460 {
461 StringBuffer sb = new StringBuffer();
462 if (getMethod() != null){
463 sb.append(getMethod().getName());
464 }
465 sb.append("[");
466 sb.append("Name=");
467 sb.append(getName());
468 sb.append(",");
469 sb.append("Alias=");
470 sb.append(getAlias());
471 sb.append(",");
472 sb.append("realField=");
473 sb.append(getRealField());
474 sb.append("]");
475 return sb.toString();
476 }
477
478
479
480
481
482
483
484
485
486
487
488 public boolean equals(Object object)
489 {
490 if (object instanceof String)
491 {
492 if (getAlias() != null && getAlias().equalsIgnoreCase(object.toString()))
493 {
494 return true;
495 } else if (getMethod() != null && getMethod().getName().equalsIgnoreCase(object.toString())) {
496
497
498 return true;
499 }
500 }
501 else if (object instanceof BeanField) {
502 if (object == this) {
503 return true;
504 }
505 Method tmpMethod = ((BeanField) object).getMethod();
506 if (tmpMethod != null) {
507 if (tmpMethod.getDeclaringClass().equals(this.getMethod().getDeclaringClass())
508 && tmpMethod.getName().equals(this.getMethod().getName())) {
509 return true;
510 }
511 }
512 }
513 return false;
514 }
515
516
517
518
519
520
521 public boolean isBaseType()
522 {
523 return baseType;
524 }
525
526
527
528
529
530
531 public void setBaseType(boolean baseType)
532 {
533 this.baseType = baseType;
534 }
535
536
537
538
539 public void setChangeMethod(Method method)
540 {
541 this.changeMethod = method;
542 }
543
544
545
546
547
548
549
550
551 public void setChangeMethod(Class clazz, String name) {
552 Method[] methods = clazz.getMethods();
553
554 for (int i = 0; i < methods.length; i++) {
555 Method m = methods[i];
556 if (m.getName().equalsIgnoreCase(name)) {
557
558 setChangeMethod(m);
559 break;
560 }
561 }
562 if (!name.startsWith("set")) {
563 setChangeMethod(clazz, "set" + name);
564 }
565 }
566
567
568
569
570
571
572 public String getAlias()
573 {
574 if (alias == null)
575 {
576 return getName();
577 }
578 return alias;
579 }
580
581
582
583
584
585
586 public void setAlias(String alias)
587 {
588 this.alias = alias;
589 }
590
591
592
593
594
595
596
597
598 public void setParameters(List parameters) {
599 this.parameterList = parameters;
600 }
601
602
603
604
605
606
607 public List getParameters() {
608 return this.parameterList;
609 }
610
611
612
613
614
615 public void addParameter(BeanParameter parameter) {
616 if (parameterList == null) {
617 parameterList = new ArrayList();
618 }
619 parameterList.add(parameter);
620 }
621
622
623
624
625 public void setRealField(String realField) {
626 this.realField = realField;
627 }
628
629
630
631
632
633
634 public void setTempRealField(String realField) {
635 this.realField = realField;
636 this.tempRealField = true;
637 }
638
639
640
641
642 public boolean hasTempRealField() {
643 return this.tempRealField;
644 }
645
646
647
648
649 public void removeTempRealField() {
650 this.realField = null;
651 this.tempRealField = false;
652 }
653
654
655
656
657
658 public String getRealField() {
659 return this.realField;
660 }
661
662
663
664
665
666 public Class getType() {
667 return this.method.getReturnType();
668 }
669
670
671
672
673 protected Method getChangeMethod() {
674 return this.changeMethod;
675 }
676
677
678
679
680 public IMapping getMapping() {
681 return this.mapping;
682 }
683
684
685
686
687
688 BeanMapping getBeanMapping() {
689 return this.mapping;
690 }
691
692
693
694
695
696 public void setMapping(BeanMapping mapping) {
697 this.mapping = mapping;
698 }
699
700
701
702
703 public void setProperty(String name, String value) {
704 if (name == null) {
705 return;
706 }
707 if (name.equalsIgnoreCase("alias")) {
708 setAlias(value);
709 } else if (name.equalsIgnoreCase("parameter")) {
710 StringTokenizer stn = new StringTokenizer(value, ",");
711 addParameter(new BeanParameter(stn.nextToken(), stn.nextToken()));
712 } else if (name.equalsIgnoreCase("changemethod")) {
713 StringTokenizer stn = new StringTokenizer(value, ",");
714 String className = stn.nextToken();
715 Class clazz = ClassLoaderUtils.getClass(className);
716 setChangeMethod(clazz, stn.nextToken());
717 }
718 }
719
720 }