PageRenderTime 59ms CodeModel.GetById 44ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://prototipomemoria.googlecode.com/
Java | 99 lines | 75 code | 12 blank | 12 comment | 5 complexity | 667e86c6c108ff53989c6b4fa44ef288 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 java.io.IOException;
 9import java.util.List;
10import java.io.File;
11import java.util.ArrayList;
12import java.util.Date;
13import java.util.Map;
14import java.util.logging.Level;
15import java.util.logging.Logger;
16import jxl.*;
17import jxl.read.biff.BiffException;
18import memoria.commons.dataAccess.query.QueryParams;
19import memoria.commons.dataAccess.query.VisualQuery;
20import memoria.commons.entities.EntidadPunto;
21import memoria.commons.entities.Escuela;
22import memoria.commons.structures.AbstractGeographicElement;
23import memoria.commons.structures.coordinates.Coordinate;
24import memoria.commons.structures.GeoReferenced;
25import memoria.commons.structures.Point;
26import memoria.commons.structures.coordinates.LatLonCoordinate;
27import memoria.dataAccess.IRepositoryDao;
28import memoria.dataAccess.SpatialDTO;
29import memoria.utils.coordinate.*;
30
31/**
32 *
33 * @author diego
34 */
35public class ExcelDao implements IRepositoryDao {
36
37    //Patron Singleton
38    private static ExcelDao instance;
39    public static IRepositoryDao getInstance(){
40        if (instance == null)
41            instance = new ExcelDao();
42        return instance;
43    }
44
45    private String fileUrl;
46
47    public List<GeoReferenced> getData(VisualQuery params) {
48        try {
49            Workbook workbook = Workbook.getWorkbook(new File(fileUrl));
50            Sheet sheet = workbook.getSheet(0);
51            List<GeoReferenced> results = new ArrayList<GeoReferenced>();
52
53            for(int i = 1; i <sheet.getRows(); i++){
54
55                EntidadPunto entidadPunto = null;
56                try {
57                    Cell id = sheet.getCell(0, i);
58                    Cell lat = sheet.getCell(1, i);
59                    Cell lon = sheet.getCell(2, i);
60                    String nombre = "";
61                    try {
62                        nombre = sheet.getCell(3, i).getContents();
63                    } catch (Exception e) {
64                         //Logger.getLogger(ExcelDao.class.getName()).log(Level.SEVERE, "Error al leer nombre", e);
65                    }
66                    String descripcion = "";
67                    try {
68                        descripcion = sheet.getCell(4, i).getContents();
69                    } catch (Exception e) {
70                         //Logger.getLogger(ExcelDao.class.getName()).log(Level.SEVERE, "Error al leer descripcion", e);
71                    }
72                    double[] latlong = CoordinateConversion.utm2LatLon("21 A " + lat.getContents() + " " + lon.getContents());
73                    LatLonCoordinate coordenadas = new LatLonCoordinate(latlong[0], latlong[1]);
74                    //System.out.println(latlong[0]+";"+latlong[1]);
75                    Point pi = new Point(Long.parseLong(id.getContents()), coordenadas);
76                    entidadPunto = new EntidadPunto( nombre,  descripcion,pi);
77                    results.add(entidadPunto);
78                } catch (Exception e) {
79                   Logger.getLogger(ExcelDao.class.getName()).log(Level.SEVERE, "Error al leer entidad", e);
80                }
81                
82            }
83            if ( params.getFiltro() != null)
84                return params.getFiltro().filter(results);
85            return results;
86        } catch (IOException ex) {
87            Logger.getLogger(ExcelDao.class.getName()).log(Level.SEVERE, null, ex);
88        } catch (BiffException ex) {
89            Logger.getLogger(ExcelDao.class.getName()).log(Level.SEVERE, null, ex);
90        }
91        return null;
92    }
93
94    public void setInitParams(Map<String, String> params) {
95        this.fileUrl = params.get("URL");
96    }
97
98    
99}