package org.slc.sli.validation;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.avro.Schema;
import org.apache.avro.Schema.Parser;
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>();
    
    @PostConstruct
    public void init() throws IOException {
        
        File root = ResourceUtils.getFile("classpath:avroSchema");
        File[] schemaFiles = root.listFiles();
        
        for (File file : schemaFiles) {
            URL url = ResourceUtils.getURL("classpath:avroSchema/" + file.getName());
        Parser parser = new Schema.Parser();
        Schema schema = parser.parse(url.openStream());
            mapSchema.put(file.getName().split("_")[0], schema);
        }

    }
    
    @Override
    public Schema findSchemaForType(Entity entity) {
        return mapSchema.get(entity.getType());
    }
    
}

@@ -742,294 +742,246 @@
     
-   File root = ResourceUtils.getFile(%22classpath:avroSchema%22);%0A        File%5B%5D schemaFiles = root.listFiles();%0A        %0A        for (File file : schemaFiles) %7B%0A            URL url = ResourceUtils.getURL(%22classpath:avroSchema/%22 + file.getName());%0A        Parser parser = new Schema.Parser(
+// TODO%0A        // By convention, load all %3Centity%3E_body schema files.%0A        %0A        Parser parser = new Schema.Parser();%0A        %0A        // FilenameUtils.%0A        URL url = ResourceUtils.getURL(%22classpath:avroSchema/school_body.avpr%22
 );%0A 
@@ -1043,20 +1043,16 @@
         
-    
 mapSchem
@@ -1061,57 +1061,26 @@
 put(
-file.getName().split(%22_%22)%5B0%5D, schema);%0A        %7D%0A
+%22school%22, schema);
 %0A   

refactored avroentityschemaregistry