package com.boissinot.jenkins.csvexporter.service.extractor.jenkins;

import com.boissinot.jenkins.csvexporter.domain.OutputCSVJobObj;
import com.boissinot.jenkins.csvexporter.domain.jenkins.job.ConfigJob;
import com.boissinot.jenkins.csvexporter.domain.maven.pom.Developer;
import com.boissinot.jenkins.csvexporter.maven.extractor.POMRemoteObj;
import com.boissinot.jenkins.csvexporter.service.extractor.jenkins.command.BuildersElementRetriever;
import com.boissinot.jenkins.csvexporter.service.extractor.jenkins.pom.DeveloperElementRetriever;
import com.boissinot.jenkins.csvexporter.service.extractor.jenkins.scm.SCMElementBuilder;
import com.boissinot.jenkins.csvexporter.service.extractor.maven.pom.POMDeveloperSectionExtractor;
import com.boissinot.jenkins.csvexporter.service.extractor.maven.pom.POMFileInfoExtractor;
import com.boissinot.jenkins.csvexporter.service.http.HttpResourceContentFetcher;
import org.springframework.integration.Message;
import org.springframework.integration.MessageHeaders;
import org.springframework.integration.annotation.ServiceActivator;
import org.w3c.dom.Node;

import java.util.List;
import java.util.Map;

import static com.boissinot.jenkins.csvexporter.domain.JobMessageHeaders.*;

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

    private HttpResourceContentFetcher httpResourceContentFetcher;

    private POMFileInfoExtractor pomFileInfoExtractor;


    public void setHttpResourceContentFetcher(HttpResourceContentFetcher httpResourceContentFetcher) {
        this.httpResourceContentFetcher = httpResourceContentFetcher;
    }

    public void setPomFileInfoExtractor(POMFileInfoExtractor pomFileInfoExtractor) {
        this.pomFileInfoExtractor = pomFileInfoExtractor;
    }

    @ServiceActivator
    public OutputCSVJobObj buildObj(Message message) {
        OutputCSVJobObj.Builder builder = new OutputCSVJobObj.Builder();
        MessageHeaders headers = message.getHeaders();

        //jobName
        String jobName = headers.get(HEADER_JOB_NAME, String.class);
        builder.name(jobName);
        String functionalJobType = headers.get(HEADER_FUNCTIONAL_JOB_TYPE, String.class);
        builder.functionalJobType(functionalJobType);

        //jenkinsJobType
        String jenkinsJobType = headers.get(HEADER_JENKINS_JOB_TYPE, String.class);
        builder.jenkinsType(jenkinsJobType);
        //functionalJobLanguage
        String functionalJobLanguage = headers.get(HEADER_FUNCTIONAL_JOB_LANGUAGE, String.class);
        builder.functionalJobLanguage(functionalJobLanguage);
        //spec
        final String spec = (String) headers.get("spec");
        builder.trigger(spec);
        //description
        final String description = (String) headers.get("description");
        builder.desc(description);
        //disabled
        final Boolean disabled = (Boolean) headers.get("disabled");
        builder.disabled(disabled);

        //scm
        final Node scmNode = (Node) headers.get("scm");
        SCMElementBuilder scmElementBuilder = new SCMElementBuilder();
        scmElementBuilder.build(scmNode);
        builder.svnURL(scmElementBuilder.getSvnURL());
        builder.gitURL(scmElementBuilder.getGitURL());
        builder.cvsRoot(scmElementBuilder.getCvsRoot());
        builder.cvsModule(scmElementBuilder.getCvsModule());
        builder.cvsBranche(scmElementBuilder.getCvsBranche());

        //builders
        final Node buildersNode = (Node) headers.get("builders");
        BuildersElementRetriever buildersElementRetriever = new BuildersElementRetriever();
        final String buildSteps = buildersElementRetriever.buildCommandSection(buildersNode);
        builder.buildSteps(buildSteps);

        //developers
        ConfigJob configJob = new ConfigJob();
        configJob.setCvsBranche(scmElementBuilder.getCvsBranche());
        configJob.setCvsModule(scmElementBuilder.getCvsModule());
        configJob.setGitURL(scmElementBuilder.getGitURL());
        configJob.setSvnURL(scmElementBuilder.getSvnURL());
        configJob.setJobName(jobName);
        POMRemoteObj pomObj = pomFileInfoExtractor.getPomUrl(configJob, (Map) message.getHeaders().get("MODULE_MAP"));
        if (pomObj != null) {
            String pomContent = httpResourceContentFetcher.getContent(pomObj.getHttpURL());
            POMDeveloperSectionExtractor sectionExtractor = new POMDeveloperSectionExtractor();
            List<Developer> developers = sectionExtractor.extract(pomContent);
            DeveloperElementRetriever retriever = new DeveloperElementRetriever();
            builder.developers(retriever.buildDeveloperSection(developers));
        }

        return builder.build();
    }
}

@@ -1097,16 +1097,34 @@
 .Node;%0A%0A
+import java.io.*;%0A
 import j
@@ -4682,30 +4682,1468 @@
     
-return builder.build()
+final OutputCSVJobObj outputCSVJobObj = builder.build();%0A%0A        //==============%0A        File jobEmailsFile = new File(%22jobEmails.txt%22);%0A        FileWriter fileWriter = null;%0A        BufferedWriter bufferedWriter = null;%0A        PrintWriter printWriter = null;%0A        try %7B%0A            fileWriter = new FileWriter(jobEmailsFile, true);%0A            bufferedWriter = new BufferedWriter(fileWriter);%0A            printWriter = new PrintWriter(bufferedWriter);%0A            printWriter.append(outputCSVJobObj.getName()).append(%22;%22).append(outputCSVJobObj.getDevelopers());%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace();  //To change body of catch statement use File %7C Settings %7C File Templates.%0A        %7D finally %7B%0A            if (printWriter != null)%0A                printWriter.close();%0A            if (bufferedWriter != null) %7B%0A                try %7B%0A                    bufferedWriter.close();%0A                %7D catch (IOException e) %7B%0A                    e.printStackTrace();  //To change body of catch statement use File %7C Settings %7C File Templates.%0A                %7D%0A            %7D%0A            if (fileWriter != null) %7B%0A                try %7B%0A                    fileWriter.close();%0A                %7D catch (IOException e) %7B%0A                    e.printStackTrace();  //To change body of catch statement use File %7C Settings %7C File Templates.%0A                %7D%0A            %7D%0A        %7D%0A        //==============%0A%0A        return outputCSVJobObj
 ;%0A  

Add begin write publisher email file