Example usage for org.hibernate.persister.entity EntityPersister getPropertyCascadeStyles

List of usage examples for org.hibernate.persister.entity EntityPersister getPropertyCascadeStyles

Introduction

In this page you can find the example usage for org.hibernate.persister.entity EntityPersister getPropertyCascadeStyles.

Prototype

CascadeStyle[] getPropertyCascadeStyles();

Source Link

Document

Get the cascade styles of the properties (optional operation)

Usage

From source file:com.miranteinfo.seam.hibernate.HibernateCascade.java

License:Open Source License

/**
 * Cascade an action from the parent entity instance to all its children.  This
 * form is typicaly called from within cascade actions.
 *
 * @param persister The parent's entity persister
 * @param parent The parent reference./*  w  w  w .j a v a 2  s . com*/
 * @param anything Anything ;)   Typically some form of cascade-local cache
 * which is specific to each CascadingAction type
 * @throws HibernateException
 */
public void cascade(final EntityPersister persister, final Object parent, final Object anything)
        throws HibernateException {

    if (persister.hasCascades() || action.requiresNoCascadeChecking()) { // performance opt
        if (log.isTraceEnabled()) {
            log.trace("processing cascade " + action + " for: " + persister.getEntityName());
        }

        Type[] types = persister.getPropertyTypes();
        CascadeStyle[] cascadeStyles = persister.getPropertyCascadeStyles();
        EntityMode entityMode = eventSource.getEntityMode();
        boolean hasUninitializedLazyProperties = persister.hasUninitializedLazyProperties(parent, entityMode);
        for (int i = 0; i < types.length; i++) {
            CascadeStyle style = cascadeStyles[i];
            if (hasUninitializedLazyProperties && persister.getPropertyLaziness()[i]
                    && !action.performOnLazyProperty()) {
                //do nothing to avoid a lazy property initialization
                continue;
            }

            if (style.doCascade(action)) {
                cascadeProperty(persister.getPropertyValue(parent, i, entityMode), types[i], style, anything,
                        false);
            } else if (action.requiresNoCascadeChecking()) {
                action.noCascade(eventSource, persister.getPropertyValue(parent, i, entityMode), parent,
                        persister, i);
            }
        }

        if (log.isTraceEnabled()) {
            log.trace("done processing cascade " + action + " for: " + persister.getEntityName());
        }
    }
}

From source file:ubic.gemma.core.security.audit.AuditAdvice.java

License:Apache License

/**
 * Fills in audit trails on newly created child objects after a 'create' or 'update'. It does not add 'update'
 * events on the child objects./*from   ww w . ja  v a2 s.c  om*/
 * 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)
        return; // don't audit audit trails.

    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.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) {
                    // If this happens, it means the object can't be 'new' so adding audit trail can't
                    // 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) {

                    // If this happens, it means the object can't be 'new' so adding audit trail can't
                    // 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);
    }
}

From source file:ubic.gemma.security.audit.AuditAdvice.java

License:Apache License

/**
 * Fills in audit trails on newly created child objects after a 'create' or 'update'. It does not add 'update'
 * events on the child objects./*w w  w .j ava  2  s . c  om*/
 * <p>
 * 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)
 * 
 * @param m
 * @param object
 * @see AclAdvice for similar code for ACLs
 */
private void processAssociations(String methodName, Object object, User user) {

    if (object instanceof AuditTrail)
        return; // don't audit audit trails.

    EntityPersister persister = crudUtils.getEntityPersister(object);
    if (persister == null) {
        throw new IllegalArgumentException("No persister found for " + object.getClass().getName());
    }
    boolean hadErrors = false;
    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 (!specialCaseForAssociationFollow(object, propertyName)
                    && (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 (Auditable.class.isAssignableFrom(propertyType)) {

                Auditable auditable = (Auditable) associatedObject;
                try {

                    maybeAddCascadeCreateEvent(object, auditable, user);

                    processAssociations(methodName, auditable, user);
                } catch (HibernateException e) {
                    // If this happens, it means the object can't be 'new' so adding audit trail can't
                    // be necessary.
                    hadErrors = true;
                    if (log.isDebugEnabled())
                        log.debug("Hibernate error while processing " + auditable + ": " + e.getMessage());
                }

            } else if (Collection.class.isAssignableFrom(propertyType)) {
                Collection<?> associatedObjects = (Collection<?>) associatedObject;

                try {
                    Hibernate.initialize(associatedObjects);
                    for (Object collectionMember : associatedObjects) {

                        if (Auditable.class.isAssignableFrom(collectionMember.getClass())) {
                            Auditable auditable = (Auditable) collectionMember;
                            try {
                                Hibernate.initialize(auditable);
                                maybeAddCascadeCreateEvent(object, auditable, user);
                                processAssociations(methodName, collectionMember, user);
                            } catch (HibernateException e) {
                                hadErrors = true;
                                if (log.isDebugEnabled())
                                    log.debug("Hibernate error while processing " + auditable + ": "
                                            + e.getMessage());
                                // If this happens, it means the object can't be 'new' so adding audit trail can't
                                // be necessary. But keep checking.
                            }

                        }
                    }
                } catch (HibernateException e) {
                    hadErrors = true;
                    // If this happens, it means the object can't be 'new' so adding audit trail can't
                    // be necessary.
                    if (log.isDebugEnabled())
                        log.debug("Hibernate error while processing " + object + ": " + e.getMessage());
                }

            }
        }
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e);
    }
    if (hadErrors) {
        // log.warn( "There were hibernate errors during association checking for " + object
        // + "; probably not critical." );
    }
}

From source file:ubic.gemma.security.authorization.acl.AclAdvice.java

License:Apache License

/**
 * Walk the tree of associations and add (or update) acls.
 * //from   w  ww.j  a v  a  2s  . c o m
 * @param methodName method name
 * @param object
 * @param previousParent The parent ACL of the given object (if it is a Securable) or of the last visited Securable.
 * @see AuditAdvice for similar code for Auditing
 */
@SuppressWarnings("unchecked")
private void processAssociations(String methodName, Object object, Acl previousParent) {

    if (canSkipAclCheck(object)) {
        return;
    }

    EntityPersister persister = crudUtils.getEntityPersister(object);
    if (persister == null) {
        log.error("No Entity Persister found for " + object.getClass().getName());
        return;
    }
    CascadeStyle[] cascadeStyles = persister.getPropertyCascadeStyles();
    String[] propertyNames = persister.getPropertyNames();

    Acl parentAcl = chooseParentForAssociations(object, previousParent);

    for (int j = 0; j < propertyNames.length; j++) {

        CascadeStyle cs = cascadeStyles[j];
        String propertyName = propertyNames[j];

        // log.warn( propertyName );

        /*
         * The goal here is to avoid following associations that don't need to be checked. Unfortunately, this can
         * be a bit tricky because there are exceptions. This is kind of inelegant, but the alternative is to check
         * _every_ association, which will often not be reachable.
         */
        if (!specialCaseForAssociationFollow(object, propertyName)
                && (canSkipAssociationCheck(object, propertyName) || !crudUtils.needCascade(methodName, cs))) {
            continue;
        }

        PropertyDescriptor descriptor = BeanUtils.getPropertyDescriptor(object.getClass(), propertyName);

        Object associatedObject = null;
        try {
            associatedObject = ReflectionUtil.getProperty(object, descriptor);
        } catch (Exception e) {
            log.error("Error while processing: " + object.getClass() + " --> " + propertyName);
            throw (new RuntimeException(e));
        }

        if (associatedObject == null)
            continue;

        Class<?> propertyType = descriptor.getPropertyType();

        if (associatedObject instanceof Collection) {
            Collection<Object> associatedObjects = (Collection<Object>) associatedObject;

            try {
                for (Object object2 : associatedObjects) {

                    if (Securable.class.isAssignableFrom(object2.getClass())) {
                        addOrUpdateAcl((Securable) object2, parentAcl);
                    }
                    processAssociations(methodName, object2, parentAcl);
                }
            } catch (LazyInitializationException ok) {
                /*
                 * This is not a problem. If this was reached via a create, the associated objects must not be new
                 * so they should already have acls.
                 */
                // log.warn( "oops" );
            }

        } else {

            if (Securable.class.isAssignableFrom(propertyType)) {
                addOrUpdateAcl((Securable) associatedObject, parentAcl);
            }
            processAssociations(methodName, associatedObject, parentAcl);
        }
    }
}