public class Diff3
extends java.lang.Object
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:
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.
newItem
is loaded.
Both objects are passed to the Diff3(BasicItem, BasicItem)
constructor.
The construcor checks if both objects are of the same version or not.
getValue(Object, Object, Object)
method to
set the properties on the newItem
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.
Modifier and Type | Field and Description |
---|---|
private boolean |
sameVersion |
private java.lang.String |
what |
Constructor and Description |
---|
Diff3(BasicItem oldItem,
BasicItem newItem)
Create a new
Diff3 object to compare properties
of the oldItem and newItem . |
Modifier and Type | Method and Description |
---|---|
<T> T |
getValue(T oldValue,
T newValue,
T requestValue)
Get the correct value to set on the
newItem object. |
static boolean |
isEqualOrNull(java.lang.Object o1,
java.lang.Object o2)
Check if two objects are both null or equal as defined
by the
Object.equals(Object) method. |
public Diff3(BasicItem oldItem, BasicItem newItem) throws InvalidDataException
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.oldItem
- The "old" representation of the itemnewItem
- The "new" representation of the itemInvalidDataException
- If one of the parameters is null
or they don't represent the same database itempublic <T> T getValue(T oldValue, T newValue, T requestValue) throws ItemModifiedException
newItem
object.
oldValue
is the same as the
newValue
the requestValue
is returned. This represents the case that no other
user has modified the item, or it was modified but not
this particular property.
newValue
is returned. This represents the case that the other
user has modified the property, but the current user hasn't.
newValue
is returned. This represents the case that both users
has modified the property and, fortunately, set it to the
same value.
ItemModifiedException
is thrown.
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 screenItemModifiedException
- If all values are differentpublic static boolean isEqualOrNull(java.lang.Object o1, java.lang.Object o2)
Object.equals(Object)
method.o1
- Object to compareo2
- Object to compare