package org.slc.sli.validation;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.avro.Schema;
import org.apache.avro.Schema.Parser;
import org.apache.commons.io.FileUtils;
import org.slc.sli.domain.Entity;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;

/**
 * Provides a registry for retrieving Avro schema
 * 
 * @author Sean Melody <smelody@wgen.net>
 * 
 */
@Component
public class AvroEntitySchemaRegistry implements EntitySchemaRegistry {
    
    private Map<String, Schema> mapSchema = new HashMap<String, Schema>();
    private final String baseDir = "classpath:avroSchema";
    
    @PostConstruct
    public void init() {
        try {
        File schemaDir = ResourceUtils.getFile(baseDir);
        
        Iterator<File> it = FileUtils.iterateFiles(schemaDir, new String[] { "avpr" }, false);
        while (it.hasNext()) {
            registerSchema(it.next());
        }
        } catch (IOException e) {
        }
    }
    
    @Override
    public Schema findSchemaForType(Entity entity) {
        return mapSchema.get(entity.getType());
    }
    
    private void registerSchema(File file) throws IOException {
        Parser parser = new Schema.Parser();
        Schema schema = parser.parse(FileUtils.openInputStream(file));
        mapSchema.put(file.getName().split("_")[0], schema);
        
    }
    
}

@@ -334,16 +334,73 @@
 Entity;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import o
@@ -675,24 +675,119 @@
 aRegistry %7B%0A
+    private static final Logger LOG = LoggerFactory.getLogger(AvroEntitySchemaRegistry.class);%0A
     %0A    pri
@@ -984,16 +984,20 @@
         
+    
 File sch
@@ -1041,25 +1041,33 @@
 r);%0A        
-%0A
+    %0A    
         Iter
@@ -1157,16 +1157,20 @@
         
+ 
+   
 while (i
@@ -1196,16 +1196,20 @@
         
+   
+ 
 register
@@ -1223,24 +1223,28 @@
 it.next());%0A
+    
         %7D%0A  
@@ -1271,24 +1271,100 @@
 eption e) %7B%0A
+            LOG.debug(%22can not load the avro schema files at %7B%7D%22, baseDir);%0A
         %7D%0A  
@@ -1743,24 +1743,96 @@
 a);%0A        
+LOG.info(%22added the avro schema file %7B%7D into registry%22, file.getName());
 %0A    %7D%0A    %0A

add logger to avroentityschemaregistry