package org.slc.sli.ingestion.transformation;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.slc.sli.ingestion.NeutralRecord;
import org.slc.sli.ingestion.dal.NeutralRecordMongoAccess;
import org.slc.sli.ingestion.validation.ErrorReport;

/**
 * Transformer for Assessment Entities
 * 
 * @author ifaybyshev
 *
 */
public class AssessmentCombiner extends AbstractCombiner<NeutralRecordMongoAccess, String> {

    private static final Logger LOG = LoggerFactory.getLogger(AssessmentCombiner.class);
    
    private Map<String, HashMap<Object, NeutralRecord>> collections = new HashMap<String, HashMap<Object, NeutralRecord>>();
    
    private NeutralRecordMongoAccess neutralRecordMongoAccess;

    private String jobId;
    
    
    public AssessmentCombiner(NeutralRecordMongoAccess neutralRecordMongoAccess) {
        this.neutralRecordMongoAccess = neutralRecordMongoAccess;
    }

    @Override
    public void transform() {
        LOG.debug("Transforming data.");
        
    }

    @Override
    public void loadData() {
        LOG.debug("Loading data for transformation.");
        
        this.addCollection("teacher");
        this.addCollection("student");
        this.addCollection("section");
        
        LOG.debug("Teacher is loaded into local storage.  Total Count = " + this.collections.get("teacher").size());
        
    }
    

    @Override
    public String persist() {
        LOG.debug("Persisting transformed data to storage.");
        
        return "OK";
    }
    
    
    /**
     * Stores all items in collection found in database to local storage (HashMap)
     * 
     * @param collectionName
     */
    private void addCollection(String collectionName) {
        Iterable<NeutralRecord> data = neutralRecordMongoAccess.getRecordRepository().findAll(collectionName);
        Iterator<NeutralRecord> iter = data.iterator();
        
        HashMap<Object, NeutralRecord> collection = new HashMap<Object, NeutralRecord>();
        NeutralRecord tempNr;
        
        while (iter.hasNext()) {
            tempNr = iter.next();
            collection.put(tempNr.getLocalId(), tempNr);
        }
        
        this.collections.put(collectionName, collection);
    }
    
    /* 
     * Transforms items inside staging database 
     */
    @Override
    String doHandling(NeutralRecordMongoAccess item, ErrorReport errorReport) {
        LOG.info("Starting Transforming Assessments: Combining");
        
        String status = "FAIL";
        
        try {
            
            //Load data into local storage (memory)
            this.loadData();
            
            //perform transformations of the data in local storage
            this.transform();
            
            //persist transformed data to storage (i.e. db)
            status = this.persist();
            
        } catch (Exception e) {
            LOG.error("IOException", e);
            
            errorReport.fatal("Could not transform data.", AssessmentCombiner.class);
        }

        return status;
    }

    @Override
    public void setJobId(String id) {
        this.jobId = id;
        
    }

    public void setNeutralRecordMongoAccess(NeutralRecordMongoAccess neutralRecordMongoAccess) {
        this.neutralRecordMongoAccess = neutralRecordMongoAccess;
    }
    
}

@@ -376,17 +376,16 @@
 ities%0A *
- 
 %0A * @aut
@@ -589,20 +589,16 @@
 class);%0A
-    
 %0A    pri
@@ -715,20 +715,16 @@
 rd%3E%3E();%0A
-    
 %0A    pri
@@ -806,25 +806,16 @@
  jobId;%0A
-    %0A    
 %0A    pub
@@ -1044,32 +1044,24 @@
 ng data.%22);%0A
-        
 %0A    %7D%0A%0A    
@@ -1150,32 +1150,24 @@
 rmation.%22);%0A
-        
 %0A        thi
@@ -1268,32 +1268,24 @@
 %22section%22);%0A
-        
 %0A        LOG
@@ -1394,27 +1394,14 @@
 ));%0A
-        
 %0A    %7D
-%0A    
 %0A%0A  
@@ -1500,32 +1500,33 @@
 storage.%22);%0A
+%0A
         
 %0A        ret
@@ -1505,32 +1505,526 @@
 ge.%22);%0A%0A        
+for (Map.Entry%3CString, HashMap%3CObject, NeutralRecord%3E%3E collectionEntry : collections.entrySet()) %7B%0A%0A            for (Map.Entry%3CObject, NeutralRecord%3E neutralRecordEntry : collectionEntry.getValue().entrySet()) %7B%0A%0A                NeutralRecord neutralRecord = neutralRecordEntry.getValue();%0A                neutralRecord.setRecordType(neutralRecord.getRecordType() + %22_transformed%22);%0A%0A                neutralRecordMongoAccess.getRecordRepository().create(neutralRecord);%0A            %7D%0A        %7D%0A
 %0A        return 
@@ -2035,25 +2035,16 @@
 ;%0A    %7D%0A
-    %0A    
 %0A    /**
@@ -2133,17 +2133,16 @@
 )%0A     *
- 
 %0A     * 
@@ -2394,24 +2394,16 @@
 ator();%0A
-        
 %0A       
@@ -2515,24 +2515,16 @@
 tempNr;%0A
-        
 %0A       
@@ -2650,24 +2650,16 @@
       %7D%0A
-        
 %0A       
@@ -2719,20 +2719,15 @@
   %7D%0A
-    
 %0A    /*
- 
 %0A   
@@ -2770,17 +2770,16 @@
 database
- 
 %0A     */
@@ -2939,24 +2939,16 @@
 ning%22);%0A
-        
 %0A       
@@ -2972,24 +2972,16 @@
 %22FAIL%22;%0A
-        
 %0A       
@@ -2987,28 +2987,16 @@
   try %7B%0A
-            
 %0A       
@@ -3002,16 +3002,17 @@
       //
+ 
 Load dat
@@ -3066,36 +3066,24 @@
 loadData();%0A
-            
 %0A           
@@ -3085,16 +3085,17 @@
       //
+ 
 perform 
@@ -3165,36 +3165,24 @@
 ransform();%0A
-            
 %0A           
@@ -3184,16 +3184,17 @@
       //
+ 
 persist 
@@ -3268,28 +3268,16 @@
 sist();%0A
-            
 %0A       
@@ -3342,28 +3342,16 @@
 n%22, e);%0A
-            
 %0A       
@@ -3547,24 +3547,16 @@
 d = id;%0A
-        
 %0A    %7D%0A%0A
@@ -3724,15 +3724,11 @@
 ;%0A    %7D%0A
-    
 %0A%7D%0A

implement persist step of combiner