1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.components; |
16 |
| |
17 |
| import java.util.ArrayList; |
18 |
| import java.util.Collections; |
19 |
| import java.util.HashMap; |
20 |
| import java.util.Iterator; |
21 |
| import java.util.List; |
22 |
| import java.util.Map; |
23 |
| |
24 |
| import org.apache.tapestry.IBinding; |
25 |
| import org.apache.tapestry.IForm; |
26 |
| import org.apache.tapestry.IMarkupWriter; |
27 |
| import org.apache.tapestry.IRequestCycle; |
28 |
| import org.apache.tapestry.Tapestry; |
29 |
| import org.apache.tapestry.TapestryUtils; |
30 |
| import org.apache.tapestry.coerce.ValueConverter; |
31 |
| import org.apache.tapestry.form.AbstractFormComponent; |
32 |
| import org.apache.tapestry.services.DataSqueezer; |
33 |
| import org.apache.tapestry.services.ExpressionEvaluator; |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| public abstract class ForBean extends AbstractFormComponent |
42 |
| { |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| private static final char DESC_VALUE = 'V'; |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| private static final char DESC_PRIMARY_KEY = 'P'; |
59 |
| |
60 |
| private final RepSource COMPLETE_REP_SOURCE = new CompleteRepSource(); |
61 |
| |
62 |
| private final RepSource KEY_EXPRESSION_REP_SOURCE = new KeyExpressionRepSource(); |
63 |
| |
64 |
| |
65 |
| public abstract String getElement(); |
66 |
| |
67 |
| public abstract String getKeyExpression(); |
68 |
| |
69 |
| public abstract IPrimaryKeyConverter getConverter(); |
70 |
| |
71 |
| public abstract Object getDefaultValue(); |
72 |
| |
73 |
| public abstract boolean getMatch(); |
74 |
| |
75 |
| public abstract boolean getVolatile(); |
76 |
| |
77 |
| |
78 |
| public abstract DataSqueezer getDataSqueezer(); |
79 |
| |
80 |
| public abstract ValueConverter getValueConverter(); |
81 |
| |
82 |
| public abstract ExpressionEvaluator getExpressionEvaluator(); |
83 |
| |
84 |
| |
85 |
| private Object _value; |
86 |
| |
87 |
| private int _index; |
88 |
| |
89 |
| private boolean _rendering; |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
| |
95 |
42
| protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
|
96 |
| { |
97 |
| |
98 |
42
| IForm form = (IForm) cycle.getAttribute(TapestryUtils.FORM_ATTRIBUTE);
|
99 |
| |
100 |
| |
101 |
| |
102 |
42
| boolean cycleRewinding = cycle.isRewinding();
|
103 |
42
| if (cycleRewinding && form != null && !form.isRewinding())
|
104 |
0
| return;
|
105 |
| |
106 |
| |
107 |
42
| Iterator dataSource = getData(cycle, form);
|
108 |
| |
109 |
| |
110 |
| |
111 |
42
| if (dataSource == null)
|
112 |
0
| return;
|
113 |
| |
114 |
42
| String element = getElement();
|
115 |
| |
116 |
| |
117 |
42
| try
|
118 |
| { |
119 |
42
| _index = 0;
|
120 |
42
| _rendering = true;
|
121 |
| |
122 |
42
| while (dataSource.hasNext())
|
123 |
| { |
124 |
| |
125 |
114
| _value = dataSource.next();
|
126 |
| |
127 |
| |
128 |
114
| updateOutputParameters();
|
129 |
| |
130 |
| |
131 |
114
| if (element != null)
|
132 |
| { |
133 |
44
| writer.begin(element);
|
134 |
44
| renderInformalParameters(writer, cycle);
|
135 |
| } |
136 |
| |
137 |
114
| renderBody(writer, cycle);
|
138 |
| |
139 |
114
| if (element != null)
|
140 |
44
| writer.end();
|
141 |
| |
142 |
114
| _index++;
|
143 |
| } |
144 |
| } |
145 |
| finally |
146 |
| { |
147 |
42
| _rendering = false;
|
148 |
42
| _value = null;
|
149 |
| } |
150 |
| } |
151 |
| |
152 |
| |
153 |
| |
154 |
| |
155 |
| |
156 |
| |
157 |
| |
158 |
| |
159 |
92
| public final Object getValue()
|
160 |
| { |
161 |
92
| if (!_rendering)
|
162 |
0
| throw Tapestry.createRenderOnlyPropertyException(this, "value");
|
163 |
| |
164 |
92
| return _value;
|
165 |
| } |
166 |
| |
167 |
| |
168 |
| |
169 |
| |
170 |
| |
171 |
| |
172 |
| |
173 |
| |
174 |
92
| public int getIndex()
|
175 |
| { |
176 |
92
| if (!_rendering)
|
177 |
0
| throw Tapestry.createRenderOnlyPropertyException(this, "index");
|
178 |
| |
179 |
92
| return _index;
|
180 |
| } |
181 |
| |
182 |
0
| public boolean isDisabled()
|
183 |
| { |
184 |
0
| return false;
|
185 |
| } |
186 |
| |
187 |
| |
188 |
| |
189 |
| |
190 |
114
| protected void updateOutputParameters()
|
191 |
| { |
192 |
114
| IBinding indexBinding = getBinding("index");
|
193 |
114
| if (indexBinding != null)
|
194 |
62
| indexBinding.setObject(new Integer(_index));
|
195 |
| |
196 |
114
| IBinding valueBinding = getBinding("value");
|
197 |
114
| if (valueBinding != null)
|
198 |
62
| valueBinding.setObject(_value);
|
199 |
| } |
200 |
| |
201 |
| |
202 |
| |
203 |
| |
204 |
10
| protected void updatePrimaryKeysParameter(String[] stringReps)
|
205 |
| { |
206 |
10
| IBinding primaryKeysBinding = getBinding("primaryKeys");
|
207 |
10
| if (primaryKeysBinding == null)
|
208 |
10
| return;
|
209 |
| |
210 |
0
| DataSqueezer squeezer = getDataSqueezer();
|
211 |
| |
212 |
0
| int repsCount = stringReps.length;
|
213 |
0
| List primaryKeys = new ArrayList(repsCount);
|
214 |
0
| for (int i = 0; i < stringReps.length; i++)
|
215 |
| { |
216 |
0
| String rep = stringReps[i];
|
217 |
0
| if (rep.length() == 0 || rep.charAt(0) != DESC_PRIMARY_KEY)
|
218 |
0
| continue;
|
219 |
0
| Object primaryKey = squeezer.unsqueeze(rep.substring(1));
|
220 |
0
| primaryKeys.add(primaryKey);
|
221 |
| } |
222 |
| |
223 |
0
| primaryKeysBinding.setObject(primaryKeys);
|
224 |
| } |
225 |
| |
226 |
| |
227 |
0
| protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
|
228 |
| { |
229 |
| } |
230 |
| |
231 |
0
| protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
|
232 |
| { |
233 |
| } |
234 |
| |
235 |
| |
236 |
| |
237 |
| |
238 |
| |
239 |
| |
240 |
| |
241 |
| |
242 |
| |
243 |
| |
244 |
| |
245 |
| |
246 |
| |
247 |
| |
248 |
| |
249 |
42
| private Iterator getData(IRequestCycle cycle, IForm form)
|
250 |
| { |
251 |
42
| if (form == null || getVolatile())
|
252 |
12
| return evaluateSourceIterator();
|
253 |
| |
254 |
30
| String name = form.getElementId(this);
|
255 |
30
| if (cycle.isRewinding())
|
256 |
10
| return getStoredData(cycle, name);
|
257 |
20
| return storeSourceData(form, name);
|
258 |
| } |
259 |
| |
260 |
| |
261 |
| |
262 |
| |
263 |
| |
264 |
| |
265 |
| |
266 |
| |
267 |
| |
268 |
| |
269 |
| |
270 |
10
| protected Iterator getStoredData(IRequestCycle cycle, String name)
|
271 |
| { |
272 |
10
| String[] stringReps = cycle.getParameters(name);
|
273 |
10
| if (stringReps == null)
|
274 |
0
| return null;
|
275 |
| |
276 |
10
| updatePrimaryKeysParameter(stringReps);
|
277 |
| |
278 |
10
| return new ReadSourceDataIterator(stringReps);
|
279 |
| } |
280 |
| |
281 |
| |
282 |
| |
283 |
| |
284 |
| |
285 |
| private class ReadSourceDataIterator implements Iterator |
286 |
| { |
287 |
| private final Iterator _sourceIterator = evaluateSourceIterator(); |
288 |
| |
289 |
| private final Iterator _fullSourceIterator = evaluateFullSourceIterator(); |
290 |
| |
291 |
| private final String[] _stringReps; |
292 |
| |
293 |
| private int _index = 0; |
294 |
| |
295 |
| private final Map _repToValueMap = new HashMap(); |
296 |
| |
297 |
10
| ReadSourceDataIterator(String[] stringReps)
|
298 |
| { |
299 |
10
| _stringReps = stringReps;
|
300 |
| } |
301 |
| |
302 |
32
| public boolean hasNext()
|
303 |
| { |
304 |
32
| return _index < _stringReps.length;
|
305 |
| } |
306 |
| |
307 |
22
| public Object next()
|
308 |
| { |
309 |
22
| String rep = _stringReps[_index++];
|
310 |
| |
311 |
22
| return getValueFromStringRep(_sourceIterator, _fullSourceIterator, _repToValueMap, rep);
|
312 |
| } |
313 |
| |
314 |
0
| public void remove()
|
315 |
| { |
316 |
0
| throw new UnsupportedOperationException("remove()");
|
317 |
| } |
318 |
| |
319 |
| } |
320 |
| |
321 |
| |
322 |
| |
323 |
| |
324 |
| |
325 |
| |
326 |
| |
327 |
| |
328 |
| |
329 |
| |
330 |
| |
331 |
20
| protected Iterator storeSourceData(IForm form, String name)
|
332 |
| { |
333 |
20
| return new StoreSourceDataIterator(form, name, evaluateSourceIterator());
|
334 |
| } |
335 |
| |
336 |
| |
337 |
| |
338 |
| |
339 |
| |
340 |
| |
341 |
| private class StoreSourceDataIterator implements Iterator |
342 |
| { |
343 |
| private final IForm _form; |
344 |
| |
345 |
| private final String _name; |
346 |
| |
347 |
| private final Iterator _delegate; |
348 |
| |
349 |
20
| StoreSourceDataIterator(IForm form, String name, Iterator delegate)
|
350 |
| { |
351 |
20
| _form = form;
|
352 |
20
| _name = name;
|
353 |
20
| _delegate = delegate;
|
354 |
| } |
355 |
| |
356 |
60
| public boolean hasNext()
|
357 |
| { |
358 |
60
| return _delegate.hasNext();
|
359 |
| } |
360 |
| |
361 |
40
| public Object next()
|
362 |
| { |
363 |
40
| Object value = _delegate.next();
|
364 |
| |
365 |
40
| String rep = getStringRepFromValue(value);
|
366 |
| |
367 |
40
| _form.addHiddenValue(_name, rep);
|
368 |
| |
369 |
40
| return value;
|
370 |
| } |
371 |
| |
372 |
0
| public void remove()
|
373 |
| { |
374 |
0
| throw new UnsupportedOperationException("remove()");
|
375 |
| } |
376 |
| } |
377 |
| |
378 |
| |
379 |
| |
380 |
| |
381 |
| |
382 |
| |
383 |
| |
384 |
| |
385 |
112
| protected String getStringRepFromValue(Object value)
|
386 |
| { |
387 |
112
| String rep;
|
388 |
112
| DataSqueezer squeezer = getDataSqueezer();
|
389 |
| |
390 |
| |
391 |
112
| Object pk = getPrimaryKeyFromValue(value);
|
392 |
112
| if (pk != null)
|
393 |
| |
394 |
70
| rep = DESC_PRIMARY_KEY + squeezer.squeeze(pk);
|
395 |
| else |
396 |
| |
397 |
42
| rep = DESC_VALUE + squeezer.squeeze(value);
|
398 |
| |
399 |
112
| return rep;
|
400 |
| } |
401 |
| |
402 |
| |
403 |
| |
404 |
| |
405 |
| |
406 |
| |
407 |
| |
408 |
| |
409 |
| |
410 |
112
| protected Object getPrimaryKeyFromValue(Object value)
|
411 |
| { |
412 |
112
| if (value == null)
|
413 |
0
| return null;
|
414 |
| |
415 |
112
| Object primaryKey = getKeyExpressionFromValue(value);
|
416 |
112
| if (primaryKey == null)
|
417 |
42
| primaryKey = getConverterFromValue(value);
|
418 |
| |
419 |
112
| return primaryKey;
|
420 |
| } |
421 |
| |
422 |
| |
423 |
| |
424 |
| |
425 |
| |
426 |
| |
427 |
| |
428 |
| |
429 |
| |
430 |
112
| protected Object getKeyExpressionFromValue(Object value)
|
431 |
| { |
432 |
112
| String keyExpression = getKeyExpression();
|
433 |
112
| if (keyExpression == null)
|
434 |
42
| return null;
|
435 |
| |
436 |
70
| Object primaryKey = getExpressionEvaluator().read(value, keyExpression);
|
437 |
70
| return primaryKey;
|
438 |
| } |
439 |
| |
440 |
| |
441 |
| |
442 |
| |
443 |
| |
444 |
| |
445 |
| |
446 |
| |
447 |
| |
448 |
42
| protected Object getConverterFromValue(Object value)
|
449 |
| { |
450 |
42
| IPrimaryKeyConverter converter = getConverter();
|
451 |
42
| if (converter == null)
|
452 |
42
| return null;
|
453 |
| |
454 |
0
| Object primaryKey = converter.getPrimaryKey(value);
|
455 |
0
| return primaryKey;
|
456 |
| } |
457 |
| |
458 |
| |
459 |
| |
460 |
| |
461 |
| |
462 |
| |
463 |
| |
464 |
| |
465 |
| |
466 |
| |
467 |
22
| protected Object getValueFromStringRep(Iterator sourceIterator, Iterator fullSourceIterator,
|
468 |
| Map repToValueMap, String rep) |
469 |
| { |
470 |
22
| Object value = null;
|
471 |
22
| DataSqueezer squeezer = getDataSqueezer();
|
472 |
| |
473 |
| |
474 |
22
| if (rep == null || rep.length() == 0)
|
475 |
0
| return getDefaultValue();
|
476 |
| |
477 |
| |
478 |
22
| boolean match = getMatch();
|
479 |
22
| if (match)
|
480 |
| { |
481 |
18
| value = findValueWithStringRep(
|
482 |
| sourceIterator, |
483 |
| fullSourceIterator, |
484 |
| repToValueMap, |
485 |
| rep, |
486 |
| COMPLETE_REP_SOURCE); |
487 |
18
| if (value != null)
|
488 |
14
| return value;
|
489 |
| } |
490 |
| |
491 |
| |
492 |
| |
493 |
8
| char desc = rep.charAt(0);
|
494 |
8
| String squeezed = rep.substring(1);
|
495 |
8
| switch (desc)
|
496 |
| { |
497 |
6
| case DESC_VALUE:
|
498 |
| |
499 |
6
| value = squeezer.unsqueeze(squeezed);
|
500 |
6
| break;
|
501 |
| |
502 |
2
| case DESC_PRIMARY_KEY:
|
503 |
| |
504 |
2
| if (!match && getKeyExpression() != null)
|
505 |
0
| value = findValueWithStringRep(
|
506 |
| sourceIterator, |
507 |
| fullSourceIterator, |
508 |
| repToValueMap, |
509 |
| rep, |
510 |
| KEY_EXPRESSION_REP_SOURCE); |
511 |
| |
512 |
| |
513 |
2
| if (value == null)
|
514 |
| { |
515 |
2
| IPrimaryKeyConverter converter = getConverter();
|
516 |
2
| if (converter != null)
|
517 |
| { |
518 |
0
| Object pk = squeezer.unsqueeze(squeezed);
|
519 |
0
| value = converter.getValue(pk);
|
520 |
| } |
521 |
| } |
522 |
2
| break;
|
523 |
| } |
524 |
| |
525 |
8
| if (value == null)
|
526 |
2
| value = getDefaultValue();
|
527 |
| |
528 |
8
| return value;
|
529 |
| } |
530 |
| |
531 |
| |
532 |
| |
533 |
| |
534 |
| |
535 |
| |
536 |
| |
537 |
| |
538 |
| |
539 |
| |
540 |
| |
541 |
| |
542 |
| |
543 |
18
| protected Object findValueWithStringRep(Iterator sourceIterator, Iterator fullSourceIterator,
|
544 |
| Map repToValueMap, String rep, RepSource repSource) |
545 |
| { |
546 |
18
| Object value = repToValueMap.get(rep);
|
547 |
18
| if (value != null)
|
548 |
2
| return value;
|
549 |
| |
550 |
16
| value = findValueWithStringRepInIterator(sourceIterator, repToValueMap, rep, repSource);
|
551 |
16
| if (value != null)
|
552 |
8
| return value;
|
553 |
| |
554 |
8
| value = findValueWithStringRepInIterator(fullSourceIterator, repToValueMap, rep, repSource);
|
555 |
8
| return value;
|
556 |
| } |
557 |
| |
558 |
| |
559 |
| |
560 |
| |
561 |
| |
562 |
| |
563 |
| |
564 |
| |
565 |
| |
566 |
| |
567 |
| |
568 |
| |
569 |
| |
570 |
| |
571 |
| |
572 |
24
| protected Object findValueWithStringRepInIterator(Iterator it, Map repToValueMap, String rep,
|
573 |
| RepSource repSource) |
574 |
| { |
575 |
24
| while (it.hasNext())
|
576 |
| { |
577 |
72
| Object sourceValue = it.next();
|
578 |
72
| if (sourceValue == null)
|
579 |
0
| continue;
|
580 |
| |
581 |
72
| String sourceRep = repSource.getStringRep(sourceValue);
|
582 |
72
| repToValueMap.put(sourceRep, sourceValue);
|
583 |
| |
584 |
72
| if (rep.equals(sourceRep))
|
585 |
12
| return sourceValue;
|
586 |
| } |
587 |
| |
588 |
12
| return null;
|
589 |
| } |
590 |
| |
591 |
| |
592 |
| |
593 |
| |
594 |
| |
595 |
| |
596 |
42
| protected Iterator evaluateSourceIterator()
|
597 |
| { |
598 |
42
| Iterator it = null;
|
599 |
42
| Object source = null;
|
600 |
| |
601 |
42
| IBinding sourceBinding = getBinding("source");
|
602 |
42
| if (sourceBinding != null)
|
603 |
42
| source = sourceBinding.getObject();
|
604 |
| |
605 |
42
| if (source != null)
|
606 |
42
| it = (Iterator) getValueConverter().coerceValue(source, Iterator.class);
|
607 |
| |
608 |
42
| if (it == null)
|
609 |
0
| it = Collections.EMPTY_LIST.iterator();
|
610 |
| |
611 |
42
| return it;
|
612 |
| } |
613 |
| |
614 |
| |
615 |
| |
616 |
| |
617 |
| |
618 |
| |
619 |
10
| protected Iterator evaluateFullSourceIterator()
|
620 |
| { |
621 |
10
| Iterator it = null;
|
622 |
10
| Object fullSource = null;
|
623 |
| |
624 |
10
| IBinding fullSourceBinding = getBinding("fullSource");
|
625 |
10
| if (fullSourceBinding != null)
|
626 |
4
| fullSource = fullSourceBinding.getObject();
|
627 |
| |
628 |
10
| if (fullSource != null)
|
629 |
4
| it = (Iterator) getValueConverter().coerceValue(fullSource, Iterator.class);
|
630 |
| |
631 |
10
| if (it == null)
|
632 |
6
| it = Collections.EMPTY_LIST.iterator();
|
633 |
| |
634 |
10
| return it;
|
635 |
| } |
636 |
| |
637 |
| |
638 |
| |
639 |
| |
640 |
| protected interface RepSource |
641 |
| { |
642 |
| String getStringRep(Object value); |
643 |
| } |
644 |
| |
645 |
| |
646 |
| |
647 |
| |
648 |
| |
649 |
| protected class CompleteRepSource implements RepSource |
650 |
| { |
651 |
72
| public String getStringRep(Object value)
|
652 |
| { |
653 |
72
| return getStringRepFromValue(value);
|
654 |
| } |
655 |
| } |
656 |
| |
657 |
| |
658 |
| |
659 |
| |
660 |
| |
661 |
| protected class KeyExpressionRepSource implements RepSource |
662 |
| { |
663 |
0
| public String getStringRep(Object value)
|
664 |
| { |
665 |
0
| Object pk = getKeyExpressionFromValue(value);
|
666 |
0
| return DESC_PRIMARY_KEY + getDataSqueezer().squeeze(pk);
|
667 |
| } |
668 |
| } |
669 |
| |
670 |
| |
671 |
| |
672 |
| |
673 |
0
| protected boolean getCanTakeFocus()
|
674 |
| { |
675 |
0
| return false;
|
676 |
| } |
677 |
| |
678 |
0
| public String getClientId()
|
679 |
| { |
680 |
0
| return null;
|
681 |
| } |
682 |
| |
683 |
0
| public String getDisplayName()
|
684 |
| { |
685 |
0
| return null;
|
686 |
| } |
687 |
| |
688 |
| } |