2.17.2: 2011-06-17

net.sf.basedb.util
Class Diff3

java.lang.Object
  extended by net.sf.basedb.util.Diff3

public class Diff3
extends Object

This class is helpful when a client application needs to implement long-running transactions and still be able to update items with as few clashes as possible. Using this class it is possible to avoid ItemModifiedException:s even when two or more users are modifying the same item at the same time. The only requirement is that they don't change the same property to different values. Here is how to use this class:

edit.jsp
Label label = Label.getById(dbControl, labelId);
sessionControl.setSessionSetting("LABEL", label);

submit.jsp
Label oldLabel = (Label)sessionControl.getSessionSetting("LABEL");
Label newLabel = Label.getById(dbControl, labelId);
Diff3 d3 = new Diff3(oldLabel, newLabel);
newLabel.setName(d3.getValue(oldLabel.getName(), newLabel.getName(), 
   request.getParameter("name")));
// ... etc ...
dbControl.commit();
Additional code to check for session timeouts and creating new items is also required.

Version:
2.0
Author:
Nicklas
Last modified
$Date: 2009-04-06 14:52:39 +0200 (Mon, 06 Apr 2009) $

Field Summary
private  boolean sameVersion
           
private  String what
           
 
Constructor Summary
Diff3(BasicItem oldItem, BasicItem newItem)
          Create a new Diff3 object to compare properties of the oldItem and newItem.
 
Method Summary
<T> T
getValue(T oldValue, T newValue, T requestValue)
          Get the correct value to set on the newItem object.
static boolean isEqualOrNull(Object o1, Object o2)
          Check if two objects are both null or equal as defined by the Object.equals(Object) method.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sameVersion

private final boolean sameVersion

what

private final String what
Constructor Detail

Diff3

public Diff3(BasicItem oldItem,
             BasicItem newItem)
      throws InvalidDataException
Create a new Diff3 object to compare properties of the oldItem and newItem. Both objects mustn't be null and they must represent the same item in the database. For new objects, not yet in the database, both parameters must be the same instance.

Parameters:
oldItem - The "old" representation of the item
newItem - The "new" representation of the item
Throws:
InvalidDataException - If one of the parameters is null or they don't represent the same database item
Method Detail

getValue

public <T> T getValue(T oldValue,
                      T newValue,
                      T requestValue)
           throws ItemModifiedException
Get the correct value to set on the newItem object.

Parameters:
oldValue - The value of the property on the old item
newValue - The value of the property on the new item
requestValue - The vale of the property the current user has set on the edit screen
Returns:
The most appropriate value as described above
Throws:
ItemModifiedException - If all values are different

isEqualOrNull

public static boolean isEqualOrNull(Object o1,
                                    Object o2)
Check if two objects are both null or equal as defined by the Object.equals(Object) method.

Parameters:
o1 - Object to compare
o2 - Object to compare
Returns:
TRUE if equal or null, FALSE otherwise.

2.17.2: 2011-06-17