package com.google.code.tickconverter.application;

import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Paths;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Appender;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.PropertyConfigurator;

import com.google.code.tickconverter.bean.IDukascopyRO;
import com.google.code.tickconverter.bean.IMetatraderRO;
import com.google.code.tickconverter.convert.ConvertAdapter;
import com.google.code.tickconverter.io.DukascopyCsvReader;
import com.google.code.tickconverter.io.MetatraderCsvWriter;
import com.google.code.tickconverter.util.AppProperties;

/**
 * @author Karsten Schulz <a href="mailto:lennylinux.ks@googlmail.com">(lennylinux.ks@googlmail.com)</a>
 */
public final class Controller
{

    /**
     * Default constructor and configure the logging framework for the application
     */
    public Controller()
    {
        configureLogging();
    }

    private void configureLogging()
    {
        String loggingLocation = AppProperties.getLoggingConfig();
        if ( StringUtils.isNotBlank( loggingLocation )
            && Files.exists( Paths.get( loggingLocation ), LinkOption.NOFOLLOW_LINKS ) )
        {
            PropertyConfigurator.configureAndWatch( loggingLocation );
        }
        else
        {
            Appender appender =
                new ConsoleAppender( new PatternLayout( "%d{yyyy-MM-dd/HH:mm:ss.SSS/zzz} [%c] %-5p %m%n" ) );
            BasicConfigurator.configure( appender );
            Logger.getRootLogger().setLevel( Level.toLevel( AppProperties.getLogLevel() ) );
        }
    }

    /**
     * Method to start the convert process in three indipendent different threads.
     * 
     * @see DukascopyCsvReader#read()
     * @see ConvertAdapter#convertProcess()
     * @see MetatraderCsvWriter#write()
     */
    public void process()
    {
        LinkedBlockingQueue<IDukascopyRO> dukasQueue = new LinkedBlockingQueue<>();
        LinkedBlockingQueue<IMetatraderRO> metatraderQueue = new LinkedBlockingQueue<>();
        Thread reader = new Thread( new DukascopyCsvReader( dukasQueue, AppProperties.getInputFile() ) );
        Thread convert = new Thread( new ConvertAdapter( dukasQueue, metatraderQueue ) );
        Thread writer = new Thread( new MetatraderCsvWriter( metatraderQueue, AppProperties.getOutputFile() ) );

        reader.start();
        convert.start();
        writer.start();
    }
}

@@ -1,8 +1,1150 @@
+/**%0A * Copyright (C) 2012 Karsten Schulz %3Clennylinux.ks@googlemail.com%3E%0A *%0A * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and%0A * associated documentation files (the %22Software%22), to deal in the Software without restriction,%0A * including without limitation the rights to use, copy, modify, merge, publish, distribute,%0A * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is%0A * furnished to do so, subject to the following conditions:%0A *%0A * The above copyright notice and this permission notice shall be included in all copies or%0A * substantial portions of the Software.%0A *%0A * THE SOFTWARE IS PROVIDED %22AS IS%22, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT%0A * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND%0A * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,%0A * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT%0A * OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.%0A */%0A
 package 

add license header to the source file Controller.java