C H A P T E R  5

Creating a Client for the Tutorial Application

This chapter shows you how to run the DiningGuide application using a provided Swing client that communicates with the web service you created in .

The provided client contains two Swing classes, RestaurantTable and CustomerReviewTable. You add these classes to the WebService package, then execute the RestaurantTable class to run the application.

This client is very primitive, provided only to illustrate how to access the methods of the client you have generated for the web service.

This chapter covers these topics:


Creating the Client With the Provided Code

The client classes are provided as Java class files in the DiningGuide.zip file, which you can download from the Developer Resources portal.

To copy the two provided Java client classes into the DiningGuide application:

1. In the IDE, create the Client package within the DiningGuide folder.

a. Right-click the DiningGuide folder and choose New right arrow Java Package.

b. In the New wizard, type Client in the Name field and click Finish.

2. Unzip the DiningGuide.zip file from the Developer Resources portal.

a. Download the DiningGuide.zip file from the Developer Resources portal.

http://forte.sun.com/ffj/documentation/tutorialsandexamples.html

b. Unzip the file to a local directory, for example, the /MyZipFiles directory.

3. Using a file system command, copy the two client files from the DiningGuide source files to the Client package, as follows:

4. In the IDE's Explorer, expand the DiningGuide/Client package and verify that the two new classes are there.



Note - When you create a Swing client in the IDE's Form Editor, the IDE generates a .form file and a .java file. The .form file enables you to edit the GUI components in the Form Editor. However, the .form files have not been provided for the RestaurantTable and CustomerReviewTable classes, which prevents you from modifying the GUI components in the Form Editor.




Running the Tutorial Application



Note - Make sure the PointBase server is running before you run the tutorial application. In addition, make sure Sun ONE Application Server 7 is running and is the default application server of the IDE. See for information.



Run the DiningGuide application by executing the RestaurantTable class, as follows:

1. In the IDE, click the Runtime tab of the Explorer.

2. Expand the Server Registry, the Installed Servers, the Sun ONE Application Server 7, and its subnodes.

3. Right-click the Deployed Applications subnode of the server instance.

4. Make sure the DiningGuideApp application is still deployed.

If it is still deployed, a DiningGuideApp node is displayed under the Deployed Applications subnode.

5. If it is not still deployed, deploy it, as described in .

6. In the Filesystems tab of the Explorer, right-click the RestaurantTable node and choose Execute.

The IDE switches to Runtime mode. A Restaurant node appears in the execution window. Then, the RestaurantTable window is displayed, as shown:

Restaurant Listing Swing window showing three restaurant records: Bay Fox, French Lemon, and Joe's House of Fish.Button is View Customer Comments. 

7. Select any restaurant in the table and Click the View Customer Comments button.

For example, select the French Lemon restaurant. The CustomerReviewTable window is displayed. If any comments exist in the database for this restaurant, they are displayed, as shown. Otherwise, an empty table is displayed.

Customer Review list window showing records: Magnifico! by Giuseppe Verdi, and This is the best! by Marcia Green.Button is Submit Customer Review.[ D ] 

8. Type a something in the Customer Name field and in the Review field and click the Submit Customer Review button.

For example:

Customer Review list Swing window showing previous valued, plus New User for Customer Name and I'm speechless in Review field. 

The record is entered in the database and is displayed on the same CustomerReviewTable window, as shown:

Previous Customer Review list Swing window showing New User record added. Input fields still have the entered text. 

9. Play around with the features, as described in User's View of the Tutorial Application.

10. Quit the application by closing any window.

After you quit the application, the execution window shows that the Sun ONE Application Server 7 process is still running. You need not stop the application server. If you redeploy any of the tutorial's J2EE applications or rerun the test clients (but not this Swing client), the server is automatically restarted.

When you quit the IDE, a dialog box is displayed for terminating any process that is still running (including the application server or the web server). Select each running process and click the End Tasks button. You can also manually terminate any process at any time while the IDE is running by right-clicking its node in the execution window and choosing Terminate Process.


Examining the Client Code

The two client classes you have installed in the DiningGuide application are composed of Swing components and actions that were created in the Form Editor, and several methods that were created in the Source Editor. The methods added in the Source Editor include the crucial task of instantiating the client so that its methods become available to the client.

To help you understand how the Swing client interacts with the web service, the next few sections discuss the main actions of the client, namely:

Displaying Restaurant Data

Displaying restaurant data is accomplished by the RestaurantTable class's methods, which instantiate the client and call its getAllRestaurants method, as follows:

1. RestaurantTable.getAllRestaurants method instantiates the client, calls the client's getAllRestaurants method to fetch the restaurant data, and returns the fetched restaurant data as a vector.

private Vector getAllRestaurants() {
	Vector restList = new Vector();
	try {
			WebService.DGWebServiceClientGenClient.DGWebService service1 = new
			WebService.DGWebServiceClientGenClient.DGWebService_Impl();
			WebService.DGWebServiceClientGenClient.DGWebServiceServantInterface port =
			service1.getDGWebServiceServantInterfacePort();
			
			restList = (java.util.Vector)port.getAllRestaurants();
	}
	catch (Exception ex) {
			System.err.println("Caught an exception." );
			ex.printStackTrace();
	}
	return restList;
}

2. The RestaurantTable constructor puts the returned restaurant data into the restaurantList variable and calls RestaurantTable.putDataToTable.

public RestaurantTable() {
	initComponents();
	restaurantList=getAllRestaurants();
	putDataToTable();
}

3. The RestaurantTable.putDataToTable method iterates through the vector and populates the table.

private void putDataToTable() {
	Iterator j=restaurantList.iterator();
	while (j.hasNext()) {
			RestaurantDetail ci = (RestaurantDetail)j.next();
			String strRating = null;
			String[] str ={ci.getRestaurantname(), ci.getCuisine(), ci.getNeighborhood(), ci.getAddress(), ci.getPhone(), ci.getDescription(), strRating.valueOf(ci.getRating()),
			};
			TableModel.addRow(str);
	}
}

4. The RestaurantTable.Main method displays the table as a Swing jTable component.

public static void main(String args[]) {
	new RestaurantTable().show();
}

Displaying Customer Review Data for a Selected Restaurant

Displaying customer review data begins when the RestaurantTable's button component's action instantiates a CustomerReviewTable. The CustomerReviewTable's methods fetch the customer review data by means of the client's methods, and populate the table. The RestaurantTable's button component's action then displays it, as follows:

1. When the RestaurantTable's button is pressed to retrieve customer review data, the RestaurantTable.jButton1ActionPerformed method instantiates a new CustomerReviewTable object, calls its putDataToTable method, and passes it the data of the selected column.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
	int r =jTable1.getSelectedRow();
	int c = jTable1.getSelectedColumnCount();
	String i =(String)TableModel.getValueAt(r,0);
	CustomerReviewTable crt = new CustomerReviewTable();
	crt.putDataToTable(i);
	crt.show();
	System.out.println(i);
}

2. The CustomerReviewTable.putDataToTable method calls the CustomerReviewTable.getCustomerReviewByName method, passing it the selected restaurant name, assigning the returned vector to the customerList variable.

public void putDataToTable(java.lang.String restaurantname) {
	RestaurantName = restaurantname;
	java.util.Vector customerList =getCustomerReviewByName(restaurantname);
	Iterator j=customerList.iterator();
	while (j.hasNext()) {
			CustomerreviewDetail ci = (CustomerreviewDetail)j.next();
			String[] str = {ci.getCustomername(),ci.getReview()
			};
			TableModel.addRow(str);
	}
}

3. The CustomerReviewTable.getCustomerReviewByName method instantiates a client (if required) and calls its getCustomerreviewsByRestaurant method, passing it the name of the selected restaurant.

private Vector getCustomerReviewByName(java.lang.String restaurantname) {
	Vector custList = new Vector();
	try {
			WebService.DGWebServiceClientGenClient.DGWebService service2 = new
			WebService.DGWebServiceClientGenClient.DGWebService_Impl();
			WebService.DGWebServiceClientGenClient.DGWebServiceServantInterface port = service2.getDGWebServiceServantInterfacePort();
			
			custList =(java.util.Vector)port.getCustomerreviewsByRestaurant(restaurantname);	}
	catch (Exception ex) {
			System.err.println("Caught an exception." );
			ex.printStackTrace();
	}
	return custList;
}

4. The review data is passed up to the CustomerReviewTable.putDataToTable method, which iterates through it and populates the table.

public void putDataToTable(java.lang.String restaurantname) {
	RestaurantName = restaurantname;
	java.util.Vector customerList =getCustomerReviewByName(restaurantname);
	Iterator j=customerList.iterator();
	while (j.hasNext()) {
			CustomerreviewDetail ci = (CustomerreviewDetail)j.next();
			String[] str = {ci.getCustomername(),ci.getReview()
			};
			TableModel.addRow(str);
	}
}

5. The RestaurantTable.jButton1ActionPerformed method then displays the data.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
	int r =jTable1.getSelectedRow();
	int c = jTable1.getSelectedColumnCount();
	String i =(String)TableModel.getValueAt(r,0);
	CustomerReviewTable crt = new CustomerReviewTable();
	crt.putDataToTable(i);
	crt.show();
	System.out.println(i);
}

Creating a New Customer Review Record

When the user types a name and review comments on the Customer Review window and clicks the Submit Customer Review button, the CustomerReviewTable's jButton1ActionPerformed method creates the review record in the database by means of the client's methods, then refreshes the Customer Review window, as follows:

1. When the CustomerReviewTable's button is pressed to submit a customer review record, the CustomerReviewTable.jButton1ActionPerformed method instantiates a new client (if required) and calls its createCustomerreview method, passing it the restaurant name, the customer name, and the review data.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
	try {
			WebService.DGWebServiceClientGenClient.DGWebService service1 = new
			WebService.DGWebServiceClientGenClient.DGWebService_Impl();
			WebService.DGWebServiceClientGenClient.DGWebServiceServantInterface port = service1.getDGWebServiceServantInterfacePort();
			
			port.createCustomerreview(RestaurantName,
			customerNameField.getText(),reviewField.getText());
	}
	catch (Exception ex) {
			System.err.println("Caught an exception." );
			ex.printStackTrace();
	}
	refreshView();
    }

2. This same method (jButton1ActionPerformed) calls the CustomerReviewTable.refreshView method.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
	try {
			WebService.DGWebServiceClientGenClient.DGWebService service1 = new
			WebService.DGWebServiceClientGenClient.DGWebService_Impl();
			WebService.DGWebServiceClientGenClient.DGWebServiceServantInterface port = service1.getDGWebServiceServantInterfacePort();
			
			port.createCustomerreview(RestaurantName,
			customerNameField.getText(),reviewField.getText());
	}
	catch (Exception ex) {
			System.err.println("Caught an exception." );
			ex.printStackTrace();
	}
	refreshView();
    }

3. The CustomerReviewTable.refreshView method calls the putDataToTable method, passing it the restaurant name.

void refreshView() {
	try{
			while(TableModel.getRowCount()>0) {
				TableModel.removeRow(0);
			}
			putDataToTable(RestaurantName);
			repaint();
	}
	catch (Exception ex) {
			ex.printStackTrace();
	}
}

4. The CustomerReviewTable.putDataToTable method populates the table.

public void putDataToTable(java.lang.String restaurantname) {
	RestaurantName = restaurantname;
	java.util.Vector customerList =getCustomerReviewByName(restaurantname);
	Iterator j=customerList.iterator();
	while (j.hasNext()) {
			CustomerreviewDetail ci = (CustomerreviewDetail)j.next();
			String[] str = {ci.getCustomername(),ci.getReview()
			};
			TableModel.addRow(str);
	}
}

5. Then the CustomerReviewTable.refreshView method repaints the window, showing the new data.

void refreshView() {
	try{
			while(TableModel.getRowCount()>0) {
				TableModel.removeRow(0);
			}
			putDataToTable(RestaurantName);
			repaint();
	}
	catch (Exception ex) {
			ex.printStackTrace();
	}
}