|
New innovations in Studio 3.0 have made it surprisingly easier for power users to write the new EJB 2.0 family of beans, which has made writing and assembling enterprise beans more complex while modelling real-world business functions more closely.
A new Container Managed Persistence (CMP) model is at the core of EJB 2.0, and hinges on Container Managed Relationships. This sample routine takes you on a straight-line path to construct two entity beans, give them a relationship, and deploy them on the in-built Pramati Server. A simple JSP acts as the client that looks up the beans.
The sample can be developed and run using other development servers. For instructions, see the relevant chapters on working with other servers in this guide.
The sample is a small banking application that takes a "Customer ID" as input and generates the detailed account information for that customer.
Lets create our first CMP 2.0 application, aptly named PiggyBank.
In the instructions that follow, we assume the user is working on Windows and that Studio is installed in the directory c:\pramati_studio on Windows.
The Desk is necessary to begin organizing application sources. Create a Desk from the main menu by selecting Desk > New to bring up a New Desk.
The Desk is created and displayed in the Explore Panel. See the chapter, "Overview of Pramati Studio", for a quick tour of Studio UI.
To add JDBC Drivers in Studio, click on Tools > Configure JDBC Drivers from the main menu. This brings up the Configure Drivers dialog, where new drivers can be added, modified and deleted. Cloudscape driver is configured in Studio, by default.
Follow the given instructions to add an Oracle driver in Studio:
Follow a similar procedure to configure other drivers.
Create the tables on a target database
PiggyBank application requires customer and account tables. Studio ships with database script files that create these tables on Oracle, IBM Informix and MS SQL Server:
To use another database, you may study these scripts and modify or write a suitable script. To execute the scripts and prepare the tables for the application, choose your database and follow these steps:
Oracle
IBM Informix
MS SQL Server
MS Access Server
Entity beans require data resources defined on the application server where they are being deployed. Resources can be defined on the in-built server with Studio, for entity beans to obtain connections.
Resources are defined in Studio using the Resource Tool and are always local to the Desk.
To start Resource Tool, select Tools > Resources. Define a resource connection under the Data Source tab as follows:
Ensure that Cloudscape is running when the resource is to be added to the database - Informix Cloudscape. To do this on a Windows platform, run cloudscape.bat available under <install_dir>. Run cloudscape.sh available under <install_dir> on a Unix platform.
Note: A sample Cloudscape database with pre-defined tables is shipped with Studio, on which the user can test his application.
Follow the given steps if the database is Informix Cloudscape:
If the database used is Oracle, follow the given steps.
Clicking directly on Add will test and, if successful, add the data resource to the server. If you are modifying a resource, click on Modify after making the changes.
To create a CMP 2.0 bean, first start the Bean Wizard from the main menu by selecting Tools > Bean Wizard.
Expert mode takes user on an exhaustive twelve-step path to writing a customized bean. Quick mode takes the user on a simpler four-step path to creating a bean with standard interfaces, and does not include creating ejbHome, ejbSelect, and the business method skeletons.It creates interfaces with default values in the Quick mode. It also does not include steps where the methods to be published in Home, Local Home, Remote and Local interfaces are added.
Container Managed Fields of the entity bean may be created using an existing table in the database as follows:
This panel assists you in generating ejbCreate methods for the bean.
Finder methods are not required for the Account bean. Click Finish to proceed.
The wizard creates the bean Account, the bean class file, its remote and local interfaces. It stores all these files under the EJB Module, PiggyBankEJB on the Desk, PiggyBank.
The second bean to be created for the application is Customer. It has a container-managed relationship with Account bean that was just created.
You can create a new bean under a module by simply right clicking on the module name on the Desk and choosing New File.
Right click on the EJB Module PiggyBankEJB and select New File. Select EJB and click OK. This starts the now familiar Bean Wizard.
The Module is already selected as PiggyBankEJB.
The Container Managed Relationship panel is used for creating relationships among beans. In this sample, Account and Customer bean are related as follows:
Cascade Delete implies deleting Customer will delete Account too.
Now the relationship is added, add the Join condition to it as follows:
This panel assists you in generating ejbCreate methods for the bean.
Since no finder methods are required in Customer bean, click Finish.
The wizard generates the code for Customer bean and stores it under the EJB Module, PiggyBankEJB on the Desk, PiggyBank.
When the beans and their interfaces are generated, they are compiled automatically. However, to force compile Account and Customer beans, right click on the EJB Module PiggyBankEJB and select Clean Build.
JSP technology enables the generation of dynamic content for a Web client. The sample JSP looks up Account and Customer beans, and displays customer details given an account ID. JSP is created in Studio through standard templates.
The JSP template panel requires the following information:
The JSP skeleton that is generated has standard place holders for business logic code. For the JSP to look up the bean and display details, the skeleton is modified as shown below:
Modification 1
Replace the line
with the following code
Modification 2
Enter the following line of code
before the line
Modification 3
Replace the section
// replace the DemoBean with the actual bean name // DemoHome demoHome = // (DemoHome)ic.lookup("java:comp/env/ejb/DemoBean"); // Demo demo = (Demo)demoHome.create();
with the following lines
CustomerLocalHome custlocalHome = (CustomerLocalHome)ic.lookup("java:comp/env/ejb/CustomerLocal");
String customerId = request.getParameter("customerid");
if(customerId!=null)
{
CustomerLocal custlocal = CustomerLocal)custlocalHome.findByPrimaryKey(customerId);
AccountLocal acclocal = AccountLocal)custlocal.getAccountcmr();
Modification 4
Add the following lines of code in the JSP page immediately after the above section. These lines are executed after the look up succeeds.
%> <h2><% out.println("CMR Sample Demonstration"); if(!lookupFailed){ %><table border=0 cellspacing=5> <tr><td><%out.println("Customer ID : " );%></td><td><% out.println(custlocal.getCustomerid());%></td></tr> <tr><td><%out.println("Customer Name : "); %></td><td><% out.println(custlocal.getName());%></td></tr> <tr><td><%out.println("Account No : " );%></td><td><% out.println(acclocal.getAccountno());%></td></tr> <tr><td><%out.println("Account Balance : "); %></ td><td><% out.println(acclocal.getBalance());%></td></tr> </table> <%} }
Since the above lines should be included in the try block, ensure they are followed by the closing parenthesis of the try statement.
Modification 5
Replace the section
with the lines
<FORM METHOD="GET" > <h3>Enter Customer ID</h3> <INPUT TYPE="TEXT" NAME="customerid" SIZE="25"> <INPUT TYPE="SUBMIT" VALUE="SUBMIT"> <INPUT TYPE="HIDDEN" NAME="first" VALUE ="YES"> </FORM>
For the JSP file to look up the bean, the /classes directory should be added to the user classpath. Do this as follows:
Compile the JSP by right clicking on it on the Desk and selecting Compile. Compiling checks the JSP for syntactical errors.
To run the application, both EJB and Web Modules have to be deployed.
This is as easy as right clicking on PiggyBankEJB on the Desk and selecting Deploy. This directly deploys the EJB Module on the server configured as the development server. By default, this is in-built Pramati Server.
Right clicking on PiggyBankWeb on the Desk and selecting Deploy brings up the Deploy tool. Follow the given steps before deploying the Web Module.
Once both modules have been deployed, your first CMP 2.0 application PiggyBank is ready to be accessed by the JSP client.
To run the JSP, right click on it on the Desk and select Run. This starts the browser that is configured for the IDE and runs the JSP in it.
Alternatively, run the JSP from a browser by keying in the following URL:
http://127.0.0.1:8181/PiggyBankWeb/index.jsp Note that, in the URL, the Web Module PiggyBankWeb is the context root of the application.
The application prompts the user for the Customer ID. On entering the ID (example: 100), the application displays the related details from the Account and Customer tables in the database.
| Pramati Technologies © Copyright |
|