Java – Designing status management for a file processing module

design-patternsfile handlingjava

The background

One of the functionality of a product that I am currently working on is to process a set of compressed files ( containing XML files ) that will be made available at a fixed location periodically (local or remote location – doesn't really matter for now) and dump the contents of each XML file in a database. I have taken care of the design for a generic parsing module that should be able to accommodate the parsing of any file type as I have explained in my question linked below. There is no need to take a look at the following link to answer my question but it would definitely provide a better context to the problem

Generic file parser design in Java using the Strategy pattern

The Goal

I want to be able to keep a track of the status of each XML file and the status of each compressed file containing the XML files. I can probably have different statuses defined for the XML files such as NEW, PROCESSING, LOADING, COMPLETE or FAILED. I can derive the status of a compressed file based on the status of the XML files within the compressed file. e.g status of the compressed file is COMPLETE if no XML file inside the compressed file is in a FAILED state or status of the compressed file is FAILED if the status of at-least one XML file inside the compressed file is FAILED.

A possible solution

The Model

I need to maintain the status of each XML file and the compressed file. I will have to define some POJOs for holding the information about an XML file as shown below. Note that there is no need to store the status of a compressed file as the status of a compressed file can be derived from the status of its XML files.

public class FileInformation {

    private String compressedFileName;
    private String xmlFileName;
    private long lastModifiedDate;
    private int status;

    public FileInformation(final String compressedFileName,
            final String xmlFileName, final long lastModified, final int status) {
        this.compressedFileName = compressedFileName;
        this.xmlFileName = xmlFileName;
        this.lastModifiedDate = lastModified;
        this.status = status;
    }
}

I can then have a class called StatusManager that aggregates a Map of FileInformation instances and provides me the status of a given file at any given time in the lifetime of the appliciation as shown below :

public class StatusManager {    
    private Map<String,FileInformation> processingMap = new HashMap<String,FileInformation>();  

    public void add(FileInformation fileInformation) {
        fileInformation.setStatus(0); // 0 will indicates that the file is in NEW state. 1 will indicate that the file is in process and so on..
        processingMap.put(fileInformation.getXmlFileName(),fileInformation);
    }

    public void update(String filename,int status) {        
        FileInformation fileInformation = processingMap.get(filename);
        fileInformation.setStatus(status);
    }   

}

That takes care of the model for the sake of explanation.

So whats my question?

Edited after comments from Loki and answer from Eric : –

I would like to know if there are any existing design patterns that I can refer to while coming up with a design. I would also like to know how I should go about designing the status management classes.

I am more interested in understanding how I can model the status management classes. I am not interested in how other components are going to be updated about a change in status at the moment as suggested by Eric.

Best Answer

The State pattern is used to define behavior of an object based on its current state, which isn't really a good fit for this problem if I read your question correctly (although I understood the instinct to look at that pattern). It'd work if you had code that was something like:

if (status.equals("COMPLETED"))
{
    doSomethingForCompleted();
}
else if (status.equals("FAILED"))
{
    doSomethingForFailure();
}
// You get the idea...

Given the way your example is set up, I think you'd want a combination of the Observer pattern like gnat suggested, and the Mediator pattern, which you're largely doing with your StatusManager already. StatusManager can notify your code with the new set of statuses after add() or update() calls.

Related Topic