package org.slc.sli.ingestion;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import java.util.UUID;

import org.slc.sli.ingestion.landingzone.IngestionFileEntry;
import org.slc.sli.ingestion.validation.ErrorReport;
import org.slc.sli.ingestion.validation.ErrorReportSupport;
import org.slc.sli.util.performance.PutResultInContext;

/**
 * Batch Job class.
 *
 * @author okrook
 *
 */
public final class BatchJob implements Serializable, ErrorReportSupport {

    private static final long serialVersionUID = -340538024579162600L;

    /**
     * holds references to the files involved in this Job
     */
    private List<IngestionFileEntry> files;

    /**
     * stores the date upon which the Job was created
     */
    private Date creationDate;

    /**
     * stores a globally unique ID for the Job
     */
    private String id;

    /**
     * stores configuration properties for the Job
     */
    private Properties configProperties;

    /**
     * holds references to errors/warnings associated with this job
     */
    private FaultsReport faults;

    /**
     * non-public constructor; use factory methods
     */
    private BatchJob() {
    }

    /**
     * Initialize a BatchJob with default settings for initialization
     *
     * @return BatchJob with default settings
     */
    public static BatchJob createDefault() {
        return BatchJob.createDefault(null);
    }


    /**
     * Initialize a BatchJob with default settings for initialization
     *
     * @param filename string representation of incoming file
     * @return BatchJob with default settings
     */
    public static BatchJob createDefault(String filename) {
        BatchJob job = new BatchJob();
        job.id = createId(filename);
        job.creationDate = new Date();
        job.configProperties = new Properties();
        job.files = new ArrayList<IngestionFileEntry>();
        job.faults = new FaultsReport();
        return job;
    }

    /**
     * generates a new unique ID
     */
    @PutResultInContext(returnName = "ingestionBatchJobId")
    protected static String createId(String filename) {
        if (filename == null)
            return System.currentTimeMillis() + "-" + UUID.randomUUID().toString();
        else
            return filename + "-" + System.currentTimeMillis() + "-" + UUID.randomUUID().toString();
    }

    /**
     * Adds a file.
     *
     * @param file
     * @return
     * @see java.util.List#add(java.lang.Object)
     */
    public boolean addFile(IngestionFileEntry file) {
        return files.add(file);
    }

    /**
     * @return the creationDate
     */
    public Date getCreationDate() {
        return creationDate;
    }

    @Override
    public ErrorReport getErrorReport() {
        return getFaultsReport();
    }

    public FaultsReport getFaultsReport() {
        return faults;
    }

    /**
     * @return the files
     */
    public List<IngestionFileEntry> getFiles() {
        return files;
    }

    /**
     * @return the jobId
     */
    public String getId() {
        return id;
    }

    /**
     * @param key
     * @return
     * @see java.util.Properties#getProperty(java.lang.String)
     */
    public String getProperty(String key) {
        return configProperties.getProperty(key);
    }

    /**
     * @param key
     * @param defaultValue
     * @return
     * @see java.util.Properties#getProperty(java.lang.String, java.lang.String)
     */
    public String getProperty(String key, String defaultValue) {
        return configProperties.getProperty(key, defaultValue);
    }

    /**
     * @return
     * @see java.util.Properties#propertyNames()
     */
    public Enumeration<?> propertyNames() {
        return configProperties.propertyNames();
    }

    /**
     * @param key
     * @param value
     * @return
     * @see java.util.Properties#setProperty(java.lang.String, java.lang.String)
     */
    public Object setProperty(String key, String value) {
        return configProperties.setProperty(key, value);
    }

    @Override
    public String toString() {
        return "BatchJob [id=" + id + ", creationDate=" + creationDate + ", configProperties=" + configProperties + "]";
    }

}

@@ -660,54 +660,43 @@
   * 
-holds references to the files involved in this
+stores a globally unique ID for the
  Job
@@ -720,38 +720,23 @@
 ate 
-List%3CIngestionFileEntry%3E files
+final String id
 ;%0A%0A 
@@ -816,16 +816,22 @@
 private 
+final 
 Date cre
@@ -861,43 +861,54 @@
   * 
-stores a globally unique ID for the
+holds references to the files involved in this
  Job
@@ -928,25 +928,46 @@
 private 
-String id
+List%3CIngestionFileEntry%3E files
 ;%0A%0A    /
@@ -1276,19 +1276,90 @@
 atchJob(
-) %7B
+String id) %7B%0A        this.id = id;%0A        this.creationDate = new Date();
 %0A    %7D%0A%0A
@@ -1594,17 +1594,16 @@
 %0A    %7D%0A%0A
-%0A
     /**%0A
@@ -1701,16 +1701,34 @@
 filename
+%0A     *           
  string 
@@ -1913,28 +1913,8 @@
 Job(
-);%0A        job.id = 
 crea
@@ -1931,48 +1931,11 @@
 ame)
+)
 ;%0A
-        job.creationDate = new Date();
 %0A   
@@ -2554,20 +2554,34 @@
  @param 
-file
+ingestionFileEntry
 %0A     * 
@@ -2695,15 +2695,76 @@
 try 
-file) %7B
+ingestionFileEntry) %7B%0A%0A        ingestionFileEntry.setBatchJobId(id);
 %0A   
@@ -2785,20 +2785,34 @@
 les.add(
-file
+ingestionFileEntry
 );%0A    %7D

set batch job id on IngestionFileEntries that are added to a BatchJob. code cleanup.