/*
 * The MIT License
 * 
 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Tom Huybrechts
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package hudson.maven;

import hudson.Extension;
import hudson.Launcher;
import hudson.maven.reporters.SurefireAggregatedReport;
import hudson.maven.reporters.SurefireReport;
import hudson.model.BuildListener;
import hudson.model.Saveable;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Descriptor;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Publisher;
import hudson.tasks.Recorder;
import hudson.tasks.junit.TestDataPublisher;
import hudson.tasks.junit.TestResult;
import hudson.tasks.junit.TestResultAction.Data;
import hudson.tasks.test.AggregatedTestResultAction.ChildReport;
import hudson.util.DescribableList;

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

import net.sf.json.JSONObject;

import org.kohsuke.stapler.StaplerRequest;

/**
 * Augments {@link SurefireReport} by executing {@link TestDataPublisher}s.
 * @since 1.320
 */
public class MavenTestDataPublisher
    extends Recorder
{
    private final DescribableList<TestDataPublisher, Descriptor<TestDataPublisher>> testDataPublishers;

    public MavenTestDataPublisher(DescribableList<TestDataPublisher, Descriptor<TestDataPublisher>> testDataPublishers)
    {
        super();
        this.testDataPublishers = testDataPublishers;
    }

    public BuildStepMonitor getRequiredMonitorService()
    {
        return BuildStepMonitor.STEP;
    }

    @Override
    public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener)
        throws InterruptedException, IOException
    {

        SurefireAggregatedReport report = build.getAction(SurefireAggregatedReport.class);
        if (report == null)
        {
            return true;
        }
        for (ChildReport child : report.getChildReports())
        {
            List<Data> data = new ArrayList<Data>();
            if (testDataPublishers != null)
            {
                for (TestDataPublisher tdp : testDataPublishers)
                {
                    Data d = tdp.getTestData(build, launcher, listener, (TestResult) child.result);
                    if (d != null)
                    {
                        data.add(d);
                    }
                }
            }
            SurefireReport sureReport = child.child.getAction(SurefireReport.class);
            sureReport.setData(data);
            sureReport.owner.save();
        }
        return true;
    }

    public DescribableList<TestDataPublisher, Descriptor<TestDataPublisher>> getTestDataPublishers()
    {
        return testDataPublishers;
    }

    @Extension
    public static class DescriptorImpl
        extends BuildStepDescriptor<Publisher>
    {

        @Override
        public String getDisplayName()
        {
            return "Additional test report features";
        }

        @Override
        public boolean isApplicable(Class<? extends AbstractProject> jobType)
        {
            return MavenModuleSet.class.isAssignableFrom(jobType) && !TestDataPublisher.all().isEmpty();
        }

        @Override
        public Publisher newInstance(StaplerRequest req, JSONObject formData)
            throws FormException
        {
            DescribableList<TestDataPublisher, Descriptor<TestDataPublisher>> testDataPublishers =
                new DescribableList<TestDataPublisher, Descriptor<TestDataPublisher>>(Saveable.NOOP);
            try
            {
                testDataPublishers.rebuild(req, formData, TestDataPublisher.all());
            }
            catch (IOException e)
            {
                throw new FormException(e, null);
            }

            return new MavenTestDataPublisher(testDataPublishers);
        }

    }

}