/*
 * Copyright 2015 Sebastian Gil.
 *
 * 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 com.create.application.configuration;

import org.apache.commons.logging.impl.SLF4JLog;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.integration.launch.JobLaunchingGateway;
import org.springframework.batch.integration.launch.JobLaunchingMessageHandler;
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.logging.LogLevel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.integration.annotation.InboundChannelAdapter;
import org.springframework.integration.annotation.IntegrationComponentScan;
import org.springframework.integration.annotation.Poller;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.channel.NullChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.channel.interceptor.WireTap;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.config.xml.LoggingChannelAdapterParser;
import org.springframework.integration.core.MessageSource;
import org.springframework.integration.file.FileReadingMessageSource;
import org.springframework.integration.file.filters.AcceptOnceFileListFilter;
import org.springframework.integration.file.filters.CompositeFileListFilter;
import org.springframework.integration.file.filters.SimplePatternFileListFilter;
import org.springframework.integration.handler.LoggingHandler;
import org.springframework.integration.scheduling.PollerMetadata;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.scheduling.support.PeriodicTrigger;
import org.springframework.transaction.PlatformTransactionManager;

import javax.transaction.TransactionManager;
import java.io.File;

@Configuration
@EnableIntegration
@ComponentScan(basePackages = {
        "com.create.batch.integration"
})
public class IntegrationConfiguration {
//    @Bean
//    public PlatformTransactionManager transactionManager() {
//        return new JpaTransactionManager();
//    }

    @Bean(name = PollerMetadata.DEFAULT_POLLER)
    public PollerMetadata defaultPoller() {
        final PollerMetadata pollerMetadata = new PollerMetadata();
        pollerMetadata.setTrigger(new PeriodicTrigger(10));
        return pollerMetadata;
    }

    @Bean
    public DirectChannel inboundFileChannel() {
        return new DirectChannel();
    }

    @Bean
    public DirectChannel outboundJobRequestChannel() {
        return new DirectChannel();
    }

    @Bean
    @ServiceActivator(inputChannel = "loggingChannel")
    public LoggingHandler loggingHandler() {
        return new LoggingHandler(LoggingHandler.Level.INFO.name());
    }

    @Bean
    @InboundChannelAdapter(value = "inboundFileChannel", poller = @Poller(cron="${ticket.poller.cron}"))
    public MessageSource<File> fileMessageSource(@Value("${ticket.poller.path}")final String path,
                                                 @Value("${ticket.poller.fileMask}")final String fileMask) {
        final FileReadingMessageSource source = new FileReadingMessageSource();
        final CompositeFileListFilter<File> compositeFileListFilter = new CompositeFileListFilter<>();
        final SimplePatternFileListFilter simplePatternFileListFilter = new SimplePatternFileListFilter(fileMask);
        final AcceptOnceFileListFilter<File> acceptOnceFileListFilter = new AcceptOnceFileListFilter<>();
        compositeFileListFilter.addFilter(simplePatternFileListFilter);
        compositeFileListFilter.addFilter(acceptOnceFileListFilter);
        source.setFilter(compositeFileListFilter);
         source.setDirectory(new File(path));
        return source;
    }

    @ServiceActivator(inputChannel = "outboundJobRequestChannel", outputChannel = "loggingChannel")
    @Bean
    public JobLaunchingMessageHandler jobLaunchingGateway(final JobLauncher jobLauncher) {
        return new JobLaunchingMessageHandler(jobLauncher);
    }

//    @ServiceActivator(inputChannel = "outboundJobRequestChannel")
//    @Bean
//    public JobLaunchingGateway jobLaunchingGateway(final JobLauncher jobLauncher) {
//        JobLaunchingGateway jobLaunchingGateway = new JobLaunchingGateway(jobLauncher);
//        jobLaunchingGateway.setRequiresReply(false);
//        return jobLaunchingGateway;
//    }

//@Bean
//public IntegrationFlow fileReadingFlow(@Value("${fpml.messages.input}") File directory) {
//    return IntegrationFlows
//            .from(s -> s.file(directory).patternFilter("*.xml"),
//                    e -> e.poller(Pollers.fixedDelay(20000)))
//    .....................
//    .get();
//}
}

@@ -647,57 +647,8 @@
 n;%0A%0A
-import org.apache.commons.logging.impl.SLF4JLog;%0A
 impo
@@ -769,178 +769,20 @@
 hing
-Gateway;%0Aimport org.springframework.batch.integration.launch.JobLaunchingMessageHandler;%0Aimport org.springframework.batch.support.transaction.ResourcelessTransactionManag
+MessageHandl
 er;%0A
@@ -844,58 +844,8 @@
 ue;%0A
-import org.springframework.boot.logging.LogLevel;%0A
 impo
@@ -1018,70 +1018,8 @@
 on;%0A
-import org.springframework.context.annotation.PropertySource;%0A
 impo
@@ -1091,84 +1091,8 @@
 er;%0A
-import org.springframework.integration.annotation.IntegrationComponentScan;%0A
 impo
@@ -1319,299 +1319,31 @@
 on.c
-hannel.NullChannel;%0Aimport org.springframework.integration.channel.QueueChannel;%0Aimport org.springframework.integration.channel.interceptor.WireTap;%0Aimport org.springframework.integration.config.EnableIntegration;%0Aimport org.springframework.integration.config.xml.LoggingChannelAdapterParser
+onfig.EnableIntegration
 ;%0Aim
@@ -1865,313 +1865,44 @@
 ork.
-messaging.Message;%0Aimport org.springframework.messaging.MessageChannel;%0Aimport org.springframework.orm.jpa.JpaTransactionManager;%0Aimport org.springframework.scheduling.support.PeriodicTrigger;%0Aimport org.springframework.transaction.PlatformTransactionManager;%0A%0Aimport javax.transaction.TransactionManager;
+scheduling.support.PeriodicTrigger;%0A
 %0Aimp
@@ -2071,137 +2071,8 @@
 on %7B
-%0A//    @Bean%0A//    public PlatformTransactionManager transactionManager() %7B%0A//        return new JpaTransactionManager();%0A//    %7D
 %0A%0A  
@@ -2810,17 +2810,19 @@
 ler(cron
-=
+ = 
 %22$%7Bticke
@@ -2919,16 +2919,17 @@
 .path%7D%22)
+ 
 final St
@@ -3023,16 +3023,17 @@
 eMask%7D%22)
+ 
 final St
@@ -3649,25 +3649,24 @@
 r);%0A        
- 
 source.setDi
@@ -3990,673 +3990,7 @@
    %7D
-%0A%0A//    @ServiceActivator(inputChannel = %22outboundJobRequestChannel%22)%0A//    @Bean%0A//    public JobLaunchingGateway jobLaunchingGateway(final JobLauncher jobLauncher) %7B%0A//        JobLaunchingGateway jobLaunchingGateway = new JobLaunchingGateway(jobLauncher);%0A//        jobLaunchingGateway.setRequiresReply(false);%0A//        return jobLaunchingGateway;%0A//    %7D%0A%0A//@Bean%0A//public IntegrationFlow fileReadingFlow(@Value(%22$%7Bfpml.messages.input%7D%22) File directory) %7B%0A//    return IntegrationFlows%0A//            .from(s -%3E s.file(directory).patternFilter(%22*.xml%22),%0A//                    e -%3E e.poller(Pollers.fixedDelay(20000)))%0A//    .....................%0A//    .get();%0A//%7D
 %0A%7D%0A

Clean up config