Search FunctionalityHomepage  « Case Study « Search Functionality

In this lesson we add imports to the StockImpl class so we can pass back a table model to our users as well as handling exceptions from our services. We then add the functionality to the search() method for passing back search results via the table model to populate the Manufacturer Panel we sketched out in the Designing The GUI lesson.

Updating The StockImpl Classgo to top of page Top

The first thing we need to do is add a couple of imports to the top of the StockImpl class after the existing imports.


import client.ManufacturerTableModel;
import services.ServicesException;

Open the StockImpl class in your text editor and cut and paste the above imports to the top of the StockImpl class after the existing imports.

Save the StockImpl class in the   c:\_Case_Study\src\model directory.

Updating The search() Function

We also need to update the search() function stub so it now returns a ManufacturerTableModel containing all Manufacturer records pertaining to search criteria. We also need to code the method to retrieve this information.


    /**
     * Search manufacturer file via criteria entered by a user in the GUI.
     *
     * @param name The name of the Manufacturer.
     * @param location The location where the Manufacturer is based.

     * @return A ManufacturerTableModel containing all Manufacturer records pertaining to 
     *         search criteria.
     * 
     */
    public ManufacturerTableModel search(String name, String location) {
        log.entering("StockImpl", "search", 
        		new Object[]{name, location});
        ManufacturerTableModel modelStockImpl = new ManufacturerTableModel();
        long[] retrecOffsets = new long[500];

        String[] criteria = {name, location};
        // Use search criteria to return an array of record numbers (500 more than enough)
        retrecOffsets = findBySearchCriteria(criteria);
        // Now read manufacturers from cache map using returned record numbers
        try {
            for (int i = 0; i < retrecOffsets.length; i++) {
                long recOffset = retrecOffsets[i];
                // Exit loop when no more records to process 
                if (recOffset == 0) {
                    break;
                }
                Manufacturer manufacturer = getManufacturer(recOffset);
                modelStockImpl.addManufacturerRecord(manufacturer);
            }
        } catch (RecordNotFoundException e) {
            log.log(Level.SEVERE, e.getMessage(), e);
            throw new ServicesException(e);
        }
        log.exiting("ServicesImpl", "search");
        return modelStockImpl;
    }

In not already open, open the StockImpl class in your text editor and cut and paste the above search() method into the StockImpl class overwriting the existing search() method and comments. The search() method can be found at the very bottom of the StockImpl class.

Save the StockImpl class in the   c:\_Case_Study\src\model directory.

Compiling Our Source File With the -cp and -d Options

Open your command line editor:

Change to directory  cd c:\_Case_Study\src\model

Compile StockImpl.java using the java compiler with the -cp and -d options
  javac -cp ..\..\classes -d ..\..\classes StockImpl.java

The following screenshot shows that the StockImpl class compiles fine after we have made the changes.

update StockImpl class

Related Java6 Tutorials

Beginning Java6 - Primitive Variables
Beginning Java6 - Conditional Statements
Beginning Java6 - Loop Statements
Objects & Classes - Arrays
Objects & Classes - Class Structure and Syntax
Objects & Classes - Methods
Flow Control - Handling Exceptions


What's Next?

In the next lesson we look at the code for the Manufacturer application tsart up and also the saved run mode / run mode options.

<<  Model Part 2                    Stock Functionality  >>

go to home page Homepage go to home page Top

All the Model Part 2 lessons are listed below. Click a link to go to that lesson.


Model Part 2

Search Functionality

Updating StockImpl Class

Stock Functionality