Package org.apache.cayenne.reflect

Examples of org.apache.cayenne.reflect.LifecycleCallbackRegistry


    /**
     * Compiles internal callback registry.
     */
    synchronized void initCallbacks() {
        if (callbackRegistry == null) {
            LifecycleCallbackRegistry callbackRegistry = new LifecycleCallbackRegistry(
                    this);

            // load default callbacks
            for (DataMap map : maps) {

                for (EntityListener listener : map.getDefaultEntityListeners()) {
                    Object listenerInstance = createListener(listener, null);
                    if (listenerInstance == null) {
                        continue;
                    }

                    CallbackDescriptor[] callbacks = listener
                            .getCallbackMap()
                            .getCallbacks();
                    for (CallbackDescriptor callback : callbacks) {

                        for (String method : callback.getCallbackMethods()) {

                            // note that callbacks[i].getCallbackType() == i
                            callbackRegistry.addDefaultListener(callback
                                    .getCallbackType(), listenerInstance, method);
                        }
                    }
                }
            }

            // load entity callbacks
            for (ObjEntity entity : getObjEntities()) {
                Class<?> entityClass = entity.getJavaClass();

                // external listeners go first, entity's own callbacks go next
                for (EntityListener listener : entity.getEntityListeners()) {
                    Object listenerInstance = createListener(listener, entity);
                    if (listenerInstance == null) {
                        continue;
                    }

                    CallbackDescriptor[] callbacks = listener
                            .getCallbackMap()
                            .getCallbacks();
                    for (CallbackDescriptor callback : callbacks) {

                        for (String method : callback.getCallbackMethods()) {
                            callbackRegistry.addListener(
                                    callback.getCallbackType(),
                                    entityClass,
                                    listenerInstance,
                                    method);
                        }
                    }
                }

                CallbackDescriptor[] callbacks = entity.getCallbackMap().getCallbacks();
                for (CallbackDescriptor callback : callbacks) {
                    for (String method : callback.getCallbackMethods()) {
                        callbackRegistry.addListener(
                                callback.getCallbackType(),
                                entityClass,
                                method);
                    }
                }
View Full Code Here


    private void invokePostLoad() {
        // TODO: andrus, 9/21/2006 - this method incorrectly calls "postLoad" when query
        // refresh flag is set to false and object is already there.

        LifecycleCallbackRegistry callbackRegistry = domain
                .getEntityResolver()
                .getCallbackRegistry();

        if (!callbackRegistry.isEmpty(LifecycleEvent.POST_LOAD)) {

            List list = response.firstList();
            if (list != null
                    && !list.isEmpty()
                    && !(query.getMetaData(domain.getEntityResolver()))
                            .isFetchingDataRows()) {
                callbackRegistry.performCallbacks(LifecycleEvent.POST_LOAD, list);
            }
        }
    }
View Full Code Here

                    throw new IllegalStateException("Unknown response object: "
                            + this.response);
                }

                // apply POST_LOAD callback
                LifecycleCallbackRegistry callbackRegistry = context
                        .getEntityResolver()
                        .getCallbackRegistry();

                if (!callbackRegistry.isEmpty(LifecycleEvent.POST_LOAD)) {
                    callbackRegistry.performCallbacks(LifecycleEvent.POST_LOAD, objects);
                }
            }
        }
    }
View Full Code Here

    public void testSelectAggregatePostLoadCallback() throws Exception {

        createTestData("prepare");
        DataContext context = createDataContext();

        LifecycleCallbackRegistry existingCallbacks = context
                .getEntityResolver()
                .getCallbackRegistry();
        LifecycleCallbackRegistry testCallbacks = new LifecycleCallbackRegistry(context
                .getEntityResolver());

        DataContextEJBQLQueryCallback listener = new DataContextEJBQLQueryCallback();
        testCallbacks.addDefaultListener(
                LifecycleEvent.POST_LOAD,
                listener,
                "postLoad");

        context.getEntityResolver().setCallbackRegistry(testCallbacks);
View Full Code Here

        EntityResolver resolver = getDomain().getEntityResolver();
        resolver.getCallbackRegistry().clear();
    }

    public void testPostLoad() throws Exception {
        LifecycleCallbackRegistry registry = getDomain()
                .getEntityResolver()
                .getCallbackRegistry();

        ObjectContext context = createDataContext();

        registry.addListener(LifecycleEvent.POST_LOAD, Artist.class, "postLoadCallback");
        MockCallingBackListener listener = new MockCallingBackListener();
        registry.addListener(
                LifecycleEvent.POST_LOAD,
                Artist.class,
                listener,
                "publicCallback");
View Full Code Here

        assertEquals(1, a1.getPostLoaded());
        assertSame(a1, listener.getPublicCalledbackEntity());
    }

    public void testPostLoad_MixedResult() throws Exception {
        LifecycleCallbackRegistry registry = getDomain()
                .getEntityResolver()
                .getCallbackRegistry();

        ObjectContext context = createDataContext();

        registry.addListener(LifecycleEvent.POST_LOAD, Artist.class, "postLoadCallback");
        MockCallingBackListener listener = new MockCallingBackListener();
        registry.addListener(
                LifecycleEvent.POST_LOAD,
                Artist.class,
                listener,
                "publicCallback");
View Full Code Here

        assertEquals(1, a1.getPostLoaded());
        assertSame(a1, listener.getPublicCalledbackEntity());
    }

    public void testPostLoad_Relationship() throws Exception {
        LifecycleCallbackRegistry registry = getDomain()
                .getEntityResolver()
                .getCallbackRegistry();

        ObjectContext context = createDataContext();

        registry.addListener(LifecycleEvent.POST_LOAD, Artist.class, "postLoadCallback");
        MockCallingBackListener listener = new MockCallingBackListener();
        registry.addListener(
                LifecycleEvent.POST_LOAD,
                Artist.class,
                listener,
                "publicCallback");
View Full Code Here

        assertEquals(1, a1.getPostLoaded());
        assertSame(a1, listener.getPublicCalledbackEntity());
    }

    public void testPostLoad_Prefetch() throws Exception {
        LifecycleCallbackRegistry registry = getDomain()
                .getEntityResolver()
                .getCallbackRegistry();

        ObjectContext context = createDataContext();

        registry.addListener(LifecycleEvent.POST_LOAD, Artist.class, "postLoadCallback");
        MockCallingBackListener listener = new MockCallingBackListener();
        registry.addListener(
                LifecycleEvent.POST_LOAD,
                Artist.class,
                listener,
                "publicCallback");
View Full Code Here

        assertSame(a1, listener.getPublicCalledbackEntity());
    }

   public void testPreUpdate() {

        LifecycleCallbackRegistry registry = getDomain()
                .getEntityResolver()
                .getCallbackRegistry();

        ObjectContext context = createDataContext();

        Artist a1 = context.newObject(Artist.class);
        a1.setArtistName("XX");
        context.commitChanges();
        assertFalse(a1.isPreUpdated());

        a1.setArtistName("YY");
        context.commitChanges();
        assertFalse(a1.isPreUpdated());

        registry
                .addListener(LifecycleEvent.PRE_UPDATE, Artist.class, "preUpdateCallback");
        a1.setArtistName("ZZ");
        context.commitChanges();
        assertTrue(a1.isPreUpdated());

        a1.resetCallbackFlags();
        assertFalse(a1.isPreUpdated());

        MockCallingBackListener listener2 = new MockCallingBackListener();
        registry.addListener(
                LifecycleEvent.PRE_UPDATE,
                Artist.class,
                listener2,
                "publicCallback");
View Full Code Here

        assertSame(a1, listener2.getPublicCalledbackEntity());
    }

    public void testPostUpdate() {

        LifecycleCallbackRegistry registry = getDomain()
                .getEntityResolver()
                .getCallbackRegistry();

        ObjectContext context = createDataContext();

        Artist a1 = context.newObject(Artist.class);
        a1.setArtistName("XX");
        context.commitChanges();
        assertFalse(a1.isPostUpdated());

        a1.setArtistName("YY");
        context.commitChanges();
        assertFalse(a1.isPostUpdated());

        registry.addListener(
                LifecycleEvent.POST_UPDATE,
                Artist.class,
                "postUpdateCallback");
        a1.setArtistName("ZZ");
        context.commitChanges();
        assertTrue(a1.isPostUpdated());

        a1.resetCallbackFlags();
        assertFalse(a1.isPostUpdated());

        MockCallingBackListener listener2 = new MockCallingBackListener();
        registry.addListener(
                LifecycleEvent.POST_UPDATE,
                Artist.class,
                listener2,
                "publicCallback");
View Full Code Here

TOP

Related Classes of org.apache.cayenne.reflect.LifecycleCallbackRegistry

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.