/*
 * Copyright 2014-2018 EMBL - European Bioinformatics Institute
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package uk.ac.ebi.eva.commons.batch.job;

import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.util.StringUtils;


import uk.ac.ebi.eva.commons.batch.exception.NoJobToExecuteException;
import uk.ac.ebi.eva.commons.batch.exception.NoParametersHaveBeenPassedException;
import uk.ac.ebi.eva.commons.batch.exception.NoPreviousJobExecutionException;

import java.util.Date;

/**
 * Utility class to change job / step status
 */
public class ManageJobsUtils {

    public static void checkIfPropertiesHaveBeenProvided(JobParameters jobParameters)
            throws NoParametersHaveBeenPassedException {
        // TODO check what happens when InputParameter class is empty because there is no properties file
        if (jobParameters == null || jobParameters.isEmpty()) {
            throw new NoParametersHaveBeenPassedException();
        }
    }

    public static void checkIfJobNameHasBeenDefined(String jobName) throws NoJobToExecuteException {
        if (!StringUtils.hasText(jobName)) {
            throw new NoJobToExecuteException();
        }
    }

    public static void markLastJobAsFailed(JobRepository jobRepository, String jobName, JobParameters
            jobParameters) throws NoPreviousJobExecutionException {
        JobExecution lastJobExecution = jobRepository.getLastJobExecution(jobName, jobParameters);
        if (lastJobExecution == null) {
            throw new NoPreviousJobExecutionException(jobName, jobParameters);
        }

        Date currentTime = new Date();
        lastJobExecution.setEndTime(currentTime);
        lastJobExecution.setStatus(BatchStatus.FAILED);
        lastJobExecution.setExitStatus(
                lastJobExecution.getExitStatus().replaceExitCode("FAILED").addExitDescription("Manually " +
                        "failed job")
        );
        jobRepository.update(lastJobExecution);

        for (StepExecution stepExecution : lastJobExecution.getStepExecutions()) {
            stepExecution.setEndTime(currentTime);
            stepExecution.setStatus(BatchStatus.FAILED);
            stepExecution.setExitStatus(lastJobExecution.getExitStatus().replaceExitCode("FAILED"));
            jobRepository.update(stepExecution);
        }
    }

}

@@ -757,32 +757,84 @@
 e.JobExecution;%0A
+import org.springframework.batch.core.JobParameter;%0A
 import org.sprin
@@ -1287,16 +1287,42 @@
 il.Date;
+%0Aimport java.util.Objects;
 %0A%0A/**%0A *
@@ -1555,143 +1555,94 @@
     
-// TODO check what happens when InputParameter class is empty because there is no properties file%0A        if (jobParameters == null %7C%7C 
+if (jobParameters == null %7C%7C jobParameters.isEmpty() %7C%7C allJobParametersValuesAreNull(
 jobP
@@ -1650,25 +1650,16 @@
 rameters
-.isEmpty(
 )) %7B%0A   
@@ -1725,32 +1725,245 @@
        %7D%0A    %7D%0A%0A
+    private static boolean allJobParametersValuesAreNull(JobParameters jobParameters) %7B%0A        return jobParameters.getParameters().values().stream().map(JobParameter::getValue).allMatch(Objects::isNull);%0A    %7D%0A%0A
     public stati

Improve ManageJobUtils.checkIfPropertiesHaveBeenProvided