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 Chapter 4.

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 proxy you have generated for the web service.

This chapter covers these topics:


Creating the Client With the Provided Code

The client classes are provided in two forms: source code and Java class files.

If you are using a Microsoft Windows system, you can create two classes in the IDE and replace their default code with the code you copy and paste from Appendix A. Code from these classes instantiates the client proxy, which is assumed to be in the same package. Therefore, create the client classes within the WebService package.

If you are using Solaris or Linux environments, copying code from a PDF file into the source editor results in an unformatted single line of code, which is rather inconvenient to read. Instead, use the class files provided in the DiningGuide2.zip file, which you can download from http://forte.sun.com/ffj/documentation/tutorialsandexamples.html

Creating the Client on Microsoft Windows Systems

To create the two classes using by copying the source in Appendix A:

1. In the Explorer, right-click the WebService node and choose New right arrow Classes right arrow Class.

The New wizard is displayed.

2. Name the class RestaurantTable and click Finish.

The new RestaurantTable classes is created under the WebService package.

3. Repeat Step 1 and Step 2 to create the CustomerReviewTable class.

4. Open the classes in the Source Editor and delete all the default code from each class.

5. Copy all the code from RestaurantTable.java Source and the following three pages and paste it into the body of the RestaurantTable class.



Tip - Copy this long code very carefully. Set your Acrobat Reader to display a whole page at a time. Select all the code from the first page of RestaurantTable code and paste it into the target file in the Source Editor. At the end of the pasted code, press Enter to start a new line. Then copy all the code from the next page of RestaurantTable code and paste it into the Source Editor, starting at the new line you created previously. Repeat until all the code is pasted.



6. Select all the pasted code in the Source Editor and press Control-Shift F.

This action reformats all the code correctly.

7. Repeat Step 5, copying all the code from CustomerReviewTable.java Source and the following three pages into the CustomerReviewTable class body.

8. Repeat Step 6 to format the pasted code properly.

9. Right-click the CustomerReviewTable node and choose Compile.

The CustomerReviewTable class should compile without errors.

10. Right-click the RestaurantTable class node and choose Compile.

The RestaurantTable class should compile without errors.

If you examine the code in the RestaurantTable and CustomerReviewTable classes, there are several comments warning against modifying some sections. These sections are Swing component code created in the Form Editor and should not be modified in the Source Editor. Normally, such restricted code has a blue background. If you restart the IDE, the source for this file will have a blue background for the restricted areas, and you will not be able to edit the code in those sections.



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.



Creating the Client in Solaris or Linux Environments

To copy the two provided Java client classes into the WebService package:

1. Unzip the DiningGuide2.zip file from the Developer Resources portal.

The portal is at http://forte.sun.com/ffj/documentation/tutorialsandexamples.html

For example, unzip the file to the /MyZipFiles directory.

2. Using a file system command, copy the two client files from the DiningGuide2 source files to the WebServices package.

For example, type:

$ cp /MyZipFiles/DiningGuide2/WebService/*.java /DiningGuide2/WebService

3. In the IDE's Explorer, expand the DiningGuide2/WebService package and observe the two new classes.



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

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 Deploying the Test Application.

6. Right-click the server instance and choose Status to verify that the instance is running.

If the Stop Server Instance button is activated, the server is running. If it is not activated, click the Start Server Instance button to start it.

7. Click the Filesystems tab of the Explorer.

8. 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. 

9. 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 ] 

10. 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. 

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

12. Quit the application by closing any window.

After you quit the application, the execution window shows that the Sun ONE Application Server 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 Tomcat 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 proxy 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 proxy and call its getAllRestaurants method, as follows:

1. RestaurantTable.getAllRestaurants method instantiates the client proxy, calls the client proxy'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 proxy'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 proxy (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 proxy'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 proxy (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();
	}
}