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

Last change on this file since 1430 was 1430, checked in by work@…, 10 years ago
  • set keyword expansion
  • Property svn:keywords set to Rev Author Date
File size: 2.0 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: 1430 $
40 * $Author: work@osx.eu $
41 * $Date: 2011-01-21 20:05:36 +0000 (vr, 21 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         * Method to increment the static iterator variable. This method
54         * is synchronized to assure a thread-safe increment.
55         * @visibility private
56         * @void
57         */
58        synchronized final private void setIdentifier() {
59                // increment the iterator variable
60                // reset iterator to 1 if it is becoming too high
61                iterator = (iterator >= maximumIdentity) ? 1 : iterator+1
62
63                // set the instance identifier
64                identifier = iterator
65        }
66
67        /**
68         * Return the identifier
69         * @visibility public
70         * @return int
71         */
72        final public int getIdentifier() {
73                // set identifier if not yet set
74                if (!identifier) setIdentifier()
75
76                return identifier
77        }
78}
Note: See TracBrowser for help on using the repository browser.