Changeset 5067


Ignore:
Timestamp:
Aug 20, 2009, 8:24:48 AM (13 years ago)
Author:
Nicklas Nordborg
Message:

References #108: Logging the change history of an item

Makes it possible to find and log dirty collections.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/core/log/EntityDetails.java

    r5052 r5067  
    2727
    2828import org.hibernate.EntityMode;
     29import org.hibernate.collection.PersistentCollection;
    2930import org.hibernate.type.Type;
    3031import org.hibernate.type.VersionType;
     
    5455  private final String[] properties;
    5556  private final Type[] types;
     57  private final int[] dirty;
    5658 
    5759  public EntityDetails(EntityLogger logger, Object entity, ChangeType changeType,
     
    6567    this.properties = properties;
    6668    this.types = types;
     69    this.dirty = new int[types.length]; // 0 = not known, -1 = not dirty, 1 = dirty
     70    findDirtyCollections();
    6771  }
    6872 
     
    147151    for (int i = 0; i < types.length; ++i)
    148152    {
    149       Type t = types[i];
    150       if (!t.isCollectionType() && !(t instanceof VersionType))
     153      if (dirty[i] == 0)
    151154      {
    152         Object c = state[i];
    153         Object o = previousState[i];
    154         if (!t.isEqual(c, o, EntityMode.POJO))
     155        Type t = types[i];
     156        if (!t.isCollectionType() && !(t instanceof VersionType))
    155157        {
    156           modified.append(s).append(properties[i]);
    157           s = separator;
    158           numModified++;
     158          Object c = state[i];
     159          Object o = previousState[i];
     160          dirty[i] = t.isEqual(c, o, EntityMode.POJO) ? -1 : 1;
    159161        }
     162      }
     163      if (dirty[i] == 1)
     164      {
     165        modified.append(s).append(properties[i]);
     166        s = separator;
     167        numModified++;
    160168      }
    161169    }
     
    200208    return details;
    201209  }
     210 
     211  /**
     212    We must do this from the constructor since the "dirtyness" of a
     213    collection is reset after flush the SQL to the database, which is
     214    completed before the rest of the logging is done.
     215  */
     216  private void findDirtyCollections()
     217  {
     218    for (int i = 0; i < types.length; ++i)
     219    {
     220      Type t = types[i];
     221      if (t.isCollectionType())
     222      {
     223        Object c = state[i];
     224        if (c instanceof PersistentCollection)
     225        {
     226          dirty[i] = ((PersistentCollection)c).isDirty() ? 1 : -1;
     227        }
     228      }
     229    }
     230  }
     231 
    202232}
Note: See TracChangeset for help on using the changeset viewer.