Statistics
| Revision:

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 @ 44632

History | View | Annotate | Download (5.86 KB)

1 43355 jjdelcerro
2
package org.gvsig.fmap.dal.store.jdbc2.spi;
3
4 43606 jjdelcerro
import java.sql.Connection;
5 43355 jjdelcerro
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 43606 jjdelcerro
import org.cresques.cts.IProjection;
11
import org.gvsig.fmap.crs.CRSFactory;
12 43355 jjdelcerro
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
13
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
14 43687 jjdelcerro
import org.slf4j.Logger;
15
import org.slf4j.LoggerFactory;
16 43355 jjdelcerro
17 43606 jjdelcerro
public class SRSSolverBase implements SRSSolver {
18 43738 jjdelcerro
    /*
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 43687 jjdelcerro
    protected static final Logger logger = LoggerFactory.getLogger(SRSSolverBase.class);
24 43355 jjdelcerro
25 43738 jjdelcerro
    protected Map<Integer,String> database2applicationAbbrev;
26
    protected Map<String,Integer> applicationAbbrev2database;
27 43355 jjdelcerro
    protected JDBCHelper helper;
28
29 43606 jjdelcerro
    public SRSSolverBase(JDBCHelper helper) {
30 43355 jjdelcerro
        this.helper = helper;
31
        this.applicationAbbrev2database = new HashMap<>();
32
        this.database2applicationAbbrev = new HashMap<>();
33
    }
34
35 43606 jjdelcerro
    @Override
36 43687 jjdelcerro
    public void add(Object databaseCode, String applicationAbbrev) {
37 43738 jjdelcerro
        this.applicationAbbrev2database.put(applicationAbbrev, (Integer) databaseCode);
38
        this.database2applicationAbbrev.put((Integer) databaseCode, applicationAbbrev);
39 43355 jjdelcerro
    }
40
41 43606 jjdelcerro
    @Override
42 43738 jjdelcerro
    public Integer getDatabaseCode(Connection connection, String applicationAbbrev) {
43 43606 jjdelcerro
        if( this.hasApplicationAbbrev(connection, applicationAbbrev) ) {
44 43355 jjdelcerro
            return this.applicationAbbrev2database.get(applicationAbbrev);
45
        }
46 43738 jjdelcerro
        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 43355 jjdelcerro
        this.add(databaseCode, applicationAbbrev);
58
        return databaseCode;
59
    }
60
61 43606 jjdelcerro
    @Override
62 43687 jjdelcerro
    public String getApplicationAbbrev(Connection connection, Object databaseCode) {
63 43606 jjdelcerro
        if( this.hasDatabaseCode(connection, databaseCode) ) {
64 43738 jjdelcerro
            return this.database2applicationAbbrev.get((Integer)databaseCode);
65 43355 jjdelcerro
        }
66 43738 jjdelcerro
        String applicationAbbrev = this.searchApplicationAbbrev(connection, (Integer) databaseCode);
67 43355 jjdelcerro
        if( StringUtils.isEmpty(applicationAbbrev) ) {
68
            return null;
69
        }
70
        this.add(databaseCode, applicationAbbrev);
71
        return applicationAbbrev;
72
    }
73
74 43606 jjdelcerro
    @Override
75 43687 jjdelcerro
    public boolean hasDatabaseCode(Connection connection, Object databaseCode) {
76 43738 jjdelcerro
        return this.database2applicationAbbrev.containsKey((Integer)databaseCode);
77 43355 jjdelcerro
    }
78
79 43606 jjdelcerro
    @Override
80
    public boolean hasApplicationAbbrev(Connection connection, String applicationAbbrev) {
81 43355 jjdelcerro
        return this.applicationAbbrev2database.containsKey(applicationAbbrev);
82
    }
83
84 43687 jjdelcerro
    protected Object searchDatabaseCode(Connection connection, String applicationAbbrev) {
85 43355 jjdelcerro
        // 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 43687 jjdelcerro
            String[] s = applicationAbbrev.split(":");
89 43734 jjdelcerro
            sql = "select srid, auth_name, auth_srid from spatial_ref_sys where upper(auth_name) = upper('" + s[0] +"') and auth_srid = "+s[1]+" ";
90 43606 jjdelcerro
            Statement st = connection.createStatement();
91 43355 jjdelcerro
            ResultSet rs = JDBCUtils.executeQuery(st, sql);
92
            if ( rs.next() ) {
93 43738 jjdelcerro
                Integer srid = rs.getInt("srid");
94 43687 jjdelcerro
                return srid;
95 43355 jjdelcerro
            }
96
            return null;
97
        } catch (Throwable ex) {
98 43687 jjdelcerro
            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 43355 jjdelcerro
        }
101
    }
102
103 43738 jjdelcerro
    protected String searchApplicationAbbrev(Connection connection, Integer databaseCode) {
104 43355 jjdelcerro
        String sql = "select srid, auth_name, auth_srid from spatial_ref_sys where srid = " + databaseCode;
105
        try {
106 43606 jjdelcerro
            Statement st = connection.createStatement();
107 43355 jjdelcerro
            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 43687 jjdelcerro
            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 43355 jjdelcerro
        }
118
    }
119
120 43606 jjdelcerro
    @Override
121 43687 jjdelcerro
    public IProjection getProjection(Connection connection, Object databaseCode) {
122
        if( databaseCode == null ) {
123 43606 jjdelcerro
            return null;
124
        }
125 43687 jjdelcerro
        String s = databaseCode.toString().trim();
126
        if( s.equals("0") ) {
127
            return null;
128
        }
129 43606 jjdelcerro
        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 43738 jjdelcerro
    public Integer getDatabaseCode(Connection connection, IProjection projection) {
139 44606 jjdelcerro
        if( projection==null ) {
140
            return 0;
141
        }
142 43738 jjdelcerro
        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 43606 jjdelcerro
    }
151
152 43355 jjdelcerro
}