YOUR FEEDBACK
More on the Software Assembly Question - Do Design Patterns Help?
Yanic wrote: Hi, > UML and MDA are being changed to be more data and doc...
SOA World Conference
Virtualization Conference
$50 Savings Expire May 23, 2008... – Register Today!


2007 West
GOLD SPONSORS:
Active Endpoints
Your SOA Needs BPEL for Orchestration
BEA
Virtualized SOA: Adaptive Infrastructure for Demanding Applications
Nexaweb
Overcoming Bandwidth Challenges with Nexaweb
TIBCO
What is Service Virtualization?
SILVER SPONSORS:
WSO2
Using Web Services Technologies and FOSS Solutions
Click For 2007 East
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
SYS-CON.TV
POWERBUILDER LINKS YOU MUST CLICK ON


Avoiding Code Duplication: A Sample Exercise
Pitfalls with the Web target explored

Digg This!

Now that Steve has discussed frameworks and building from scratch, I'll cover some things to help you avoid writing duplicate code.

Remember that the purpose of these articles is not a tutorial with step-by-step instructions but a guide to assist in the design and development process. Our goal is to help new developers get started in the right direction and to avoid pitfalls. This month's column will center on pitfalls with the Web target.

Before I discuss Web targets I want to reemphasize the foundation that Steve covered last month. A solid foundation and base classes are extremely important to an object-oriented development effort. A very simple example of a base class that is a pitfall is the MessageBox functionality. The MessageBox function is used to display information and errors to users and to ask questions. This function is used hundreds of times in any application and possibly even more. I had a relatively small application at a new client last year that contained 263 MessageBox function calls. The client wanted to customize the look of the message window and add an additional button for help.

Unfortunately the original developer didn't use any object-oriented practices. I had to replace 263 MessageBox functions with a standard approach. The approach I took was to implement my framework for all the client's applications. One of the framework's base class objects is a function to display a message. This function originally used a MessageBox when it was called but was easily modified to use a new Response window customized to the client's needs. Yes, the original implementation of the framework took some time and maintenance coding, but it made it all worthwhile. The next time they want to make a change to the messages presented to the user, it will be changed in one place only! I believe this is a pitfall to be avoided.

DataWindows
One of the most important objects in any PowerBuilder application is the DataWindow. Sybase extended this functionality in version 7 with the creation of the Web DataWindow. As the name implies, the Web DataWindow takes a DataWindow object and data and dynamically creates a page of HTML and JavaScript to be displayed in a browser. Why is this functionality so important? It allows us to use the same object to display and update information in a client/server and a Web application. We don't need to have different objects for each type of application. We will be using the same DataWindow object in both of our applications.

You may ask why am I talking about Web DataWindows but not writing any code? The answer is that one of the pitfalls that occur when creating your first Web application is coding the connection to EAServer in each page. The pages may work fine and the application runs well on your laptop or test server; however, what happens when you move into production? You need to go and change all the places that reference the server. Remember the MessageBox example? Same thing.

The Web DataWindow is not magic. It's a PowerBuilder nonvisual object (NVO) that is deployed to EAServer as a CORBA component. Once it exists in EAServer, it can be executed by anything that can communicate with CORBA, such as Java or JSP. There are five steps in a simple Web DataWindow page. Listing 1 shows the JSP code.

  1. Create an instance of the component.
  2. Set the PBL and dataobject.
  3. Set the transaction object.
  4. Retrieve the data.
  5. Generate the HTML.
JSP Target
What we are planning is to develop two applications that have the same functionality. One is written as a client/server application and the other a Web application. The Web application is a JSP target that we started in my last article. The purpose of a JSP (JavaServer Page) page is to generate dynamic content, typically from a database query. As an example, think about ordering software products from Sybase.com. There are many different pages that are used when placing an order: searching for products, displaying the specifications and pictures of a product, placing products into a shopping cart, and then going through the checkout and payment process. Many of these pages may use the same logic to display information, so I will concentrate on how to avoid duplicating code.

The section of the code that I'll review creates an instance of a PowerBuilder component named HTMLGenerator100. This is a sample component that Sybase provides to demonstrate how to write a PowerBuilder component that creates a Web DataWindow. Take a look at the line of code that begins with "Manager manager = ". Notice at the end of the line that the server name is specified, localhost in this instance. Also notice the next line, which specifies the username (jagadmin) and password (empty). Any page written in this manner has all critical settings hard-coded.

As you can see this is a pitfall to be avoided. All that is necessary to avoid it is to know a technique to store settings. The technique to be used is called include files. While this is not the only technique available, it is a proven technique. An include file is Java code that is placed within a file and can be added to a page. I prefer to name these files with an extension of .inc. A file named EAServerManager.inc is created with a text editor and contains the following lines:

<%
String s_machine = "iiop://localhost:9000";
%>

The first and last lines are identifiers to indicate that Java code is between the pair. The second line defines a String variable named s_machine and sets it to the server name to access. This is the one file that needs to be changed when deploying the application to a different server. Simple, right? The other change is to use this include file in the JSP. The following line is added to the JSP to use the include file:

<%@ include file="EAServerManager.inc" %>

The final change is to remove the hard-coded value in the page and replace it with the s_machine variable as shown below. Listing 2 shows the new page.

Manager manager = ManagerHelper.narrow(orb.string_to_object(s_machine));

Result
Naturally this was a very simple example of how to avoid a common pitfall. I also didn't put the other hard-coded values into the include file. Think of this as a sample exercise. Remember that design is important before coding begins. My recommendation is to try and think of as many of these potential pitfalls as possible before you start coding. On a final note, remember that there is often more than one way to accomplish the same task. The method demonstrated has been working for years but I'm certain there are other methods that are used each and every day. In the next article we'll finally start writing some code - promise.

About Steve Katz
Steve Katz is a senior developer at HSBC Bank USA and has extensive experience developing applications utilizing PowerBuilder, Java, and other technologies. He has used PowerBuilder since v2.0a, taught at Techwave, and even wrote some articles about PowerBuilder a very long time ago.

About Larry Cermak
Larry Cermak is president of Branick Consulting, Inc., a consulting firm specializing in the Sybase family of products. He has been working with the Web DataWindow since it was first introduced. He's a member of Team Sybase, a writer for the Sybase Developer Network and PBDJ, a frequent speaker at Sybase conferences and seminars across the country, and is author of The Web DataWindow.

PBDJ LATEST STORIES . . .
3rd International Virtualization Conference & Expo: Themes & Topics
From Application Virtualization to Xen, a round-up of the virtualization themes & topics being discussed in NYC June 23-24, 2008 by the world-class speaker faculty at the 3rd International Virtualization Conference & Expo being held by SYS-CON Events in The Roosevelt Hotel, in midtown
The PB Future: More on Graphs in PowerBuilder 11.5
Last week I posted a screen shot of the new 3D Rendering capabilities being added to some of the 3D graphs in PowerBuilder 11.5. It was met with mixed reviews on the PowerBuilder Futures newsgroup (forums.sybase.com) so I went back to the drawing board to see what I could come up with.
BluePhoenix Expands Modernization Collaboration with Microsoft
BluePhoenix announced that it has expanded its collaboration with Microsoft on legacy modernization projects. The collaboration provides customers moving their applications or databases to .NET-based environments the best in both modernization services and technical support. BluePhoeni
Sybase PowerBuilder Delivers AJAX and .NET Enhancements Enabling Rich Internet Application Development
Sybase announced that AJAX development capabilities and further Microsoft .NET enhancements have been added to the latest version of Sybase PowerBuilder 11, the premier 4GL rapid application development (RAD) tool. PowerBuilder 11.2 represents another milestone in the PowerBuilder road
PowerBuilder 11.2 Released: Sybase's Flagship IDE
Sybase has released the production version of its flagship .NET development tool - PowerBuilder version 11.2. This latest release of its premier IDE for RAD includes not only standard fixes but also a good list of new features. Here is the 'Coles Notes' version of these new features.
PowerBuilder Takes You To .NET
In June of 2007, Sybase released PowerBuilder 11. PowerBuilder developers can now deploy PowerBuilder components as .NET Assemblies or as .NET Web Services. A PowerBuilder developer can now create these .NET resources so that those who develop .NET solutions can benefit from PowerBuild
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON FEATURED WHITEPAPERS

ADS BY GOOGLE
BREAKING POWERBUILDER / SYBASE NEWS
Sybase and Sun Set Guinness World Record for World's Largest Data Warehouse
Sybase, Inc. (NYSE:SY), the largest enterprise software and services company exclusively