Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ import org.grails.datastore.mapping.engine.internal.MappingUtils
import org.grails.datastore.mapping.model.EmbeddedPersistentEntity
import org.grails.datastore.mapping.model.PersistentEntity
import org.grails.datastore.mapping.model.PersistentProperty
import org.grails.datastore.mapping.model.config.GormProperties
import org.grails.datastore.mapping.model.types.Association
import org.grails.datastore.mapping.model.types.Embedded
import org.grails.datastore.mapping.model.types.EmbeddedCollection
Expand Down Expand Up @@ -249,12 +248,6 @@ class PersistentEntityCodec extends BsonPersistentEntityCodec {
}

}
else {
// schedule lastUpdated if necessary
if (entity.getPropertyByName(GormProperties.LAST_UPDATED) != null) {
dirtyProperties.add(GormProperties.LAST_UPDATED)
}
}

for (propertyName in dirtyProperties) {
def prop = entity.getPropertyByName(propertyName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEvent;
import org.springframework.util.ReflectionUtils;

import grails.gorm.annotation.AutoTimestamp;
import org.grails.datastore.gorm.timestamp.DefaultTimestampProvider;
import org.grails.datastore.gorm.timestamp.TimestampProvider;
import org.grails.datastore.mapping.config.Entity;
import org.grails.datastore.mapping.config.Settings;
import org.grails.datastore.mapping.core.Datastore;
import org.grails.datastore.mapping.dirty.checking.DirtyCheckable;
import org.grails.datastore.mapping.engine.EntityAccess;
import org.grails.datastore.mapping.engine.event.AbstractPersistenceEvent;
import org.grails.datastore.mapping.engine.event.AbstractPersistenceEventListener;
Expand Down Expand Up @@ -148,10 +150,25 @@ private void initializeIfNecessary(PersistentEntity entity, String name) {
public boolean beforeUpdate(PersistentEntity entity, EntityAccess ea) {
Set<String> props = getLastUpdatedPropertyNames(entity.getName());
if (props != null) {
Object entityObject = ea.getEntity();
boolean isDirtyCheckable = entityObject instanceof DirtyCheckable;

// For dirty-checking datastores (e.g., MongoDB), only set autotimestamp if entity has dirty properties
if (isDirtyCheckable) {
List<String> dirtyPropertyNames = ((DirtyCheckable) entityObject).listDirtyPropertyNames();
if (dirtyPropertyNames.isEmpty()) {
return true;
}
}

for (String prop : props) {
Class<?> lastUpdateType = ea.getPropertyType(prop);
Object timestamp = timestampProvider.createTimestamp(lastUpdateType);
ea.setProperty(prop, timestamp);
// Mark property as dirty for datastores that use dirty checking (e.g., MongoDB)
if (isDirtyCheckable) {
((DirtyCheckable) entityObject).markDirty(prop);
}
}
}
return true;
Expand All @@ -167,18 +184,6 @@ protected Set<String> getDateCreatedPropertyNames(String entityName) {
return properties == null ? null : properties.orElse(null);
}

private static Field getFieldFromHierarchy(Class<?> entity, String fieldName) {
Class<?> clazz = entity;
while (clazz != null) {
try {
return clazz.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
clazz = clazz.getSuperclass();
}
}
return null;
}

protected void storeDateCreatedAndLastUpdatedInfo(PersistentEntity persistentEntity) {
if (persistentEntity.isInitialized()) {
ClassMapping<?> classMapping = persistentEntity.getMapping();
Expand All @@ -190,13 +195,15 @@ protected void storeDateCreatedAndLastUpdatedInfo(PersistentEntity persistentEnt
} else if (property.getName().equals(DATE_CREATED_PROPERTY)) {
storeTimestampAvailability(entitiesWithDateCreated, persistentEntity, property);
} else {
Field field = getFieldFromHierarchy(persistentEntity.getJavaClass(), property.getName());
if (field != null && field.isAnnotationPresent(AutoTimestamp.class)) {
AutoTimestamp autoTimestamp = field.getAnnotation(AutoTimestamp.class);
if (autoTimestamp.value() == AutoTimestamp.EventType.UPDATED) {
storeTimestampAvailability(entitiesWithLastUpdated, persistentEntity, property);
} else {
storeTimestampAvailability(entitiesWithDateCreated, persistentEntity, property);
Field field = ReflectionUtils.findField(persistentEntity.getJavaClass(), property.getName());
if (field != null) {
if (field.isAnnotationPresent(AutoTimestamp.class)) {
AutoTimestamp autoTimestamp = field.getAnnotation(AutoTimestamp.class);
if (autoTimestamp.value() == AutoTimestamp.EventType.UPDATED) {
storeTimestampAvailability(entitiesWithLastUpdated, persistentEntity, property);
} else {
storeTimestampAvailability(entitiesWithDateCreated, persistentEntity, property);
}
}
}
}
Expand Down
Loading