[prev in list] [next in list] [prev in thread] [next in thread] 

List:       jboss-cvs-commits
Subject:    [jboss-cvs] jboss/src/main/org/jboss/ejb/plugins/cmp/jdbc/bridge JDBCCMP2xFieldBridge.java JDBCCMRFi
From:       Alexey Loubyansky <loubyansky () users ! sourceforge ! net>
Date:       2004-02-28 17:38:09
Message-ID: E1Ax8Ph-0005Sa-Jc () sc8-pr-cvs1 ! sourceforge ! net
[Download RAW message or body]

  User: loubyansky
  Date: 04/02/28 09:38:09

  Modified:    src/main/org/jboss/ejb/plugins/cmp/jdbc/bridge
                        JDBCCMP2xFieldBridge.java JDBCCMRFieldBridge.java
                        JDBCEntityBridge.java
  Log:
  merged from Branch_3_2
  
  Revision  Changes    Path
  1.25      +11 -4     \
jboss/src/main/org/jboss/ejb/plugins/cmp/jdbc/bridge/JDBCCMP2xFieldBridge.java  
  Index: JDBCCMP2xFieldBridge.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss/src/main/org/jboss/ejb/plugins/cmp/jdbc/bridge/JDBCCMP2xFieldBridge.java,v
  retrieving revision 1.24
  retrieving revision 1.25
  diff -u -r1.24 -r1.25
  --- JDBCCMP2xFieldBridge.java	4 Feb 2004 19:45:49 -0000	1.24
  +++ JDBCCMP2xFieldBridge.java	28 Feb 2004 17:38:09 -0000	1.25
  @@ -80,12 +80,12 @@
            cmpField.getFieldName(),
            cmpField.getFieldType(),
            cmpField.getJDBCType(),
  -         cmpField.isReadOnly(),               // should always be false?
  +         cmpField.isReadOnly(), // should always be false?
            cmpField.getReadTimeOut(),
            cmpField.getPrimaryKeyClass(),
            cmpField.getPrimaryKeyField(),
            cmpField,
  -         null,                                // it should not be a foreign key
  +         null, // it should not be a foreign key
            cmpField.getColumnName()
         );
         this.stateFactory = stateFactory;
  @@ -201,7 +201,8 @@
                     + getFieldName()
                     + " changed the value of a primary key field "
                     + cmpFieldIAmMappedTo.getFieldName()
  -                  + "[" + fieldState.value + "]");
  +                  + "[" + fieldState.value + "]"
  +               );
               }
               else
               {
  @@ -212,6 +213,7 @@
         else
         {
            if(cmrChainLink != null
  +            && JDBCEntityBridge.isEjbCreateDone(ctx)
               && fieldState.isLoaded()
               && fieldState.isValueChanged(value))
            {
  @@ -348,6 +350,8 @@
          */
         public Object getValue()
         {
  +         //if(checkDirtyAfterGet)
  +         //   setCheckDirty();
            return value;
         }
   
  @@ -555,7 +559,10 @@
                  // set foreign key to a new value
                  cmrField.setForeignKey(ctx, newRelatedId);
                  // put calculated relatedId to the waiting list
  -               cmrField.getRelatedCMRField().addRelatedPKWaitingForMyPK(newRelatedId, \
ctx.getId());  +               if(ctx.getId() != null)
  +               {
  +                  \
cmrField.getRelatedCMRField().addRelatedPKWaitingForMyPK(newRelatedId, ctx.getId());  \
+               }  }
            }
            catch(Exception e)
  
  
  
  1.75      +152 -48   \
jboss/src/main/org/jboss/ejb/plugins/cmp/jdbc/bridge/JDBCCMRFieldBridge.java  
  Index: JDBCCMRFieldBridge.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss/src/main/org/jboss/ejb/plugins/cmp/jdbc/bridge/JDBCCMRFieldBridge.java,v
  retrieving revision 1.74
  retrieving revision 1.75
  diff -u -r1.74 -r1.75
  --- JDBCCMRFieldBridge.java	19 Jan 2004 19:18:51 -0000	1.74
  +++ JDBCCMRFieldBridge.java	28 Feb 2004 17:38:09 -0000	1.75
  @@ -46,8 +46,8 @@
   import org.jboss.ejb.plugins.cmp.jdbc.JDBCType;
   import org.jboss.ejb.plugins.cmp.jdbc.SQLUtil;
   import org.jboss.ejb.plugins.cmp.jdbc.JDBCUtil;
  -import org.jboss.ejb.plugins.cmp.jdbc.ReadAheadCache;
   import org.jboss.ejb.plugins.cmp.jdbc.CascadeDeleteStrategy;
  +import org.jboss.ejb.plugins.cmp.jdbc.RelationData;
   import org.jboss.tm.TransactionLocal;
   import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCCMPFieldMetaData;
   import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCReadAheadMetaData;
  @@ -136,8 +136,8 @@
      /** cascade-delete strategy */
      private CascadeDeleteStrategy cascadeDeleteStrategy;
   
  -   /** Batch cascade-delete SQL for the opposite side */
  -   public String batchCascadeDeleteSql;
  +   /** This CMR field and its related CMR field share the same RelationDataManager \
*/  +   private RelationDataManager relationManager;
   
      /**
       * Creates a cmr field for the entity based on the metadata.
  @@ -163,6 +163,11 @@
         this.log = Logger.getLogger(categoryName);
      }
   
  +   public RelationDataManager getRelationDataManager()
  +   {
  +      return relationManager;
  +   }
  +
      public void resolveRelationship() throws DeploymentException
      {
         //
  @@ -296,6 +301,8 @@
         {
            initializeForeignKeyFields();
         }
  +
  +      relationManager = relatedCMRField.initRelationManager(this);
      }
   
      /**
  @@ -502,14 +509,6 @@
      }
   
      /**
  -    * Gets the EntityCache from the related entity.
  -    */
  -   private final EntityCache getRelatedCache()
  -   {
  -      return (EntityCache)getRelatedContainer().getInstanceCache();
  -   }
  -
  -   /**
       * @param ctx - entity's context
       * @return true if entity is loaded, false - otherwise.
       */
  @@ -523,29 +522,30 @@
       * to be created.
       * @param ctx - entity's context.
       */
  -   public synchronized void addRelatedPKsWaitedForMe(EntityEnterpriseContext ctx)
  +   public void addRelatedPKsWaitedForMe(EntityEnterpriseContext ctx)
      {
  -      List relatedPKsWaitingForMe = \
                (List)getRelatedPKsWaitingForMyPK().get(ctx.getId());
  -      if(relatedPKsWaitingForMe == null)
  -         return;
  -
  -      for(Iterator waitingPKsIter = relatedPKsWaitingForMe.iterator(); \
waitingPKsIter.hasNext();)  +      final Map relatedPKsMap = \
getRelatedPKsWaitingForMyPK();  +      synchronized(relatedPKsMap)
         {
  -         Object waitingPK = waitingPKsIter.next();
  -         waitingPKsIter.remove();
  -         try
  +         List relatedPKsWaitingForMe = (List)relatedPKsMap.get(ctx.getId());
  +         if(relatedPKsWaitingForMe != null)
            {
  -            EntityEnterpriseContext relatedCtx = \
                (EntityEnterpriseContext)getRelatedCache().get(waitingPK);
  -            if(relatedManager.loadEntity(relatedCtx, false))
  +            for(Iterator waitingPKsIter = relatedPKsWaitingForMe.iterator(); \
waitingPKsIter.hasNext();)  {
  -               relatedCtx.setValid(true);
  -               createRelationLinks(ctx, waitingPK);
  +               Object waitingPK = waitingPKsIter.next();
  +               waitingPKsIter.remove();
  +               try
  +               {
  +                  EJBLocalHome relatedHome = \
getRelatedContainer().getLocalProxyFactory().getEJBLocalHome();  +                  \
relatedFindByPrimaryKey.invoke(relatedHome, new Object[]{waitingPK});  +              \
createRelationLinks(ctx, waitingPK);  +               }
  +               catch(Exception e)
  +               {
  +                  // no such object
  +               }
               }
            }
  -         catch(Exception e)
  -         {
  -            // no such object
  -         }
         }
      }
   
  @@ -1157,12 +1157,12 @@
      }
   
      /**
  -    * Adds the foreign key to the set of related ids, and updates
  -    * any foreign key fields.
  +    * Adds the foreign key to the set of related ids, and updates any foreign key \
                fields.
       */
      public void addRelation(EntityEnterpriseContext myCtx, Object fk)
      {
         addRelation(myCtx, fk, true);
  +      relationManager.addRelation(this, myCtx.getId(), relatedCMRField, fk);
      }
   
      private void addRelation(EntityEnterpriseContext myCtx, Object fk, boolean \
updateForeignKey)  @@ -1187,12 +1187,12 @@
      }
   
      /**
  -    * Removes the foreign key to the set of related ids, and updates
  -    * any foreign key fields.
  +    * Removes the foreign key to the set of related ids, and updates any foreign \
                key fields.
       */
      public void removeRelation(EntityEnterpriseContext myCtx, Object fk)
      {
         removeRelation(myCtx, fk, true, true);
  +      relationManager.removeRelation(this, myCtx.getId(), relatedCMRField, fk);
      }
   
      private void removeRelation(EntityEnterpriseContext myCtx,
  @@ -1450,14 +1450,11 @@
   
      /**
       * This method is never called.
  -    * In case of a CMR
  -    * - with foreign key fields, only the foreign key fields are asked for the \
                dirty state.
  -    * - from m:m relationship, it is never dirty because added/removed key pairs \
                are stored in application
  -    * tx data map.
  +    * In case of a CMR with foreign key fields, only the foreign key fields are \
                asked for the dirty state.
       */
      public boolean isDirty(EntityEnterpriseContext ctx)
      {
  -      throw new UnsupportedOperationException();
  +      return foreignKeyFields == null ? relationManager.isDirty() : false;
      }
   
      /**
  @@ -1477,24 +1474,32 @@
         return hasFKFieldsMappedToCMPFields;
      }
   
  -   public synchronized void addRelatedPKWaitingForMyPK(Object myPK, Object \
relatedPK)  +   public void addRelatedPKWaitingForMyPK(Object myPK, Object relatedPK)
      {
         Map relatedPKsWaitingForMyPK = getRelatedPKsWaitingForMyPK();
  -      List relatedPKs = (List)relatedPKsWaitingForMyPK.get(myPK);
  -      if(relatedPKs == null)
  +      synchronized(relatedPKsWaitingForMyPK)
         {
  -         relatedPKs = new ArrayList(1);
  -         relatedPKsWaitingForMyPK.put(myPK, relatedPKs);
  +         List relatedPKs = (List)relatedPKsWaitingForMyPK.get(myPK);
  +         if(relatedPKs == null)
  +         {
  +            relatedPKs = new ArrayList(1);
  +            relatedPKsWaitingForMyPK.put(myPK, relatedPKs);
  +         }
  +         relatedPKs.add(relatedPK);
         }
  -      relatedPKs.add(relatedPK);
      }
   
  -   public synchronized void removeRelatedPKWaitingForMyPK(Object myPK, Object \
relatedPK)  +   public void removeRelatedPKWaitingForMyPK(Object myPK, Object \
relatedPK)  {
  -      List relatedPKs = (List)getRelatedPKsWaitingForMyPK().get(myPK);
  -      if(relatedPKs == null)
  -         return;
  -      relatedPKs.remove(relatedPK);
  +      final Map relatedPKMap = getRelatedPKsWaitingForMyPK();
  +      synchronized(relatedPKMap)
  +      {
  +         List relatedPKs = (List)relatedPKMap.get(myPK);
  +         if(relatedPKs != null)
  +         {
  +            relatedPKs.remove(relatedPK);
  +         }
  +      }
      }
   
      /**
  @@ -1625,6 +1630,22 @@
         return (Map)relatedPKValuesWaitingForMyPK.get();
      }
   
  +   private RelationDataManager initRelationManager(JDBCCMRFieldBridge \
relatedField)  +   {
  +      if(relationManager == null)
  +      {
  +         if(metadata.isMultiplicityMany() && \
metadata.getRelatedRole().isMultiplicityMany())  +         {
  +            relationManager = new M2MRelationManager(this, relatedField);
  +         }
  +         else
  +         {
  +            relationManager = EMPTY_RELATION_MANAGER;
  +         }
  +      }
  +      return relationManager;
  +   }
  +
      public String toString()
      {
         return entity.getEntityName() + '.' + getFieldName();
  @@ -1925,4 +1946,87 @@
         {
         }
      }
  +
  +   public static interface RelationDataManager
  +   {
  +      void addRelation(JDBCCMRFieldBridge field, Object id, JDBCCMRFieldBridge \
relatedField, Object relatedId);  +
  +      void removeRelation(JDBCCMRFieldBridge field, Object id, JDBCCMRFieldBridge \
relatedField, Object relatedId);  +
  +      boolean isDirty();
  +
  +      RelationData getRelationData();
  +   }
  +
  +   private static final RelationDataManager EMPTY_RELATION_MANAGER = new \
RelationDataManager()  +   {
  +      public void addRelation(JDBCCMRFieldBridge field, Object id, \
JDBCCMRFieldBridge relatedField, Object relatedId)  +      {
  +      }
  +
  +      public void removeRelation(JDBCCMRFieldBridge field, Object id, \
JDBCCMRFieldBridge relatedField, Object relatedId)  +      {
  +      }
  +
  +      public boolean isDirty()
  +      {
  +         return false;
  +      }
  +
  +      public RelationData getRelationData()
  +      {
  +         throw new UnsupportedOperationException();
  +      }
  +   };
  +
  +   public static class M2MRelationManager
  +      implements RelationDataManager
  +   {
  +      private final JDBCCMRFieldBridge leftField;
  +      private final JDBCCMRFieldBridge rightField;
  +
  +      private final TransactionLocal relationData = new TransactionLocal()
  +      {
  +         protected Object initialValue()
  +         {
  +            return new RelationData(leftField, rightField);
  +         }
  +      };
  +
  +      public M2MRelationManager(JDBCCMRFieldBridge leftField, JDBCCMRFieldBridge \
rightField)  +      {
  +         this.leftField = leftField;
  +         this.rightField = rightField;
  +      }
  +
  +      public void addRelation(JDBCCMRFieldBridge field,
  +                              Object id,
  +                              JDBCCMRFieldBridge relatedField,
  +                              Object relatedId)
  +      {
  +         final RelationData local = getRelationData();
  +         local.addRelation(field, id, relatedField, relatedId);
  +      }
  +
  +      public void removeRelation(JDBCCMRFieldBridge field,
  +                                 Object id,
  +                                 JDBCCMRFieldBridge relatedField,
  +                                 Object relatedId)
  +      {
  +         RelationData local = getRelationData();
  +         local.removeRelation(field, id, relatedField, relatedId);
  +      }
  +
  +      public boolean isDirty()
  +      {
  +         RelationData local = getRelationData();
  +         return local.isDirty();
  +      }
  +
  +      public RelationData getRelationData()
  +      {
  +         final RelationData local = (RelationData)relationData.get();
  +         return local;
  +      }
  +   }
   }
  
  
  
  1.47      +73 -33    \
jboss/src/main/org/jboss/ejb/plugins/cmp/jdbc/bridge/JDBCEntityBridge.java  
  Index: JDBCEntityBridge.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss/src/main/org/jboss/ejb/plugins/cmp/jdbc/bridge/JDBCEntityBridge.java,v
  retrieving revision 1.46
  retrieving revision 1.47
  diff -u -r1.46 -r1.47
  --- JDBCEntityBridge.java	11 Jan 2004 15:09:57 -0000	1.46
  +++ JDBCEntityBridge.java	28 Feb 2004 17:38:09 -0000	1.47
  @@ -651,11 +651,6 @@
         }
      }
   
  -   public static boolean isCreated(EntityEnterpriseContext ctx)
  -   {
  -      return getEntityState(ctx).isCreated();
  -   }
  -
      public static boolean isEjbCreateDone(EntityEnterpriseContext ctx)
      {
         return getEntityState(ctx).ejbCreateDone;
  @@ -671,17 +666,52 @@
         getEntityState(ctx).ejbCreateDone = true;
      }
   
  -   /**
  -    * Returns the mask for dirty fields.
  -    */
  -   public boolean isDirty(EntityEnterpriseContext ctx)
  +   public boolean isModified(EntityEnterpriseContext ctx)
      {
  -      return getEntityState(ctx).isDirty(ctx);
  +      boolean modified = false;
  +      final EntityState entityState = getEntityState(ctx);
  +      if(entityState.isCreated())
  +      {
  +         for(int i = 0; i < tableFields.length; ++i)
  +         {
  +            final JDBCCMPFieldBridge field = tableFields[i];
  +            if(entityState.isCheckDirty(i) && field.isDirty(ctx))
  +            {
  +               modified = true;
  +               break;
  +            }
  +         }
  +
  +         if(!modified)
  +         {
  +            for(int i = 0; i < cmrFields.length; ++i)
  +            {
  +               if(cmrFields[i].isDirty(ctx))
  +               {
  +                  modified = true;
  +                  break;
  +               }
  +            }
  +         }
  +      }
  +      return modified;
      }
   
      public FieldIterator getDirtyIterator(EntityEnterpriseContext ctx)
      {
  -      return getEntityState(ctx).getDirtyIterator(ctx);
  +      int dirtyFields = 0;
  +      final EntityState entityState = getEntityState(ctx);
  +      for(int i = 0; i < tableFields.length; ++i)
  +      {
  +         JDBCCMPFieldBridge field = tableFields[i];
  +         if(entityState.isCheckDirty(i) && field.isDirty(ctx))
  +         {
  +            entityState.setUpdateRequired(i);
  +            ++dirtyFields;
  +         }
  +      }
  +
  +      return dirtyFields > 0 ? getEntityState(ctx).getDirtyIterator(ctx) : \
EMPTY_FIELD_ITERATOR;  }
   
      public boolean hasLockedFields(EntityEnterpriseContext ctx)
  @@ -1195,6 +1225,15 @@
         }
   
         /**
  +       * Marks the field to be updated.
  +       * @param fieldIndex  index of the field.
  +       */
  +      public void setUpdateRequired(int fieldIndex)
  +      {
  +         fieldFlags[fieldIndex] |= DIRTY;
  +      }
  +
  +      /**
          * The field will be checked for dirty state at commit.
          * @param fieldIndex  index of the field.
          */
  @@ -1204,6 +1243,15 @@
         }
   
         /**
  +       * @param fieldIndex the index of the field that should be checked for dirty \
state.  +       * @return true if the field should be checked for dirty state.
  +       */
  +      public boolean isCheckDirty(int fieldIndex)
  +      {
  +         return (fieldFlags[fieldIndex] & CHECK_DIRTY) > 0;
  +      }
  +
  +      /**
          * Marks the field as clean.
          * @param fieldIndex  nextIndex of the field.
          */
  @@ -1221,23 +1269,9 @@
            fieldFlags[fieldIndex] = tableFields[fieldIndex].getDefaultFlags();
         }
   
  -      public boolean isDirty(EntityEnterpriseContext ctx)
  -      {
  -         int dirtyFields = 0;
  -         for(int i = 0; i < fieldFlags.length; ++i)
  -         {
  -            if((fieldFlags[i] & CHECK_DIRTY) > 0 && tableFields[i].isDirty(ctx))
  -            {
  -               fieldFlags[i] |= DIRTY;
  -               ++dirtyFields;
  -            }
  -         }
  -         return dirtyFields > 0;
  -      }
  -
         public FieldIterator getDirtyIterator(EntityEnterpriseContext ctx)
         {
  -         return new MaskFieldIterator(DIRTY | ADD_TO_SET_ON_UPDATE);
  +         return new MaskFieldIterator((byte)(DIRTY | ADD_TO_SET_ON_UPDATE));
         }
   
         public boolean hasLockedFields()
  @@ -1256,7 +1290,7 @@
   
         public FieldIterator getLockedIterator(EntityEnterpriseContext ctx)
         {
  -         return new MaskFieldIterator(LOCKED | ADD_TO_WHERE_ON_UPDATE);
  +         return new MaskFieldIterator((byte)(LOCKED | ADD_TO_WHERE_ON_UPDATE));
         }
   
         public boolean lockValue(int fieldIndex)
  @@ -1280,22 +1314,28 @@
   
         private class MaskFieldIterator implements FieldIterator
         {
  -         private final int flagMask;
  +         private final byte flagMask;
            private int nextIndex = 0;
            private int curIndex = -1;
   
  -         public MaskFieldIterator(int flagMask)
  +         public MaskFieldIterator(byte flagMask)
            {
               this.flagMask = flagMask;
            }
   
            public boolean hasNext()
            {
  -            boolean hasNext;
  -            while((hasNext = nextIndex < fieldFlags.length)
  -               && (fieldFlags[nextIndex] & flagMask) == 0)
  +            while(nextIndex < fieldFlags.length)
  +            {
  +               if((fieldFlags[nextIndex] & flagMask) > 0)
  +               {
  +                  return true;
  +               }
  +
                  ++nextIndex;
  -            return hasNext;
  +            }
  +
  +            return false;
            }
   
            public JDBCCMPFieldBridge next()
  
  
  


-------------------------------------------------------
SF.Net is sponsored by: Speed Start Your Linux Apps Now.
Build and deploy apps & Web services for Linux with
a free DVD software kit from IBM. Click Now!
http://ads.osdn.com/?ad_id=1356&alloc_id=3438&op=click
_______________________________________________
jboss-cvs-commits mailing list
jboss-cvs-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jboss-cvs-commits


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic