1 | package dbnp.studycapturing |
---|
2 | |
---|
3 | /** |
---|
4 | * Identity Domain Class |
---|
5 | * |
---|
6 | * This class keeps an internal incremented static identity |
---|
7 | * which can be used to identify objects. This is particularly |
---|
8 | * handy for usage in large dynamic web forms. You could do |
---|
9 | * something like: |
---|
10 | * |
---|
11 | * <g:each var="subject" in="${Subject.findAll()}"> |
---|
12 | * <g:each var="field" in="${Subject.giveFields}"> |
---|
13 | * <g:textField name="subject_${subject.getIdentifier()}_${field.escapedName()}" ... /> |
---|
14 | * </g:each> |
---|
15 | * </g:each> |
---|
16 | * |
---|
17 | * So you can easily handle the post data in the controller |
---|
18 | * without relying on an iterator of your own as this proves |
---|
19 | * to be very unreliable and quite some extra code and effort |
---|
20 | * in both controller and views: |
---|
21 | * |
---|
22 | * Subject.findAll().each() { subject-> |
---|
23 | * subject.giveFields() { field-> |
---|
24 | * subject.setFieldValue( |
---|
25 | * field.name, |
---|
26 | * params.get('subject_${subject.getIdentifier()}_${field.escapedName()') |
---|
27 | * ) |
---|
28 | * } |
---|
29 | * if (!subject.validate()) { .... } |
---|
30 | * } |
---|
31 | * |
---|
32 | * Comparing the internal object identifier makes things a lot |
---|
33 | * easier. |
---|
34 | * |
---|
35 | * @Author Jeroen Wesbeek <J****n.W*****k@gmail.com> |
---|
36 | * @Since 20100805 |
---|
37 | * |
---|
38 | * Revision information: |
---|
39 | * $Rev: 786 $ |
---|
40 | * $Author: duh $ |
---|
41 | * $Date: 2010-08-06 10:33:12 +0000 (vr, 06 aug 2010) $ |
---|
42 | */ |
---|
43 | abstract class Identity implements Serializable { |
---|
44 | // keep an internal identifier for use in dynamic forms |
---|
45 | private int identifier = 0 |
---|
46 | private int maximumIdentity = 99999 |
---|
47 | static int iterator = 0 |
---|
48 | |
---|
49 | // transients |
---|
50 | static transients = [ "identifier", "iterator", "maximumIdentity" ] |
---|
51 | |
---|
52 | /** |
---|
53 | * Class constructor increments that static iterator |
---|
54 | * and sets the object's identifier (used in dynamic webforms) |
---|
55 | * @void |
---|
56 | */ |
---|
57 | public Identity() { |
---|
58 | // does this instance have an identifier? |
---|
59 | if (!identifier) { |
---|
60 | // no, increment the iterator |
---|
61 | identifier = iterator++ |
---|
62 | |
---|
63 | // has the iterator become too large? |
---|
64 | if (iterator >= maximumIdentity) { |
---|
65 | // yes, reset it back to 0 as this iterator |
---|
66 | // works for the complete application, not |
---|
67 | // only the user session. We don't want it |
---|
68 | // to go too high :) |
---|
69 | iterator = 0 |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | println ".instantiating [" + super.getClass() + "] ("+ identifier + ")" |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * Return the identifier |
---|
78 | * @return int |
---|
79 | */ |
---|
80 | final public int getIdentifier() { |
---|
81 | return identifier |
---|
82 | } |
---|
83 | } |
---|