Chapter 17

Online Employee Phonebook


CONTENTS


tel e phone \tel'e-fon'\ n: an electronic device or system for sound reception or reproduction at a distance

Introduction

Welcome to your fifth sample intranet application. If you thought the last program-the Online In/Out Board-was simple, wait until you see this application. This is the Online Employee Phonebook.

This read-only application displays the phone numbers of employees in a scrolling list. This is useful for all employees to look up the phone numbers of their coworkers.

This chapter will cover the following topics in regards to the Online Employee Phonebook application:

This four step format will be used throughout all of the sample application chapters. Hopefully, it will provide you with valuable insight and ideas for creating your own intranet applications.

Application Design

This application, like the last, is one of the simpler applications in the book. Once this application has been connected to a data source, it will present the user with a list of all the employees on file, as well as their phone numbers and names formatted in an attractive manner.

Figure 17.1 is the proposed user interface for the Online Employee Phonebook program.

Figure 17.1 : The Online Employee Phonebook user interface.

The interface will utilize a JifTextArea component to display employees and their phone numbers, and a single Refresh button to refresh the list.

The list needs to be refreshed periodically. The Refresh button is just the ticket. By pressing that button, the user initiates the process of reloading the JifTextArea.

Database Design

This application utilizes the employee table that was defined in Chapter 13, "Employee Files." There really is nothing new here. The application retrieves the rows from the table and displays the data.

Implementation

In the rest of this chapter I will discuss the implementation of the Online Employee Phonebook program. I'll first discuss the user interface and how it was created. Secondly, I'll discuss the database access used in the program. Finally, I'll cover any programming pitfalls that came up during the application construction.

Each sample application in this book uses a different approach to developing the user interface. This variety will show you the different ways you can go about doing your own interfaces. Hopefully, you will get a nice cross-section of many different styles and choose the one that suits you the best.

User Interface

To achieve the design goal presented above, you do not need special user interface components. The stock BorderLayout is sufficient. You'll also employ the JifTextArea class and a button.

Tip
In order to show the data separated by tabs, you use a TextArea component. Using a List component produces unstable results. Sometimes the columns will line up, sometimes they won't. Whenever you need evenly spaced text, use a TextArea com-ponent.

The following is the user interface construction code for the Employee program:

//****************************************************************************
//* Members                                         &nb sp;                        *
//****************************************************************************

    JifTextArea                    empList;

//****************************************************************************
//* Constructor                                          ;                     *
//****************************************************************************

    public
    PhoneBookUI( SimpleDBJiflet jiflet )
    {
        super( jiflet );
        setLayout( new BorderLayout() );

        empList = new JifTextArea( 15, 40 );
        add( "Center", empList );

        JifPanel p = new JifPanel();
        p.setLayout( new FlowLayout( FlowLayout.CENTER, 5, 5 ) );
        saveButton.setLabel( "Refresh" );
        saveButton.disable();
        p.add( saveButton );
        add( "South", p );
    }

First, set the layout to a new BorderLayout. The JifTextArea component is created and placed in the center of the layout. This is your employee phone list. Referring to Figure 17.1, you'll see that this list expands on all sides to fill the space.

Your Refresh button is next. You'll use a neat trick here: In order to get an automatic notification that your button was pressed, you'll rename the Save button to Refresh. This button is also disabled.

Overriding the saveRecord() method and placing any customized row-saving codes in it free you from monitoring for special events, or even a new button's events. For example, if you want to change the Save button's name to something like Play, you could then override the saveRecord() method to receive notification of the button being clicked. I'll cover this in the database access section later in this chapter.

Figure 17.2 illustrates the layout of this application.

Figure 17.2 : The layout of the Online Employee Phonebook.

Handling the Refresh Button

You want your Refresh button to enable, or light up, when the database connection has been established. To achieve this, you override the default connectToDatabase() method:

//****************************************************************************
//* connectToDatabase                                         ;                *
//****************************************************************************

    public void
    connectToDatabase()
    {
        //    Call my dad...
        super.connectToDatabase();

        //    Get all the news...
        if ( getConnector().connected() )
        {
            //    Enable the refresh button...
            getUIPanel().saveButton.enable();

            //    Load the panel up...
            loadPanel();
        }
    }

In the overridden method you call the base class' version. If it is successful, you enable the Refresh button, and load up the employee phone list.

Your Refresh button is really the Save button in sheep's clothing: You change the text on it to say Refresh. Changing the text does not alter its behavior. It still generates JifMessage.SAVE application messages in your framework.

It still generates these messages because instead of checking the text of the button when the initial ACTION_EVENT event is generated, you check the event's target with the member instance variables for all the buttons that you created in your base SimpleDBUI class.

What does this all mean to you? It means you can reliably change the text of the Save button. When this occurs, you will receive notification of the event by having your saveRecord() method called.

To handle the refreshing of the employee list, you will utilize the saveRecord() method:

//****************************************************************************
//* saveRecord                                                                *
//****************************************************************************

    public boolean
    saveRecord()
    {
        loadPanel();
        return( true );
    }

The loadPanel() method is called to load up the employee phone list.

Database Access

This program reuses the EmployeeRecord object that was introduced in Chapter 13. It is a
versatile class that represents a single row in the employee table. This DBRecord derivation knows how to create, read, update, and delete records from the employee table.

Note
The EmployeeRecord and other database classes are reused in several other applications. They have been placed in their own package along with other shared code. This package is called jif.common. It contains all the common classes between all the applications.

There is only a single database access area to this program: the employee phone list retrieval.

Retrieving the Phone Numbers

At startup, and during the lifetime of the application, the database needs to be queried, and the results displayed for the user. These results are massaged data columns taken from the database.

Retrieve all of the employees for display:

//****************************************************************************
//* loadPanel                                         & nbsp;                      *
//****************************************************************************

    public void
    loadPanel()
    {
        String sql = "select * from emp";

        //    Clear out the old stuff...
        getUIPanel().clearScreen();

        try
        {
            if ( getConnector().getStatement().execute( sql ) )
            {
                ResultSet rs = getConnector().getStatement().getResultSet();

                int row = 0;

                while ( rs.next() )
                {
                    EmployeeRecord er = new EmployeeRecord( rs );
                    setDBRecord( er );
                    getUIPanel().moveToScreen();
                }
            }
        } catch ( SQLException e )
        {
            errorLog( "Error during loading: " + e.toString() );
        }

        return;
    }

This retrieval issues an SQL query that returns all of the employees in your employee table. Each returned row is stored into an EmployeeRecord object. The stored row is then placed into a Vector for later use.

Notice that the employee phone list is never populated here because you've placed that code in the moveToScreen() method of your user interface class. It is only there that data is moved to the screen. Before it can move data to your list, though, you need to tell the base class which DBRecord to use. This is done with the call to setDBRecord().

The moveToScreen() code is quite simple:

//****************************************************************************
//* moveToScreen                                        &nbs p;                    *
//****************************************************************************

    public void
    moveToScreen()
    {
        if ( getJiflet().getDBRecord() == null )
            return;

        //    Cast one off...
        EmployeeRecord er = ( EmployeeRecord )getJiflet().getDBRecord();

        String s = er.first_name + " " + er.last_name + "\t\t";

        if ( er.work_ext_nbr == null )
            s += "(None)";
        else
            s += er.work_ext_nbr;

        s +="\t";

        if ( er.home_phone_nbr == null )
            s += "(None)";
        else
            s += er.home_phone_nbr;

        s += System.getProperty( "line.separator" );

        empList.appendText( s );
    }

Here, you retrieve the EmployeeRecord from your jiflet, concatenating the first and last names with a string holding the employees' work extensions and their home phone numbers.

Programming Considerations

Aside from the database access, this application is another simple one in your intranet application suite. It does nothing more than list the rows in the employee table in a visually pleasing format.

This application presented you with very little challenge. You needed to present an intuitive interface to the user, while making it effortless to use. You did this by using the JifTextArea class.

To recap, this application introduced the following Java intranet programming topics:

Summary

This chapter introduced you to the fifth sample application in your intranet application suite: the Online Employee Phonebook. This program is not responsible for updating any data; it is completely read-only. This program will be useful for any employee who needs to know the phone numbers of another employee.

In Chapter 18, "News and Announcements," you will design and create an application that will display important company news and announcements. This program will allow employees to know what is going on at all times.