package com.epam.jenkins.plugin.jobolizer;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.logging.Level;

import com.cloudbees.plugins.flow.BuildFlow;
import com.cloudbees.plugins.flow.FlowRun;
import org.kohsuke.stapler.DataBoundConstructor;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.tasks.Builder;
import lombok.extern.java.Log;

import com.epam.jenkins.plugin.jobolizer.flow.FlowExecutionException;
import com.epam.jenkins.plugin.jobolizer.flow.FlowExecutor;
import com.epam.jenkins.plugin.jobolizer.job.JobCreatable;
import com.epam.jenkins.plugin.jobolizer.job.JobCreationException;
import com.epam.jenkins.plugin.jobolizer.job.JobCreator;
import com.epam.jenkins.plugin.jobolizer.dsl.Pipeline;

@Log
public class JobExecutionConfigurationBuilder extends Builder {

    @DataBoundConstructor
    public JobExecutionConfigurationBuilder() {
    }

    @Override
    public boolean perform(final AbstractBuild build, final Launcher launcher, final BuildListener listener) throws IOException, InterruptedException {
        URI uri = build.getWorkspace().toURI();
        String workspacePath = uri.getPath();
        File pipeLine = new File(workspacePath + "/.pipeline");
        boolean result = false;
        if (pipeLine.exists()) {
            log.info("Is about to start pipeline");
            JobCreatable jobFacade = new JobCreator(build, listener, uri);
            try {
                BuildFlow buildFlow = new BuildFlow(build.getParent().getParent(), "test");
                FlowRun flowRun = new FlowRun(buildFlow);
                new Pipeline(jobFacade, new FlowExecutor(flowRun,listener )).execute(pipeLine);
                result = true;
            } catch (FlowExecutionException fee) {
                log.log(Level.SEVERE, "Unable to perform execution of build flow", fee);
            } catch (JobCreationException jce) {
                log.log(Level.SEVERE, "Unable to create job(s) for pipeline", jce);
            }
        } else {
            log.warning("Pipeline file does not exist in directory: " + workspacePath);
        }
        return result;
    }

}

@@ -144,96 +144,8 @@
 l;%0A%0A
-import com.cloudbees.plugins.flow.BuildFlow;%0Aimport com.cloudbees.plugins.flow.FlowRun;%0A
 impo
@@ -344,16 +344,221 @@
 a.Log;%0A%0A
+import com.cloudbees.plugins.flow.BuildFlow;%0Aimport com.cloudbees.plugins.flow.FlowRun;%0Aimport com.epam.jenkins.plugin.jobolizer.dsl.Pipeline;%0Aimport com.epam.jenkins.plugin.jobolizer.flow.FlowExecutable;%0A
 import c
@@ -865,63 +865,8 @@
 tor;
-%0Aimport com.epam.jenkins.plugin.jobolizer.dsl.Pipeline;
 %0A%0A@L
@@ -1471,45 +1471,70 @@
     
-JobCreatable jobFacade = new 
+try %7B%0A                JobCreatable jobCreator = create
 JobCreat
 or(b
@@ -1529,18 +1529,20 @@
 JobCreat
-or
+able
 (build, 
@@ -1573,161 +1573,78 @@
     
-try %7B%0A                BuildFlow buildFlow = new BuildFlow(build.getParent().getParent(), %22test%22);%0A                FlowRun flowRun = new FlowRun(buildFlow
+    FlowExecutable flowExecutor = createFlowExecutable(build, listener
 );%0A 
@@ -1662,85 +1662,50 @@
     
-new Pipeline(jobFacade, new FlowExecutor(flowRun,listener )).execute(pipeLine
+execute(pipeLine, jobCreator, flowExecutor
 );%0A 
@@ -2166,10 +2166,694 @@
 %0A    %7D%0A%0A
+    private void execute(final File pipeLine, final JobCreatable jobCreator, final FlowExecutable flowExecutor) %7B%0A        new Pipeline(jobCreator, flowExecutor).execute(pipeLine);%0A    %7D%0A%0A    private JobCreatable createJobCreatable(final AbstractBuild build, final BuildListener listener, final URI uri) %7B%0A        return new JobCreator(build, listener, uri);%0A    %7D%0A%0A    private FlowExecutable createFlowExecutable(final AbstractBuild build, final BuildListener listener) throws IOException %7B%0A        BuildFlow buildFlow = new BuildFlow(build.getParent().getParent(), %22test%22);%0A        FlowRun flowRun = new FlowRun(buildFlow);%0A        return new FlowExecutor(flowRun, listener);%0A    %7D%0A%0A
 %7D%0A

Improve code readability