root/trunk/grails-app/domain/dbnp/studycapturing/Identity.groovy @ 936

Revision 936, 2.3 KB (checked in by t.w.abma@…, 4 years ago)

- Nimble removed, AAAA-plugin installed (based on Spring Security), login panel needs fix still, please login using gscf/login

  • Property svn:keywords set to Author Rev Date
Line 
1package dbnp.studycapturing
2
3import org.nmcdsp.plugins.aaaa.Entity
4
5/**
6 * Identity Domain Class
7 *
8 * This class keeps an internal incremented static identity
9 * which can be used to identify objects. This is particularly
10 * handy for usage in large dynamic web forms. You could do
11 * something like:
12 *
13 * <g:each var="subject" in="${Subject.findAll()}">
14 *   <g:each var="field" in="${Subject.giveFields}">
15 *              <g:textField name="subject_${subject.getIdentifier()}_${field.escapedName()}" ... />
16 *   </g:each>
17 * </g:each>
18 *
19 * So you can easily handle the post data in the controller
20 * without relying on an iterator of your own as this proves
21 * to be very unreliable and quite some extra code and effort
22 * in both controller and views:
23 *
24 * Subject.findAll().each() { subject->
25 *       subject.giveFields() { field->
26 *              subject.setFieldValue(
27 *                      field.name,
28 *                      params.get('subject_${subject.getIdentifier()}_${field.escapedName()')
29 *              )
30 *       }
31 *       if (!subject.validate()) { .... }
32 * }
33 *
34 * Comparing the internal object identifier makes things a lot
35 * easier.
36 *
37 * @Author      Jeroen Wesbeek  <J****n.W*****k@gmail.com>
38 * @Since       20100805
39 *
40 * Revision information:
41 * $Rev$
42 * $Author$
43 * $Date$
44 */
45abstract class Identity extends Entity implements Serializable {
46        // keep an internal identifier for use in dynamic forms
47        private int identifier = 0
48        private int maximumIdentity = 99999
49        static int iterator = 0
50
51        // transients
52        static transients = [ "identifier", "iterator", "maximumIdentity" ]
53
54        /**
55         * Class constructor increments that static iterator
56         * and sets the object's identifier
57         * @visibility public
58         * @void
59         */
60        public Identity() {
61                // set the local identifier
62                setIdentifier()
63
64                // feedback
65                println ".instantiating [${identifier}:${super.getClass()}]"
66        }
67
68        /**
69         * Method to increment the static iterator variable. This method
70         * is synchronized to assure a thread-safe increment.
71         * @visibility private
72         * @void
73         */
74        synchronized final private void setIdentifier() {
75                // increment the iterator variable
76                // reset iterator to 1 if it is becoming too high
77                iterator = (iterator >= maximumIdentity) ? 1 : iterator+1
78
79                // set the instance identifier
80                identifier = iterator
81        }
82
83        /**
84         * Return the identifier
85         * @visibility public
86         * @return int
87         */
88        final public int getIdentifier() {
89                return identifier
90        }
91}
Note: See TracBrowser for help on using the browser.