package com.boissinot.jenkins.csvexporter.launch;

import org.springframework.batch.core.*;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author Gregory Boissinot
 */
public class Main {

    public static void main(String[] args) throws JobInstanceAlreadyCompleteException, JobParametersInvalidException, JobRestartException, JobExecutionAlreadyRunningException {

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-csv.xml");

        JobLauncher jobLauncher = applicationContext.getBean("jobLauncher", JobLauncher.class);
        Job job = applicationContext.getBean("extract-jenkins-job", Job.class);

        JobExecution jobExecution = jobLauncher.run(job, getJobParameters());
        System.out.println(jobExecution.getExitStatus());
        System.out.println(jobExecution.getFailureExceptions());
    }

    private static JobParameters getJobParameters() {
        JobParametersBuilder parametersBuilder = new JobParametersBuilder();

        final String onFolder = System.getProperty("onFolder");
        if (onFolder == null) {
            parametersBuilder.addString("on.folder", "true");
        } else {
            parametersBuilder.addString("on.folder", onFolder);
        }

        final String jenkinsURL = System.getProperty("jenkinsURL");
        if (jenkinsURL == null) {
            parametersBuilder.addString("jenkins.url", "http://calypso/jenkins/");
        } else {
            parametersBuilder.addString("jenkins.url", jenkinsURL);
        }

        final String folderPath = System.getProperty("folderPath");
        if (folderPath == null) {
            parametersBuilder.addString("folder.path", "/Users/gregory/Dev/configs/");
        } else {
            parametersBuilder.addString("folder.path", folderPath);
        }

        final String exportJobsPath = System.getProperty("exportJobsPath");
        if (exportJobsPath == null) {
            parametersBuilder.addString("exportcsv.filepath", "export-jobs.csv");
        } else {
            parametersBuilder.addString("exportcsv.filepath", exportJobsPath);
        }

        return parametersBuilder.toJobParameters();
    }
}

@@ -1538,11 +1538,12 @@
 %22, %22
-tru
+fals
 e%22);
@@ -1552,32 +1552,81 @@
        %7D else %7B%0A
+            if (Boolean.valueOf(onFolder)) %7B%0A    
             para
@@ -1673,35 +1673,32 @@
 older);%0A        
-%7D%0A%0A
         final St
@@ -1702,26 +1702,26 @@
  String 
-jenkinsURL
+folderPath
  = Syste
@@ -1735,26 +1735,26 @@
 operty(%22
-jenkinsURL
+folderPath
 %22);%0A    
@@ -1761,22 +1761,30 @@
     
+ 
-if (jenkinsURL
+       if (folderPath
  == 
@@ -1783,32 +1783,40 @@
 Path == null) %7B%0A
+        
             para
@@ -1844,58 +1844,73 @@
 ng(%22
-jenkins.url%22, %22http://calypso/jenkins/%22);%0A
+folder.path%22, %22/Users/gregory/Dev/_old/configs/%22);%0A      
         
 %7D el
@@ -1897,32 +1897,34 @@
 ;%0A              
+  
 %7D else %7B%0A       
@@ -1920,32 +1920,40 @@
 e %7B%0A            
+    
+    
 parametersBuilde
@@ -1969,34 +1969,66 @@
 ng(%22
-jenkins.url%22, jenkinsURL);
+folder.path%22, folderPath);%0A                %7D%0A            %7D
 %0A   
@@ -2052,34 +2052,34 @@
 inal String 
-folderPath
+jenkinsURL
  = System.ge
@@ -2085,34 +2085,34 @@
 etProperty(%22
-folderPath
+jenkinsURL
 %22);%0A        
@@ -2111,34 +2111,34 @@
         if (
-folderPath
+jenkinsURL
  == null) %7B%0A
@@ -2182,48 +2182,44 @@
 ng(%22
-folder.path%22, %22/Users/gregory/Dev/config
+jenkins.url%22, %22http://calypso/jenkin
 s/%22)
@@ -2282,32 +2282,32 @@
 ng(%22
-folder.path%22, folderPath
+jenkins.url%22, jenkinsURL
 );%0A 

Fix parameters