db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b..@apache.org
Subject cvs commit: jakarta-ojb/src/java/org/apache/ojb/broker/util ProxyHelper.java
Date Tue, 17 Dec 2002 21:09:54 GMT
brj         2002/12/17 13:09:54

  Modified:    src/java/org/apache/ojb/broker/singlevm
                        PersistenceBrokerImpl.java
               src/java/org/apache/ojb/broker/util ProxyHelper.java
  Log:
  use ProxyHelper to get the real materialized object
  
  Revision  Changes    Path
  1.93      +22 -58    jakarta-ojb/src/java/org/apache/ojb/broker/singlevm/PersistenceBrokerImpl.java
  
  Index: PersistenceBrokerImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ojb/src/java/org/apache/ojb/broker/singlevm/PersistenceBrokerImpl.java,v
  retrieving revision 1.92
  retrieving revision 1.93
  diff -u -r1.92 -r1.93
  --- PersistenceBrokerImpl.java	17 Dec 2002 19:27:49 -0000	1.92
  +++ PersistenceBrokerImpl.java	17 Dec 2002 21:09:53 -0000	1.93
  @@ -565,15 +565,10 @@
           }
       }
   
  +
       /**
  -     * Method declaration
  -     *
  -     *
  -     * @param obj
  -     *
  -     * @throws PersistenceBrokerException
  -     *
  -     *
  +     * Store an Object
  +     * @see org.apache.ojb.broker.PersistenceBroker#store(Object)
        */
       public void store(Object obj) throws PersistenceBrokerException
       {
  @@ -582,37 +577,19 @@
   
       private void store(Object obj, Map markedForStore)
       {
  -        //logger.info("STORING " + obj);
           // only do something if obj != null
           if (obj != null)
           {
               // ProxyObjects only have to be updated if their real subjects have been loaded
  -            if (obj instanceof VirtualProxy)
  -            {
  -                VirtualProxy proxy = (VirtualProxy) obj;
  -                if (proxy.alreadyMaterialized())
  -                {
  -                    obj = proxy.getRealSubject();
  -                }
  -                else
  -                {
  -                    return;
  -                }
  -            }
  -            else if (Proxy.isProxyClass(obj.getClass()))
  +            obj = ProxyHelper.getRealObjectIfMaterialized(obj);
  +            if (obj == null)    // null for unmaterialized Proxy
               {
  -                IndirectionHandler handler = (IndirectionHandler) Proxy.getInvocationHandler(obj);
  -                if (handler.alreadyMaterialized())
  -                {
  -                    obj = handler.getRealSubject();
  -                }
  -                else
  -                {
  -                    return;
  -                }
  +                return;
               }
  +
               ClassDescriptor cld = descriptorRepository.getDescriptorFor(obj.getClass());
               Identity oid = new Identity(obj, this);
  +            
               // check if update or insert is needed
               boolean doInsert = false;
               // lookup cache or db to see whether object needs insert or update
  @@ -625,7 +602,9 @@
           }
           // if Object == null do nothing
           else
  +        {
               return;
  +        }
       }
   
       /**
  @@ -646,9 +625,6 @@
               Object ref = rds.getPersistentField().get(obj);
               if (rds.getCascadeStore())
               {
  -                //logger.info("STORING reference for "+obj);
  -                //logger.info("STORING reference "+rds.getItemClass()+"="+ref);
  -
                   store(ref, markedForStore);
               }
           }
  @@ -802,8 +778,7 @@
       }
   
       /**
  -     * This method is used to store values of a M:N association in a indirection
  -     * table.
  +     * Stores new values of a M:N association in a indirection table.
        */
       private void storeMtoNImplementor(CollectionDescriptor cod, Object obj, Object otherObj,
Collection mnKeys)
       {
  @@ -823,15 +798,7 @@
           }    
                   
           String insertStmt = sqlGenerator.getInsertStatement(table, pkColumns, pkValues
,otherPkColumns ,otherPkValues);
  -
  -        try
  -        {
  -            dbAccess.executeUpdateSQL(insertStmt, cld);
  -        }
  -        catch (PersistenceBrokerException e)
  -        {
  -            // attempts to store existing entries are ignored
  -        }
  +		dbAccess.executeUpdateSQL(insertStmt, cld);
       }
   
       /**
  @@ -874,22 +841,20 @@
           return result;
       }
     
  +   /**
  +    * delete all rows from m:n table belonging to obj
  +    * @param cod
  +    * @param obj
  +    */
       private void deleteMtoNImplementor(CollectionDescriptor cod, Object obj)
       {
           ClassDescriptor cld = getDescriptorRepository().getDescriptorFor(obj.getClass());
           Object[] pkValues = cld.getKeyValues(obj);
           Object[] pkColumns = cod.getFksToThisClass();
           String table = cod.getIndirectionTable();
  +
           String deleteStmt = sqlGenerator.getDeleteStatement(table, pkColumns, pkValues,
null ,null);
  -        try
  -        {
  -            dbAccess.executeUpdateSQL(deleteStmt, cld);
  -        }
  -        catch (PersistenceBrokerException e)
  -        {
  -            if (logger.isDebugEnabled()) logger.debug("// attempts to del existing entries
are ignored:" + e.getMessage(), e);
  -            // attempts to del existing entries are ignored
  -        }
  +        dbAccess.executeUpdateSQL(deleteStmt, cld);
       }
     
       /**
  @@ -1881,9 +1846,6 @@
           // Invoke events on PersistenceBrokerAware instances and listeners
           invokeEvent(obj, EVENT_BEFORE_STORE);
   
  -        //logger.info("STORING :" + obj + ", doInsert ? " + insert);
  -
  -        // proxies have to be stored only when their real subjects have been materialized
           if (obj != null)
           {
               /**
  @@ -1910,7 +1872,9 @@
               storeToDb(obj, cld, oid, markedForStore, insert);
           }
           else
  +        {
               return;
  +        }
   
           // Invoke events on PersistenceBrokerAware instances and listeners
           invokeEvent(obj, EVENT_AFTER_STORE);
  
  
  
  1.2       +41 -1     jakarta-ojb/src/java/org/apache/ojb/broker/util/ProxyHelper.java
  
  Index: ProxyHelper.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ojb/src/java/org/apache/ojb/broker/util/ProxyHelper.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ProxyHelper.java	17 Dec 2002 19:00:39 -0000	1.1
  +++ ProxyHelper.java	17 Dec 2002 21:09:54 -0000	1.2
  @@ -94,6 +94,46 @@
       }    
   
       /**
  +     * Get the real Object for already materialized Handler
  +     * 
  +     * @param objectOrProxy
  +     * @return Object or null if the Handel is not materialized
  +     */
  +    public static final Object getRealObjectIfMaterialized(Object objectOrProxy)
  +    {
  +        IndirectionHandler handler;
  +
  +        if (Proxy.isProxyClass(objectOrProxy.getClass()))
  +        {
  +            handler = (IndirectionHandler) Proxy.getInvocationHandler(objectOrProxy);
  +            if (handler.alreadyMaterialized())
  +            {
  +                return handler.getRealSubject();
  +            }
  +            else
  +            {
  +                return null;
  +            }
  +        }
  +        else if (objectOrProxy instanceof VirtualProxy)
  +        {
  +            VirtualProxy proxy = (VirtualProxy)objectOrProxy;
  +            if (proxy.alreadyMaterialized())
  +            {
  +                return proxy.getRealSubject();
  +            }
  +            else
  +            {
  +                return null;
  +            }
  +        }
  +        else
  +        {
  +            return objectOrProxy;
  +        }
  +    }    
  +
  +    /**
        * Get the real Class
        * 
        * @param objectOrProxy
  
  
  

Mime
View raw message