Changeset 5052


Ignore:
Timestamp:
Aug 18, 2009, 8:47:26 AM (13 years ago)
Author:
Nicklas Nordborg
Message:

References #108: Logging the change history of an item

  • Added option to include a list with the modified properties in the change log
  • Added special logger implementation for biomaterial events
Location:
trunk
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/config/dist/base.config

    r5048 r5052  
    149149# If the "Change history" tab should be visible in the web interface
    150150# or not. It is hidden by default.
    151 # changelog.show-in-web = false
     151# changelog.show-in-web = true
     152
     153# If DB logger should log detailed information about which properties
     154# that has been updated (default: false)
     155# changelog.dblogger.detailed-properties = true
    152156
    153157# ===============
  • trunk/src/core/net/sf/basedb/core/data/BioMaterialEventData.java

    r4889 r5052  
    4040public class BioMaterialEventData
    4141  extends BasicData
    42   implements RegisteredData
     42  implements RegisteredData, LoggableData
    4343{
    4444
  • trunk/src/core/net/sf/basedb/core/log/EntityDetails.java

    r5038 r5052  
    2626import net.sf.basedb.core.data.ChangeHistoryDetailData;
    2727
     28import org.hibernate.EntityMode;
    2829import org.hibernate.type.Type;
     30import org.hibernate.type.VersionType;
    2931
    3032/**
     
    125127 
    126128  /**
     129    Get all modified properties as a string, separating each
     130    property name with the given separator.
     131    @param prefix A string that is added first in the result, or null
     132    @param separator A separator string (if null, comma is used)
     133    @param suffix A string that is added last in the result, or null
     134    @return A string with the modified properties, or null
     135      if this is a CREATE or DELETE change or if no modified
     136      properties was found
     137  */
     138  public String getModifiedProperties(String prefix, String separator, String suffix)
     139  {
     140    if (changeType != ChangeType.UPDATE) return null;
     141   
     142    StringBuilder modified = new StringBuilder();
     143    if (prefix != null) modified.append(prefix);
     144    if (separator == null) separator = ",";
     145    String s = "";
     146    int numModified = 0;
     147    for (int i = 0; i < types.length; ++i)
     148    {
     149      Type t = types[i];
     150      if (!t.isCollectionType() && !(t instanceof VersionType))
     151      {
     152        Object c = state[i];
     153        Object o = previousState[i];
     154        if (!t.isEqual(c, o, EntityMode.POJO))
     155        {
     156          modified.append(s).append(properties[i]);
     157          s = separator;
     158          numModified++;
     159        }
     160      }
     161    }
     162    if (suffix != null) modified.append(suffix);
     163    return numModified == 0 ? null : modified.toString();
     164  }
     165 
     166  /**
    127167    Create a new ChangeHistoryDetailData object from the information
    128168    in this object assuming that the entity is a subclass of
    129169    {@link BasicData}.
     170    @param detailedProperties If TRUE, the {@link ChangeHistoryDetailData#getChangeInfo()}
     171      is populated with a list of the names of the modified properties (only
     172      relevant for UPDATE changes)
    130173    @return A new ChangeHistoryDetailData instance
    131174  */
    132   public ChangeHistoryDetailData toChangeHistoryDetailData()
     175  public ChangeHistoryDetailData toChangeHistoryDetailData(boolean detailedProperties)
     176  {
     177    return toChangeHistoryDetailData((BasicData)entity, changeType, detailedProperties);
     178  }
     179
     180  /**
     181    Create a new ChangeHistoryDetailData object using a mix of the information
     182    in this object, and the given parameters.
     183    @param data The entity that was changed
     184    @param changeType The change type
     185    @param detailedProperties If TRUE, the {@link ChangeHistoryDetailData#getChangeInfo()}
     186      is populated with a list of the names of the modified properties (only
     187      relevant for UPDATE changes)
     188    @return  A new ChangeHistoryDetailData instance
     189  */
     190  public ChangeHistoryDetailData toChangeHistoryDetailData(BasicData data, ChangeType changeType, boolean detailedProperties)
    133191  {
    134192    ChangeHistoryDetailData details = new ChangeHistoryDetailData();
    135     BasicData data = (BasicData)entity;
    136193    details.setChangeType(changeType.getValue());
    137194    details.setItemId(data.getId());
    138195    details.setItemType(Item.fromDataObject(data).getValue());
     196    if (detailedProperties)
     197    {
     198      details.setChangeInfo(getModifiedProperties("Updated: ", ", ", null));
     199    }
    139200    return details;
    140201  }
  • trunk/src/core/net/sf/basedb/core/log/db/DbLogManagerFactory.java

    r5038 r5052  
    2525import java.util.Map;
    2626
     27import net.sf.basedb.core.Config;
    2728import net.sf.basedb.core.LogControl;
     29import net.sf.basedb.core.data.BioMaterialEventData;
    2830import net.sf.basedb.core.data.LoggableData;
    2931import net.sf.basedb.core.log.EntityLogger;
     
    4648  private final Map<Class, EntityLogger> specialLoggers;
    4749  private final DefaultEntityLogger defaultLogger;
    48  
     50  private final boolean detailedProperties;
    4951 
    5052  public DbLogManagerFactory()
    5153  {
    5254    this.specialLoggers = new HashMap<Class, EntityLogger>();
    53     this.defaultLogger = new DefaultEntityLogger();
     55    this.detailedProperties = Config.getBoolean("changelog.dblogger.detailed-properties");
     56    this.defaultLogger = new DefaultEntityLogger(detailedProperties);
     57    setSpecialLogger(BioMaterialEventData.class, new BioMaterialEventLogger(detailedProperties));
    5458    //setSpecialLogger(AnnotationData.class, new AnnotationLogger());
    5559  }
  • trunk/src/core/net/sf/basedb/core/log/db/DefaultEntityLogger.java

    r5038 r5052  
    4040{
    4141
    42   public DefaultEntityLogger()
    43   {}
     42  private final boolean detailedProperties;
     43 
     44  /**
     45    Creates a new entity logger.
     46    @param detailedProperties If TRUE, the log includes a list with
     47      the properties that was modified
     48  */
     49  public DefaultEntityLogger(boolean detailedProperties)
     50  {
     51    this.detailedProperties = detailedProperties;
     52  }
    4453 
    4554  /*
     
    5564  {
    5665    DbLogManager dbLogManager = (DbLogManager)logManager;
    57     ChangeHistoryDetailData change = details.toChangeHistoryDetailData();
     66    ChangeHistoryDetailData change = details.toChangeHistoryDetailData(detailedProperties);
    5867    dbLogManager.logChangeDetails(change);
    5968  }
Note: See TracChangeset for help on using the changeset viewer.