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 / operations / CalculateEnvelopeOfColumnOperation.java @ 43020
History | View | Annotate | Download (4.58 KB)
1 |
package org.gvsig.fmap.dal.store.jdbc2.spi.operations; |
---|---|
2 |
|
3 |
import java.sql.Connection; |
4 |
import java.sql.ResultSet; |
5 |
import java.sql.SQLException; |
6 |
import java.sql.Statement; |
7 |
import org.apache.commons.lang3.StringUtils; |
8 |
import org.cresques.cts.IProjection; |
9 |
import org.gvsig.fmap.dal.exception.DataException; |
10 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
11 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
12 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
13 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
14 |
import org.gvsig.fmap.geom.Geometry; |
15 |
import org.gvsig.fmap.geom.primitive.Envelope; |
16 |
|
17 |
public class CalculateEnvelopeOfColumnOperation extends AbstractConnectionOperation { |
18 |
|
19 |
private final String subquery; |
20 |
private final String schemaName; |
21 |
private final String tableName; |
22 |
private final String columnName; |
23 |
private final String baseFilter; |
24 |
private final Envelope limit; |
25 |
private final IProjection crs; |
26 |
private final String dbName; |
27 |
|
28 |
public CalculateEnvelopeOfColumnOperation(
|
29 |
JDBCHelper helper, |
30 |
String subquery,
|
31 |
String dbName,
|
32 |
String schemaName,
|
33 |
String tableName,
|
34 |
String columnName,
|
35 |
String baseFilter,
|
36 |
Envelope limit, |
37 |
IProjection crs |
38 |
) { |
39 |
super(helper);
|
40 |
this.subquery = subquery;
|
41 |
this.dbName = dbName;
|
42 |
this.schemaName = schemaName;
|
43 |
this.tableName = tableName;
|
44 |
this.columnName = columnName;
|
45 |
this.baseFilter = baseFilter;
|
46 |
this.limit = limit;
|
47 |
this.crs = crs;
|
48 |
} |
49 |
|
50 |
@Override
|
51 |
public final Object perform(Connection conn) throws DataException { |
52 |
Envelope env = calculateEnvelopeOfColumn( |
53 |
conn, |
54 |
subquery, |
55 |
dbName, |
56 |
schemaName, |
57 |
tableName, |
58 |
columnName, |
59 |
baseFilter, |
60 |
limit, |
61 |
crs |
62 |
); |
63 |
return env;
|
64 |
} |
65 |
|
66 |
public Envelope calculateEnvelopeOfColumn(
|
67 |
Connection conn,
|
68 |
String subquery,
|
69 |
String dbName,
|
70 |
String schemaName,
|
71 |
String tableName,
|
72 |
String columnName,
|
73 |
String baseFilter,
|
74 |
Envelope limit, |
75 |
IProjection crs |
76 |
) throws DataException {
|
77 |
|
78 |
JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
|
79 |
sqlbuilder.select().column().value( |
80 |
sqlbuilder.getAsGeometry( |
81 |
sqlbuilder.ST_ExtentAggregate( |
82 |
sqlbuilder.column(columnName) |
83 |
) |
84 |
) |
85 |
); |
86 |
|
87 |
if (StringUtils.isEmpty(subquery)) {
|
88 |
sqlbuilder.select().from().table().database(dbName).schema(schemaName).name(tableName); |
89 |
} else {
|
90 |
sqlbuilder.select().from().subquery(subquery); |
91 |
} |
92 |
|
93 |
if (StringUtils.isEmpty(baseFilter)) {
|
94 |
if (limit != null) { |
95 |
sqlbuilder.select().where().set( |
96 |
sqlbuilder.ST_Intersects( |
97 |
sqlbuilder.ST_Envelope( |
98 |
sqlbuilder.column(columnName) |
99 |
), |
100 |
sqlbuilder.ST_Envelope( |
101 |
sqlbuilder.geometry(limit.getGeometry(), crs) |
102 |
) |
103 |
) |
104 |
); |
105 |
} |
106 |
} else {
|
107 |
sqlbuilder.select().where().set( sqlbuilder.custom(baseFilter) ); |
108 |
if (limit != null) { |
109 |
sqlbuilder.select().where().and( |
110 |
sqlbuilder.ST_Intersects( |
111 |
sqlbuilder.ST_Envelope( |
112 |
sqlbuilder.column(columnName) |
113 |
), |
114 |
sqlbuilder.ST_Envelope( |
115 |
sqlbuilder.geometry(limit.getGeometry(), crs) |
116 |
) |
117 |
) |
118 |
); |
119 |
} |
120 |
} |
121 |
|
122 |
String sql = sqlbuilder.select().toString();
|
123 |
|
124 |
Statement st = null; |
125 |
ResultSet rs = null; |
126 |
try {
|
127 |
st = conn.createStatement(); |
128 |
rs = JDBCUtils.executeQuery(st, sql); |
129 |
if (!rs.next()) {
|
130 |
return null; |
131 |
} |
132 |
Geometry geom = this.helper.getGeometryFromColumn(rs, 1); |
133 |
if (geom == null) { |
134 |
return null; |
135 |
} |
136 |
return geom.getEnvelope();
|
137 |
|
138 |
} catch (SQLException ex) { |
139 |
throw new JDBCSQLException(ex); |
140 |
} finally {
|
141 |
JDBCUtils.closeQuietly(st); |
142 |
JDBCUtils.closeQuietly(rs); |
143 |
} |
144 |
} |
145 |
|
146 |
} |