package org.slc.sli.ingestion.xml.idref;

import java.util.concurrent.Callable;

import org.slc.sli.ingestion.BatchJobStageType;
import org.slc.sli.ingestion.Fault;
import org.slc.sli.ingestion.FaultType;
import org.slc.sli.ingestion.Job;
import org.slc.sli.ingestion.landingzone.IngestionFileEntry;
import org.slc.sli.ingestion.model.Error;
import org.slc.sli.ingestion.model.da.BatchJobDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * Id Reference Resolution of the future...
 *
 * @author shalka
 */
@Scope("prototype")
@Component
public class IdRefResolutionCallable implements Callable<Boolean> {

    private static final Logger LOG = LoggerFactory.getLogger(IdRefResolutionCallable.class);

    private final IdRefResolutionHandler resolver;
    private final IngestionFileEntry entry;
    private final Job job;
    private final BatchJobDAO batchJobDao;

    /**
     * Default constructor for the id reference resolution callable.
     *
     * @param fileEntry
     *            ingestion file entry.
     * @param job
     *            batch job.
     * @param handler
     *            IdRefResolutionHandler to resolve references in ingestion file entries.
     */
    public IdRefResolutionCallable(IdRefResolutionHandler resolver, IngestionFileEntry fileEntry, Job job,
            BatchJobDAO batchJobDao) {
        this.resolver = resolver;
        this.entry = fileEntry;
        this.job = job;
        this.batchJobDao = batchJobDao;
    }

    /**
     * Entry point of IdRefResolutionCallable.
     */
    @Override
    public Boolean call() throws Exception {
        LOG.info("Starting IdRefResolutionCallable for: " + entry.getFileName());

        resolver.handle(entry, entry.getErrorReport());

        boolean hasErrors = aggregateAndLogResolutionErrors(entry);

        LOG.info("Finished IdRefResolutionCallable for: " + entry.getFileName());
        return hasErrors;
    }

    /**
     * Logs errors incurred during id reference resolution.
     *
     * @param fileEntry
     *            ingestion file entry.
     * @return integer representing number of errors during id reference resolution.
     */
    private boolean aggregateAndLogResolutionErrors(IngestionFileEntry fileEntry) {
        int errorCount = 0;
        for (Fault fault : fileEntry.getFaultsReport().getFaults()) {
            String faultMessage = fault.getMessage();
            String faultLevel = fault.isError() ? FaultType.TYPE_ERROR.getName()
                    : fault.isWarning() ? FaultType.TYPE_WARNING.getName() : "Unknown";

            Error error = Error.createIngestionError(job.getId(), fileEntry.getFileName(),
                    BatchJobStageType.XML_FILE_PROCESSOR.getName(), null, null, null, faultLevel, faultLevel,
                    faultMessage);
            batchJobDao.saveError(error);

            if (fault.isError()) {
                errorCount++;
            }
        }
        return errorCount > 0;
    }
}

@@ -33,24 +33,47 @@
 l.idref;%0D%0A%0D%0A
+import java.util.Set;%0D%0A
 import java.
@@ -99,16 +99,16 @@
 lable;%0D%0A
-
 %0D%0Aimport
@@ -637,16 +637,17 @@
 e...%0D%0A *
+ 
 %0D%0A * @au
@@ -766,18 +766,22 @@
 ean%3E %7B%0D%0A
+    
 %0D%0A
-
     priv
@@ -863,26 +863,30 @@
 le.class);%0D%0A
+    
 %0D%0A
-
     private 
@@ -1042,18 +1042,82 @@
 obDao;%0D%0A
+    private Set%3CString%3E idReferenceResolutionInterchanges;%0D%0A    
 %0D%0A
-
     /**%0D
@@ -1185,32 +1185,33 @@
 allable.%0D%0A     *
+ 
 %0D%0A     * @param 
@@ -1577,16 +1577,63 @@
 chJobDao
+, Set%3CString%3E idReferenceResolutionInterchanges
 ) %7B%0D%0A   
@@ -1763,25 +1763,114 @@
 obDao;%0D%0A
+        this.idReferenceResolutionInterchanges = idReferenceResolutionInterchanges;%0D%0A
     %7D%0D%0A
+    
 %0D%0A
-
     /**%0D
@@ -1984,24 +1984,164 @@
 xception %7B%0D%0A
+        boolean hasErrors = false;%0D%0A        %0D%0A        if (idReferenceResolutionInterchanges.contains(entry.getFileType().getName())) %7B%0D%0A    
         LOG.
@@ -2207,26 +2207,28 @@
 leName());%0D%0A
-%0D%0A
+    
         reso
@@ -2268,24 +2268,36 @@
 Report());%0D%0A
+            
 %0D%0A        bo
@@ -2286,39 +2286,35 @@
       %0D%0A        
-boolean
+   
  hasErrors = agg
@@ -2347,26 +2347,28 @@
 rs(entry);%0D%0A
-%0D%0A
+    
         LOG.
@@ -2430,32 +2430,227 @@
 etFileName());%0D%0A
+        %7D else %7B%0D%0A            LOG.info(%22Not performing id reference resolution for interchange: %7B%7D (type: %7B%7D)%22, entry.getFileName(),%0D%0A                    entry.getFileType().name());%0D%0A        %7D%0D%0A
         return h
@@ -2667,18 +2667,22 @@
 %0A    %7D%0D%0A
+    
 %0D%0A
-
     /**%0D
@@ -2749,16 +2749,17 @@
 %0D%0A     *
+ 
 %0D%0A     *
@@ -3324,18 +3324,30 @@
 nown%22;%0D%0A
+            
 %0D%0A
-
         
@@ -3620,16 +3620,28 @@
 rror);%0D%0A
+            
 %0D%0A      

update id reference resolution callable to take set of interchange entity types as a constructor argument