Writing Models Programmatically
In order to write a model you need to have configured an action within the model capable of performing a write action. For more information on creating actions, see the section within this chapter titled "State Model".
Below is a simple example of writing a model to the database using an action.
Book bookObj=
new
Book();
bookObj.setTitle(
"Lord of the Rings"
);
bookObj.setAuthor(
"J.R.R. Tolkien"
);
//etc. with setting up the object.
//set up model data services, model list, and set the action
String actionName =
"ZBKS.Create"
;
ModelDataService mds = Services.get(ModelDataService.
class
);
ModelList<Book> mlBook =
new
ModelList<Book>(actionName, bookObj);
mbs2.setActionName(actionName);
//now to write it
ModelList<Book> failedBooks = mds.write(mlBook, getPlatformUserContext());
//make sure they worked
if
(failedBooks.getErrors().size() >
0
) {
//do something if the write fails
}
Once you create the object you want to write, and have identified an action to use, create an instance of ModelDataService. ModelDataService is responsible for all persistence operations in the SDK. Its write method takes two arguments. The first is a ModelList, which is a type-safe collection of whatever type of model you wish to write. You can write many models in one pass by adding them each to the model list. The second parameter is the Platform User Context for the current user. This is available in any of the classes where you would want to programmatically manipulate models such as within a REST resource class, a workflow, an IXM Engine, etc.
The write operation returns a second model list, again type-safe to the type you are working with. If any of the write actions from the model list failed, they are returned in this returned model list with error codes attached.