PageRenderTime 64ms CodeModel.GetById 53ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

/Prototipo/PrototipoMemoria/src/java/memoria/dataAccess/dao/CsvDao.java

http://prototipomemoria.googlecode.com/
Java | 77 lines | 53 code | 15 blank | 9 comment | 5 complexity | c520380345b4ffb0c510549d18fc8cc8 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.dao;
 7
 8import com.csvreader.CsvReader;
 9import java.io.FileNotFoundException;
10import java.io.IOException;
11import java.util.ArrayList;
12import java.util.List;
13import java.util.Map;
14import memoria.commons.dataAccess.query.VisualQuery;
15import memoria.commons.entities.EntidadPunto;
16import memoria.commons.entities.Escuela;
17import memoria.commons.structures.GeoReferenced;
18import memoria.commons.structures.Point;
19import memoria.commons.structures.coordinates.LatLonCoordinate;
20import memoria.dataAccess.IRepositoryDao;
21
22/**
23 *
24 * @author diego
25 */
26public class CsvDao implements IRepositoryDao{
27
28     //Patron Singleton
29    private static CsvDao instance;
30    public static IRepositoryDao getInstance(){
31        if (instance == null)
32            instance = new CsvDao();
33        return instance;
34    }
35
36    private String fileUrl;
37
38    public List<GeoReferenced> getData(VisualQuery params) {
39         List<GeoReferenced> results = new ArrayList<GeoReferenced>();
40        try {
41
42                CsvReader educacion = new CsvReader(fileUrl);
43                educacion.readHeaders();
44                while (educacion.readRecord())
45                {
46                    String id = educacion.get("id");
47                    Double longitud = Double.parseDouble(educacion.get("longitud"));
48                    Double latitud = Double.parseDouble(educacion.get("latitud"));
49                    String nombre = educacion.get("nombre");
50                    String descripcion = educacion.get("descripcion");
51
52                    LatLonCoordinate coordenadas = new LatLonCoordinate(latitud, longitud);
53                    Point pi = new Point(Long.parseLong(id), coordenadas);
54                    Escuela entidad = new Escuela(pi,nombre, descripcion);
55                    results.add(entidad);
56
57                }
58
59                educacion.close();
60
61        } catch (FileNotFoundException e) {
62                e.printStackTrace();
63        } catch (IOException e) {
64                e.printStackTrace();
65        }
66
67
68        if ( params.getFiltro() != null)
69            return params.getFiltro().filter(results);
70        return results;
71    }
72
73    public void setInitParams(Map<String, String> params) {
74        this.fileUrl = params.get("URL");
75    }
76
77}