db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/broker IdentityFactory.java Identity.java PersistenceBroker.java
Date Tue, 06 Jul 2004 10:01:03 GMT
arminw      2004/07/06 03:01:03

  Modified:    src/java/org/apache/ojb/broker/core
                        DelegatingPersistenceBroker.java
                        PersistenceBrokerImpl.java
               src/java/org/apache/ojb/broker Identity.java
                        PersistenceBroker.java
  Added:       src/java/org/apache/ojb/broker/core IdentityFactoryImpl.java
               src/java/org/apache/ojb/broker IdentityFactory.java
  Log:
  add first version of new service.
  IdentityFactory supports building of Identity objects and make
  findByPrimaryKey calls much easier
  
  Revision  Changes    Path
  1.12      +6 -0      db-ojb/src/java/org/apache/ojb/broker/core/DelegatingPersistenceBroker.java
  
  Index: DelegatingPersistenceBroker.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/DelegatingPersistenceBroker.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- DelegatingPersistenceBroker.java	22 May 2004 09:51:25 -0000	1.11
  +++ DelegatingPersistenceBroker.java	6 Jul 2004 10:01:02 -0000	1.12
  @@ -14,6 +14,7 @@
   import org.apache.ojb.broker.TransactionAbortedException;
   import org.apache.ojb.broker.TransactionInProgressException;
   import org.apache.ojb.broker.TransactionNotInProgressException;
  +import org.apache.ojb.broker.IdentityFactory;
   import org.apache.ojb.broker.accesslayer.ConnectionManagerIF;
   import org.apache.ojb.broker.accesslayer.JdbcAccess;
   import org.apache.ojb.broker.accesslayer.StatementManagerIF;
  @@ -244,6 +245,11 @@
   	{
   		return getBroker().serviceObjectCache();
   	}
  +
  +    public IdentityFactory serviceIdentity()
  +    {
  +        return getBroker().serviceIdentity();
  +    }
   
   	public void fireBrokerEvent(PersistenceBrokerEvent event)
   	{
  
  
  
  1.85      +9 -1      db-ojb/src/java/org/apache/ojb/broker/core/PersistenceBrokerImpl.java
  
  Index: PersistenceBrokerImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/PersistenceBrokerImpl.java,v
  retrieving revision 1.84
  retrieving revision 1.85
  diff -u -r1.84 -r1.85
  --- PersistenceBrokerImpl.java	3 Jul 2004 18:22:14 -0000	1.84
  +++ PersistenceBrokerImpl.java	6 Jul 2004 10:01:02 -0000	1.85
  @@ -32,6 +32,7 @@
   import org.apache.ojb.broker.TransactionAbortedException;
   import org.apache.ojb.broker.TransactionInProgressException;
   import org.apache.ojb.broker.TransactionNotInProgressException;
  +import org.apache.ojb.broker.IdentityFactory;
   import org.apache.ojb.broker.accesslayer.ChainingIterator;
   import org.apache.ojb.broker.accesslayer.ConnectionManagerFactory;
   import org.apache.ojb.broker.accesslayer.ConnectionManagerIF;
  @@ -119,6 +120,7 @@
       private SequenceManager sequenceManager = null;
       private StatementManagerIF statementManager = null;
       private SqlGenerator sqlGenerator;
  +    private IdentityFactory identityFactory;
       private PBKey pbKey;
   
       /**
  @@ -190,11 +192,17 @@
                           connectionManager.getSupportedPlatform());
           mtoNBroker = new MtoNBroker(this);
           referencesBroker = new QueryReferenceBroker(this);
  +        identityFactory = new IdentityFactoryImpl(this);
       }
   
       public InternalCache getInternalCache()
       {
           return objectCache;
  +    }
  +
  +    public IdentityFactory serviceIdentity()
  +    {
  +        return this.identityFactory;
       }
   
       public SqlGenerator serviceSqlGenerator()
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/core/IdentityFactoryImpl.java
  
  Index: IdentityFactoryImpl.java
  ===================================================================
  package org.apache.ojb.broker.core;
  
  import org.apache.ojb.broker.Identity;
  import org.apache.ojb.broker.PersistenceBroker;
  import org.apache.ojb.broker.PersistenceBrokerException;
  import org.apache.ojb.broker.IdentityFactory;
  import org.apache.ojb.broker.metadata.ClassDescriptor;
  import org.apache.ojb.broker.metadata.FieldDescriptor;
  
  /**
   * @see org.apache.ojb.broker.IdentityFactory
   * 
   * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
   * @version $Id: IdentityFactoryImpl.java,v 1.1 2004/07/06 10:01:02 arminw Exp $
   */
  public class IdentityFactoryImpl implements IdentityFactory
  {
      private PersistenceBroker broker;
  
      public IdentityFactoryImpl(PersistenceBroker broker)
      {
          this.broker = broker;
      }
  
      /**
       * @see org.apache.ojb.broker.IdentityFactory#buildIdentity(java.lang.Object)
       */
      public Identity buildIdentity(Object obj)
      {
          return new Identity(obj, broker);
      }
  
      /**
       * @see IdentityFactory#buildIdentity(java.lang.Object)
       */
      public Identity buildIdentity(ClassDescriptor cld, Object obj)
      {
          return new Identity(obj, broker, cld);
      }
  
      /**
       * @see IdentityFactory#buildIdentity(java.lang.Class, java.lang.Class, java.lang.String[],
java.lang.Object[]))
       */
      public Identity buildIdentity(Class realClass, Class topLevelClass, String[] pkFieldNames,
Object[] pkValues)
      {
          Object[] orderedPKValues = pkValues;
          if(pkValues == null)
          {
              throw new NullPointerException("Given primary key value array can't be null");
          }
          if(pkValues.length == 1 && (pkFieldNames == null || pkFieldNames.length
== 1))
          {
              /*
              we assume only a single PK field is defined and do no further checks,
              we have nothing to do
              */
          }
          else
          {
              // in other more complex cases we do several check
              FieldDescriptor[] flds = broker.getClassDescriptor(realClass).getPkFields();
              if(!isOrdered(flds, pkFieldNames))
              {
                  orderedPKValues = reorderPKValues(flds, pkFieldNames, pkValues);
              }
          }
          return new Identity(realClass, topLevelClass, orderedPKValues);
      }
  
      private Object[] reorderPKValues(FieldDescriptor[] flds, String[] pkFieldNames, Object[]
pkValues)
      {
          String fieldName = null;
          Object[] orderedPKValues = new Object[flds.length];
          for (int i = 0; i < flds.length; i++)
          {
              fieldName = flds[i].getPersistentField().getName();
              int realPosition = findFieldName(pkFieldNames, fieldName);
              orderedPKValues[i] = pkValues[realPosition];
          }
          return orderedPKValues;
      }
  
      private int findFieldName(String[] fieldNames, String searchName)
      {
          for (int i = 0; i < fieldNames.length; i++)
          {
              if(searchName.equals(fieldNames[i]))
              {
                  return i;
              }
          }
          throw new PersistenceBrokerException("Can't find field name '" + searchName +
                  "' in given array of field names");
      }
  
      /**
       * Checks length and compare order of field names with declared PK fields in metadata.
       */
      private boolean isOrdered(FieldDescriptor[] flds, String[] pkFieldNames)
      {
          if((flds.length > 1 && pkFieldNames == null) || flds.length != pkFieldNames.length)
          {
              throw new PersistenceBrokerException("pkFieldName length does not match number
of defined PK fields." +
                      " Expected number of PK fields is " + flds.length + ", given number
was " +
                      (pkFieldNames != null ? pkFieldNames.length : 0));
          }
          boolean result = true;
          for (int i = 0; i < flds.length; i++)
          {
              FieldDescriptor fld = flds[i];
              result = result && fld.getPersistentField().getName().equals(pkFieldNames[i]);
          }
          return result;
      }
  
      /**
       * @see org.apache.ojb.broker.IdentityFactory#buildIdentity(java.lang.Class, java.lang.String[],
java.lang.Object[]))
       */
      public Identity buildIdentity(Class realClass, String[] pkFieldNames, Object[] pkValues)
      {
          return buildIdentity(realClass, broker.getTopLevelClass(realClass), pkFieldNames,
pkValues);
      }
  
      /**
       * @see org.apache.ojb.broker.IdentityFactory#buildIdentity(java.lang.Class, java.lang.Object)
       */
      public Identity buildIdentity(Class realClass, Object pkValue)
      {
          return buildIdentity(realClass, null, new Object[]{pkValue});
      }
  }
  
  
  
  1.37      +28 -4     db-ojb/src/java/org/apache/ojb/broker/Identity.java
  
  Index: Identity.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/Identity.java,v
  retrieving revision 1.36
  retrieving revision 1.37
  diff -u -r1.36 -r1.37
  --- Identity.java	9 Apr 2004 13:22:30 -0000	1.36
  +++ Identity.java	6 Jul 2004 10:01:03 -0000	1.37
  @@ -33,9 +33,33 @@
   import java.util.zip.GZIPOutputStream;
   
   /**
  - * represents the identity of an object.
  - * identity (it's primary keys) must be unique accross extents !
  - *
  + * Represents the identity of an object.
  + * <br/>
  + * It's composed of:
  + * <ul>
  + * <li>
  + * class of the real object
  + * </li>
  + * <li>
  + * top-level class of the real object (could be an abstract class or interface or the
  + * class of the object itself), used to make an object unique across extent classes
  + * </li>
  + * <li>
  + * an array of all primary key value objects
  + * </li>
  + * </ul>
  + * <p>
  + * If in the metadata of an persistent capable object class the attribute <em>autoincrement</em>
  + * is set true, new primary key values will be automatic assigned to the given object passed
as
  + * constructor argument.
  + * </p>
  + * <p>
  + * NOTE: An <em>Identity</em> object must be unique
  + * accross extents. Means all objects with the same top-level class need unique
  + * PK values.
  + * </p>
  + * @see org.apache.ojb.broker.IdentityFactory
  +
    * @author <a href="mailto:thma@apache.org">Thomas Mahler<a>
    * @version $Id$
    */
  
  
  
  1.31      +6 -2      db-ojb/src/java/org/apache/ojb/broker/PersistenceBroker.java
  
  Index: PersistenceBroker.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/PersistenceBroker.java,v
  retrieving revision 1.30
  retrieving revision 1.31
  diff -u -r1.30 -r1.31
  --- PersistenceBroker.java	4 Apr 2004 23:53:30 -0000	1.30
  +++ PersistenceBroker.java	6 Jul 2004 10:01:03 -0000	1.31
  @@ -88,7 +88,11 @@
        */
       public ObjectCache serviceObjectCache();
   
  -
  +    /**
  +     * Return the {@link IdentityFactory} instance associated with this broker.
  +     * @return service to create {@link Identity} objects.
  +     */
  +    public IdentityFactory serviceIdentity();
   
   
       // *************************************************************************
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/IdentityFactory.java
  
  Index: IdentityFactory.java
  ===================================================================
  package org.apache.ojb.broker;
  
  import org.apache.ojb.broker.Identity;
  import org.apache.ojb.broker.metadata.ClassDescriptor;
  
  /**
   * Builds {@link Identity} objects to identify persistence capable objects within OJB.
   * In many cases the primary key (based on metadata declaration) of an object is known
   * and the whole object should be materialized (e.g. findByPrimaryKey(...) calls).
   * This class make available a bunch of methods help to create {@link Identity} objects
based on
   * <ul>
   *    <li>the persistence capable object itself</li>
   *    <li>the primary key values of a persistence capable object</li>
   * </ul>
   * NOTE:
   * <br/>
   * It is possible to assign new created objects
   * with a valid UID before they are written to database - more info see {@link Identity}.
This should be
   * used with care, because not all {@link org.apache.ojb.broker.util.sequence.SequenceManager}
   * implementations returns the "real" UID value before the object was stored (e.g. when
database based
   * Identity columns were used a temporary placeholder was returned).
   *
   * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
   * @version $Id: IdentityFactory.java,v 1.1 2004/07/06 10:01:03 arminw Exp $
   */
  public interface IdentityFactory
  {
      /**
       * Build a unique {@link Identity} for the given
       * persistence capable object.
       *
       * @param obj The object to build the {@link Identity} for.
       * @return The a new created <em>Identity</em> object.
       */
      Identity buildIdentity(Object obj);
  
      /**
       * Build a unique {@link Identity} for the given
       * persistence capable object.
       *
       * @param cld The {@link org.apache.ojb.broker.metadata.ClassDescriptor} of the
       * object.
       * @param obj The object to build the {@link Identity} for.
       * @return The a new created <em>Identity</em> object.
       */
      Identity buildIdentity(ClassDescriptor cld, Object obj);
  
      /**
       * Build a unique {@link org.apache.ojb.broker.Identity}
       * for the given primary key values (composite PK's) of a
       * persistence capable object.
       *
       * @param realClass The class of the associated object.
       * @param topLevelClass The top-level class of the associated object.
       * @param pkFieldName The field names of the PK fields.
       * @param pkValues The PK values.
       * @return The a new created <em>Identity</em> object.
       */
      Identity buildIdentity(Class realClass, Class topLevelClass, String[] pkFieldName, Object[]
pkValues);
  
      /**
       * Convenience method for
       * {@link #buildIdentity(java.lang.Class, java.lang.Class, java.lang.String[], java.lang.Object[]))}
       */
      Identity buildIdentity(Class realClass, String[] fieldName, Object[] pkValues);
  
      /**
       * Convenience method for persistent objects with single primary key.
       * NOTE: Do not use for objects with composed PK!
       *
       * @see #buildIdentity(java.lang.Class, java.lang.String[], java.lang.Object[])
       * @param realClass The class of the associated object.
       * @param pkValue The PK value.
       * @return The a new created <em>Identity</em> object.
       */
      Identity buildIdentity(Class realClass, Object pkValue);
  }
  
  
  

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


Mime
View raw message