Package org.apache.ojb.broker.metadata

Examples of org.apache.ojb.broker.metadata.CollectionDescriptor


    private void cascadeInsertCollectionReferences(ObjectEnvelope source, List descriptor, List alreadyPrepared)
    {
        PersistenceBroker pb = getTransaction().getBroker();
        for(int i = 0; i < descriptor.size(); i++)
        {
            CollectionDescriptor col = (CollectionDescriptor) descriptor.get(i);
            Object collOrArray = col.getPersistentField().get(source.getObject());
            CollectionProxy proxy = ProxyHelper.getCollectionProxy(collOrArray);
            /*
            on insert we perform only materialized collection objects. This should be
            sufficient, because in method #cascadingDependents() we make sure that on
            move of unmaterialized collection objects the proxy was materialized if needed.
            */
            if(proxy == null && collOrArray != null)
            {
                Iterator it = BrokerHelper.getCollectionIterator(collOrArray);
                while(it.hasNext())
                {
                    Object colObj =  it.next();
                    if(colObj != null)
                    {
                        Identity oid = pb.serviceIdentity().buildIdentity(colObj);
                        /*
                        arminw:
                        only when the main object need insert we start with FK assignment
                        of the 1:n and m:n relations. If the main objects need update (was already persisted)
                        it should be handled by the object state detection in ObjectEnvelope
                        */
                        if(source.needsInsert())
                        {
                            /*
                            arminw:
                            TODO: what is the valid way to go, when the collection object itself is
                            a unmaterialized proxy object? Think in this case we should materialize the
                            object when the main object needs insert, because we have to assign the FK values
                            to the main object
                            */
                            colObj = ProxyHelper.getRealObject(colObj);
                            ObjectEnvelope oe = getByIdentity(oid);
                            if(oe == null)
                            {
                                getTransaction().lock(colObj, Transaction.WRITE);
                                oe = getByIdentity(oid);
                            }
                            if(col.isMtoNRelation())
                            {
                                // the main objects needs insert, thus add new m:n link
                                addM2NLinkEntry(col, source.getObject(), colObj);
                            }
                            else
View Full Code Here


    private void cascadeDeleteCollectionReferences(ObjectEnvelope source, List descriptor, List alreadyPrepared)
    {
        PersistenceBroker pb = getTransaction().getBroker();
        for(int i = 0; i < descriptor.size(); i++)
        {
            CollectionDescriptor col = (CollectionDescriptor) descriptor.get(i);
            boolean cascadeDelete = getTransaction().cascadeDeleteFor(col);
            Object collOrArray = col.getPersistentField().get(source.getObject());
            // TODO: remove cast
            CollectionProxyDefaultImpl proxy = (CollectionProxyDefaultImpl) ProxyHelper.getCollectionProxy(collOrArray);
            // on delete we have to materialize dependent objects
            if(proxy != null)
            {
                collOrArray = proxy.getData();
            }
            if(collOrArray != null)
            {
                Iterator it = BrokerHelper.getCollectionIterator(collOrArray);
                while(it.hasNext())
                {
                    Object colObj =  ProxyHelper.getRealObject(it.next());
                    Identity oid = pb.serviceIdentity().buildIdentity(colObj);
                    ObjectEnvelope colMod = get(oid, colObj, false);
                    if(cascadeDelete)
                    {
                        colMod.setModificationState(colMod.getModificationState().markDelete());
                        cascadeDeleteFor(colMod, alreadyPrepared);
                    }
                    else
                    {
                        if(!col.isMtoNRelation())
                        {
                            colMod.addLinkOneToN(col, source.getObject(), true);
                            colMod.setModificationState(colMod.getModificationState().markDirty());
                        }
                    }
                    if(col.isMtoNRelation())
                    {
                        addM2NUnlinkEntry(col, source.getObject(), colObj);
                    }
                }
            }
View Full Code Here

            addObjectReferenceEdges(vertex, rds);
        }
        Iterator cdsIter = cld.getCollectionDescriptors().iterator();
        while (cdsIter.hasNext())
        {
            CollectionDescriptor cds = (CollectionDescriptor) cdsIter.next();
            addCollectionEdges(vertex, cds);
        }
    }
View Full Code Here

    public void setUp() throws Exception
    {
        super.setUp();
        ClassDescriptor cld = MetadataManager.getInstance().getRepository().getDescriptorFor(ProductGroup.class);
        CollectionDescriptor cod = cld.getCollectionDescriptorByName("allArticlesInGroup");
        oldValue = cod.getCascadingStore();
        // odmg-api need false
        cod.setCascadeStore(false);
    }
View Full Code Here

    }

    void changeRelationMetadata(String field, boolean autoRetrieve, int autoUpdate, int autoDelete, boolean proxy)
    {
        ClassDescriptor cld = broker.getClassDescriptor(Node.class);
        CollectionDescriptor cod = (CollectionDescriptor) cld.getCollectionDescriptorByName(field);
        cod.setLazy(proxy);
        cod.setCascadeRetrieve(autoRetrieve);
        cod.setCascadingStore(autoUpdate);
        cod.setCascadingDelete(autoDelete);
    }
View Full Code Here

     * Store m-side and intermediary
     */
    public void testStoringWithAutoUpdateFalse1()
    {
        ClassDescriptor cld = broker.getClassDescriptor(Paper.class);
        CollectionDescriptor cod = cld.getCollectionDescriptorByName("qualifiers");
        int autoUpdate = cod.getCascadingStore();

        cod.setCascadingStore(ObjectReferenceDescriptor.CASCADE_LINK);

        try
        {
            String now = new Date().toString();
            Paper paper = new Paper();
            paper.setAuthor("Jonny Myers");
            paper.setDate(now);
            Qualifier qual = new Topic();
            qual.setName("qual " + now);
            paper.setQualifiers(Arrays.asList(new Qualifier[] { qual }));
            broker.beginTransaction();
            // TODO: use constraint in DB and fix test
            // store paper and set indirection table, ignore new Qualifier
            // object. Will cause Key Constraint Exception when constraint are set
            broker.store(paper);
            Identity paperId = new Identity(paper, broker);
            broker.commitTransaction();

            broker.clearCache();
            broker.beginTransaction();
            Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);
            assertEquals(0, retPaper.getQualifiers().size());
            broker.commitTransaction();
        }
        finally
        {
            cod.setCascadingStore(autoUpdate);
        }
    }
View Full Code Here

     * n-side forced by using broker.store()
     */
    public void testStoringWithAutoUpdateFalse2()
    {
        ClassDescriptor cld = broker.getClassDescriptor(Paper.class);
        CollectionDescriptor cod = cld.getCollectionDescriptorByName("qualifiers");
        int autoUpdate = cod.getCascadingStore();

        cod.setCascadingStore(ObjectReferenceDescriptor.CASCADE_LINK);

        try
        {
            String now = new Date().toString();
            Paper paper = new Paper();
            paper.setAuthor("Jonny Myers");
            paper.setDate(now);
            Qualifier qual = new Topic();
            qual.setName("qual " + now);
            paper.setQualifiers(Arrays.asList(new Qualifier[] { qual }));
            broker.beginTransaction();
            broker.store(paper);        // store Paper and intermediary table only
            broker.store(qual);         // store Qualifier
            Identity paperId = new Identity(paper, broker);
            broker.commitTransaction();

            broker.clearCache();
            broker.beginTransaction();
            Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);
            assertEquals(1, retPaper.getQualifiers().size());
            broker.commitTransaction();
        }
        finally
        {
            cod.setCascadingStore(autoUpdate);
        }
    }
View Full Code Here

     * Store m-side, intermediary and n-side
     */
    public void testStoringWithAutoUpdateTrue()
    {
        ClassDescriptor cld = broker.getClassDescriptor(Paper.class);
        CollectionDescriptor cod = cld.getCollectionDescriptorByName("qualifiers");
        int autoUpdate = cod.getCascadingStore();

        cod.setCascadingStore(ObjectReferenceDescriptor.CASCADE_OBJECT);

        try
        {
            String now = new Date().toString();
            Paper paper = new Paper();
            paper.setAuthor("Jonny Myers");
            paper.setDate(now);
            Qualifier qual = new Topic();
            qual.setName("qual " + now);
            paper.setQualifiers(Arrays.asList(new Qualifier[] { qual }));
            broker.beginTransaction();
            broker.store(paper);        // store Paper, intermediary and Qualifier
            Identity paperId = new Identity(paper, broker);
            broker.commitTransaction();

            broker.clearCache();
            broker.beginTransaction();
            Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);
            assertEquals(1, retPaper.getQualifiers().size());
            broker.commitTransaction();
        }
        finally
        {
            cod.setCascadingStore(autoUpdate);
        }
    }
View Full Code Here

    // delete from intermediary table only when collection NOT removal aware
    public void testDelete_NonRemovalAware()
    {
        ClassDescriptor cld = broker.getClassDescriptor(Paper.class);
        CollectionDescriptor cod = cld.getCollectionDescriptorByName("qualifiers");
        Class collectionClass = cod.getCollectionClass();

        cod.setCollectionClass(ManageableArrayList.class);

        try
        {
            Paper paper = createPaper();
            Identity paperId = new Identity(paper, broker);
            List qualifiers = paper.getQualifiers();
            Qualifier qual1 = (Qualifier) qualifiers.get(0);
            Qualifier qual2 = (Qualifier) qualifiers.get(1);

            // remove first object
            qualifiers.remove(0);
            broker.beginTransaction();
            broker.store(paper);
            broker.commitTransaction();

            broker.clearCache();
            broker.beginTransaction();
            Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);
            assertEquals(1, retPaper.getQualifiers().size());

            // target object qual1 should NOT be deleted
            Qualifier retQual1 = (Qualifier) broker.getObjectByIdentity(new Identity(qual1, broker));
            Qualifier retQual2 = (Qualifier) broker.getObjectByIdentity(new Identity(qual2, broker));

            assertNotNull(retQual1);
            assertNotNull(retQual2);

            broker.commitTransaction();
        }
        finally
        {
            cod.setCollectionClass(collectionClass);
        }

    }
View Full Code Here

    // delete from intermediary AND target-table when collection removal aware
    public void testDelete_RemovalAware()
    {
        ClassDescriptor cld = broker.getClassDescriptor(Paper.class);
        CollectionDescriptor cod = cld.getCollectionDescriptorByName("qualifiers");
        Class collectionClass = cod.getCollectionClass();

        cod.setCollectionClass(RemovalAwareCollection.class);

        try
        {
            Paper paper = createPaper();
            List qualifiers = paper.getQualifiers();
            Qualifier qual1 = (Qualifier) qualifiers.get(0);
            Qualifier qual2 = (Qualifier) qualifiers.get(1);
            Identity paperId = new Identity(paper, broker);

            // remove first object
            qualifiers.remove(0);
            broker.beginTransaction();
            broker.store(paper);
            broker.commitTransaction();

            broker.clearCache();
            broker.beginTransaction();
            Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);
            assertEquals(1, retPaper.getQualifiers().size());

            // target object qual1 should be deleted
            Qualifier retQual1 = (Qualifier) broker.getObjectByIdentity(new Identity(qual1, broker));
            Qualifier retQual2 = (Qualifier) broker.getObjectByIdentity(new Identity(qual2, broker));

            assertNull(retQual1);
            assertNotNull(retQual2);

            broker.commitTransaction();
        }
        finally
        {
            cod.setCollectionClass(collectionClass);
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.ojb.broker.metadata.CollectionDescriptor

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.