Programmatic Access to Models

When you build a One Network SDK module, the module definitions you create in Studio are used to generate a number of different artifacts including plain ol' Java object (POJO) representations of those models. As such you can instantiate them in any context just as you would any other object. If you want to work with core models, you must import them.

If you want to work with application models, they are in a different package. The package name will depend on the package name you defined when the project was created. For example in the bookstore tutorial, found in the training guide, the package import code would be:

You can find out the package name very easily if you don't happen to know it. Just look at your source tree. Find the model folder in your project under src. Follow it back up to src.

A second way to accomplish the task is to look at the generated POJO's in studio. In order for this trick to work you must have built your module project successfully at least once. Within any Java editor window in Studio, press Ctrl+Shift+R (open resource). Type the name of any of your models and find its POJO. Open the POJO and you'll find the package name at the top of the file.

Beyond that, it is a simple matter of instantiating the object you want to work with.

Be sure you fill in all required fields and supply any related models needed.

You can easily review the required fields by looking at the write action you intend to use. The action's write mask will tell you what fields are required as well as which fields are model references. Model reference fields are listed in all caps as shown in the figure below. The buffer object requires an item and a site, both of which are references to other core models, as intrinsically necessary fields. BufferType in this case was set manually.