Statistics
| Revision:

root / org.gvsig.projection.jcrs / trunk / org.gvsig.projection.jcrs / org.gvsig.projection.jcrs.lib / src / main / java / es / idr / teledeteccion / connection / EpsgConnection.java @ 238

History | View | Annotate | Download (5.65 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 Instituto de Desarrollo Regional and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 */
20
package es.idr.teledeteccion.connection;
21

    
22
import java.sql.Connection;
23
import java.sql.SQLException;
24
import java.sql.Statement;
25

    
26
import org.gvsig.crs.CrsFactory;
27
import org.hsqldb.jdbc.jdbcDataSource;
28
import org.slf4j.Logger;
29
import org.slf4j.LoggerFactory;
30

    
31
import es.idr.teledeteccion.connection.epsg.HSQLDataSource;
32

    
33
/**
34
 * Clase para la conexi?n con la base de datos de hsqldb.
35
 * Establece el driver necesario, as? como la cadena de
36
 * conexi?n a la base de datos de la EPSG y la IAU2000
37
 *
38
 * @author Jos? Luis G?mez Mart?nez (jolugomar@gmail.com)
39
 *
40
 */
41
public class EpsgConnection extends jdbcDataSource {
42

    
43
    private static final Logger logger = LoggerFactory.getLogger(EpsgConnection.class);
44
    
45
    Connection connect = null;
46
    String connectionType = null;
47

    
48
    public EpsgConnection() {
49
        /*        try {
50
         Class.forName("org.hsqldb.jdbcDriver");
51
         } catch (ClassNotFoundException e) {
52
         logger.error("Can't create EpsgConnection",e); 
53
         }*/
54
    }
55

    
56
    private void setConnection(Connection connection, String type) {
57
        this.connect = connection;
58
        this.connectionType = type;
59
        logger.debug("Created connection to '"+connectionType+"'.");
60
    }
61

    
62
    public void setConnectionEPSG() {
63
        HSQLDataSource ds = new HSQLDataSource();
64

    
65
        try {
66
            setConnection(ds.getConnection(), "EPSG");
67
        } catch (SQLException e) {
68
            logger.warn("Cant get conection from HSQLDataSource.", e);
69
        }
70
    }
71

    
72
    /**
73
     * Establece la conexi?n con la base de datos de la IAU2000
74
     *
75
     */
76
    public void setConnectionIAU2000() {
77
        String db = "jdbc:hsqldb:file:" + CrsFactory.getDataBaseFolder().getAbsolutePath() + "/iau2000";
78
        setDatabase(db);
79
        setUser("sa");
80
        try {
81
            setConnection(super.getConnection(),"IAU2000");
82
        } catch (SQLException e) {
83
            logger.warn("Can't get conection from database '" + db + "'.", e);
84
        }
85
    }
86

    
87
    /**
88
     * Establece la conexi?n con la base de datos de ESRI
89
     *
90
     */
91
    public void setConnectionEsri() {
92
        String db = "jdbc:hsqldb:file:" + CrsFactory.getDataBaseFolder().getAbsolutePath() + "/esri";
93
        setDatabase(db);
94
        setUser("sa");
95
        try {
96
            setConnection(super.getConnection(),"Esri");
97
        } catch (SQLException e) {
98
            logger.warn("Can't get conection from database '" + db + "'.", e);
99
        }
100
    }
101

    
102
    /**
103
     * Sets connection for CRS:* coordinate reference systems
104
     * such as CRS:84
105
     *
106
     */
107
    public void setConnectionNoAuth() {
108
        String db = "jdbc:hsqldb:file:" + CrsFactory.getDataBaseFolder().getAbsolutePath() + "/noauth";
109
        setDatabase(db);
110
        setUser("sa");
111
        try {
112
            setConnection(super.getConnection(),"NoAuth");
113
        } catch (SQLException e) {
114
            logger.warn("Can't get conection from database '" + db + "'.", e);
115
        }
116
    }
117

    
118
    /**
119
     * Establece la conexi?n con la base de datos de USR
120
     *
121
     */
122
    public void setConnectionUsr() {
123
        String db = "jdbc:hsqldb:file:" + CrsFactory.getDataBaseFolder().getAbsolutePath() + "/usr";
124
        setDatabase(db);
125
        setUser("sa");
126
        try {
127
            setConnection(super.getConnection(),"Usr");
128
        } catch (SQLException e) {
129
            logger.warn("Can't get conection from database '" + db + "'.", e);
130
        }
131
    }
132

    
133
    public Connection getConnection() {
134
        return connect;
135
    }
136

    
137
    public void close() throws SQLException {
138

    
139
        try {
140
            connect.close();    // if there are no other open connection
141
            logger.debug("Shutdown connection to '" + connectionType + "' database.");
142
        } catch (Exception ex) {
143
            logger.warn("Can't shutdown the conexion.",ex);
144
        }
145
    }
146

    
147
    public void shutdown() throws SQLException {
148

    
149
        // db writes out to files and performs clean shuts down
150
        // otherwise there will be an unclean shutdown
151
        // when program ends
152
        try {
153
            Statement st = connect.createStatement();
154
            st.execute("SHUTDOWN");
155
            connect.close();    // if there are no other open connection
156
            logger.debug("Shutdown connection to '" + connectionType + "' database.");
157
        } catch (Exception ex) {
158
            logger.warn("Can't shutdown the conexion.",ex);
159
        }
160
    }
161

    
162
    public synchronized void update(String expression) throws SQLException {
163

    
164
        Statement st = null;
165
        st = connect.createStatement();
166
        int i = st.executeUpdate(expression);
167
        if ( i == -1 ) {
168
            logger.warn("db("+connectionType+")  error in statement: " + expression);
169
        }
170
        st.close();
171
    }
172
}