/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014, Groupon, Inc.
 *
 * 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 com.groupon.jenkins.dynamic.build.plugins.downstream;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.groupon.jenkins.buildtype.InvalidBuildConfigurationException;
import com.groupon.jenkins.buildtype.plugins.DotCiPluginAdapter;
import com.groupon.jenkins.dynamic.build.DynamicBuild;
import com.groupon.jenkins.dynamic.build.DynamicProject;
import com.groupon.jenkins.github.NoDuplicatesParameterAction;
import hudson.Extension;
import hudson.Launcher;
import hudson.model.*;
import jenkins.model.Jenkins;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Extension
public class DownstreamJobPlugin extends DotCiPluginAdapter {
    public DownstreamJobPlugin() {
        super("downstream_job");
    }

    @Override
    public boolean perform(DynamicBuild dynamicBuild, Launcher launcher, BuildListener listener) {
        if(!(options instanceof Map)){
           throw new InvalidBuildConfigurationException("Invalid format specified for " + getName()+" . Expecting a Map.") ;
        }
        Map<String,Object> jobOptions = (Map<String,Object>) options;
        for(String jobName: jobOptions.keySet()){
            DynamicProject job = findJob(jobName);
            Map<String,String> jobParams = new HashMap<String, String>((Map<String, String>) jobOptions.get(jobName));
            jobParams.put("SOURCE_BUILD",getSourceBuildNumber(dynamicBuild));
            job.scheduleBuild(0, getCause(dynamicBuild, job, jobOptions.get(jobName)), getParamsAction(jobParams));
        }

        return true;
    }

    private String getSourceBuildNumber(DynamicBuild dynamicBuild) {
        if(dynamicBuild.getCause() instanceof  DotCiUpstreamTriggerCause){
            List<ParameterValue> params = dynamicBuild.getAction(ParametersAction.class).getParameters();
            for(ParameterValue param : params){
               if(param.getName().equals("SOURCE_BUILD")){
                  return (String) param.getValue();
               }
            }
        }
        return "" +dynamicBuild.getNumber();
    }

    private NoDuplicatesParameterAction getParamsAction(Map<String,String> jobParams) {
        List<ParameterValue> params = new ArrayList<ParameterValue>();
        for(Map.Entry<String, String> entry : jobParams.entrySet()){
            params.add(new StringParameterValue(entry.getKey(),entry.getValue()));
        }

        return new NoDuplicatesParameterAction(params);
    }

    private DynamicProject findJob(String jobName) {
        final String orgName = jobName.split("/")[0];
        final String repoName = jobName.split("/")[1];
        return Iterables.find(Jenkins.getInstance().getAllItems(DynamicProject.class), new Predicate<DynamicProject>() {
            @Override
            public boolean apply(DynamicProject project) {
                return project.getParent().getName().equals(orgName) && project.getName().equals(repoName);
            }
        });
    }

    public Cause getCause(DynamicBuild sourceBuild, DynamicProject targetJob, Object jobOptions) {
        return new DotCiUpstreamTriggerCause(sourceBuild, targetJob, (Map<String, String>) jobOptions);
    }
}

@@ -1726,88 +1726,9 @@
 til.
-ArrayList;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map
+*
 ;%0A%0A@
@@ -1799,16 +1799,78 @@
 apter %7B%0A
+%0A    public static final String ON_RESULT_KEY = %22on_result%22;%0A%0A
     publ
@@ -2099,16 +2099,17 @@
         
+ 
 throw ne
@@ -2306,49 +2306,107 @@
     
-for(String jobName: jobOptions.keySet())%7B
+if(shouldKickOffJob(dynamicBuild,jobOptions))%7B%0A            String jobName = getJobName(jobOptions);
 %0A   
@@ -2662,16 +2662,112 @@
         
+listener.getLogger().println(%22Lauching dowstream job :%22 +job.getFullName());%0A            return 
 job.sche
@@ -2864,32 +2864,591 @@
 ms));%0A        %7D%0A
+        return true;%0A    %7D%0A%0A    private String getJobName(Map%3CString, Object%3E jobOptions) %7B%0A        Set%3CString%3E keySet = jobOptions.keySet();%0A        keySet.remove(ON_RESULT_KEY);%0A        return Iterables.getOnlyElement(keySet);%0A    %7D%0A%0A    private boolean shouldKickOffJob(DynamicBuild dynamicBuild, Map%3CString, Object%3E jobOptions)%7B%0A        if(jobOptions.keySet().contains(ON_RESULT_KEY))%7B%0A            String onResult = jobOptions.get(ON_RESULT_KEY).toString().toUpperCase() ;%0A            return dynamicBuild.getResult().toString().equals(onResult);%0A        %7D
 %0A        return 
@@ -3773,16 +3773,17 @@
         
+ 
 if(param
@@ -3832,24 +3832,26 @@
             
+  
 return (Stri
@@ -3868,24 +3868,25 @@
 getValue();%0A
+ 
             

Add on_result option to downstream job plugin