/*
 * This file is part of Dependency-Check Jenkins plugin.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jenkinsci.plugins.DependencyCheck;

import hudson.model.Action;
import hudson.model.Run;
import jenkins.model.RunAction2;
import jenkins.tasks.SimpleBuildStep;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.jenkinsci.plugins.DependencyCheck.model.Finding;
import org.jenkinsci.plugins.DependencyCheck.model.SeverityDistribution;
import org.jenkinsci.plugins.DependencyCheck.transformer.FindingsTransformer;
import org.kohsuke.stapler.bind.JavaScriptMethod;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Ported from the Dependency-Track Jenkins plugin.
 *
 * @author Steve Springett (steve.springett@owasp.org)
 * @since 5.0.0
 */
public class ResultAction implements RunAction2, SimpleBuildStep.LastBuildAction {

    private Run<?, ?> run;
    private List<Finding> findings;
    private SeverityDistribution severityDistribution;
    private List<JobAction> projectActions;

    public ResultAction(Run<?, ?> build, List<Finding> findings, SeverityDistribution severityDistribution) {
        this.findings = findings;
        this.severityDistribution = severityDistribution;

        List<JobAction> projectActions = new ArrayList<>();
        projectActions.add(new JobAction(build.getParent()));
        this.projectActions = projectActions;
    }

    @Override
    public String getIconFileName() {
        return "/plugin/" + DependencyCheckPlugin.PLUGIN_ID + "/icons/dependency-check-icon.svg";
    }

    @Override
    public String getDisplayName() {
        return "Dependency-Check";
    }

    @Override
    public String getUrlName() {
        return "dependency-check-findings";
    }

    @Override
    public void onAttached(Run<?, ?> run) {
        this.run = run;
    }

    @Override
    public void onLoad(Run<?, ?> run) {
        this.run = run;
    }

    @Override
    public Collection<? extends Action> getProjectActions() {
        return this.projectActions;
    }

    public Run getRun() {
        return run;
    }

    public SeverityDistribution getSeverityDistribution() {
        return severityDistribution;
    }

    public List<Finding> getFindings() {
        return findings;
    }

    /**
     * Returns the UI model for an ECharts line chart that shows the findings.
     *
     * @return the UI model as JSON
     */
    @JavaScriptMethod
    @SuppressWarnings("unused") // Called by jelly view
    public JSONObject getFindingsJson() {
        final FindingsTransformer transformer = new FindingsTransformer();
        return transformer.transform(findings);
    }

    /**
     * Returns a JSON response with the statistics for severity.
     *
     * @return the UI model as JSON
     */
    @JavaScriptMethod
    @SuppressWarnings("unused") // Called by jelly view
    public JSONObject getSeverityDistributionJson() {
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setExcludes( new String[]{ "buildNumber"} );
        return JSONObject.fromObject(severityDistribution, jsonConfig);
    }

}

@@ -1127,25 +1127,26 @@
 va.util.
-ArrayList
+Collection
 ;%0Aimport
@@ -1158,32 +1158,33 @@
 .util.Collection
+s
 ;%0Aimport java.ut
@@ -1423,16 +1423,26 @@
 private 
+transient 
 Run%3C?, ?
@@ -1443,24 +1443,72 @@
 n%3C?, ?%3E run;
+ // transient: see RunAction2, and JENKINS-45892
 %0A    private
@@ -1590,52 +1590,8 @@
 ion;
-%0A    private List%3CJobAction%3E projectActions;
 %0A%0A  
@@ -1793,177 +1793,8 @@
 ion;
-%0A%0A        List%3CJobAction%3E projectActions = new ArrayList%3C%3E();%0A        projectActions.add(new JobAction(build.getParent()));%0A        this.projectActions = projectActions;
 %0A   
@@ -2414,27 +2414,61 @@
 urn 
-this.projectActions
+Collections.singleton(new JobAction(run.getParent()))
 ;%0A  

fix ResultAction serialization (JENKINS-45892)