Controller Part 1J8 Home « Controller Part 1

In this lesson of the case study we do the first part of our controller code by creating an exception class for our services and the services and RemoteServices interfaces.

Whether accessing the Manufacturer file locally or remotely problems can arise using I/O and we can also have problems with remote access. So we need to catch and pass back exception information to the GUI users about exceptions that are not covered elsewhere within the code. In this lesson we will create a ServicesException class to handle exceptions thrown when using our services.

Creating The ServicesException Class Top

The following ServicesException class will be be thrown when an exception occurs when using our services.

Create the ServicesException class in the services package and cut and paste the following code into it.


package services;

/**
 * Holds all services exceptions that may occur in Services
 * 
 * @author Charlie 
 * @version 1.0
 * @see Services
 *
 */
public class ServicesException extends RuntimeException {

    /**
     * A version number for the ServicesException class so that serialisation 
     * can occur without worrying about the underlying class changing 
     * between serialisation and deserialisation.
     * 
     */
    private static final long serialVersionUID = 2498052502L;

    /**
     * Create a default ServicesException instance.
     * 
     */
    public ServicesException() {
        super();
    }

    /**
     * Create a ServicesException that returns a String.
     * 
     * @param e The exception to wrap
     */
    public ServicesException(String e) {
        super(e);
    }

    /**
     * Create a ServicesException instance and chains an exception.
     * 
     * @param e The exception to wrap
     */
    public ServicesException(Throwable e) {
        super(e);
    }
}

Services InterfacesTop

We will need to seamlessly connect local or remote clients to a Manufacturer file a user has chosen and to do this we will need two interfaces that connect locally which we will call Services and remotely which we will call RemoteServices. The interfaces will need to include the stocking, unstocking and search functionality requested by the stakeholder Stocking Goods Limited. In this lesson we create the interfaces and methods required.

Creating The Services Interface Top

The following Services interface will have to have methods for implementation that will encompass the following processes requested by the stakeholder Stocking Goods Limited:

  1. Stocking - A user of the system should be able to stock goods up to but not exceeding the amount of stock level held.
    • If there is a problem accessing the Manufacturer file we should throw a IOException back to the GUI.
    • If there is a problem updating the Manufacturer file or problems accessing records from the cache map we should throw a ServicesException back to the GUI.
    • A situation may arise where a user tries to stock goods that another user has already stocked and in this scenario we should throw a StockingException back to the GUI.
  2. Unstocking - A user of the system should be able to stock goods up to but not exceeding the amount of stock level held.
    • If there is a problem accessing the Manufacturer file we should throw a IOException back to the GUI.
  3. Searching - A user of the system should be able search for goods by name, by location, by name and location or search all records. We will use the client.ManufacturerTableModel class to pass back search results to the GUI.
    • If there is a problem accessing the Manufacturer file we should throw a IOException back to the GUI.

Create the Services interface in the services package and cut and paste the following code into it.


package services;

import java.io.IOException;
import java.rmi.Remote;

import model.StockingException;
import client.ManufacturerTableModel;

/**
 * An interface implemented by services that provide access to and interaction 
 * with the Manufacturer file from a GUI.
 * 
 * @author Charlie 
 * @version 1.0
 */
public interface Services extends Remote {

    /**
     * Attempts to stock the Manufacturer product specified by the GUI user.
     * 
     * @param name The name of the Manufacturer.
     * @param location The location where the Manufacturer is based.
     * @param stockLevel The amount of stock remaining.
     * @param stockOrdered The amount of stock order.
     * 
     * @throws IOException Indicates there was a problem accessing the data.
     * @throws ServicesException Indicates there was a problem updating the data.
     * @throws StockingException Indicates stocking already done by another user.
     */
    void stockFromManufacturer( String name, String location,
            int stockLevel, int stockOrdered) 
            throws IOException, ServicesException, StockingException;

    /**
     * Attempts to unstock the Manufacturer product specified by the GUI user.
     * 
     * @param name The name of the Manufacturer.
     * @param location The location where the Manufacturer is based.
     * @param stockOrdered The amount of stock order.
     * 
     * @throws IOException Indicates there was a problem updating the data.
     */
    void unstockBackToManufacturer(String name, String location,
            int stockOrdered) throws IOException;

    /**
     * Search contractor cache map for Manufacturer records matching the
     * criteria specified by the GUI user.
     * 
     * @param name The name of the Manufacturer.
     * @param location The location where the Manufacturer is based.
     * 
     * @return A ManufacturerTableModel holding selected records.
     * 
     * @throws IOException Indicates there was a problem accessing the data.
     */
    ManufacturerTableModel searchManufacturers(String name, String location)
            throws IOException;
			
}

services class and intreface
Screenshot 1. The services package after adding the ServicesException class and the Services interface.

Creating The RemoteServices Interface Top

The RemoteServices interface is a marker interface that is implemented by services that provide remote access to and interaction with the Manufacturer file from a GUI.

Create the RemoteServices interface in the remoteservices package and cut and paste the following code into it.


package remoteservices;

import services.Services;

/**
 * An interface implemented by services that provide remote access to and interaction 
 * with the Manufacturer file from a GUI. This is a marker interface that has no body.
 * 
 * @author Charlie 
 * @version 1.0
 */
public interface RemoteServices extends Services {

}

remote services interface
Screenshot 2. The remoteservices package after adding the RemoteServices interface.

Lesson 11 Complete

In this lesson we set up the initial Controller elements of the MVC pattern that can be derived from the project proposal.

Related Java Tutorials

Fundamentals - Primitive Variables
Objects & Classes - Class Structure and Syntax
OO Concepts - Inheritance Concepts - Using the super keyword
OO Concepts - Interfaces
Exceptions - Creating Our Own Exceptions
API Contents - Inheritance - Using the package keyword
API Contents - Inheritance - Using the import keyword
Swing - RMI - Serialization

What's Next?

In the next section we finish coding the Model elements of the MVC pattern for our case study.