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 / SRSSolverBase.java @ 44606
History | View | Annotate | Download (5.86 KB)
1 |
|
---|---|
2 |
package org.gvsig.fmap.dal.store.jdbc2.spi; |
3 |
|
4 |
import java.sql.Connection; |
5 |
import java.sql.ResultSet; |
6 |
import java.sql.Statement; |
7 |
import java.util.HashMap; |
8 |
import java.util.Map; |
9 |
import org.apache.commons.lang3.StringUtils; |
10 |
import org.cresques.cts.IProjection; |
11 |
import org.gvsig.fmap.crs.CRSFactory; |
12 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
13 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
14 |
import org.slf4j.Logger; |
15 |
import org.slf4j.LoggerFactory; |
16 |
|
17 |
public class SRSSolverBase implements SRSSolver { |
18 |
/*
|
19 |
Esta clase asume que los SRS de la BBDD son Integer.
|
20 |
En caso de que para un gestor de BBDD no lo sea deberia usarse una
|
21 |
implemetacion alternativa y no esta.
|
22 |
*/
|
23 |
protected static final Logger logger = LoggerFactory.getLogger(SRSSolverBase.class); |
24 |
|
25 |
protected Map<Integer,String> database2applicationAbbrev; |
26 |
protected Map<String,Integer> applicationAbbrev2database; |
27 |
protected JDBCHelper helper;
|
28 |
|
29 |
public SRSSolverBase(JDBCHelper helper) {
|
30 |
this.helper = helper;
|
31 |
this.applicationAbbrev2database = new HashMap<>(); |
32 |
this.database2applicationAbbrev = new HashMap<>(); |
33 |
} |
34 |
|
35 |
@Override
|
36 |
public void add(Object databaseCode, String applicationAbbrev) { |
37 |
this.applicationAbbrev2database.put(applicationAbbrev, (Integer) databaseCode); |
38 |
this.database2applicationAbbrev.put((Integer) databaseCode, applicationAbbrev); |
39 |
} |
40 |
|
41 |
@Override
|
42 |
public Integer getDatabaseCode(Connection connection, String applicationAbbrev) { |
43 |
if( this.hasApplicationAbbrev(connection, applicationAbbrev) ) { |
44 |
return this.applicationAbbrev2database.get(applicationAbbrev); |
45 |
} |
46 |
Integer databaseCode = (Integer) this.searchDatabaseCode(connection, applicationAbbrev); |
47 |
// if( databaseCode==null ) {
|
48 |
// logger.debug("database code srs null.");
|
49 |
// return null;
|
50 |
// }
|
51 |
// logger.debug("database code srs {}, type {}.",
|
52 |
// new Object[] {
|
53 |
// databaseCode,
|
54 |
// databaseCode.getClass().getSimpleName()
|
55 |
// }
|
56 |
// );
|
57 |
this.add(databaseCode, applicationAbbrev);
|
58 |
return databaseCode;
|
59 |
} |
60 |
|
61 |
@Override
|
62 |
public String getApplicationAbbrev(Connection connection, Object databaseCode) { |
63 |
if( this.hasDatabaseCode(connection, databaseCode) ) { |
64 |
return this.database2applicationAbbrev.get((Integer)databaseCode); |
65 |
} |
66 |
String applicationAbbrev = this.searchApplicationAbbrev(connection, (Integer) databaseCode); |
67 |
if( StringUtils.isEmpty(applicationAbbrev) ) {
|
68 |
return null; |
69 |
} |
70 |
this.add(databaseCode, applicationAbbrev);
|
71 |
return applicationAbbrev;
|
72 |
} |
73 |
|
74 |
@Override
|
75 |
public boolean hasDatabaseCode(Connection connection, Object databaseCode) { |
76 |
return this.database2applicationAbbrev.containsKey((Integer)databaseCode); |
77 |
} |
78 |
|
79 |
@Override
|
80 |
public boolean hasApplicationAbbrev(Connection connection, String applicationAbbrev) { |
81 |
return this.applicationAbbrev2database.containsKey(applicationAbbrev); |
82 |
} |
83 |
|
84 |
protected Object searchDatabaseCode(Connection connection, String applicationAbbrev) { |
85 |
// Initialize sql only for debugging purposes
|
86 |
String sql = "select srid, auth_name, auth_srid from spatial_ref_sys where auth_name/auth_srid is '"+applicationAbbrev+"'."; |
87 |
try {
|
88 |
String[] s = applicationAbbrev.split(":"); |
89 |
sql = "select srid, auth_name, auth_srid from spatial_ref_sys where upper(auth_name) = upper('" + s[0] +"') and auth_srid = "+s[1]+" "; |
90 |
Statement st = connection.createStatement();
|
91 |
ResultSet rs = JDBCUtils.executeQuery(st, sql);
|
92 |
if ( rs.next() ) {
|
93 |
Integer srid = rs.getInt("srid"); |
94 |
return srid;
|
95 |
} |
96 |
return null; |
97 |
} catch (Throwable ex) { |
98 |
logger.warn("Can't retrieve SRS code from spatial_ref_sys ("+sql+").", ex); |
99 |
throw new RuntimeException("Can't retrieve SRS code from spatial_ref_sys ("+sql+").",ex); |
100 |
} |
101 |
} |
102 |
|
103 |
protected String searchApplicationAbbrev(Connection connection, Integer databaseCode) { |
104 |
String sql = "select srid, auth_name, auth_srid from spatial_ref_sys where srid = " + databaseCode; |
105 |
try {
|
106 |
Statement st = connection.createStatement();
|
107 |
ResultSet rs = JDBCUtils.executeQuery(st, sql);
|
108 |
if ( rs.next() ) {
|
109 |
int auth_code = rs.getInt("auth_srid"); |
110 |
String auth_name = rs.getString("auth_name"); |
111 |
return auth_name + ":" + auth_code; |
112 |
} |
113 |
return null; |
114 |
} catch (Throwable ex) { |
115 |
logger.warn("Can't retrieve SRS from spatial_ref_sys ("+sql+").", ex); |
116 |
throw new RuntimeException("Can't retrieve SRS from spatial_ref_sys ("+sql+").",ex); |
117 |
} |
118 |
} |
119 |
|
120 |
@Override
|
121 |
public IProjection getProjection(Connection connection, Object databaseCode) { |
122 |
if( databaseCode == null ) { |
123 |
return null; |
124 |
} |
125 |
String s = databaseCode.toString().trim();
|
126 |
if( s.equals("0") ) { |
127 |
return null; |
128 |
} |
129 |
String abbrev = this.getApplicationAbbrev(connection, databaseCode); |
130 |
if( StringUtils.isEmpty(abbrev) ) {
|
131 |
return null; |
132 |
} |
133 |
IProjection proj = CRSFactory.getCRS(abbrev); |
134 |
return proj;
|
135 |
} |
136 |
|
137 |
@Override
|
138 |
public Integer getDatabaseCode(Connection connection, IProjection projection) { |
139 |
if( projection==null ) { |
140 |
return 0; |
141 |
} |
142 |
Object srscode = this.getDatabaseCode(connection, projection.getAbrev()); |
143 |
// logger.debug("database code srs {}, type {}.",
|
144 |
// new Object[] {
|
145 |
// srscode,
|
146 |
// srscode==null? "null":srscode.getClass().getSimpleName(),
|
147 |
// }
|
148 |
// );
|
149 |
return (Integer) srscode; |
150 |
} |
151 |
|
152 |
} |