Portlet Factory, Version 6.1.2
EJB Call builder: working with linked objects
To use the EJB Call builder effectively, be familiar with standard WebSphere Portlet Factory techniques for dealing with linked objects. For example, there are two ways of calling a method of the linked account EJB from within a script. You might use:
Object o =webAppAccess.getVariables().getObject("Account"); examples.ejb.basic.beanManaged.Account account = (examples.ejb.basic.beanManaged.Account)o; balance = account.getBalance();or
Double balance = (Double)webAppAccess.callMethod("Account.balance");For most purposes these approaches are equivalent.
When you call a method of the remote interface, WebSphere Portlet Factory first verifies if it has already instantiated that object. If not, it calls the constructor method (default or custom) to do so. Once the object is instantiated, its object reference will exist until you explicitly destroy the object, by calling (for example):
webAppAccess.getVariables().setObject("Account", null);An example custom constructor for the account bean might look as follows:
1 String accountID = webAppAccess.getVariables().getString("accountID"); 2 Variable home = webAppAccess.getVariables().getVariable("AccountHome"); 3 examples.ejb.basic.beanManaged.AccountHome accountHome = 4 (examples.ejb.basic.beanManaged.AccountHome)home.getValue(); 5 Object account = null; 6 try { 7 account = accountHome.findByPrimaryKey(accountID); 8 } catch(Exception error) { 9 try { 10 account = accountHome.create(accountID, 0); 11 } catch(Exception e) {} 12 webAppAccess.getVariables().setString("accountBalance",""); 13 return account;The line-by-line action in this code sample is as follows: (1) Get the user's account ID from a text input field. (2-5) Fetch the home object, which is used to create and find remote objects. (6, 7) Try to find an account with the given ID number. (8) If no account with that ID is found, some sort of "ObjectNotFound" exception is thrown, which you can catch, (9-12) If exception occurs, create a new account with the given ID and starting balance. (13) Return this information.
Parent topic: EJB Call builder
Library | Support |