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";
    
    @Autowired(required = false)
    private MongoTemplate mongoTemplate;
    
    @Override
    public void afterPropertiesSet() throws Exception {
        if (this.mongoTemplate != null) {
            
            LOG.debug("Creating compound index on attendance collection");
            IndexDefinition indexDefinition = new Index().on("body.eventDate", ASCENDING)
                    .on("body.studentId", ASCENDING).on("body.attendanceEventCategory", ASCENDING).unique();
            try {
                mongoTemplate.ensureIndex(indexDefinition, ATTENDANCE_EVENT_COLLECTION);
                LOG.debug("Successfully created compound index on attendance collection");
            } catch (Exception e) {
                LOG.warn("There was an error creating index");
            }
        }
    }
}

@@ -884,16 +884,78 @@
 dance%22;%0A
+    private static final String PARENT_COLLECTION = %22parent%22;%0A
     %0A   
@@ -1151,24 +1151,100 @@
            %0A
+            // .on(%22metaData.idNamespace%22, ASCENDING)%0A            try %7B%0A    
             
@@ -1318,16 +1318,20 @@
         
+    
 IndexDef
@@ -1343,26 +1343,16 @@
 on index
-Definition
  = new I
@@ -1382,32 +1382,36 @@
 te%22, ASCENDING)%0A
+    
                 
@@ -1519,86 +1519,629 @@
     
-try %7B%0A                mongoTemplate.ensureIndex(indexDefinition, ATTENDANCE_EV
+    mongoTemplate.ensureIndex(index, ATTENDANCE_EVENT_COLLECTION);%0A                LOG.debug(%22Successfully created compound index on attendance collection%22);%0A            %7D catch (Exception e) %7B%0A                LOG.warn(%22There was an error creating index on attendance collection%22);%0A            %7D%0A            %0A            try %7B%0A                LOG.debug(%22Creating compound index on parent collection%22);%0A                IndexDefinition index = new Index().on(%22body.parentUniqueStateId%22, ASCENDING)%0A                        .on(%22metaData.idNamespace%22, ASCENDING).unique();%0A                mongoTemplate.ensureIndex(index, PAR
 ENT_
@@ -2215,34 +2215,30 @@
 nd index on 
-attendance
+parent
  collection%22
@@ -2335,16 +2335,37 @@
 ng index
+ on parent collection
 %22);%0A    

add uniqueness constraint on parent collection