Case Study – Search Functionality

·

·Case Study
·Scenario & Setup
·Model Part 1
·View Part 1
·Controller Part 1
·Model Part 2
·Controller Part 2
·View Part 2
·Testing & Wrap Up

 
Stock FunctionalityHomepage  « Case Study « Stock Functionality
In this lesson we finish the model code for the case study by completing the stocking() and unstocking() methods requested by Stocking Goods Limited. These methods are delegated to, via the Services singleton, whenever a user requests stocking or unstocking within the Manufacturer Application GUI.
Updating The stocking() Method Top
The stocking() method stub needs to be coded to accept a stocking amount entered in the Manufacturer GUI. We will have to update the information held in our cache map and also update the physical Manufacturer file held on disk. We must also ensure that the stock ordered hasn’t been set by another user, before setting our locks and updating the information.
Open the StockImpl class in your text editor and cut and paste the following stocking() method into the StockImpl class overwriting the existing stocking() method and comments. This stock() method can be found near the bottom of the StockImpl class.

/**
* Stock the requested amount entered by a user in the GUI.
*
* @param name The name of the Manufacturer.
* @param location The location where the Manufacturer is based.
* @param stockLevel The amount of stock left.
* @param stockOrdered The amount of stock requested.
*
* @throws StockingException Stock quantity already ordered by another user.
*
*/
public void stocking(String name, String location,
int stockLevel, int stockOrdered) throws StockingException {
log.entering(“StockImpl”, “stocking”,
new Object[]{name, location, stockLevel, stockOrdered});
Long lock = 0L;
long[] retrecOffset = new long[1];
String[] criteria = {name, location};
boolean unlockRequired = true;
retrecOffset = findBySearchCriteria(criteria);
// Return the record number
long recOffset = retrecOffset[0];

if (recOffset == 0) {
log.log(Level.INFO, “Problem stocking Manufacturer: ” + name + “, ” + location);
String e = “Problem stocking Manufacturer: ” + name + “, ” + location;
throw new ServicesException(e);
}
try {
log.log(Level.FINE, “Run by: ” + Thread.currentThread().getName());
Manufacturer manufacturer = getManufacturer(recOffset);
// Firstly make sure that stock ordered hasn’t been set by another user
if (!manufacturer.getStockOrdered().equals(“”)) {
log.log(Level.INFO, “Manufacturer: ” + name + “, ” + location +
” , ” + ” product: ” + manufacturer.getProduct() +
“, stock quantity already ordered by another user.”);
unlockRequired = false;
throw new ServicesException(“Stock quantity already ordered by another user.”);
} else {
String strStockLevel = “” + stockLevel;
manufacturer.setStockLevel(strStockLevel);
String strStockOrdered = “” + stockOrdered;
manufacturer.setStockOrdered(strStockOrdered);
String[] manufacturerData = {manufacturer.getDeletedFlag(), manufacturer.getName(),
manufacturer.getLocation(), manufacturer.getProduct(),
manufacturer.getPrice(), manufacturer.getStockLevel(),
manufacturer.getStockOrdered()};
//Add record number to the locking map
lock = lockRecord(recOffset);
//Update record with new stock totals
updateRecord(recOffset, manufacturerData, lock);
log.log(Level.INFO, “Manufacturer: ” + name + “, ” + location +
” product: ” + manufacturer.getProduct() +
” stock quantity left: ” + stockLevel +
“, stock quantity ordered: ” + stockOrdered);
}
} catch (ServicesException e) {
log.log(Level.WARNING, e.getMessage(), e);
throw new StockingException(e);
} catch (ManufacturerFileAccessException e) {
log.log(Level.SEVERE, e.getMessage(), e);
throw new ServicesException(e);
} catch (RecordNotFoundException e) {
log.log(Level.SEVERE, e.getMessage(), e);
throw new ServicesException(e);
} catch (SecurityException e) {
log.log(Level.SEVERE, e.getMessage(), e);
throw new ServicesException(e);
} finally {
if (unlockRequired) {
//Remove record number from the locking map
unlock(recOffset, lock);
}
}
log.exiting(“StockImpl”, “stocking”);
}

Save the StockImpl class in the   c:_Case_Studysrcmodel directory.
Updating The unstocking() Method Top
The unstocking() method stub needs to be coded to accept an unstocking amount entered in the Manufacturer GUI. We will have to update the information held in our cache map and also update the physical Manufacturer file held on disk. We must also ensure that the unstocking hasn’t already been done by another user, before setting our locks and updating the information.
In not already open, open the StockImpl class in your text editor and cut and paste the above unstocking() method into the StockImpl class overwriting the existing unstocking() method and comments. The unstocking() method can be found near the bottom of the StockImpl class just below the stocking() method.

/**
* Unstock the requested amount entered by a user in the GUI.
*
* @param name The name of the Manufacturer.
* @param location The location where the Manufacturer is based.
* @param stockOrdered The amount of unstocking to do.
*
*/
public void unstocking(String name, String location, int stockOrdered) {
log.entering(“StockImpl”, “unstocking”,
new Object[]{name, location, stockOrdered});
Long lock = 0L;
long[] retrecOffset = new long[1];
String[] criteria = {name, location};
boolean unlockRequired = true;
retrecOffset = findBySearchCriteria(criteria);
// Return the record number
long recOffset = retrecOffset[0];

if (recOffset == 0) {
log.log(Level.INFO, “Problem restocking Manufacturer: ” + name + “, ” + location);
String e = “Problem restocking Manufacturer: ” + name + “, ” + location;
throw new ServicesException(e);
}
try {
log.log(Level.FINE, “Run by: ” + Thread.currentThread().getName());
Manufacturer manufacturer = getManufacturer(recOffset);
// Firstly make sure that ordered stock hasn’t been cancelled by another user
if (manufacturer.getStockOrdered().equals(“”)) {
log.log(Level.INFO, “Manufacturer: ” + name + “, ” + location +
” , product: ” + manufacturer.getProduct() +
” stock quantity ordered, already reset by another user.”);
unlockRequired = false;
} else {
//Restock manufacturer and remove stock ordered
int manStockLevel = 0;
try {
manStockLevel = Integer.parseInt(manufacturer.getStockLevel());
} catch (NumberFormatException e) {
log.log(Level.INFO, “Manufacturer: ” + name + “, ” + location +
“, ” + ” stock level was non numeric”);
} finally {
manStockLevel = manStockLevel + stockOrdered;
String strStockLevel = “” + manStockLevel;
manufacturer.setStockLevel(strStockLevel);
manufacturer.setStockOrdered(“”);
String[] manufacturerData = {manufacturer.getDeletedFlag(),
manufacturer.getName(), manufacturer.getLocation(),
manufacturer.getProduct(), manufacturer.getPrice(),
manufacturer.getStockLevel(), manufacturer.getStockOrdered()};
//Add record number to the locking map
lock = lockRecord(recOffset);
//Update record with new stock totals
updateRecord(recOffset, manufacturerData, lock);
log.log(Level.INFO, “Manufacturer: ” + name + “, ” + location +
“, product: ” + manufacturer.getProduct() +
“, new Stock Level: ” + manufacturer.getStockLevel());
}
}
} catch (ManufacturerFileAccessException e) {
log.log(Level.SEVERE, e.getMessage(), e);
throw new ServicesException(e);
} catch (RecordNotFoundException e) {
log.log(Level.SEVERE, e.getMessage(), e);
throw new ServicesException(e);
} catch (SecurityException e) {
log.log(Level.SEVERE, e.getMessage(), e);
throw new ServicesException(e);
} finally {
if (unlockRequired) {
//Remove record number from the locking map
unlock(recOffset, lock);
}
}
log.exiting(“StockImpl”, “unstocking”);
}

Save the StockImpl class in the   c:_Case_Studysrcmodel directory.
Compiling Our Source File With the -cp and -d Options
Open your command line editor:
Change to directory  cd c:_Case_Studysrcmodel
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.

Related Java6 Tutorials
Beginning Java6 – Primitive Variables
Beginning Java6 – Conditional Statements
Objects & Classes – Arrays
Objects & Classes – Class Structure and Syntax
Objects & Classes – Reference Variables
Objects & Classes – Methods
Objects & Classes – Instance Variables & Scope
Objects & Classes – Constructors
Objects & Classes – Static Members
Objects & Classes – Enumerations
OO Concepts – Encapsulation – Getters & Setters
Flow Control – Handling Exceptions
Swing – RMI – Serialization

What’s Next?
That concludes the model code for the case study. In the next section we complete the controller code.
<<  Search Functionality                    Controller Part 2  >>
 Homepage  Top
All the Model Part 2 lessons are listed below. Click a link to go to that lesson.

Model Part 2
Search Functionality
Stock Functionality
Updating stocking() Method
Updating unstocking() Method
 

Read More Post