com.mycompany.expense
Class FileReportRegistry

java.lang.Object
  extended bycom.mycompany.expense.ReportRegistry
      extended bycom.mycompany.expense.FileReportRegistry

public class FileReportRegistry
extends ReportRegistry

This class is an implementation of the ReportRegistry for the sample expense report application that uses the file system for permanent storage. It's only intended as an example. For real usage of the sample application, an implementation that uses a database is a better choice.

Version:
1.0
Author:
Hans Bergsten, Gefion Software

Field Summary
private  int currentReportId
           
private  java.util.Map reports
           
 
Constructor Summary
FileReportRegistry()
          Creates an instance and loads the current content, if any, from the file system by calling the load() method.
 
Method Summary
 void addReport(Report report)
          Adds a copy of the report to the registry.
private  void checkExists(Report report)
          Tries to locate a report with the ID of the provided report, and throws an IllegalStateException if there's no such report.
 Report getReport(int id)
          Returns a copy of the report with the specified ID, or null if there's no matching report in the registry.
 java.util.List getReports(java.lang.String owner, java.util.Date fromDate, java.util.Date toDate, int[] status)
          Returns a List with copies of all reports matching the search criteria.
private  java.io.File getStore()
          Returns a File instance for a file named ".expense/store.ser" in the home directory for the account running the JVM.
private  void load()
          Loads the registry from the file returned by the getStore() method.
private  boolean matchesCriteria(Report report, java.lang.String owner, java.util.Date from, java.util.Date to, int[] status)
          Returns true if the report matches the non-null parameter values.
 void removeReport(Report report)
          Removes an existing report in the registry with ID of the provided report.
private  void save()
          Saves the registry to the file returned by the getStore() method.
 void updateReport(Report report)
          Replaces an existing report in the registry with a copy of the provided report.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

currentReportId

private int currentReportId

reports

private java.util.Map reports
Constructor Detail

FileReportRegistry

public FileReportRegistry()
                   throws RegistryException
Creates an instance and loads the current content, if any, from the file system by calling the load() method.

Method Detail

addReport

public void addReport(Report report)
               throws RegistryException
Adds a copy of the report to the registry.

Specified by:
addReport in class ReportRegistry
Throws:
RegistryException

updateReport

public void updateReport(Report report)
                  throws RegistryException
Replaces an existing report in the registry with a copy of the provided report. Throws an IllegalStateException if there's no report with the ID of the provided report in the registry.

Specified by:
updateReport in class ReportRegistry
Throws:
RegistryException

removeReport

public void removeReport(Report report)
                  throws RegistryException
Removes an existing report in the registry with ID of the provided report. Throws an IllegalStateException if there's no report with the ID of the provided report in the registry.

Specified by:
removeReport in class ReportRegistry
Throws:
RegistryException

getReport

public Report getReport(int id)
Returns a copy of the report with the specified ID, or null if there's no matching report in the registry.

Specified by:
getReport in class ReportRegistry

getReports

public java.util.List getReports(java.lang.String owner,
                                 java.util.Date fromDate,
                                 java.util.Date toDate,
                                 int[] status)
Returns a List with copies of all reports matching the search criteria.

Specified by:
getReports in class ReportRegistry

matchesCriteria

private boolean matchesCriteria(Report report,
                                java.lang.String owner,
                                java.util.Date from,
                                java.util.Date to,
                                int[] status)
Returns true if the report matches the non-null parameter values.


checkExists

private void checkExists(Report report)
Tries to locate a report with the ID of the provided report, and throws an IllegalStateException if there's no such report.


load

private void load()
           throws java.io.IOException
Loads the registry from the file returned by the getStore() method.

Throws:
java.io.IOException

save

private void save()
           throws java.io.IOException
Saves the registry to the file returned by the getStore() method.

Throws:
java.io.IOException

getStore

private java.io.File getStore()
Returns a File instance for a file named ".expense/store.ser" in the home directory for the account running the JVM.