Changeset 913 for trunk/grails-app/conf/BootStrap.groovy
- Timestamp:
- May 1, 2011, 12:46:31 PM (14 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
-
Property
svn:mergeinfo
set to
/branches/features/grailsUpgrade merged eligible
-
Property
svn:mergeinfo
set to
-
trunk/grails-app/conf/BootStrap.groovy
r814 r913 1 1 class BootStrap 2 2 { 3 4 def sessionFactory // used by isDirty().5 def grailsApplication // used by isDirty().6 3 def createDataService 7 4 8 5 def init = { servletContext -> 9 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.html13 */14 addDirtyCheckMethods()15 6 16 7 /** Environment specific settings. … … 46 37 } 47 38 48 private void addDirtyCheckMethods() {49 for (dc in grailsApplication.domainClasses) {50 addDirtyCheckMethods dc.clazz.metaClass51 }52 }53 54 private void addDirtyCheckMethods(metaClass) {55 metaClass.isDirty = { ->56 def session = sessionFactory.currentSession57 def entry = findEntityEntry(delegate, session)58 if (!entry) {59 return false60 }61 62 Object[] values = entry.persister.getPropertyValues(delegate, session.entityMode)63 def dirtyProperties = entry.persister.findDirty(values, entry.loadedState, delegate, session)64 return dirtyProperties != null65 }66 67 metaClass.isDirty = { String fieldName ->68 def session = sessionFactory.currentSession69 def entry = findEntityEntry(delegate, session)70 if (!entry) {71 return false72 }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 dirtyProperties78 }79 80 metaClass.getDirtyPropertyNames = { ->81 def session = sessionFactory.currentSession82 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 names94 }95 96 metaClass.getPersistentValue = { String fieldName ->97 def session = sessionFactory.currentSession98 def entry = findEntityEntry(delegate, session, false)99 if (!entry) {100 return null101 }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 null112 }113 114 if (forDirtyCheck && !entry.requiresDirtyCheck(instance) && entry.loadedState) {115 return null116 }117 118 entry119 }120 121 39 } // end class
Note: See TracChangeset
for help on using the changeset viewer.