# Conditional update
The method BeanDatabaseService.newUpdateIf
enables to read and change a bean atomic in
a transaction without allowing other processes to read and change the
Bean in the meantime. For this purpose, the bean is read with
ReadForUpdate
and is written in the transaction.
# Using the method with an example
boolean updated = beanDatabaseService.newUpdateIf("contact", "1")
.setProperty("firstname", new CEVarchar("Toplevel changed"))
.setProperty("lastname", new CEVarchar("Administrator changed"))
.addSelectFields("firstname")
.setIf(bean -> {
CEVarchar firstname = CETypeUtil.getCEType(bean.getProperties().get("firstname"));
if (firstname.getValue().equals("Toplevel")) {
return true;
} else {
return false;
}
}).execute(identVO.getSessionID());
The method newUpdateIf uses the functionality of the method
BeanDatabaseService.newUpdate
with the restriction that unused
constructors and the method setId(...)
are not implemented.
The method newUpdateIf needs the module
and the id
as parameters
of the bean which should be read and changed. The method returns a
UpdateIfBuilder
back.
On the 'UpdateIfBuilder' the following functions can be executed
Method | Description |
---|---|
setProperty | Contents that are set when the condition is fulfilled. Can be called several times. |
addSelectFields | Fields of the bean that must be read to check the condition. Can be called multiple times or with an array. |
setIf | Condition to be checked, whether the bean needs to be updated or not |
execute | First a transaction is opened, the bean is read, the condition is checked, if the condition is fulfilled, the bean is updated. If the update is successful, true is returned, otherwise false. |