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";
    
    @Autowired(required = false)
    private MongoTemplate mongoTemplate;
    
    @Override
    public void afterPropertiesSet() throws Exception {
        if (this.mongoTemplate != null) {
            
            // .on("metaData.idNamespace", ASCENDING)
            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.idNamespace", 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");
            }
        }
    }
}

@@ -946,21 +946,121 @@
 arent%22;%0A
+    private static final String STUDENT_PARENT_ASSOCIATION_COLLECTION = %22studentParentAssociation%22;%0A
     %0A
-
     @Aut
@@ -1493,33 +1493,8 @@
 ING)
-%0A                        
 .on(
@@ -1513,32 +1513,57 @@
 tId%22, ASCENDING)
+%0A                        
 .on(%22body.attend
@@ -2368,32 +2368,32 @@
 (Exception e) %7B%0A
-
                 
@@ -2466,32 +2466,696 @@
 ;%0A            %7D%0A
+            %0A            try %7B%0A                LOG.debug(%22Creating compound index on student-parent association collection%22);%0A                IndexDefinition index = new Index().on(%22body.studentId%22, ASCENDING).on(%22body.parentId%22, ASCENDING)%0A                        .on(%22metaData.idNamespace%22, ASCENDING).unique();%0A                mongoTemplate.ensureIndex(index, STUDENT_PARENT_ASSOCIATION_COLLECTION);%0A                LOG.debug(%22Successfully created compound index on student-parent association collection%22);%0A            %7D catch (Exception e) %7B%0A                LOG.warn(%22There was an error creating index on student-parent association collection%22);%0A            %7D%0A
         %7D%0A    %7D%0A

add compound index on student parent association collection