db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From baser...@apache.org
Subject cvs commit: db-ojb/src/test/org/apache/ojb/broker MtoNTest.java
Date Tue, 17 Feb 2004 20:52:31 GMT
baserose    2004/02/17 12:52:31

  Modified:    src/java/org/apache/ojb/broker/core MtoNBroker.java
                        PersistenceBrokerImpl.java
               src/java/org/apache/ojb/broker MtoNImplementor.java
               src/test/org/apache/ojb/broker MtoNTest.java
  Added:       src/java/org/apache/ojb/broker/core Key.java
  Log:
  Implementing add/storeMtoNImplementor
  
  Revision  Changes    Path
  1.6       +52 -22    db-ojb/src/java/org/apache/ojb/broker/core/MtoNBroker.java
  
  Index: MtoNBroker.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/MtoNBroker.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- MtoNBroker.java	16 Feb 2004 20:54:13 -0000	1.5
  +++ MtoNBroker.java	17 Feb 2004 20:52:30 -0000	1.6
  @@ -243,10 +243,30 @@
           }
       }
   
  +	/**
  +	 * @param m2n
  +	 */
  +	public void storeMtoNImplementor(MtoNImplementor m2n)
  +	{
  +		if(log.isDebugEnabled()) log.debug("Storing M2N implementor ["+m2n+"]");
  +		insertOrDeleteMtoNImplementor(m2n,true);
  +	}
  +
  +	/**
  +	 * @param m2n
  +	 */
  +	public void deleteMtoNImplementor(MtoNImplementor m2n)
  +	{
  +		if(log.isDebugEnabled()) log.debug("Deleting M2N implementor ["+m2n+"]");
  +		insertOrDeleteMtoNImplementor(m2n,false);
  +	}
  +
  +
       /**
        * @see org.apache.ojb.broker.PersistenceBroker#deleteMtoNImplementor
        */
  -    public void deleteMtoNImplementor(MtoNImplementor m2nImpl) throws PersistenceBrokerException
  +    private void insertOrDeleteMtoNImplementor(MtoNImplementor m2nImpl, boolean insert)

  +    	throws PersistenceBrokerException
       {
           //look for a collection descriptor on left  such as left.element-class-ref='right'
           DescriptorRepository dr = pb.getDescriptorRepository();
  @@ -256,30 +276,13 @@
           Object rightObject = m2nImpl.getRightObject();
           Class rightClass = rightObject.getClass();
   
  -        log.debug("Deleting MtoN implementor ["+leftClass+"] ["+rightClass+"]");
  -
           //
           //are written per class, maybe referencing abstract classes or interfaces
           //so let's look for collection descriptors on the left class and try to handle
extents on teh right class
           ClassDescriptor leftCld = dr.getDescriptorFor(leftClass);
           ClassDescriptor rightCld = dr.getDescriptorFor(rightClass);
           Vector leftColds = leftCld.getCollectionDescriptors();
  -        CollectionDescriptor wanted = null;
  -        for (Iterator iter = leftColds.iterator(); iter.hasNext();)
  -        {
  -            CollectionDescriptor element = (CollectionDescriptor) iter.next();
  -            //try super classes and interfaces too
  -            if(element.getItemClass().isAssignableFrom(rightClass))
  -            {
  -                //we found it !
  -                wanted = element;
  -            }
  -        }
  -        if(wanted == null)
  -        {
  -            throw new PersistenceBrokerException("Can't find reasonable collection descriptor
for MtoN implementor left["+leftClass
  -                +"] right["+rightClass+"]");
  -        }
  +        CollectionDescriptor wanted = findCollectionDescriptor(leftClass, rightClass, leftColds);
   
           if(leftObject == null || rightObject == null)
           {
  @@ -295,9 +298,36 @@
               String[] pkRightColumns = wanted.getFksToItemClass();
               String table = wanted.getIndirectionTable();
               if(table == null) throw new PersistenceBrokerException("Can't remove MtoN implementor
withou an indirection table");
  -            String deleteStmt = pb.serviceSqlGenerator().getDeleteMNStatement(table, pkLeftColumns,
pkRightColumns);
  -            pb.serviceJdbcAccess().executeUpdateSQL(deleteStmt, leftCld, leftPkValues,
rightPkValues);
  +            
  +            String stmt = null;
  +            if(insert)
  +            {
  +				stmt = pb.serviceSqlGenerator().getInsertMNStatement(table, pkLeftColumns, pkRightColumns);

  +            }
  +            else
  +            {
  +				stmt = pb.serviceSqlGenerator().getDeleteMNStatement(table, pkLeftColumns, pkRightColumns);
  +            }
  +            
  +            pb.serviceJdbcAccess().executeUpdateSQL(stmt, leftCld, leftPkValues, rightPkValues);
  +        }
  +    }
  +
  +    private CollectionDescriptor findCollectionDescriptor(Class leftClass, Class rightClass,
Vector leftColds)
  +    {
  +        for (Iterator iter = leftColds.iterator(); iter.hasNext();)
  +        {
  +            CollectionDescriptor element = (CollectionDescriptor) iter.next();
  +        
  +            //try super classes and interfaces too
  +            if(element.getItemClass().isAssignableFrom(rightClass))
  +            {
  +                //we found it !
  +                return element;
  +            }
           }
  +		throw new PersistenceBrokerException("Can't find reasonable collection descriptor for
MtoN implementor left["+leftClass
  +			+"] right["+rightClass+"]");
       }
   
   
  
  
  
  1.68      +4 -5      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.67
  retrieving revision 1.68
  diff -u -r1.67 -r1.68
  --- PersistenceBrokerImpl.java	15 Feb 2004 22:17:09 -0000	1.67
  +++ PersistenceBrokerImpl.java	17 Feb 2004 20:52:30 -0000	1.68
  @@ -990,6 +990,8 @@
        */
       public void retrieveReference(Object pInstance, String pAttributeName) throws PersistenceBrokerException
       {
  +    	logger.info("Retrieving reference named["+pAttributeName+"] on ["
  +    		+pInstance.getClass().getName()+"]");
           ClassDescriptor cld = getClassDescriptor(pInstance.getClass());
           CollectionDescriptor cod = cld.getCollectionDescriptorByName(pAttributeName);
           if (cod != null)
  @@ -1779,9 +1781,6 @@
        */
       public void addMtoNImplementor(MtoNImplementor m2n) throws PersistenceBrokerException
       {
  -        /*
  -        TODO: needs implementation!??
  -        */
  -        throw new UnsupportedOperationException("Not implemented");
  +		mtoNBroker.storeMtoNImplementor(m2n);
       }
   }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/core/Key.java
  
  Index: Key.java
  ===================================================================
  package org.apache.ojb.broker.core;
  
  /**
   * This is a helper class to model a Key of an Object
  */
  public class Key
  {
  
      final Object[] m_key;
  
      public Key(final Object[] aKey)
      {
          m_key = new Object[aKey.length];
  
          for (int i = 0; i < aKey.length; i++)
          {
              // BRJ:
              // convert all Numbers to Long to simplify equals
              // could lead to problems when Floats are used as key
              if (aKey[i] instanceof Number)
              {
                  m_key[i] = new Long(((Number)aKey[i]).longValue());
              }
              else
              {
                  m_key[i] = aKey[i];
              }
          }
      }
  
      public boolean equals(Object otherKey)
      {
          boolean result = false;
  
          if (otherKey instanceof Key)
          {
              Key k = (Key)otherKey;
  
              if (k.m_key.length == m_key.length)
              {
                  for (int i = 0; i < m_key.length; i++)
                  {
                      if (k.m_key[i].equals(m_key[i]))
                      {
                          result = true;
                      }
                      else
                      {
                          result = false;
                          break;
                      }
                  }
              }
          }
          return result;
      }
  }
  
  
  
  1.2       +87 -7     db-ojb/src/java/org/apache/ojb/broker/MtoNImplementor.java
  
  Index: MtoNImplementor.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/MtoNImplementor.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MtoNImplementor.java	10 Jul 2003 22:05:17 -0000	1.1
  +++ MtoNImplementor.java	17 Feb 2004 20:52:31 -0000	1.2
  @@ -6,37 +6,117 @@
    */
   package org.apache.ojb.broker;
   
  +import java.util.Collection;
  +
  +import org.apache.ojb.broker.core.Key;
  +import org.apache.ojb.broker.metadata.ClassDescriptor;
  +import org.apache.ojb.broker.metadata.CollectionDescriptor;
  +import org.apache.ojb.broker.metadata.DescriptorRepository;
  +import org.apache.ojb.broker.util.ProxyHelper;
  +
   /**
    * 
    * @author <a href="leandro@ibnetwork.com.br">Leandro Rodrigo Saad Cruz</a>
    */
   public class MtoNImplementor
   {
  -    private Object left = null;
  +    private String _collectionName;
  +
  +    private Object _left;
       
  -    private Object right = null;
  +    private Object _right;
   
  +	private CollectionDescriptor _collectionDesc;
  +	
  +	private PersistenceBroker _pb;
   
       /**
        * 
        */
  -    public MtoNImplementor(Object left, Object right)
  +    public MtoNImplementor(PersistenceBroker pb , CollectionDescriptor cod, Object left,
Object right)
       {
           if(left == null || right == null)
           {
               throw new IllegalArgumentException("both objects must exist");
           }
  -        this.left = left;
  -        this.right = right;
  +        
  +        _pb = pb;
  +        _collectionDesc = cod;
  +        _left = left;
  +        _right = right;
  +        
       }
  +
  +	public MtoNImplementor(PersistenceBroker pb , String collectionName, Object left, Object
right)
  +	{
  +		if(left == null || right == null)
  +		{
  +			throw new IllegalArgumentException("both objects must exist");
  +		}
  +        
  +		_pb = pb;
  +		_collectionName = collectionName;
  +		_left = left;
  +		_right = right;
  +        
  +	}
  +
       
       public Object getLeftObject()
       {
  -        return left;
  +        return _left;
       }
       
       public Object getRightObject()
       {
  -        return right;
  +        return _right;
  +    }
  +    
  +    public CollectionDescriptor getCollectionDescriptor()
  +    {
  +    	return _collectionDesc;
       }
  +
  +	/**
  +	 * 
  +	 * @param mnKeys
  +	 * @return
  +	 */    
  +    public String getInsertStmt(Collection mnKeys)
  +    {
  +		_left = ProxyHelper.getRealObject(_left);
  +		String[] leftPkColumns = _collectionDesc.getFksToThisClass();
  +		String[] rightPkColumns = _collectionDesc.getFksToItemClass();
  +		String table = _collectionDesc.getIndirectionTable();
  +		Key key = new Key(getRightPkValues());
  +
  +		if (mnKeys.contains(key))
  +		{
  +			return null;
  +		}
  +		
  +		return _pb.serviceSqlGenerator().getInsertMNStatement(table, leftPkColumns, rightPkColumns);
  +    }
  +
  +
  +	/**
  +	 * 
  +	 * @return an Object Array with the primary key values of the left object
  +	 */
  +	public Object[] getLeftPkValues()
  +	{
  +		ClassDescriptor leftCld = _pb.getDescriptorRepository().getDescriptorFor(_left.getClass());
  +		return _pb.serviceBrokerHelper().getKeyValues(leftCld, _left);
  +	}
  +
  +	/**
  +	 * 
  +	 * @return an Object Array with the primary key values of the right object
  +	 */
  +	public Object[] getRightPkValues()
  +	{
  +		ClassDescriptor rightCld = _pb.getDescriptorRepository().getDescriptorFor(_right.getClass());
  +		return _pb.serviceBrokerHelper().getKeyValues(rightCld, _right);
  +	}
   }
  +
  
  
  
  1.12      +32 -10    db-ojb/src/test/org/apache/ojb/broker/MtoNTest.java
  
  Index: MtoNTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/MtoNTest.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- MtoNTest.java	16 Feb 2004 19:40:50 -0000	1.11
  +++ MtoNTest.java	17 Feb 2004 20:52:31 -0000	1.12
  @@ -8,6 +8,7 @@
   import java.util.Arrays;
   import java.util.Collection;
   import java.util.Date;
  +import java.util.Iterator;
   import java.util.List;
   import java.util.Vector;
   
  @@ -348,20 +349,41 @@
           throws Exception
       {
           News newsId2 = new News(2);
  +		Identity id = new Identity(newsId2,broker);
  +		News newNews = (News) broker.getObjectByIdentity(id);
  +		int size = newNews.getQualifiers().size();
  +
           Category categoryId1 = new Category(1);
   
  -        MtoNImplementor m2n = new MtoNImplementor(newsId2,categoryId1);
  +        MtoNImplementor m2n = new MtoNImplementor(broker,(String)null,newsId2,categoryId1);
           broker.deleteMtoNImplementor(m2n);
  +
  +		broker.clearCache();
  +		newNews = (News) broker.getObjectByIdentity(id);
           
  -        //asserts
  -        Identity id = new Identity(newsId2,broker);
  -        News newNews = (News) broker.getObjectByIdentity(id);
  -//        for(Iterator it = newNews.getQualifiers().iterator() ; it.hasNext() ; )
  -//        {
  -//            System.out.println("Qualifier : " + it.next());
  -//        }
  -        assertEquals(1,newNews.getQualifiers().size());
  +        assertEquals(size - 1,newNews.getQualifiers().size());
       }
  +
  +	public void testStoreMtoNImplementor()
  +		throws Exception
  +	{
  +		News newsId2 = new News(2);
  +		Category categoryId2 = new Category(2);
  +
  +		Identity id = new Identity(newsId2,broker);
  +		News newNews = (News) broker.getObjectByIdentity(id);
  +		int size = newNews.getQualifiers().size();
  +
  +		MtoNImplementor m2n = new MtoNImplementor(broker,(String)null,newsId2,categoryId2);
  +		broker.addMtoNImplementor(m2n);
  +
  +		broker.clearCache();
  +		newNews = (News) broker.getObjectByIdentity(id);
  +        
  +		assertEquals(size + 1,newNews.getQualifiers().size());
  +	
  +	}
  +
    
       // Bidirectional m:n relationship using Collection
       public void testStoreBidirectionalCollection()
  @@ -425,5 +447,5 @@
           broker.store(proj1);
           broker.store(proj2);
           broker.commitTransaction();
  -    }    
  +    }
   }
  
  
  

---------------------------------------------------------------------
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