package hudson.plugins.warnings;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Maps;

import hudson.Launcher;
import hudson.matrix.MatrixAggregator;
import hudson.matrix.MatrixRun;
import hudson.matrix.MatrixBuild;

import hudson.model.BuildListener;

import hudson.plugins.analysis.core.HealthDescriptor;
import hudson.plugins.analysis.core.ParserResult;

/**
 * Aggregates {@link WarningsResultAction}s of {@link MatrixRun}s into
 * {@link MatrixBuild}.
 *
 * @author Ulli Hafner
 */
public class WarningsAnnotationsAggregator extends MatrixAggregator {
    private final HealthDescriptor healthDescriptor;
    private final String defaultEncoding;
    private final Map<String, ParserResult> totalsPerParser = Maps.newHashMap();
    private final boolean useStableBuildAsReference;

    /**
     * Creates a new instance of {@link WarningsAnnotationsAggregator}.
     *
     * @param build
     *            the matrix build
     * @param launcher
     *            the launcher
     * @param listener
     *            the build listener
     * @param healthDescriptor
     *            health descriptor
     * @param defaultEncoding
     *            the default encoding to be used when reading and parsing files
     * @param useStableBuildAsReference
     *            determines whether only stable builds should be used as
     *            reference builds or not
     */
    public WarningsAnnotationsAggregator(final MatrixBuild build, final Launcher launcher, final BuildListener listener,
            final HealthDescriptor healthDescriptor, final String defaultEncoding,
            final boolean useStableBuildAsReference) {
        super(build, launcher, listener);

        this.healthDescriptor = healthDescriptor;
        this.defaultEncoding = defaultEncoding;
        this.useStableBuildAsReference = useStableBuildAsReference;
    }

    @Override
    public boolean endRun(final MatrixRun run) throws InterruptedException, IOException {
        List<WarningsResultAction> actions = run.getActions(WarningsResultAction.class);
        if (!actions.isEmpty()) {
            for (WarningsResultAction action : actions) {
                if (!totalsPerParser.containsKey(action.getParser())) {
                    WarningsResult result = action.getResult();
                    ParserResult aggregation = new ParserResult();
                    aggregation.addAnnotations(result.getAnnotations());
                    aggregation.addModules(result.getModules());
                    totalsPerParser.put(action.getParser(), aggregation);
                }
            }
        }
        return true;
    }

    @Override
    public boolean endBuild() throws InterruptedException, IOException {
        for (String parser : totalsPerParser.keySet()) {
            WarningsBuildHistory history = new WarningsBuildHistory(build, parser, useStableBuildAsReference);
            WarningsResult result = new WarningsResult(build, history, totalsPerParser.get(parser), defaultEncoding, parser);
            build.addAction(new WarningsResultAction(build, healthDescriptor, result, parser));
        }

        return true;
    }
}


@@ -2295,50 +2295,66 @@
     
-WarningsResult result = action.get
+totalsPerParser.put(action.getParser(), new Parser
 Result()
 ;%0A  
@@ -2341,32 +2341,33 @@
 w ParserResult()
+)
 ;%0A              
@@ -2351,32 +2351,44 @@
 sult());%0A       
+     
+    %7D%0A%0A
              Par
@@ -2384,16 +2384,19 @@
         
+   
 ParserRe
@@ -2414,26 +2414,107 @@
 ation = 
-new Parser
+totalsPerParser.get(action.getParser());%0A                WarningsResult result = action.get
 Result()
@@ -2515,20 +2515,16 @@
 sult();%0A
-    
         
@@ -2592,36 +2592,32 @@
                 
-   
- 
 aggregation.addM
@@ -2649,100 +2649,8 @@
 ));%0A
-                    totalsPerParser.put(action.getParser(), aggregation);%0A                %7D%0A
     

Fix WarningsAnnotationsAggregator not aggregating