package org.slc.sli.ingestion.transformation;

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

import org.slc.sli.common.constants.EntityNames;
import org.slc.sli.ingestion.NeutralRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * Transformer for StudentTranscriptAssociation Entities
 * 
 * @author jcole
 * @author shalka
 */
@Scope("prototype")
@Component("studentTranscriptAssociationTransformationStrategy")
public class StudentTranscriptAssociationCombiner extends AbstractTransformationStrategy {
    
    private static final Logger LOG = LoggerFactory.getLogger(StudentTranscriptAssociationCombiner.class);
    
    private Map<Object, NeutralRecord> studentTranscripts;
    
    /**
     * Default constructor.
     */
    public StudentTranscriptAssociationCombiner() {
        this.studentTranscripts = new HashMap<Object, NeutralRecord>();
    }
    
    /**
     * The chaining of transformation steps. This implementation assumes that all data will be
     * processed in "one-go"
     */
    @Override
    public void performTransformation() {
        loadData();
        transform();
    }
    
    /**
     * Pre-requisite interchanges for student transcript data to be successfully transformed:
     * student
     */
    public void loadData() {
        LOG.info("Loading data for studentTranscriptAssociation transformation.");
        this.studentTranscripts = getCollectionFromDb(EntityNames.STUDENT_TRANSCRIPT_ASSOCIATION);
        LOG.info("{} is loaded into local storage.  Total Count = {}", EntityNames.STUDENT_TRANSCRIPT_ASSOCIATION,
                studentTranscripts.size());
    }
    
    /**
     * Transforms student transcript association data to pass SLI data validation and writes into
     * staging mongo db.
     */
    public void transform() {
        LOG.info("Transforming and persisting student transcript association data");
        for (Map.Entry<Object, NeutralRecord> neutralRecordEntry : studentTranscripts.entrySet()) {
            NeutralRecord neutralRecord = neutralRecordEntry.getValue();
            Map<String, Object> attributes = neutralRecord.getAttributes();
            if (attributes.get("creditsAttempted") == null) {
                attributes.remove("creditsAttempted");
            }
            neutralRecord.setRecordType(neutralRecord.getRecordType() + "_transformed");
            getNeutralRecordMongoAccess().getRecordRepository().createForJob(neutralRecord, getJob().getId());
        }
        LOG.info("Finished transforming and persisting student transcript association data");
    }
}

@@ -2362,32 +2362,168 @@
 ;%0A            %7D%0A
+            %0A            if (attributes.get(%22gradeType%22) == null) %7B%0A                attributes.put(%22gradeType%22, %22Final%22);%0A            %7D%0A
             neut

add gradeType onto student transcript associations if null