svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.db / org.gvsig.fmap.dal.db.jdbc / src / main / java / org / gvsig / fmap / dal / store / jdbc2 / spi / JDBCSRSsBase.java @ 43355
History | View | Annotate | Download (3.68 KB)
1 |
|
---|---|
2 |
package org.gvsig.fmap.dal.store.jdbc2.spi; |
3 |
|
4 |
import java.sql.ResultSet; |
5 |
import java.sql.Statement; |
6 |
import java.util.HashMap; |
7 |
import java.util.Map; |
8 |
import org.apache.commons.lang3.StringUtils; |
9 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
10 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
11 |
|
12 |
public class JDBCSRSsBase implements JDBCSRSs { |
13 |
|
14 |
protected Map<String,String> database2applicationAbbrev; |
15 |
protected Map<String,String> applicationAbbrev2database; |
16 |
protected JDBCHelper helper;
|
17 |
|
18 |
public JDBCSRSsBase(JDBCHelper helper) {
|
19 |
this.helper = helper;
|
20 |
this.applicationAbbrev2database = new HashMap<>(); |
21 |
this.database2applicationAbbrev = new HashMap<>(); |
22 |
} |
23 |
|
24 |
public void add(String databaseCode, String applicationAbbrev) { |
25 |
this.applicationAbbrev2database.put(applicationAbbrev, databaseCode);
|
26 |
this.database2applicationAbbrev.put(databaseCode, applicationAbbrev);
|
27 |
} |
28 |
|
29 |
public String getDatabaseCode(String applicationAbbrev) { |
30 |
if( this.hasApplicationAbbrev(applicationAbbrev) ) { |
31 |
return this.applicationAbbrev2database.get(applicationAbbrev); |
32 |
} |
33 |
String databaseCode = this.searchDatabaseCode(applicationAbbrev); |
34 |
if( StringUtils.isEmpty(databaseCode) ) {
|
35 |
return null; |
36 |
} |
37 |
this.add(databaseCode, applicationAbbrev);
|
38 |
return databaseCode;
|
39 |
} |
40 |
|
41 |
public String getApplicationAbbrev(String databaseCode) { |
42 |
if( this.hasDatabaseCode(databaseCode) ) { |
43 |
return this.database2applicationAbbrev.get(databaseCode); |
44 |
} |
45 |
String applicationAbbrev = this.searchApplicationAbbrev(databaseCode); |
46 |
if( StringUtils.isEmpty(applicationAbbrev) ) {
|
47 |
return null; |
48 |
} |
49 |
this.add(databaseCode, applicationAbbrev);
|
50 |
return applicationAbbrev;
|
51 |
} |
52 |
|
53 |
public boolean hasDatabaseCode(String databaseCode) { |
54 |
return this.database2applicationAbbrev.containsKey(databaseCode); |
55 |
} |
56 |
|
57 |
public boolean hasApplicationAbbrev(String applicationAbbrev) { |
58 |
return this.applicationAbbrev2database.containsKey(applicationAbbrev); |
59 |
} |
60 |
|
61 |
protected String searchDatabaseCode(String applicationAbbrev) { |
62 |
// Initialize sql only for debugging purposes
|
63 |
String sql = "select srid, auth_name, auth_srid from spatial_ref_sys where auth_name/auth_srid is '"+applicationAbbrev+"'."; |
64 |
try {
|
65 |
String[] s = applicationAbbrev.split(applicationAbbrev); |
66 |
sql = "select srid, auth_name, auth_srid from spatial_ref_sys where auth_name = '" + s[0] +"' and auth_srid = '"+s[1]+"' "; |
67 |
Statement st = this.helper.getConnection().createStatement(); |
68 |
ResultSet rs = JDBCUtils.executeQuery(st, sql);
|
69 |
if ( rs.next() ) {
|
70 |
int srid = rs.getInt("srid"); |
71 |
return String.valueOf(srid); |
72 |
} |
73 |
return null; |
74 |
} catch (Throwable ex) { |
75 |
throw new RuntimeException("Problems with SQL '"+sql+"'.",ex); |
76 |
} |
77 |
} |
78 |
|
79 |
protected String searchApplicationAbbrev(String databaseCode) { |
80 |
String sql = "select srid, auth_name, auth_srid from spatial_ref_sys where srid = " + databaseCode; |
81 |
try {
|
82 |
Statement st = this.helper.getConnection().createStatement(); |
83 |
ResultSet rs = JDBCUtils.executeQuery(st, sql);
|
84 |
if ( rs.next() ) {
|
85 |
int auth_code = rs.getInt("auth_srid"); |
86 |
String auth_name = rs.getString("auth_name"); |
87 |
return auth_name + ":" + auth_code; |
88 |
} |
89 |
return null; |
90 |
} catch (Throwable ex) { |
91 |
throw new RuntimeException("Problems with SQL '"+sql+"'.",ex); |
92 |
} |
93 |
} |
94 |
|
95 |
} |