/**
 * 
 */
package edu.yu.einstein.wasp.plugin.babraham.batch.tasklet;

import java.util.Set;

import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.annotation.BeforeStep;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.beans.factory.annotation.Autowired;

import edu.yu.einstein.wasp.Assert;
import edu.yu.einstein.wasp.daemon.batch.tasklets.WaspRemotingTasklet;
import edu.yu.einstein.wasp.grid.GridHostResolver;
import edu.yu.einstein.wasp.grid.work.GridResult;
import edu.yu.einstein.wasp.grid.work.WorkUnit;
import edu.yu.einstein.wasp.integration.messages.WaspJobParameters;
import edu.yu.einstein.wasp.model.Run;
import edu.yu.einstein.wasp.model.Sample;
import edu.yu.einstein.wasp.model.SampleSource;
import edu.yu.einstein.wasp.plugin.babraham.batch.tasklet.jobparameters.TrimGaloreParameters;
import edu.yu.einstein.wasp.plugin.babraham.software.TrimGalore;
import edu.yu.einstein.wasp.service.AdaptorService;
import edu.yu.einstein.wasp.service.RunService;
import edu.yu.einstein.wasp.service.SampleService;

/**
 * 
 * Performs the registration of files on a per- cellLibrary basis.
 * 
 * @author calder
 * 
 */
public class TrimGaloreRegisteringTasklet extends WaspRemotingTasklet {

    private int runId;
    private int cellLibraryId;
    private String softwareClass;

    @Autowired
    private SampleService sampleService;

    @Autowired
    private AdaptorService adaptorService;

    @Autowired
    private TrimGalore trimGalore;

    @Autowired
    private GridHostResolver hostResolver;

    @Autowired
    private RunService runService;

    public TrimGaloreRegisteringTasklet(String runId, String softwareName, String cellLibraryId) {
        this.runId = Integer.decode(runId);
        this.cellLibraryId = Integer.decode(cellLibraryId);
        this.softwareClass = softwareName;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void doExecute(ChunkContext context) throws Exception {

        SampleSource cellLibrary = sampleService.getCellLibraryBySampleSourceId(cellLibraryId);

        WorkUnit w = trimGalore.getRegisterTrimmedCommand(runId, cellLibrary.getId(), softwareClass);

        GridResult result = hostResolver.execute(w);

        storeStartedResult(context, result);

    }

    @Override
    @BeforeStep
    public void beforeStep(StepExecution stepExecution) {

        Assert.assertParameterNotNullNotZero(runId, "Must set RUN_ID");
        Assert.assertParameterNotNullNotZero(cellLibraryId, "Must set CELL_LIBRARY_ID");
        Assert.assertParameterNotNull(softwareClass, "Must set SOFTWARE_CLASS_NAME");

        super.beforeStep(stepExecution);
    }

}

@@ -87,66 +87,42 @@
 til.
-Se
+ArrayLis
 t;%0A
-%0A
 import 
-org.springframework.batch.core.JobParameters;
+java.util.List;%0A
 %0Aimp
@@ -298,16 +298,72 @@
 ontext;%0A
+import org.springframework.batch.item.ExecutionContext;%0A
 import o
@@ -416,16 +416,126 @@
 towired;
+%0Aimport org.springframework.transaction.annotation.Transactional;%0Aimport org.springframework.util.StringUtils;
 %0A%0Aimport
@@ -816,269 +816,71 @@
 asp.
-integration.messages.WaspJobParameters;%0Aimport edu.yu.einstein.wasp.model.Run;%0Aimport edu.yu.einstein.wasp.model.Sample;%0Aimport edu.yu.einstein.wasp.model.SampleSource;%0Aimport edu.yu.einstein.wasp.plugin.babraham.batch.tasklet.jobparameters.TrimGaloreParameters
+model.FileGroup;%0Aimport edu.yu.einstein.wasp.model.SampleSource
 ;%0Aim
@@ -986,32 +986,81 @@
 AdaptorService;%0A
+import edu.yu.einstein.wasp.service.FileService;%0A
 import edu.yu.ei
@@ -1457,32 +1457,89 @@
 e sampleService;
+%0A    %0A    @Autowired%0A    private FileService fileService;
 %0A%0A    @Autowired
@@ -2044,24 +2044,60 @@
   @Override%0A
+    @Transactional(%22entityManager%22)%0A
     public v
@@ -2409,59 +2409,1000 @@
 w);%0A
-%0A        storeStartedResult(context, result);%0A%0A    
+        %0A        // save results files id's in StepExecutionContext%0A        List%3CInteger%3E resultsFileIds = new ArrayList%3C%3E();%0A        for (FileGroup fg : w.getResultFiles())%0A        %09resultsFileIds.add(fg.getId());%0A        context.getStepContext().getStepExecution().getExecutionContext().putString(%22resultsFilesIdStr%22, StringUtils.collectionToCommaDelimitedString(resultsFileIds));%0A        %0A        storeStartedResult(context, result);%0A%0A    %7D%0A    %0A    /** %0A%09 * %7B@inheritDoc%7D%0A%09 */%0A%09@Override%0A%09@Transactional(%22entityManager%22)%0A%09public void doPreFinish(ChunkContext context) %7B%0A%09%09// get results files and make them active%0A%09%09ExecutionContext stepExecutionContext = context.getStepContext().getStepExecution().getExecutionContext();%0A%09%09if (stepExecutionContext.containsKey(%22resultsFilesIdStr%22))%0A%09%09%09for (String fgIdStr : StringUtils.commaDelimitedListToStringArray(stepExecutionContext.getString(%22resultsFilesIdStr%22)))%0A%09%09%09%09fileService.getFileGroupById(Integer.parseInt(fgIdStr)).setIsActive(1);%0A%09%09%09%0A%09
 %7D%0A%0A 

set results files to active after processing