package uk.org.sappho.code.heatmap.engine;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.apache.log4j.Logger;

import com.google.inject.Inject;
import com.thoughtworks.xstream.XStream;

import uk.org.sappho.code.heatmap.config.Configuration;
import uk.org.sappho.code.heatmap.config.ConfigurationException;
import uk.org.sappho.code.heatmap.issues.IssueManagementException;
import uk.org.sappho.code.heatmap.mapping.HeatMapSelector;

public class Releases {

    private final List<String> releaseNames;
    private final String storeFilename;
    private final Map<String, HeatMaps> releases = new HashMap<String, HeatMaps>();
    private ReleasesRawData releasesRawData = new ReleasesRawData();
    private final HeatMapSelector heatMapSelector;
    private static final Logger LOG = Logger.getLogger(Releases.class);

    @Inject
    public Releases(Configuration config, HeatMapSelector heatMapSelector) throws ConfigurationException {

        releaseNames = config.getPropertyList("releases");
        storeFilename = config.getProperty("releases.store.filename", "heatmap-data.xml");
        this.heatMapSelector = heatMapSelector;
    }

    public void update(ChangeSet change) throws IssueManagementException {

        update(change, true);
    }

    private void update(ChangeSet change, boolean updateRawData) throws IssueManagementException {

        if (updateRawData) {
            releasesRawData.update(change);
        }
        List<String> issueReleases = change.getIssue().getReleases();
        for (String issueRelease : issueReleases) {
            HeatMaps heatMaps = releases.get(issueRelease);
            if (heatMaps == null) {
                heatMaps = new HeatMaps();
                releases.put(issueRelease, heatMaps);
            }
            heatMaps.update(change, heatMapSelector);
        }
    }

    public final List<String> getUsedReleaseNames() {

        List<String> usedReleaseNames = new Vector<String>();
        for (String releaseName : releaseNames) {
            if (releases.get(releaseName) != null) {
                usedReleaseNames.add(releaseName);
            }
        }
        return usedReleaseNames;
    }

    public final HeatMaps getHeatMaps(String releaseName) {

        return releases.get(releaseName);
    }

    @SuppressWarnings("unchecked")
    public void load() throws IOException, IssueManagementException {

        LOG.info("Loading data from " + storeFilename);
        XStream xstream = new XStream();
        Reader reader = new FileReader(storeFilename);
        releasesRawData = (ReleasesRawData) xstream.fromXML(reader);
        reader.close();
        for (ChangeSet change : releasesRawData.getChanges()) {
            update(change, false);
        }
    }

    public void save() throws IOException {

        LOG.info("Saving data to " + storeFilename);
        XStream xstream = new XStream();
        Writer writer = new FileWriter(storeFilename);
        xstream.toXML(releasesRawData, writer);
        writer.close();
    }
}

@@ -570,32 +570,88 @@
 mentException;%0D%0A
+import uk.org.sappho.code.heatmap.issues.IssueWrapper;%0D%0A
 import uk.org.sa
@@ -694,16 +694,16 @@
 ector;%0D%0A
-
 %0D%0Apublic
@@ -961,24 +961,76 @@
 RawData();%0D%0A
+    private final XStream xstream = new XStream();%0D%0A
     private 
@@ -1323,24 +1323,24 @@
 eleases%22);%0D%0A
-
         stor
@@ -1415,24 +1415,199 @@
 ata.xml%22);%0D%0A
+        for (Class clazz : new Class%5B%5D %7B ReleasesRawData.class, IssueWrapper.class, ChangeSet.class %7D) %7B%0D%0A            xstream.alias(clazz.getSimpleName(), clazz);%0D%0A        %7D%0D%0A
         this
@@ -3000,50 +3000,8 @@
 );%0D%0A
-        XStream xstream = new XStream();%0D%0A
     
@@ -3373,50 +3373,8 @@
 );%0D%0A
-        XStream xstream = new XStream();%0D%0A
     

Use xstream aliases to attempt to cut down on output file sizes and increase clarity. Not sure if this will work out yet.