API Business Services - Creation or modification triggers
Implement a class extending the class com.noheto.restapi.APIBusinessServiceAdapter
to modify some properties of an instance object, before saving it after a creation or modification.
It is necessary to indicate in the action configuration (so in the JSON description) which component you want to use (indicating the plugin and the name of the class).
Methods
beforeCreate: executed before saving after creating a new instance
beforeUpdate: executed before saving after updating an instance
beforeCreateItem: executed before saving a new collection item
updateOrCreate: allow to intercept a creation or modification if you need to replace the instance, or cancel the process
First argument (com.noheto.restapi.BusinessServiceContext) provides methods:
getSurfer() to get the surfer
getAction() to get the action name
getLogger() to get a RESTAPI plug-in logger
getLocale() to get the current invokation locale (not the surfer locale)
isUpdate() to test if the current action does a modification
isCreate() to test if the current action does a creation
setInfo(String) to provides an info in response of invocation (string or json string)
getInfo() to get the current info
getUpdateProperties() to get the names of updatable properties
getConfiguration() to get the current action configuration
Example
Copy the concatenation of properties code1
and code2
to property name
.
package fr.wedia.demo;
import com.noheto.restapi.APIBusinessServiceAdapter;
import wsnoheto.engine.IObjectWritable;
public class DemoBusinessService extends APIBusinessServiceAdapter {
@Override
public void beforeCreate(String action, IObjectWritable newObject) {
switch(newObject.getObjectType().toString()) {
case "product":
// build the name by concatenating the both codes
try {
String name = newObject.getProperty("code1") + newObject.getProperty("code2");
newObject.setProperty(8, name);
} catch (Throwable e) {
throw new RuntimeException("Couldn't set product name",e);
}
break;
default:
// does nothing
}
}
}
DAM services
It is possible to specify the trigger in the service configuration, but to have a service configuration without JSON, you can use the extension com.noheto.restapi.DamContributionAdapter