Example usage for org.hibernate.proxy AbstractLazyInitializer isUninitialized

List of usage examples for org.hibernate.proxy AbstractLazyInitializer isUninitialized

Introduction

In this page you can find the example usage for org.hibernate.proxy AbstractLazyInitializer isUninitialized.

Prototype

@Override
    public final boolean isUninitialized() 

Source Link

Usage

From source file:stroom.entity.server.util.BaseEntityDeProxyProcessor.java

License:Apache License

/**
 * Do the work of processing the object and it's children.
 *///from  w w w . ja  va2 s.c om
private Object doProcess(final Object source) throws IntrospectionException, InvocationTargetException,
        InstantiationException, IllegalAccessException {
    Object target = source;

    // Walk unless we say otherwise
    boolean walk = true;

    // Convert all collections to standard collection types.
    if (source instanceof Collection || source instanceof Map) {
        target = replaceCollectionType(source);
    }

    // Are we dealing with a proxy?
    if (target instanceof HibernateProxy) {
        if (!(target instanceof BaseEntity)) {
            throw new RuntimeException("We are expecting all lazy objects to be BaseEntities");
        }
        // Return back our own proxy for the hibernate one that just returns
        // the id. We create a real base entity with just the ID set. The
        // client an then choose to load this up using the loadXYZ API's on
        // the service layer
        BaseEntity replacement = null;
        try {
            final HibernateProxy hibernateProxy = (HibernateProxy) target;
            final LazyInitializer lazyInitializer = hibernateProxy.getHibernateLazyInitializer();
            if (lazyInitializer != null) {
                if (lazyInitializer instanceof AbstractLazyInitializer) {
                    final AbstractLazyInitializer abstractLazyInitializer = (AbstractLazyInitializer) lazyInitializer;
                    if (!abstractLazyInitializer.isUninitialized()) {
                        replacement = (BaseEntity) abstractLazyInitializer.getImplementation();
                    }
                }

                if (replacement == null) {
                    final String entityName = lazyInitializer.getEntityName();
                    final Class<?> clazz = Class.forName(entityName);
                    replacement = (BaseEntity) clazz.newInstance();
                    final long lazyId = ((BaseEntity) source).getId();
                    replacement.setStub(lazyId);

                    // No Point in walking the child objects.
                    walk = false;
                }
            }
        } catch (final ClassNotFoundException e) {
            LOGGER.error(e, e);
        }

        target = replacement;
    }

    if (target instanceof HasEntity<?>) {
        final HasEntity<?> entityRow = (HasEntity<?>) target;

        if (entityRow.getEntity() != null) {
            doProcess(entityRow.getEntity());
        }
    } else {
        // Only walk children of base entities
        if (walk) {
            if (target instanceof BaseEntity) {
                final BaseEntity child = (BaseEntity) target;
                final BaseEntity objectDone = objectsDone.get(child);

                // Here is check if we are on a node that we have already
                // processed (but it was not a JPA PROXY thus a STUB)
                if (objectDone != null && !objectDone.isStub()) {
                    // Already done this one and it was not a stub
                    target = objectsDone.get(child);

                } else {
                    // If is was a stub replace with the real thing
                    objectsDone.put(child, child);

                    // One last check that we don't walk our dummy entities
                    // We know this as they will have the id set but not the
                    // version
                    if (!child.isStub()) {
                        // Now Walk the fields of the class.
                        walkChildren(child);
                    }
                }
            } else if (target instanceof SharedObject) {
                // Now Walk the fields of the class.... some kind of front
                // end holder
                walkChildren(target);
            }
        }
    }

    if (target instanceof HasPassword) {
        final HasPassword hasPassword = (HasPassword) target;
        if (hasPassword.isPassword()) {
            if (incoming) {
                // Password not changed!
                if (PASSWORD_MASK.equals(hasPassword.getValue())) {
                    throw new EntityServiceException("Password not set");
                }
            } else {
                // Outgoing
                hasPassword.setValue(PASSWORD_MASK);
            }
        }
    }

    return target;
}