Package net.sf.basedb.util
Class Diff3
java.lang.Object
net.sf.basedb.util.Diff3
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:
- The
oldItem
is loaded and the user is presented with a screen to modify the properties of the item. The client application must keep the item in memory while the user is doing the modifications. - When the user submits the changes the
newItem
is loaded. Both objects are passed to theDiff3(BasicItem, BasicItem)
constructor. The construcor checks if both objects are of the same version or not. - The client application uses the
getValue(Object, Object, Object)
method to set the properties on thenewItem
object.
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 (må, 06 apr 2009) $
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescription<T> T
getValue
(T oldValue, T newValue, T requestValue) Get the correct value to set on thenewItem
object.static boolean
isEqualOrNull
(Object o1, Object o2) Check if two objects are both null or equal as defined by theObject.equals(Object)
method.
-
Field Details
-
sameVersion
private final boolean sameVersion -
what
-
-
Constructor Details
-
Diff3
Create a newDiff3
object to compare properties of theoldItem
andnewItem
. 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 itemnewItem
- 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 Details
-
getValue
Get the correct value to set on thenewItem
object.- If the old and new item have the same version
or if the
oldValue
is the same as thenewValue
therequestValue
is returned. This represents the case that no other user has modified the item, or it was modified but not this particular property. - If the old and request value are equal the
newValue
is returned. This represents the case that the other user has modified the property, but the current user hasn't. - If the new and request value are equal the
newValue
is returned. This represents the case that both users has modified the property and, fortunately, set it to the same value. - Otherwise, it all three values are different, a
ItemModifiedException
is thrown.
- Parameters:
oldValue
- The value of the property on the old itemnewValue
- The value of the property on the new itemrequestValue
- 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
- If the old and new item have the same version
or if the
-
isEqualOrNull
Check if two objects are both null or equal as defined by theObject.equals(Object)
method.- Parameters:
o1
- Object to compareo2
- Object to compare- Returns:
- TRUE if equal or null, FALSE otherwise.
-