/*
 * Copyright 2017 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.dbsnpimporter.configuration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.ItemWriteListener;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.listener.StepListenerSupport;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import uk.ac.ebi.eva.commons.core.models.IVariant;
import uk.ac.ebi.eva.dbsnpimporter.models.SubSnpCoreFields;
import uk.ac.ebi.eva.dbsnpimporter.parameters.Parameters;

import java.util.List;

@Configuration
public class ListenersConfiguration {

    @Bean
    public StepListenerSupport<SubSnpCoreFields, IVariant> variantsWriteListener(Parameters parameters) {
        return new writerListener(parameters);
    }

    private static class writerListener extends StepListenerSupport<SubSnpCoreFields, IVariant> {

        private static final Logger logger = LoggerFactory.getLogger(writerListener.class);

        private Parameters parameters;

        private StepExecution stepExecution;

        public writerListener(Parameters parameters) {
            this.parameters = parameters;
        }

        @Override
        public void beforeStep(StepExecution stepExecution) {
            logger.info("Starting a step");
            this.stepExecution = stepExecution;
        }

        @Override
        public void beforeChunk(ChunkContext context) {
            logger.info("Starting a batch");
        }

        @Override
        public void beforeRead() {
            if (stepExecution.getReadCount() % parameters.getChunkSize() == 0) {
                logger.info("About to read element (this log appears once every {} elements read)",
                            parameters.getChunkSize());
            }
        }

        @Override
        public void afterRead(SubSnpCoreFields item) {
            if (stepExecution.getReadCount() % parameters.getChunkSize() == 0) {
                logger.info("Element was read (this log appears once every {} elements read)",
                            parameters.getChunkSize());
            }
        }

        @Override
        public void beforeWrite(List<? extends IVariant> items) {
            logger.info("About to write a batch");
        }

        @Override
        public void afterWrite(List<? extends IVariant> items) {
            IVariant lastElement = items.get(items.size() - 1);
            logger.info("Wrote a batch of {} elements. Last element was {}: {}", items.size(),
                        lastElement.getMainId(), lastElement);
        }

        @Override
        public void afterChunk(ChunkContext context) {
            logger.info("Finished a batch");
        }

        @Override
        public ExitStatus afterStep(StepExecution stepExecution) {
            logger.info("Finished a step");
            return stepExecution.getExitStatus();
        }
    }
}

@@ -1805,35 +1805,22 @@
 ate 
-StepExecution stepExecution
+long itemsRead
 ;%0A%0A 
@@ -1915,16 +1915,48 @@
 meters;%0A
+            this.itemsRead = 0;%0A
         
@@ -2086,56 +2086,8 @@
 %22);%0A
-            this.stepExecution = stepExecution;%0A
     
@@ -2296,36 +2296,17 @@
 if (
-stepExecution.getReadCount()
+itemsRead
  %25 p
@@ -2500,32 +2500,57 @@
 ;%0A            %7D%0A
+            itemsRead++;%0A
         %7D%0A%0A     
@@ -2637,36 +2637,17 @@
 if (
-stepExecution.getReadCount()
+itemsRead
  %25 p

fix listeners