package org.slc.sli.ingestion.landingzone;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slc.sli.ingestion.FileFormat;
import org.slc.sli.ingestion.FileType;

/**
 * Represents control file information.
 *
 */
public class ControlFile implements Serializable {

    private static final long serialVersionUID = 3231739301361458948L;

    protected File file;
    protected List<IngestionFileEntry> fileEntries = new ArrayList<IngestionFileEntry>();
    protected Properties configProperties = new Properties();

    public static ControlFile parse(File file) throws IOException {

        Scanner scanner = new Scanner(file);
        Pattern fileItemPattern = Pattern.compile("^([^\\s]+)\\,([^\\s]+)\\,([^,]+)\\,(\\w+)\\s*$");
        Matcher fileItemMatcher;
        Pattern configItemPattern = Pattern.compile("^@(.*)$");
        Matcher configItemMatcher;
        String line;
        FileFormat fileFormat;
        FileType fileType;

        Properties configProperties = new Properties();
        ArrayList<IngestionFileEntry> fileEntries = new ArrayList<IngestionFileEntry>();

        try {
            while (scanner.hasNextLine()) {
                line = scanner.nextLine();
                fileItemMatcher = fileItemPattern.matcher(line);
                if (fileItemMatcher.matches()) {
                    fileFormat = FileFormat.findByCode(fileItemMatcher.group(1));
                    fileType = FileType.findByNameAndFormat(fileItemMatcher.group(2), fileFormat);
                    fileEntries.add(new IngestionFileEntry(fileFormat, fileType, fileItemMatcher.group(3), fileItemMatcher
                            .group(4)));
                    continue;
                }

                configItemMatcher = configItemPattern.matcher(line);
                if (configItemMatcher.matches()) {
                    configProperties.load(new ByteArrayInputStream(configItemMatcher.group(1).getBytes()));
                    // System.err.println("found configItem: ["+configItemMatcher.group(1)+"]");
                    continue;
                }

                // blank lines are ignored silently, but stray marks are not
                if (line.trim().length() > 0) {
                    // line was not parseable
                    // TODO fault or custom exception?
                    throw new RuntimeException("invalid control file entry: " + line);
                }
            }

            return new ControlFile(file, fileEntries, configProperties);

        } finally {
            scanner.close();
        }
    }

    public List<IngestionFileEntry> getFileEntries() {
        return this.fileEntries;
    }

    /**
     * @param file
     * @param fileEntries
     * @param configProperties
     */
    protected ControlFile(File file, List<IngestionFileEntry> fileEntries, Properties configProperties) {
        this.file = file;
        this.fileEntries = fileEntries;
        this.configProperties = configProperties;
    }

}

@@ -323,16 +323,74 @@
 ttern;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0A
 import o
@@ -460,16 +460,78 @@
 ileType;
+%0Aimport org.slc.sli.ingestion.processors.ControlFileProcessor;
 %0A%0A/**%0A *
@@ -697,16 +697,98 @@
 458948L;
+%0A    private static final Logger LOG = LoggerFactory.getLogger(ControlFile.class);
 %0A%0A    pr
@@ -1544,13 +1544,320 @@
     
-try %7B
+LOG.debug(%22parsing control file: %7B%7D%22, file);%0A%0A        try %7B%0A            try %7B%0A                // TODO: remove this sleep - using for now to test theory on possible race condition%0A                Thread.sleep(1000);%0A            %7D catch (InterruptedException e) %7B%0A                e.printStackTrace();%0A            %7D
 %0A   
@@ -1893,24 +1893,25 @@
 xtLine()) %7B%0A
+%0A
             
@@ -1937,24 +1937,83 @@
 nextLine();%0A
+                LOG.debug(%22scanned next line: %7B%7D%22, line);%0A%0A
             

add debug logging