Introduction
This document contains an application development scenario that shows how a simple application (the "CustomerService application") is developed using Informix Object Translator. This example application allows bookstore personnel to track customer orders.
This document contains the following information:
The application accesses data from these database tables: CUSTOMERS, ORDERS, and ORDER_LINES.
The CUSTOMERS table contains information about bookstore
customers and how to contact them. The table has the following columns:
CUST_ID | Unique ID for the customer. This column is the primary-key column for the CUSTOMERS table; the primary key is used to identify each customer record in the table. |
NAME | Customer name. |
SERVICE_REP | Employee ID for the service representative assigned to the customer. |
CONTACT | Name of the contact person. This can be the customer name. If the customer is a business, this is the contact person for the business. |
POSITION | Position held by the contact person. |
PHONE | Customer phone number. |
FAX | Customer fax number. |
ADDRESS1 | First line of the customer’s address. |
ADDRESS2 | Second line of the customer’s address. |
CITY | City of the customer’s address. |
STATE | State of the customer’s address. |
ZIP | Zip code of the customer’s address. |
Note: In a real database design, there would probably be a separate table to store the SERVICE_REP and related information about the service representative, because a customer could have multiple orders with different representatives assigned to each order.
The ORDERS table contains information about each
order. The information includes when the order was placed and the shipping
address for the order. This table has the following columns:
ORDER_ID | Unique ID for the order. This column is the primary key for the ORDERS table; the primary key is used to identify each record in the table. |
CUST_ID | ID for the customer who placed the order. In the database, this column is a foreign-key column that references the primary-key column in the CUSTOMERS table. |
DATE_ENTERED | Date when the order was placed. |
DATE_SHIPPED | Date when the order was shipped. |
DISCOUNT | Discount information for the order, if applicable. |
TAX_RATE | Tax rate for the order. |
SHIPPING | Shipping cost associated with the order. |
S_NAME | Name of the person to whom the order was shipped. |
S_ADDRESS1 | First line of the shipping address. |
S_ADDRESS2 | Second line of the shipping address. |
S_CITY | City of the shipping address. |
S_STATE | State of the shipping address. |
S_ZIP | Zip code of the shipping address. |
SHIP_VIA | Method of shipment. |
NOTES | Any notes about the shipment. |
The ORDER_LINES table contains information
about each item in a particular order. This table has the following columns:
OL_PK | Unique ID for the item. This column is the primary key for the ORDER_LINES table; the primary key is used to identify each record in the table. |
ORDER_ID | ID that uniquely identifies the order associated with this order_line. In the database, this column is a foreign-key column that references the primary-key column in the ORDERS table. |
TITLE_ID | ID that identifies the item. In our sample application, this is the title of the book (for example, "MobyDick"). This column is a foreign-key column that uniquely identifies the ordered item. |
LINE_NUMBER | Line number of the item. |
QTY_ORDERED | Quantity of the item that has been ordered. |
QTY_SHIPPED | Quantity of the item that has been shipped. |
How Bookstore Personnel Use the Application
Bookstore personnel access the CustomerService application via an HTML page running in their browser of choice. The HTML call triggers a custom servlet that calls the application. Once the application is displayed in their browsers, bookstore personnel can perform the following operations:
The following tools were used to develop the CustomerService application:
Launching and Using Object Translator
Because Object Translator was designed to work seamlessly with Visual Café, you can access Object Translator as a Visual Café plug-in. After you start Visual Café, launch Informix Object Translator by selecting Object Translator->Object Translator from the Informix menu.
After you launch Object Translator, the Object Translator console appears. Use the menu and toolbar commands on the Object Translator window to start a project, save a project, open an existing project, and launch the Model Viewer and Code Generator components of Object Translator.
In developing the sample CustomerService application, use the Object Translator GUI to perform these tasks:
The first step in creating the application is to use the Model Import wizard to reverse-engineer the database schema into an Object Translator data model. The data model is a snapshot of the database structure. Data-model-driven development allows:
To access the Model Import wizard from the Object Translator console:
The wizard guides you through the import process. You can choose the database elements (such as tables, columns, foreign keys, and stored procedures) that you want to import from the database. If the database does not have foreign keys defined or supported, the wizard infers relationships based on primary-key-column names. (These inferred relationships do not enforce referential integrity constraints; they only aid in data access.) See the What's This? help provided with the GUI for information about individual text boxes and buttons.
Because the CustomerService application uses only three tables, de-select all tables except CUSTOMERS, ORDERS, and ORDER_LINES during the import process.
The foreign keys imported from the sample database used for the CustomerService application are as follows:
Note: You can also use the Model Import wizard to refresh an existing data model with changes made in the database after the database schema was imported. You refresh a data model the same way you import one.
After you finish creating or modifying a data model, the data model is again displayed on the Data Model page in the Object Translator window. If you make additional changes to your data model using the Model Viewer, save those changes, and then refresh the data model displayed on the Data Model page. To do this, right-click the data model icon on the Data Model page and select Refresh from the popup menu.
Now you can use the Object Translator console to map the elements of the data model to the attributes, methods, and events of a language-independent object, called a map object.
In Object Translator, each map object defines the mapping between an application object and the schema table/columns that are used to hold the application object's state. The only part of the mapping that is visible to you is the mapping between the application object and the map object. (The mapping between the map object and the schema is not visible.)
After you map application objects, you can view and modify the properties of the map object, as well as the properties of its attributes, methods, and events.
To map application objects:
For the CustomerService application, use the Object Translator console to create the following map objects (details provided below):
After you create objects, you can specify whether objects can contain another object as either of these types of objects:To create a Customer object:
To create the Address object:
To create the Orders and OrderLines objects:
To define relationships among the application objects:
After creating and saving the map objects, use Object Translator to generate code for the mapped objects. Do this by launching the Code Generator from the Object Translator and completing the Code Generation wizard.
To launch the Code Generator, click the Code Generator toolbar button on the Object Translator window. See the What's This? help provided with the wizard for information about individual text boxes and buttons.
After you complete the wizard for the CustomerService scenario, the Code Generator generates the following Java classes:
Each generated Java class contains the methods and attributes needed to run in the WebLogic runtime. For each attribute of the map object, the Code Generator adds a corresponding attribute with the same name. The attributes are private by default.
For each map object, the Code Generator automatically adds getter/setter methods for accessing attribute values. In general, the data is returned in the native type (for example, the getCust_ID call returns the Cust_Id column as an integer).
Object Translator map objects have Restore, Store and, Destroy methods. During a restore, the object’s state is initialized from the database. At store time, the object’s state is inserted or updated in the database. During a destroy, the object is deleted from the database.
Note: Each object generated will have methods to get/set the values of its own attributes. However, each object, by default, has Restore, Store, and Destroy methods.
The CustomerService application is based on a client-server architecture in which the browser is a client that triggers a servlet (CustomerServlet) on the server that runs the application. To develop a servlet, you must have the Servlet API class files for compiling the servlet and a servlet engine, such as a web server, that is used to deploy the servlet. For the servlet API, install the Java Server Development Kit (JSDK 2.0) available at http://java.sun.com/products/servlet/. Be sure to register the servlet on the web server used to deploy the servlet.
In the CustomerService example, the wrapper class named ServerWrapper.java makes calls to the methods in the Object Translator-generated code. The servlet draws the HTML forms and calls the methods in the ServerWrapper, which in turn accesses methods in the Object Translator-generated code. You can combine the functionality of the ServerWrapper into the servlet so you have only one class. However, the ServerWrapper lets you separate the creation and updating of the HTML forms from the functionality used to operate on the different objects.
The CustomerServlet extends the HttpServlet class and thus has implementations for the doGet and doPost methods. The doPost method creates a session object and instantiates a ServerWrapper session object. The servlet initially displays the customer record and then performs operations based on the button the end user clicks. If the end user clicks:
The ServerWrapper has methods to store (insert and update), restore, and delete Customer, Orders, and OrderLines objects. Each of these methods access the corresponding methods in the Object Translator-generated code. See the source code for implementation details. Also see the Java documentation for these classes; this documentation has been provided with the sample application.
Running the Created Application
The CustomerService application is now ready to run. Start by navigating to the URL that displays the form. When the servlet is first activated, the init method (which defines the URL for the database connection and driver name) is executed. The Object Translator runtime component is then called to make a connection to the database. This connection is kept open until the last instance of the CustomerServlet class is deactivated. Subsequent calls to the servlet return the same connection object while incrementing the activation count on the connection. This allows scarce database connections to be pooled among many object instances.
To browse the customer data, click the Query button to display the first customer record. The button sends an HTTP request to the server and the doPost method of the CustomerServlet class executes. The servlet creates a new session and instantiates a ServerWrapper object for that session.
When you click a button (in this case, the Query button of the Customer form), the servlet calls the restoreCustomer method of the ServerWrapper class.
The restoreCustomer method in the ServerWrapper instantiates and initializes the CustomerCollection object defined in the ServerWrapper. It uses the same connection object defined in the CustomerServlet and calls the Restore method of the CustomerCollection class generated by Object Translator. After restoring the collection, it returns the first Customer object in the CustomerCollection object. The data from the returned Customer object populates the customer form. Subsequent calls to the restoreCustomer method use the instantiated CustomerCollection object.
You can navigate between customer objects using the first (|<), last (>|), previous (<), and next (>) buttons.
To update a customer record, edit the text fields and click Update.
To delete a customer record, provide the ID for that customer and click Delete.
When you click the different buttons, the associated methods in the ServerWrapper are executed and the form displayed on the screen changes accordingly.
To review all orders for a particular customer, click the ReviewOrders button to display the first order from the OrdersCollection object for the customer. Then use the navigation buttons to go to other orders, or click the ReviewOrderLines button to display the OrderLines form with the first OrderLines record from the OrderLinesCollection for the current Order object.
To return to the previously displayed Customer form and continue browsing the customer data, click the ReviewCustomer button from either the Orders form or the OrderLines form.
The Orders and the OrderLines forms have similar buttons, so you can query, save, update, and delete Orders and OrderLines records. You can also navigate through the collections of Orders and OrderLines.
Last updated June 19, 2000.