package org.slc.sli.dal.init;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.index.Index;
import org.springframework.data.mongodb.core.index.IndexDefinition;
import org.springframework.data.mongodb.core.query.Order;
import org.springframework.stereotype.Component;

/**
 * Creates compound indices on mongo database collections.
 * 
 * @author shalka
 * 
 */
@Component
public class MongoIndexLoader implements InitializingBean {
    private static final Logger LOG = LoggerFactory.getLogger(MongoIndexLoader.class);
    private static final Order ASCENDING = Order.ASCENDING;
    private static final String ATTENDANCE_EVENT_COLLECTION = "attendance";
    private static final String PARENT_COLLECTION = "parent";
    private static final String STUDENT_PARENT_ASSOCIATION_COLLECTION = "studentParentAssociation";
    
    @Autowired(required = false)
    private MongoTemplate mongoTemplate;
    
    @Override
    public void afterPropertiesSet() throws Exception {
        if (this.mongoTemplate != null) {
            
            try {
                LOG.debug("Creating compound index on attendance collection");
                IndexDefinition index = new Index().on("body.eventDate", ASCENDING).on("body.studentId", ASCENDING)
                        .on("body.attendanceEventCategory", ASCENDING).unique();
                mongoTemplate.ensureIndex(index, ATTENDANCE_EVENT_COLLECTION);
                LOG.debug("Successfully created compound index on attendance collection");
            } catch (Exception e) {
                LOG.warn("There was an error creating index on attendance collection");
            }
            
            try {
                LOG.debug("Creating compound index on parent collection");
                IndexDefinition index = new Index().on("body.parentUniqueStateId", ASCENDING)
                        .on("metaData.tenantId", ASCENDING).unique();
                mongoTemplate.ensureIndex(index, PARENT_COLLECTION);
                LOG.debug("Successfully created compound index on parent collection");
            } catch (Exception e) {
                LOG.warn("There was an error creating index on parent collection");
            }
            
            try {
                LOG.debug("Creating compound index on student-parent association collection");
                IndexDefinition index = new Index().on("body.studentId", ASCENDING).on("body.parentId", ASCENDING)
                        .on("metaData.tenantId", ASCENDING).unique();
                mongoTemplate.ensureIndex(index, STUDENT_PARENT_ASSOCIATION_COLLECTION);
                LOG.debug("Successfully created compound index on student-parent association collection");
            } catch (Exception e) {
                LOG.warn("There was an error creating index on student-parent association collection");
            }
        }
    }
}

@@ -24,16 +24,65 @@
 .init;%0A%0A
+import org.slc.sli.common.constants.EntityNames;%0A
 import o
@@ -94,24 +94,24 @@
 f4j.Logger;%0A
-
 import org.s
@@ -861,246 +861,8 @@
 NG;%0A
-    private static final String ATTENDANCE_EVENT_COLLECTION = %22attendance%22;%0A    private static final String PARENT_COLLECTION = %22parent%22;%0A    private static final String STUDENT_PARENT_ASSOCIATION_COLLECTION = %22studentParentAssociation%22;%0A
     
@@ -1053,29 +1053,16 @@
 null) %7B%0A
-            %0A
         
@@ -1215,40 +1215,8 @@
 ody.
-eventDate%22, ASCENDING).on(%22body.
 stud
@@ -1272,31 +1272,76 @@
 ody.
-at
+schoolId%22, ASCENDING)%0A                        .on(%22metaData.
 ten
-d
 an
-ceEventCategory
+tId
 %22, A
@@ -1413,35 +1413,30 @@
 ex, 
-ATTENDANCE_EVENT_COLLECTION
+EntityNames.ATTENDANCE
 );%0A 
@@ -1573,36 +1573,37 @@
             LOG.
-warn
+error
 (%22There was an e
@@ -1987,25 +1987,26 @@
 ex, 
-PARENT_COLLECTION
+EntityNames.PARENT
 );%0A 
@@ -2139,36 +2139,37 @@
             LOG.
-warn
+error
 (%22There was an e
@@ -2586,16 +2586,28 @@
 (index, 
+EntityNames.
 STUDENT_
@@ -2628,19 +2628,8 @@
 TION
-_COLLECTION
 );%0A 
@@ -2794,12 +2794,13 @@
 LOG.
-warn
+error
 (%22Th

update compound index on attendance collection to reflect attendance transformation strategy