Search in sources :

Example 1 with CascadeStyle

use of org.hibernate.engine.CascadeStyle in project Gemma by PavlidisLab.

the class AuditAdvice method processAssociations.

/**
 * Fills in audit trails on newly created child objects after a 'create' or 'update'. It does not add 'update'
 * events on the child objects.
 * Thus if the update is on an expression experiment that has a new Characteristic, the Characteristic will have a
 * 'create' event, and the EEE will get an added update event (via the addUpdateAuditEvent call elsewhere, not here)
 *
 * @see AclAdvice for similar code for ACLs
 */
private void processAssociations(String methodName, Object object, User user) {
    if (object instanceof AuditTrail)
        // don't audit audit trails.
        return;
    EntityPersister persister = crudUtils.getEntityPersister(object);
    if (persister == null) {
        throw new IllegalArgumentException("No persister found for " + object.getClass().getName());
    }
    CascadeStyle[] cascadeStyles = persister.getPropertyCascadeStyles();
    String[] propertyNames = persister.getPropertyNames();
    try {
        for (int j = 0; j < propertyNames.length; j++) {
            CascadeStyle cs = cascadeStyles[j];
            String propertyName = propertyNames[j];
            if (!this.specialCaseForAssociationFollow(object, propertyName) && (this.canSkipAssociationCheck(object, propertyName) || !crudUtils.needCascade(methodName, cs))) {
                continue;
            }
            PropertyDescriptor descriptor = BeanUtils.getPropertyDescriptor(object.getClass(), propertyName);
            Object associatedObject = ReflectionUtil.getProperty(object, descriptor);
            if (associatedObject == null)
                continue;
            Class<?> propertyType = descriptor.getPropertyType();
            if (AbstractAuditable.class.isAssignableFrom(propertyType)) {
                AbstractAuditable auditable = (AbstractAuditable) associatedObject;
                try {
                    this.maybeAddCascadeCreateEvent(object, auditable, user);
                    this.processAssociations(methodName, auditable, user);
                } catch (LazyInitializationException e) {
                    // be necessary.
                    if (AuditAdvice.log.isDebugEnabled())
                        AuditAdvice.log.debug("Caught lazy init error while processing " + auditable + ": " + e.getMessage() + " - skipping creation of cascade event.");
                }
            } else if (Collection.class.isAssignableFrom(propertyType)) {
                Collection<?> associatedObjects = (Collection<?>) associatedObject;
                try {
                    Hibernate.initialize(associatedObjects);
                    for (Object collectionMember : associatedObjects) {
                        if (AbstractAuditable.class.isAssignableFrom(collectionMember.getClass())) {
                            AbstractAuditable auditable = (AbstractAuditable) collectionMember;
                            try {
                                Hibernate.initialize(auditable);
                                this.maybeAddCascadeCreateEvent(object, auditable, user);
                                this.processAssociations(methodName, collectionMember, user);
                            } catch (LazyInitializationException e) {
                                if (AuditAdvice.log.isDebugEnabled())
                                    AuditAdvice.log.debug("Caught lazy init error while processing " + auditable + ": " + e.getMessage() + " - skipping creation of cascade event.");
                            // If this happens, it means the object can't be 'new' so adding audit trail can't
                            // be necessary. But keep checking.
                            }
                        }
                    }
                } catch (LazyInitializationException e) {
                    // be necessary.
                    if (AuditAdvice.log.isDebugEnabled())
                        AuditAdvice.log.debug("Caught lazy init error while processing " + object + ": " + e.getMessage() + " - skipping creation of cascade event.");
                }
            }
        }
    } catch (IllegalAccessException | InvocationTargetException e) {
        throw new RuntimeException(e);
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) CascadeStyle(org.hibernate.engine.CascadeStyle) PropertyDescriptor(java.beans.PropertyDescriptor) AbstractAuditable(ubic.gemma.model.common.AbstractAuditable) JoinPoint(org.aspectj.lang.JoinPoint) InvocationTargetException(java.lang.reflect.InvocationTargetException) LazyInitializationException(org.hibernate.LazyInitializationException) Collection(java.util.Collection) AuditTrail(ubic.gemma.model.common.auditAndSecurity.AuditTrail)

Aggregations

PropertyDescriptor (java.beans.PropertyDescriptor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Collection (java.util.Collection)1 JoinPoint (org.aspectj.lang.JoinPoint)1 LazyInitializationException (org.hibernate.LazyInitializationException)1 CascadeStyle (org.hibernate.engine.CascadeStyle)1 EntityPersister (org.hibernate.persister.entity.EntityPersister)1 AbstractAuditable (ubic.gemma.model.common.AbstractAuditable)1 AuditTrail (ubic.gemma.model.common.auditAndSecurity.AuditTrail)1