Examples of PersistentField


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

     * @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, Object cv)
    {
        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

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

            super(owner, retrievalTasks, key, limit);
        }

        protected void addThisListenerTo(Object owner)
        {
            PersistentField collectionField =
                    ((CollectionDescriptor) _key).getPersistentField();
            _listenedCollection = (CollectionProxyDefaultImpl) collectionField.get(owner);
            _listenedCollection.addListener(this);
        }
View Full Code Here

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

        Object freshInstance = getDBObject(oid);

        // update all primitive typed attributes
        FieldDescriptor[] fields = cld.getFieldDescriptions();
        FieldDescriptor fmd;
        PersistentField fld;
        for (int i = 0; i < fields.length; i++)
        {
            fmd = fields[i];
            fld = fmd.getPersistentField();
            fld.set(cachedInstance, fld.get(freshInstance));
        }
    }
View Full Code Here

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

         */
        FieldDescriptor[] fieldDescs = mif.getFieldDescriptions();
        for(int i = 0; i < fieldDescs.length; i++)
        {
            FieldDescriptor fd = fieldDescs[i];
            PersistentField f = fd.getPersistentField();
            fieldValues.put(fd.getColumnName(), f.get(myObj));
        }
        /**
         * MBAIRD
         * 2. register all 1:1 references
         * field changes to 1:1 mapped objects should also be registered in the map,
View Full Code Here

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

     * @param rds - the ObjectReferenceDescriptor of the reference attribute to be loaded
     * @param forced - if set to true, the reference is loaded even if the rds differs.
     */
    public void retrieveReference(Object obj, ClassDescriptor cld, ObjectReferenceDescriptor rds, boolean forced)
    {
        PersistentField refField;
        Object refObj = null;

        if (forced || rds.getCascadeRetrieve())
        {
            Identity id = getReferencedObjectIdentity(obj, rds, cld);
            boolean isRefObjDefined = true;

            if (id == null)
            {
                refObj = null;
            } //JMM : why not see if the object has already been loaded
            else if ( pb.serviceObjectCache().lookup(id) != null )
            {
                refObj = pb.doGetObjectByIdentity(id);
            }
            else if ((m_retrievalTasks != null)
                    && !rds.isLazy()
                    && (rds.getItemProxyClass() == null))
            {
                addRetrievalTask(obj, rds);
                isRefObjDefined = false;
            }
            else
            {
                refObj = getReferencedObject(id, rds, cld);
            }

            if (isRefObjDefined)
            {
                refField = rds.getPersistentField();
                refField.set(obj, refObj);

                if ((refObj != null) && prefetchProxies
                        && (m_retrievalTasks != null)
                        && (rds.getProxyPrefetchingLimit() > 0))
                {
View Full Code Here

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

            }
            else
            {
                // this collection type will be used:
                Class collectionClass = cds.getCollectionClass();
                PersistentField collectionField = cds.getPersistentField();
                Query fkQuery = getFKQuery(obj, cld, cds);
                Object value;

                if (collectionClass == null)
                {
                    Collection result = getCollectionByQuery(fkQuery, cds.isLazy());

                    // assign collection to objects attribute
                    // if attribute has an array type build an array, else assign collection directly
                    if (collectionField.getType().isArray())
                    {
                        int length = result.size();
                        Class itemtype = collectionField.getType().getComponentType();
                        Object resultArray = Array.newInstance(itemtype, length);
                        int j = 0;
                        for (Iterator iter = result.iterator(); iter.hasNext();j++)
                        {
                            Array.set(resultArray, j, iter.next());
                        }
                        collectionField.set(obj, resultArray);
                    }
                    else
                    {
                        collectionField.set(obj, result);
                    }
                    value = result;
                }
                else
                {
                    ManageableCollection result = getCollectionByQuery(collectionClass, fkQuery, cds.isLazy());
                    collectionField.set(obj, result);
                    value = result;
                }

                if (prefetchProxies && (m_retrievalTasks != null)
                        && (cds.getProxyPrefetchingLimit() > 0)
View Full Code Here

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

        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

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

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

        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

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

        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
TOP
Copyright © 2018 www.massapi.com. 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.