FileDocCategorySizeDatePackage
FileReportRegistry.javaAPI DocExample6928Tue Jun 08 11:26:42 BST 2004com.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.
author
Hans Bergsten, Gefion Software
version
1.0

Fields Summary
private int
currentReportId
private Map
reports
Constructors Summary
public FileReportRegistry()
Creates an instance and loads the current content, if any, from the file system by calling the load() method.

        reports = new HashMap();
        try {
            load();
        }
        catch (IOException e) {
            throw new RegistryException("Can't load ReportRegistry", e);
        }
    
Methods Summary
public synchronized voidaddReport(Report report)
Adds a copy of the report to the registry.

        report.setId(currentReportId++);
        reports.put(new Integer(report.getId()), new Report(report));
        try {
            save();
        }
        catch (IOException e) {
            throw new RegistryException("Can't save ReportRegistry", e);
        }
    
private voidcheckExists(Report report)
Tries to locate a report with the ID of the provided report, and throws an IllegalStateException if there's no such report.

        Integer id = new Integer(report.getId());
        if (reports == null || reports.get(id) == null) {
            throw new IllegalStateException("Report " + report.getId() +
                                            " doesn't exist");
        }
    
public synchronized ReportgetReport(int id)
Returns a copy of the report with the specified ID, or null if there's no matching report in the registry.

        return (Report) reports.get(new Integer(id));
    
public synchronized java.util.ListgetReports(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.

        List matches = new ArrayList();
        Iterator i = reports.values().iterator();
        while (i.hasNext()) {
            Report report = (Report) i.next();
            if (matchesCriteria(report, owner, fromDate, toDate, status)) {
                matches.add(new Report(report));
            }
        }
        return matches;
    
private java.io.FilegetStore()
Returns a File instance for a file named ".expense/store.ser" in the home directory for the account running the JVM.

        File store = null;
        File homeDir = new File(System.getProperty("user.home"));
        File persistenceDir = new File(homeDir, ".expense");
        if (!persistenceDir.exists()) {
            persistenceDir.mkdir();
        }
        return new File(persistenceDir, "store.ser");
    
private voidload()
Loads the registry from the file returned by the getStore() method.

        File store = getStore();
        try {
            ObjectInputStream is = 
                new ObjectInputStream(new FileInputStream(store));
            currentReportId = is.readInt();
            reports = (Map) is.readObject();
        }
        catch (FileNotFoundException fnfe) {
            // Ignore.
        }
        catch (ClassNotFoundException cnfe) {
            // Shouldn't happen, but log it if it does
            System.err.println("Error loading ReportRegistry: " +
                               cnfe.getMessage());
        }
    
private booleanmatchesCriteria(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.

        boolean matches = false;
        if ((owner == null || owner.equals(report.getOwner())) &&
            (from == null || (report.getStartDate() != null &&
             report.getStartDate().getTime() >= from.getTime())) &&
            (to == null || (report.getStartDate() != null &&
             report.getStartDate().getTime() <= to.getTime()))) {
            if (status == null) {
                matches = true;
            }
            else {
                for (int i = 0; i < status.length; i++) {
                    if (report.getStatus() == status[i]) {
                        matches = true;
                        break;
                    }
                }
            }
        }
        return matches;
    
public synchronized voidremoveReport(Report report)
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.

        checkExists(report);
        reports.remove(new Integer(report.getId()));
        try {
            save();
        }
        catch (IOException e) {
            throw new RegistryException("Can't save ReportRegistry", e);
        }
    
private voidsave()
Saves the registry to the file returned by the getStore() method.

        File store = getStore();
        ObjectOutputStream os = 
            new ObjectOutputStream(new FileOutputStream(store));
        os.writeInt(currentReportId);
        os.writeObject(reports);
    
public synchronized voidupdateReport(Report report)
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.

        checkExists(report);
        reports.put(new Integer(report.getId()), new Report(report));
        try {
            save();
        }
        catch (IOException e) {
            throw new RegistryException("Can't save ReportRegistry", e);
        }