You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ar...@apache.org on 2005/12/04 03:44:13 UTC

cvs commit: db-ojb/src/java/org/apache/ojb/broker/util BrokerHelper.java

arminw      2005/12/03 18:44:13

  Modified:    src/java/org/apache/ojb/broker/util Tag: OJB_1_0_RELEASE
                        BrokerHelper.java
  Log:
  add new "link" method, minor imrovements
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.57.2.20 +76 -11    db-ojb/src/java/org/apache/ojb/broker/util/BrokerHelper.java
  
  Index: BrokerHelper.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/BrokerHelper.java,v
  retrieving revision 1.57.2.19
  retrieving revision 1.57.2.20
  diff -u -r1.57.2.19 -r1.57.2.20
  --- BrokerHelper.java	10 Oct 2005 00:00:33 -0000	1.57.2.19
  +++ BrokerHelper.java	4 Dec 2005 02:44:13 -0000	1.57.2.20
  @@ -34,7 +34,6 @@
   import org.apache.ojb.broker.OJBRuntimeException;
   import org.apache.ojb.broker.PBKey;
   import org.apache.ojb.broker.PersistenceBrokerException;
  -import org.apache.ojb.broker.PersistenceBrokerSQLException;
   import org.apache.ojb.broker.accesslayer.StatementManagerIF;
   import org.apache.ojb.broker.accesslayer.sql.SqlExistStatement;
   import org.apache.ojb.broker.core.PersistenceBrokerImpl;
  @@ -669,8 +668,17 @@
       private Map sqlSelectMap = new ReferenceIdentityMap(ReferenceIdentityMap.WEAK, ReferenceIdentityMap.HARD);
       /**
        * TODO: This method should be moved to {@link org.apache.ojb.broker.accesslayer.JdbcAccess}
  -     * before 1.1 release. This method only checks if the requested object can be
  -     * found in DB (without full object materialization).
  +     * before 1.1 release.
  +     *
  +     * This method checks if the requested object can be
  +     * found in database (without object materialization).
  +     *
  +     * @param cld The {@link org.apache.ojb.broker.metadata.ClassDescriptor} of the
  +     * object/{@link org.apache.ojb.broker.Identity} to check.
  +     * @param obj The <em>object</em> to check.
  +     * @param oid The associated {@link org.apache.ojb.broker.Identity}.
  +     * {@link org.apache.ojb.broker.Identity} of the object
  +     * @return Return <em>true</em> if the object is already persisted, <em>false</em> if the object is transient.
        */
       public boolean doesExist(ClassDescriptor cld, Identity oid, Object obj)
       {
  @@ -681,21 +689,29 @@
               sql = new SqlExistStatement(cld, LoggerFactory.getDefaultLogger()).getStatement();
               sqlSelectMap.put(cld, sql);
           }
  +        ValueContainer[] pkValues;
  +        if(oid == null)
  +        {
  +            pkValues = getKeyValues(cld, obj, true);
  +        }
  +        else
  +        {
  +            pkValues = getKeyValues(cld, oid);
  +        }
           StatementManagerIF sm = m_broker.serviceStatementManager();
           PreparedStatement stmt = null;
           ResultSet rs = null;
           try
           {
               stmt = sm.getPreparedStatement(cld, sql, false, 1, false);
  -            sm.bindSelect(stmt, oid, cld, false);
  +            sm.bindValues(stmt, pkValues, 1);
               rs = stmt.executeQuery();
               result = rs.next();
           }
           catch(SQLException e)
           {
  -            LoggerFactory.getDefaultLogger().error("[BrokerHelper#doesExist] Can't check for existance", e);
  -            throw new PersistenceBrokerSQLException("Exist check failed for identity " + oid
  -                    + ", sql was " + sql, e);
  +            throw ExceptionHelper.generateException("[BrokerHelper#doesExist] Can't check if specified" +
  +                    " object is already persisted", e, sql, cld, pkValues, null, obj);
           }
           finally
           {
  @@ -786,14 +802,63 @@
       }
   
       /**
  -     * Unlink the specified reference from this object.
  +     * This method concatenate the main object and the specified reference
  +     * object (1:1 reference a referenced object, 1:n and m:n reference a
  +     * collection of referenced objects) by hand. This method is needed when
  +     * in the reference metadata definitions the auto-xxx setting was disabled.
        * More info see OJB doc.
  +     *
        * @param obj Object with reference
        * @param attributeName field name of the reference
  +     * @param reference The referenced object
  +     * @param insert flag signals insert operation
  +     * @return true if the specified reference was found and linking was successful
  +     */
  +    public boolean link(Object obj, String attributeName, Object reference, boolean insert)
  +    {
  +        ClassDescriptor cld = m_broker.getDescriptorRepository().getDescriptorFor(ProxyHelper.getRealClass(obj));
  +        ObjectReferenceDescriptor ord;
  +        boolean match = false;
  +        // first look for reference then for collection
  +        ord = cld.getObjectReferenceDescriptorByName(attributeName);
  +        if (ord != null)
  +        {
  +            linkOrUnlinkOneToOne(true, obj, ord, insert);
  +            match = true;
  +        }
  +        else
  +        {
  +            CollectionDescriptor cod = cld.getCollectionDescriptorByName(attributeName);
  +            if (cod != null)
  +            {
  +                linkOrUnlinkXToMany(true, obj, cod, insert);
  +                match = true;
  +            }
  +        }
  +        return match;
  +    }
  +
  +    /**
  +     * Unlink the specified reference object.
  +     * More info see OJB doc.
  +     * @param source The source object with the specified reference field.
  +     * @param attributeName The field name of the reference to unlink.
  +     * @param target The referenced object to unlink.
  +     */
  +    public boolean unlink(Object source, String attributeName, Object target)
  +    {
  +        return linkOrUnlink(false, source, attributeName, false);
  +    }
  +
  +    /**
  +     * Unlink all referenced objects of the specified field.
  +     * More info see OJB doc.
  +     * @param source The source object with the specified reference.
  +     * @param attributeName The field name of the reference to unlink.
        */
  -    public boolean unlink(Object obj, String attributeName)
  +    public boolean unlink(Object source, String attributeName)
       {
  -        return linkOrUnlink(false, obj, attributeName, false);
  +        return linkOrUnlink(false, source, attributeName, false);
       }
   
       /**
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org