For most AS/400 programmers, Java is an unfamiliar language: first, its syntax is unlike RPG or COBOL; second, it is object-oriented. But, with IBM's VisualAge for Java, it may not be as difficult to learn, because you can learn how to use it incrementally. You can start with the Rapid Application Development (RAD) environment to get applications up quickly without writing one line of code. Later, as you get more comfortable with Java's syntax and object-oriented programming, you can dig deeper and start writing and modifying Java code. This article will give you an overview about how you might use VisualAge for Java to create an Internet- or intranet-ready application. To illustrate how easy it is, a hypothetical order-entry applet/application is presented.
VisualAge for Java is available in three editions: Entry (which can be downloaded free from http://www.software.ibm.com/ad/vajava), Professional, and Enterprise. (In the first quarter of 1998, VisualAge for Java, AS/400 Feature will be available for the Enterprise edition. It will include special AS/400 features such as the AS/400 Toolbox for Java and a server-side debugger. A beta version of the AS/400 Toolbox for Java is available today at http://iws.as400.ibm.com/toolbox/javabeta.htm.)
To develop this application, we selected the Enterprise edition of VisualAge for Java, because the VisualAge for Java Enterprise edition includes the following components not included in the Professional edition:
o Database Access allows access to any relational database that supports an ODBC or a JDBC driver.
o CICS Access allows CICS transactions to be wrappered (externalized) and used within a Java application.
o C ++ Server Access allows access to C++ services by generating Java beans and C++ code to allow interoperability between Java and C++.
o RMI Access allows a Java program in one virtual machine to send messages to another Java object running in a different virtual machine. These objects can even be on different machines.
Enterprise Access Builder for Data (also referred to as Data Access Builder or DAX) is part of the VisualAge for Java Enterprise edition. You can use it to generate data access classes based on your existing relational database tables, such as DB2/400.
You use DAX to generate the Java classes to access data. These generated Java classes, which are JavaBeans, can be used directly in your Java programs or within VisualAge for Java composition editor. (JavaBeans are a standard Java class architecture, so these generated classes can be used in any JavaBeans-compliant IDE or utility.) Some of the other key features of DAX include the following:
o JDBC access to data. DAX generates classes that use JDBC to access your database. You can use the JDBC driver, which is part of the AS/400 Toolbox for Java.
o Rapid development, but still object-oriented. In minutes, DAX can generate Java source code that allows you to add, update, delete, and retrieve rows in your database. DAX generates the code in a consistent, extendable, object-oriented fashion, enabling the benefits of object-oriented programming.
o Stored procedures. You can use DAX to generate code that calls stored procedures resident on DB2/400 files. You will often get better performance with stored procedures than with JDBC data access.
o Commitment control and connection. Services are provided for connecting to your databases. In addition, commit and rollback methods are generated for transactions.
This section describes how to create a sample application/applet using the VisualAge for Java DAX. The application you will build is for the ABC Parts Supply Company, a fictitious parts wholesaler. The application simply allows ABC customers to place orders by selecting the customer and the part being ordered, and then entering the quantity of the part being ordered. Today, this function of order taking is being performed by five ABC employee service personnel via the telephone. With some user interface design changes, this application could allow ABC customers to submit orders 24 hours a day, from anywhere, via the Internet while reducing ABC's customer service staff requirements.
The application operates over three DB2/400 files described in Figure 1.
The application being created should look like that illustrated in Figure 2. This window allows you to view a list of parts and customers and to create orders. When the Display Records push button is clicked, all the customer records are read from the database and placed in the customer multicolumn list box. Then, the parts records are read from the database and placed in the parts multicolumn list box. The user can then select a customer and part from the lists.
When the Place Order push button is clicked, the Parts file record is updated (based on the user- specified order quantity), the quantity available is reduced, and the quantity sold field is incremented. Then, a new order record (which includes the customer ID, part ID, and quantity ordered) is written to the ORDERS database.
A key feature of Java is its support of object-oriented programming (OOP). The concept of OOP is too large to cover in detail here, but I will use and discuss elementary elements of OOP. The diagram in Figure 3 illustrates the use of Unified Modeling Language (UML) to describe our object model. UML is a diagramming language to describe object data properties, actions, and relationships with other objects. (Refer to Rational Software Corporation's Web site at http://www.rational.com for more information on UML.) An object model is produced with UML through object-oriented analysis (OOA) and object-oriented design (OOD).
OOP's goal is to increase programmer productivity and quality of software development. Toward this goal, OOP software should be designed to do two things:
1. Model the real world. OOP allows us to create objects and classes that are like their real-world counterparts, making software simpler and more understandable.
2. Reuse software. Objects can be created in an abstract way and then subclassed or extended using inheritance. This allows object properties and operations to be reused.
The UML object model in Figure 3 illustrates the software design we will use for constructing our sample application.
UML uses a rectangle with three compartments to describe a class. The top compartment contains the class name. The middle compartment contains the attributes or properties of the class. The bottom compartment contains the operations or methods that this class will be able to do.
The table in Figure 4 describes all the classes illustrated in Figure 3. Note that only the most significant attributes and operations are listed.
Without the VisualAge for Java Enterprise DAX, we would have to create all the classes manually as well as code all the logic to select, update, and insert records into our database. With the DAX, much of the code is generated for us. First, we create a VisualAge project and package to hold the classes we will create. Next, we start the DAX and select Map Schema, which starts the database-to-Java object mapping process. Selecting the ODBC Data Source that represents your target system identifies the location of your data source. From this point, clicking the Get Tables button will retrieve the available tables and views on the target system. Selecting a particular file, such as the PARTS file, results in a screen like that shown in Figure 5.
The DAX has at this point targeted the database that you specified and retrieved all the fields present in the PARTS database file you selected. DAX will create a Java class named Parts and add variables for each field in the file as well as the Java methods to retrieve and set the variables value. For example, the database contains a field named partno, so DAX will generate an instance variable called partno and a method called getPartno to get the value. DAX will also generate a method called setPartno to set the value of the Java instance variable. We can also change the names of the instance variables to something more descriptive, such as partNumber instead of partno.
Along with the Parts class, several other classes are generated, including a PartsManager. The PartsManager class is capable of retrieving and instantiating (materializing) a collection of Part objects.
Upon completion of the generation process, the classes shown in Figure 6 should be generated by DAX in your Java package. The table contained in Figure 7 describes each generated class. These generated classes are the reusable elements that we will use to build our application.
We then follow the same process to generate Order file and Customer file classes, such as Order, OrderManager, OrderResultForm, Customer, CustomerManager, CustomerResultForm, and so forth (the same as what was generated for the Parts file).
The Company class contains a PartsManager object, an OrderManager object, and a CustomerManager object. This class handles processing for a new order. It integrates our other xxxManager classes to give us the ability to create orders. DAX generated the xxxManager classes for us, but we must create the Company class because it is part of our object model, which DAX knows nothing about. To create the Company class, we simply create a class within one of our packages.
Figure 8 shows the Company class created within a package called SalesCompany. It shows partsManager, orderManager, and customerManager instance variables created as private variables. Along with these variables are methods to get the values of these variables. For example, the customerManager variable has a getCustomerManager method, which returns the value of the variable. The returned value will be an instance of a CustomerManager object. These variables do not have associated set methods, because there is no need in this application to set the variables.
The import statements allow you to use classes and objects that exist in a different package. The defaultDatastore variable is used to hold our connection object. This connection object, which is generated by DAX, contains the URL, connection status, and methods to connect and disconnect from the database. The defaultDatastore variable has a "getter" method to return the datastore object.
Figure 9 shows the "newOrder(Parts aPart, Customer aCustomer, String aQuantity)" method, which becomes our most important method. It is called when the user clicks the Place Order button after selecting a customer and a part and specifying a quantity. This method accepts those three objects in the parameter list. The method then creates a new order object and sets the appropriate data in it, updates the part object by reducing the inventory and incrementing the number sold, and adds the order record to the database.
Our last task is to create a user interface for our ABC Parts Ordering System. We will just assemble and connect the classes that DAX created for us, along with our custom Company class.
We create a new class called OrderMainFrame, which extends from the java.awt.Frame that we will use to compose our GUI windows. Leveraging the power of VisualAge's visual building technology, we quickly and easily connect the components, resulting in a working application as illustrated in Figure 10.
The technical benefits of using VisualAge for Java's DAX instead of coding custom data access classes offer significant timesavings. DAX can generate in minutes what it would take several days to generate by custom coding. In addition, programmers can extend and customize DAX- generated classes. Many advanced capabilities-such as asynchronous processing via threads-are
also generated for your use.
The business benefits of increased customer service and lower employee labor costs are an obvious goal of many organizations that want to leverage the enormous capabilities of the Internet. Collectively, these technologies will allow organizations to wake up and smell the coffee!
Paul Holm is an advisory software engineer within IBM's AS/400 Division. His background includes five years as a DB2/400 developer. More recently, he spent five years as an object- oriented development specialist focusing on Smalltalk and Java. He can be reached by email at
Figure 1: The AS/400 files used by the sample Java application
|
Figure 2: The "Parts Order Management" window
|
|
Figure 4: Sample application class descriptions
|
Figure 5: The DAX generation window
|
|
|
Figure 8: The DAX generation window
|
public void newOrder(Parts aPart , Customer aCustomer, String aQuantity) {
/* convert the input string aQuantity to a short value. It comes in a string because it comes from the
user interface */
short quantitySold = (new Short(aQuantity)).shortValue();
/* Create a new order and populate the order data */
Orders newOrder = new Orders();
newOrder.setQuantity(quantitySold);
newOrder.setCustomerId(aCustomer.getCustomerId());
newOrder.setPartId(aPart.getPartId());
newOrder.setOrdertmsp(new java.sql.Timestamp(System.currentTimeMillis())); // sets the timestamp to
the current clock timestamp
/* Update the part inventory quantity and amount sold. */
short newQuantity = (short)(aPart.getQuantity().shortValue() - quantitySold);
aPart.setQuantity(new Short(newQuantity));
short newNumSold = (short)(aPart.getNumSold().shortValue() + quantitySold);
aPart.setNumSold(new Short(newNumSold));
/** There is a bug in the EAB that prevents the update() methods from running. Instead of using
update() methods we will delete the part record and re-add it with the updated inventory quantity */
Parts aNewPart = (Parts) aPart.clone(); //Create a copy of the part record.
/* Delete and re-add the part */
try {
aPart.delete();
aNewPart.add(); }
catch(Exception e) {
System.out.println("Error updating part "+e); }
/** Add the new order to the database */
try {
newOrder.add(); }
catch(Exception problem) {
System.out.println("error adding order " + problem); }
}
Figure 10: The working application
|
LATEST COMMENTS
MC Press Online