Package org.apache.ojb.broker.metadata.fieldaccess

Examples of org.apache.ojb.broker.metadata.fieldaccess.PersistentField


        final FieldDescriptor[] fieldDescs = cld.getFieldDescriptions();
//        final BrokerHelper brokerHelper = broker.serviceBrokerHelper();
        for (int i = 0; i < fieldDescs.length; i++)
        {
            final FieldDescriptor fd = fieldDescs[i];
            final PersistentField f = fd.getPersistentField();
            Object fieldValue = f.get(toCopy);
/*
arminw:
TODO: ensure that the autoincrement values be assigned before the copy was done
If possible we should avoid to declare BrokerHelper#getAutoIncrementValue public and
if we copy an object user don't expect the change of fields.
*/
//            // If the field is auto increment, assign its value before copying!
//            if (fd.isAutoIncrement())
//            {
//                fieldValue = brokerHelper.getAutoIncrementValue(fd, toCopy, fieldValue);
//            }

            f.set(retval, fieldValue);
        }

        /**
         * then copy all the 1:1 references
         */
        final Collection refDescsCol = cld.getObjectReferenceDescriptors();
        final ObjectReferenceDescriptor[] rds = (ObjectReferenceDescriptor[]) refDescsCol.toArray(new ObjectReferenceDescriptor[refDescsCol.size()]);
        for (int i = 0; i < rds.length; i++)
        {
            final ObjectReferenceDescriptor rd = rds[i];
            final PersistentField f = rd.getPersistentField();
            /**
             * recursively copy the referenced objects
             * register in the objMap first
             */
            final Object object = f.get(toCopy);
            final Object clone = clone(object, objMap, broker);
            objMap.put(object, clone);
            f.set(retval, clone);
        }
        /**
         * then copy all the 1:M and M:N references
         */
        final Collection colDescsCol = cld.getCollectionDescriptors();
        final Iterator it = colDescsCol.iterator();
        while (it.hasNext())
        {
            final CollectionDescriptor cd = (CollectionDescriptor) it.next();
            final PersistentField f = cd.getPersistentField();
            final Object collection = f.get(toCopy);
            /**
             * handle collection proxies where the entire Collection is a big proxy
             * (vs all the elements in the collection are proxies
             */
            if (collection == null)
            {
                f.set(retval, null);
            }
            else if (collection instanceof CollectionProxyDefaultImpl)
            {
                f.set(retval, _reflective.copy(collection, null));
            }
            else if (collection instanceof Collection)
            {
                try
                {
                    final Collection newCollection = (Collection) collection.getClass().newInstance();
                    final Iterator tempIter = ((Collection) collection).iterator();
                    Object obj;
                    while (tempIter.hasNext())
                    {
                        obj = tempIter.next();
                        /**
                        * if this is a proxy, just copy the proxy, don't materialize it, and stop recursing
                        */
                        if (ProxyHelper.isNormalOjbProxy(obj))  // tomdz: what about VirtualProxy ?
                        {
                            newCollection.add(obj);
                        }
                        else
                        {
                            final Object clone = clone(obj, objMap, broker);
                            objMap.put(obj, clone);
                            newCollection.add(clone);
                        }
                    }
                    f.set(retval, newCollection);
                }
                catch (InstantiationException e)
                {
                    throw new ObjectCopyException("InstantiationException", e);
                }
View Full Code Here


        ClassDescriptor mif = _pb.getClassDescriptor(userObject.getClass());

        // N:1 relations
        Iterator iter = mif.getObjectReferenceDescriptors().iterator();
        ObjectReferenceDescriptor rds = null;
        PersistentField f;
        Object relUserObj;
        Identity relOid;
        boolean isDependent;

        while (iter.hasNext())
        {
            rds = (ObjectReferenceDescriptor) iter.next();
            isDependent = rds.getOtmDependent();
            if (onlyDependants && !isDependent)
            {
                continue;
            }
            f = rds.getPersistentField();
            relUserObj = f.get(userObject);
            if (relUserObj != null)
            {
                relOid = new Identity(relUserObj, _pb);
                entry = (ContextEntry) _objects.get(relOid);
                if ((entry == null) || (entry.userObject != relUserObj))
                {
                    entry = insertInternal(relOid, relUserObj, lock, isDependent,
                                           oid, stack);
                    if (buildingObject && (entry != null))
                    {
                        f.set(userObject, entry.userObject);
                        f.set(cacheObject, entry.cacheObject);
                    }
                }
            }
        }

        // 1:N relations
        Iterator collections = mif.getCollectionDescriptors().iterator();
        CollectionDescriptor cds;
        Object userCol;
        Iterator userColIterator;
        Class type;
        ArrayList newUserCol = null;
        ArrayList newCacheCol = null;

        while (collections.hasNext())
        {
            cds = (CollectionDescriptor) collections.next();
            f = cds.getPersistentField();
            type = f.getType();
            isDependent = cds.getOtmDependent();
            if (onlyDependants && !isDependent)
            {
                continue;
            }
            userCol = f.get(userObject);
            if (userCol != null)
            {
                if ((userCol instanceof CollectionProxyDefaultImpl)
                        && !((CollectionProxyDefaultImpl) userCol).isLoaded())
                {
View Full Code Here

        count++;

        for (int i = 0; i < fieldDescs.length; i++)
        {
            FieldDescriptor fd = fieldDescs[i];
            PersistentField f = fd.getPersistentField();
            fields[count] = f.get(obj);
            count++;
        }

        int countRefs = 0;
        for (Iterator it = refDescs.iterator(); it.hasNext(); count++, countRefs++)
        {
            ObjectReferenceDescriptor rds = (ObjectReferenceDescriptor) it.next();
            PersistentField f = rds.getPersistentField();
            Object relObj = f.get(obj);
            if (relObj != null)
            {
                fields[count] = new Identity(relObj, _pb);
                if (withObjects)
                {
                    references[countRefs] = relObj;
                }
            }
        }

        count = 0;
        for (Iterator it = colDescs.iterator(); it.hasNext(); count++)
        {
            CollectionDescriptor cds = (CollectionDescriptor) it.next();
            PersistentField f = cds.getPersistentField();
            Class type = f.getType();
            Object col = f.get(obj);

            if ((col != null) && (col instanceof CollectionProxyDefaultImpl)
                    && !((CollectionProxyDefaultImpl) col).isLoaded())
            {
                if (addListeners)
View Full Code Here

        count++;

        for (int i = 0; i < fieldDescs.length; i++)
        {
            FieldDescriptor fd = fieldDescs[i];
            PersistentField f = fd.getPersistentField();
            f.set(obj, fields[count]);
            count++;
        }

        for (Iterator it = refDescs.iterator(); it.hasNext(); count++)
        {
            ObjectReferenceDescriptor rds = (ObjectReferenceDescriptor) it.next();
            PersistentField f = rds.getPersistentField();
            Identity oid = (Identity) fields[count];
            Object relObj;
            if (oid == null)
            {
                relObj = null;
            }
            else
            {
                relObj = _pb.getObjectByIdentity(oid);
            }
            f.set(obj, relObj);
        }

        count = 0;
        for (Iterator it = colDescs.iterator(); it.hasNext(); count++)
        {
            CollectionDescriptor cds = (CollectionDescriptor) it.next();
            PersistentField f = cds.getPersistentField();
            ArrayList list = collections[count];
            ArrayList newCol;

            if (list == null)
            {
                f.set(obj, null);
            }
            else
            {
                newCol = new ArrayList();
                for (Iterator it2 = list.iterator(); it2.hasNext(); )
View Full Code Here

        {
            ObjectReferenceDescriptor rds = (ObjectReferenceDescriptor) it.next();

            if (rds.getOtmDependent())
            {
                PersistentField f = rds.getPersistentField();
                Object relObj = f.get(obj);

                if (relObj != null)
                {
                    countCascadeDeleted +=
                            markDelete(new Identity(relObj, _pb), oid, false);
                }
            }
        }

        for (Iterator it = colDescs.iterator(); it.hasNext(); )
        {
            CollectionDescriptor cds = (CollectionDescriptor) it.next();

            if (cds.getOtmDependent())
            {
                PersistentField f = cds.getPersistentField();
                Class type = f.getType();
                Object col = f.get(obj);

                if (col != null)
                {
                    Iterator colIterator;
View Full Code Here

        FieldDescriptor[] fieldDescs = mif.getFieldDescriptions();

        for (int i = 0; i < fieldDescs.length; i++)
        {
            FieldDescriptor fd = fieldDescs[i];
            PersistentField f = fd.getPersistentField();
            f.set(oldObj, f.get(newObj));
        }

        // N:1 relations
        Iterator iter = mif.getObjectReferenceDescriptors().iterator();
        ObjectReferenceDescriptor rds;
        PersistentField field;
        Object newRelObj;
        Identity newRelOid;
        Object oldRelObj;

        while (iter.hasNext())
        {
            rds = (ObjectReferenceDescriptor) iter.next();
            field = rds.getPersistentField();
            newRelObj = field.get(newObj);
            oldRelObj = field.get(oldObj);
            if ((newRelObj == null) && (oldRelObj != null))
            {
                field.set(oldObj, null);
            }
            else if (newRelObj != null)
            {
                newRelOid = new Identity(newRelObj, pb);
                if ((oldRelObj == null) ||
                        !newRelOid.equals(new Identity(oldRelObj, pb)))
                {
                    // seek for existing old object with the new identity
                    oldRelObj = cache.lookup(newRelOid);
                    if (oldRelObj == null)
                    {
                        throw new IllegalStateException("Related object not found in the context: " + newRelOid);
                    }
                    field.set(oldObj, oldRelObj);
                }
            }
        }

        // 1:N relations
        Iterator collections = mif.getCollectionDescriptors().iterator();
        CollectionDescriptor collectionDescriptor;

        while (collections.hasNext())
        {
            collectionDescriptor = (CollectionDescriptor) collections.next();
            field = collectionDescriptor.getPersistentField();
            if (Collection.class.isAssignableFrom(field.getType()))
            {
                Collection newCol;
                Collection oldCol;

                newCol = (Collection) field.get(newObj);
                if (newCol == null)
                {
                    field.set(oldObj, null);
                    continue;
                }

                oldCol = (Collection) field.get(oldObj);
                if (newCol instanceof CollectionProxyDefaultImpl)
                {
                    CollectionProxyDefaultImpl cp = (CollectionProxyDefaultImpl) newCol;
                    if (newCol instanceof List)
                    {
                        oldCol = new ListProxyDefaultImpl(pb.getPBKey(), cp.getCollectionClass(), cp.getQuery());
                    }
                    else if (newCol instanceof Set)
                    {
                        oldCol = new SetProxyDefaultImpl(pb.getPBKey(), cp.getCollectionClass(), cp.getQuery());
                    }
                    else
                    {
                        oldCol = new CollectionProxyDefaultImpl(pb.getPBKey(), cp.getCollectionClass(), cp.getQuery());
                    }
                    if (!((CollectionProxyDefaultImpl) newCol).isLoaded())
                    {
                        field.set(oldObj, oldCol);
                        continue;
                    }
                    oldCol.clear();
                }
                else
                {
                    try
                    {
                        oldCol = (Collection) newCol.getClass().newInstance();
                    }
                    catch (Exception ex)
                    {
                        System.err.println("Cannot instantiate collection field which is neither Collection nor array: " + field);
                        ex.printStackTrace();
                        return newObj;
                    }
                }
                field.set(oldObj, oldCol);
                for (Iterator it = newCol.iterator(); it.hasNext(); )
                {
                    newRelObj = it.next();
                    newRelOid = new Identity(newRelObj, pb);
                    oldRelObj = cache.lookup(newRelOid);
                    if (oldRelObj == null)
                    {
                        oldRelObj = newRelObj;
                    }
                    oldCol.add(oldRelObj);
                }
            }
            else if (field.getType().isArray())
            {
                Object newArray = field.get(newObj);
                int length = Array.getLength(newArray);
                Object oldArray =
                        Array.newInstance(field.getType().getComponentType(), length);

                for (int i = 0; i < length; i++)
                {
                    newRelObj = Array.get(newArray, i);
                    newRelOid = new Identity(newRelObj, pb);
                    oldRelObj = cache.lookup(newRelOid);
                    if (oldRelObj == null)
                    {
                        throw new IllegalStateException("Related object not found for swizzle: " + newRelOid);
                    }
                    Array.set(oldArray, i, oldRelObj);
                }
                field.set(oldObj, oldArray);
            }
            else
            {
                throw new IllegalStateException("Cannot swizzle collection field: " + field);
            }
View Full Code Here

    private void setFieldValue(Object obj, FieldDescriptor field, Long identifier) throws SequenceManagerException
    {
        Object result = field.getJdbcType().sequenceKeyConversion(identifier);
        result = field.getFieldConversion().sqlToJava(result);
        PersistentField pf = field.getPersistentField();
        pf.set(obj, result);
    }
View Full Code Here

     * @return the autoincremented value set on given object
     * @throws PersistenceBrokerException if there is an erros accessing obj field values
     */
    private Object setAutoIncrementValue(FieldDescriptor fd, Object obj)
    {
        PersistentField f = fd.getPersistentField();
        try
        {
            // lookup SeqMan for a value matching db column an
            Object result = m_broker.serviceSequenceManager().getUniqueValue(fd);
            // reflect autoincrement value back into object
            f.set(obj, result);
            return result;
        }
        catch(MetadataException e)
        {
            throw new PersistenceBrokerException(
                    "Error while trying to autoincrement field " + f.getDeclaringClass() + "#" + f.getName(),
                    e);
        }
        catch(SequenceManagerException e)
        {
            throw new PersistenceBrokerException("Could not get key value", e);
View Full Code Here

        Object owner;
        Object relatedObject;
        Object fkValues[];
        Identity id;
        PersistenceBroker pb = getBroker();
        PersistentField field = ord.getPersistentField();
        Class topLevelClass = pb.getTopLevelClass(ord.getItemClass());
        HashMap childrenMap = new HashMap(children.size());


        for (Iterator it = children.iterator(); it.hasNext(); )
        {
            relatedObject = it.next();
            childrenMap.put(pb.serviceIdentity().buildIdentity(relatedObject), relatedObject);
        }

        for (Iterator it = owners.iterator(); it.hasNext(); )
        {
            owner = it.next();
            fkValues = ord.getForeignKeyValues(owner,cld);
            if (isNull(fkValues))
            {
                field.set(owner, null);
                continue;
            }
            id = pb.serviceIdentity().buildIdentity(null, topLevelClass, fkValues);
            relatedObject = childrenMap.get(id);
            field.set(owner, relatedObject);
        }
    }
View Full Code Here

    private static Criteria buildCriteria(Object anExample)
    {
        Criteria criteria = new Criteria();
        ClassDescriptor cld = MetadataManager.getInstance().getRepository().getDescriptorFor(anExample.getClass());
        FieldDescriptor[] fds = cld.getFieldDescriptions();
        PersistentField f;
        Object value;

        for (int i = 0; i < fds.length; i++)
        {
            try
            {
                f = fds[i].getPersistentField();
                value = f.get(anExample);
                if (value != null)
                {
                    criteria.addEqualTo(f.getName(), value);
                }
            }
            catch (Throwable ex)
            {
                LoggerFactory.getDefaultLogger().error(ex);
View Full Code Here

TOP

Related Classes of org.apache.ojb.broker.metadata.fieldaccess.PersistentField

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.