Writing methods
In this topic ...
Utilizing the WebAppAccess Object
Choosing a Method Implementation: LJO or Method Builder
Related Topics ...
Methods do the work of processing form inputs, executing other parts of the Web applications (even other Web applications), and performing any other logic that the WebApp requires to function the way you want it to.
The primary object used in methods is the webAppAccess object, which is an instance of the com.bowstreet.webapp.WebAppAccess class. The webAppAccess object serves as a proxy to the WebApp's structure, represented by the webApp object, which is an instance of the com.bowstreet.webapp.WebApp class.
Utilizing the WebAppAccess Object
An instance of the WebAppAccess class, usually named webAppAccess, is your interface into the structure of the Web application generated from the model object in one way or another. In general terms the webAppAccess object allows you to process user inputs, get and set variable values, and interact in other ways with the current model or other models in the application.
The webAppAccess object also allows you to access system properties, the HttpServletRequest and HttpServletResponse objects, as well as the user's session.
Note: The webAppAccess object changes over the life of the session, so do not store the webAppAccess object as a member variable to an LJO because it will become stale as conditions and variable values change.
Accessing a WebAppAccess Object
We can access a WebAppAccess object for the current model as well as to any linked models. We can also instantiate a WebAppAccess object for models outside the scope of the current model.
To get a WebAppAccess object for the current model:
In the body of a method, use the webAppAccess object that Factory servlet passes to the underlying Java implementation of the method. In the methods of a class used as a Linked Java Object, be sure to declare that the methods first argument is a WebAppAccess object. The Factory servlet passes the method the current webAppAccess object to your method.
To get a WebAppAccess object for a linked model:
In a method, use the following code to instantiate a WebAppAccess object for the specified linked model. You would want to do this if you wanted to get or set the variable values in that model or if you wanted to access any other information about the linked model.
webAppAccess.getLinkedModelInstance("myLinkedModelBuilderCall");
where myLinkedModelBuilderCall is the name of the Linked Model builder call in the model.
To get a WebAppAccess object for a model outside the scope of the current model:
In a method, use the following code to instantiate a WebAppAccess object for the specified linked model. You would want to do this if you wanted to get or set the variable values in that model or if you wanted to access any other information about the linked model.
WebAppAccess remoteWebAppAccess = webAppAccess.getModelInstance("myModels/ModelName", "Region!Southwest", false);
where:
myModels/ModelName Is the path (relative from WEB-INF/models) and name of the model for which you want the webAppAccess
Region!Southwest Is the profile set and profile name to apply to the model. For the model defaults, use "" (an empty String)
false Determines whether or not model instance is created as a singleton object
Choosing a Method Implementation: LJO or Method Builder?
The Designer provides you with two ways to implement logic in Java in your models. We can use a Linked Java Object builder call or we can use a Method builder call. The Linked Java Object adds the public methods of a Java class to your model, while the Method builder adds one Java method to your model. A general rule of thumb is to implement small, simple methods (processing pages, calling model actions, etc.) with the Method builder and use an LJO for larger, more complex methods.
When to Use a Linked Java Object
Use a Linked Java Object builder call to re-use an existing Java class or if you anticipate the methods that you will be writing for the current model will be re-usable by other models. Also, use a Linked Java Object to implement if you want to take advantage of the code development features of a full IDE when writing more complex methods.
When to Use the Method Builder
Use a Method builder call to implement methods that provide model-specific functionality such as displaying pages, setting variable values, invoking service calls, etc. Also, use a method builder if you want to profile various elements of the method, such as arguments or even the code for the method.