source: trunk/grails-app/conf/BootStrap.groovy @ 901

Last change on this file since 901 was 814, checked in by gav, 14 years ago

Add isDirty() during bootstrap, must be removed after upgrading to Grails-1.3 >.

File size: 4.1 KB
RevLine 
[149]1class BootStrap
[58]2{
[814]3
4    def sessionFactory // used by isDirty().
5    def grailsApplication // used by isDirty().
[149]6    def createDataService
[58]7
8    def init = { servletContext ->
9
[814]10        /** Add isDirty() to domainClasses.
11        * @todo remove after upgrading to Grails-1.3 >.
12        * See: http://grails.1312388.n4.nabble.com/beforeUpdate-Compare-with-previous-set-value-isDirty-not-working-in-Events-td1695661.html
13        */
14        addDirtyCheckMethods()
15
[504]16        /** Environment specific settings.
17        * Note that (circa grails-1.2.2) if running "test test-app integration:" from "grails interactive"
18        * the correct environment is applied for DataSource and Config,
19        * but for BootStrap (and others) the development environment is applied.
20        */
21        environments {
22            development {
23                log.info "Starting DEVELOPMENT bootstrap."
24                createDataService.ensureSystemAndAdminAccess()
25                createDataService.createBaseData()
26                createDataService.createDemoData()
[622]27                createDataService.startSearchableIndex()
[504]28            }
29            test {
30                log.info "Starting TEST bootstrap."
31                createDataService.ensureSystemAndAdminAccess()
[508]32                createDataService.createBaseData()
[622]33                createDataService.startSearchableIndex(false)
[504]34            }
35            production {
36                log.info "Starting PRODUCTION bootstrap."
37                createDataService.ensureSystemAndAdminAccess()
[508]38                createDataService.createBaseData()
[622]39                createDataService.startSearchableIndex()
[504]40            }
[58]41        }
[149]42
[504]43    } // init
[58]44
45    def destroy = {
46    }
[504]47
[814]48   private void addDirtyCheckMethods() {
49      for (dc in grailsApplication.domainClasses) {
50         addDirtyCheckMethods dc.clazz.metaClass
51      }
52   }
53
54    private void addDirtyCheckMethods(metaClass) {
55      metaClass.isDirty = { ->
56         def session = sessionFactory.currentSession
57         def entry = findEntityEntry(delegate, session)
58         if (!entry) {
59            return false
60         }
61
62         Object[] values = entry.persister.getPropertyValues(delegate, session.entityMode)
63         def dirtyProperties = entry.persister.findDirty(values, entry.loadedState, delegate, session)
64         return dirtyProperties != null
65      }
66
67      metaClass.isDirty = { String fieldName ->
68         def session = sessionFactory.currentSession
69         def entry = findEntityEntry(delegate, session)
70         if (!entry) {
71            return false
72         }
73
74         Object[] values = entry.persister.getPropertyValues(delegate, session.entityMode)
75         int[] dirtyProperties = entry.persister.findDirty(values, entry.loadedState, delegate, session)
76         int fieldIndex = entry.persister.propertyNames.findIndexOf { fieldName == it }
77         return fieldIndex in dirtyProperties
78      }
79
80      metaClass.getDirtyPropertyNames = { ->
81         def session = sessionFactory.currentSession
82         def entry = findEntityEntry(delegate, session)
83         if (!entry) {
84            return []
85         }
86
87         Object[] values = entry.persister.getPropertyValues(delegate, session.entityMode)
88         int[] dirtyProperties = entry.persister.findDirty(values, entry.loadedState, delegate, session)
89         def names = []
90         for (index in dirtyProperties) {
91            names << entry.persister.propertyNames[index]
92         }
93         names
94      }
95
96      metaClass.getPersistentValue = { String fieldName ->
97         def session = sessionFactory.currentSession
98         def entry = findEntityEntry(delegate, session, false)
99         if (!entry) {
100            return null
101         }
102
103         int fieldIndex = entry.persister.propertyNames.findIndexOf { fieldName == it }
104         return fieldIndex == -1 ? null : entry.loadedState[fieldIndex]
105      }
106   }
107
108   private findEntityEntry(instance, session, boolean forDirtyCheck = true) {
109      def entry = session.persistenceContext.getEntry(instance)
110      if (!entry) {
111         return null
112      }
113
114      if (forDirtyCheck && !entry.requiresDirtyCheck(instance) && entry.loadedState) {
115         return null
116      }
117
118      entry
119   }
120
[504]121} // end class
Note: See TracBrowser for help on using the repository browser.