PageRenderTime 14ms CodeModel.GetById 2ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 1ms

/Prototipo/PrototipoMemoria/src/java/memoria/dataAccess/RepositoriesManager.java

http://prototipomemoria.googlecode.com/
Java | 128 lines | 83 code | 23 blank | 22 comment | 9 complexity | 45aef8346ae9d1d41480668833d92a7d MD5 | raw file
  1/*
  2 * To change this template, choose Tools | Templates
  3 * and open the template in the editor.
  4 */
  5
  6package memoria.dataAccess;
  7
  8import java.io.BufferedReader;
  9import java.io.DataInputStream;
 10import java.io.File;
 11import java.io.FileInputStream;
 12import java.io.InputStreamReader;
 13import memoria.dataAccess.dao.ExcelDao;
 14import memoria.commons.dataAccess.query.QueryParams;
 15import java.util.ArrayList;
 16import java.util.Date;
 17import java.util.HashMap;
 18import java.util.List;
 19import memoria.commons.dataAccess.RepoConfig;
 20import memoria.commons.dataAccess.RepositoriesMetadata;
 21import memoria.commons.dataAccess.query.VisualQuery;
 22import memoria.commons.entities.Escuela;
 23import memoria.commons.structures.GeoReferenced;
 24import memoria.commons.xml.XStreamInstance;
 25import memoria.dataAccess.dao.CsvDao;
 26import memoria.dataAccess.dao.ShapeFileDao;
 27
 28/**
 29 *
 30 * @author diego
 31 */
 32public class RepositoriesManager {
 33
 34    //Patron Singleton
 35    private static RepositoriesManager instance;
 36
 37    public static RepositoriesManager getInstance() {
 38        if (instance == null) {
 39            instance = new RepositoriesManager();
 40        }
 41        return instance;
 42    }
 43
 44    private List<IRepositoryDao> repositories = new ArrayList<IRepositoryDao>();
 45    private HashMap<String,List<IRepositoryDao>> entitiesLocationMap = new HashMap<String, List<IRepositoryDao>>();
 46
 47    public RepositoriesManager() {
 48          loadRepoConfig();
 49//        entitiesLocationMap = new HashMap<String,List<IRepositoryDao>>();
 50//        List<IRepositoryDao> repositoriosDeEscuela = new ArrayList<IRepositoryDao>();
 51//        repositoriosDeEscuela.add(ExcelDao.getInstance());
 52//        repositoriosDeEscuela.add(CsvDao.getInstance());
 53//        repositoriosDeEscuela.add(ShapeFileDao.getInstance());
 54//        entitiesLocationMap.put(Escuela.class.getSimpleName(),repositoriosDeEscuela);
 55    }
 56
 57    public List<GeoReferenced> getData(VisualQuery params){
 58        //Voy a buscar en que repositorios encuentro la data
 59
 60        long startMilis = new Date().getTime();
 61
 62        String capa = params.getCapa();
 63        List<IRepositoryDao> daos = entitiesLocationMap.get(capa);
 64
 65        List<GeoReferenced> results = new ArrayList<GeoReferenced>();
 66        for (IRepositoryDao iRepositoryDao : daos) {
 67            results.addAll(iRepositoryDao.getData(params));
 68        }
 69
 70        long endMilis = new Date().getTime();
 71        System.out.println("Tiempo de ejecuci?n en transformador: "+(endMilis - startMilis));
 72        return results;
 73    }
 74
 75    public List<IRepositoryDao> getRepositories() {
 76        return repositories;
 77    }
 78
 79    public void setRepositories(List<IRepositoryDao> repositories) {
 80        this.repositories = repositories;
 81    }
 82
 83    private void loadRepoConfig(){
 84
 85        try{
 86          // Open the file that is the first
 87          // command line parameter
 88          File file = new File(this.getClass().getResource("/memoria/resources/repoConfig.xml").getPath());
 89          FileInputStream fstream = new FileInputStream(file);
 90          // Get the object of DataInputStream
 91          DataInputStream in = new DataInputStream(fstream);
 92          BufferedReader br = new BufferedReader(new InputStreamReader(in));
 93          String strLine, xmlLine = "";
 94          //Read File Line By Line
 95          while ((strLine = br.readLine()) != null)   {
 96          // Print the content on the console
 97            xmlLine += strLine.trim();
 98          }
 99          //Close the input stream
100          in.close();
101          List<RepositoriesMetadata> metadata = (List<RepositoriesMetadata>)XStreamInstance.toObject(xmlLine);
102
103            for (RepositoriesMetadata repositoriesMetadata : metadata) {
104                String entity = repositoriesMetadata.getEntity();
105                List<IRepositoryDao> repositoriosDeEntity = new ArrayList<IRepositoryDao>();
106                for (RepoConfig config: repositoriesMetadata.getRepositories()){
107                    Class daoClass = Class.forName(config.getRepoDaoClass());
108                    IRepositoryDao dao = (IRepositoryDao) daoClass.newInstance();
109                    dao.setInitParams(config.getAttributes());
110                    this.repositories.add(dao);
111                    repositoriosDeEntity.add(dao);
112                }
113                
114                List<IRepositoryDao> repos = this.entitiesLocationMap.get(entity);
115                if (repos != null)
116                    repositoriosDeEntity.addAll(repos);
117                this.entitiesLocationMap.put(entity, repositoriosDeEntity);
118            }
119          
120        }catch (Exception e){//Catch exception if any
121            System.err.println("Error: " + e.getMessage());
122        }
123
124
125
126    }
127
128}