source: trunk/grails-app/domain/dbnp/studycapturing/Identity.groovy @ 1350

Last change on this file since 1350 was 1350, checked in by work@…, 10 years ago
  • fix for Javassist issue #254, needs some thorough testing...
  • Property svn:keywords set to Author Date Rev
File size: 2.3 KB
Line 
1package 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: 1350 $
40 * $Author: work@osx.eu $
41 * $Date: 2011-01-07 15:09:49 +0000 (vr, 07 jan 2011) $
42 */
43abstract 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
55         * @visibility public
56         * @void
57        public Identity() {
58                // set the local identifier
59                setIdentifier()
60
61                // debug info
62                //log.debug ".instantiating [${identifier}:${super.getClass()}]"
63        }
64         */
65
66        /**
67         * Method to increment the static iterator variable. This method
68         * is synchronized to assure a thread-safe increment.
69         * @visibility private
70         * @void
71         */
72        synchronized final private void setIdentifier() {
73                // increment the iterator variable
74                // reset iterator to 1 if it is becoming too high
75                iterator = (iterator >= maximumIdentity) ? 1 : iterator+1
76
77                // set the instance identifier
78                identifier = iterator
79        }
80
81        /**
82         * Return the identifier
83         * @visibility public
84         * @return int
85         */
86        final public int getIdentifier() {
87                // set identifier if not yet set
88                if (!identifier) setIdentifier()
89
90                return identifier
91        }
92}
Note: See TracBrowser for help on using the repository browser.