root / branches / v2_0_0_prep / libraries / org.gvsig.fmap.dal.db.h2spatial / src / main / java / org / gvsig / fmap / dal / db / h2spatial / impl / H2SpatialStoreProvider.java @ 32695
History | View | Annotate | Download (7.94 KB)
1 |
package org.gvsig.fmap.dal.db.h2spatial.impl; |
---|---|
2 |
|
3 |
import java.util.List; |
4 |
import java.util.regex.Matcher; |
5 |
import java.util.regex.Pattern; |
6 |
|
7 |
import org.gvsig.fmap.dal.DALLocator; |
8 |
import org.gvsig.fmap.dal.DataManager; |
9 |
import org.gvsig.fmap.dal.DataServerExplorer; |
10 |
import org.gvsig.fmap.dal.DataTypes; |
11 |
import org.gvsig.fmap.dal.exception.DataException; |
12 |
import org.gvsig.fmap.dal.exception.InitializeException; |
13 |
import org.gvsig.fmap.dal.exception.ReadException; |
14 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
15 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
16 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
17 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
18 |
import org.gvsig.fmap.dal.feature.FeatureType; |
19 |
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider; |
20 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
21 |
import org.gvsig.fmap.dal.store.jdbc.JDBCHelper; |
22 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
23 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreProviderWriter; |
24 |
import org.gvsig.fmap.geom.Geometry; |
25 |
import org.gvsig.tools.ToolsLocator; |
26 |
import org.gvsig.tools.dynobject.DynClass; |
27 |
import org.gvsig.tools.dynobject.DynObjectManager; |
28 |
import org.gvsig.tools.exception.BaseException; |
29 |
import org.slf4j.Logger; |
30 |
import org.slf4j.LoggerFactory; |
31 |
|
32 |
/**
|
33 |
*
|
34 |
* @author Vicente Caballero Navarro
|
35 |
*/
|
36 |
public class H2SpatialStoreProvider extends JDBCStoreProviderWriter { |
37 |
|
38 |
|
39 |
public final static Logger logger = LoggerFactory |
40 |
.getLogger(H2SpatialStoreProvider.class); |
41 |
|
42 |
public static String NAME = "H2Spatial"; |
43 |
public static String DESCRIPTION = "H2Spatial source"; |
44 |
private static final String DYNCLASS_NAME = "H2SpatialStore"; |
45 |
private static DynClass DYNCLASS = null; |
46 |
|
47 |
protected static void registerDynClass() { |
48 |
DynObjectManager dynman = ToolsLocator.getDynObjectManager(); |
49 |
DynClass dynClass; |
50 |
if (DYNCLASS == null) { |
51 |
dynClass = dynman.add(DYNCLASS_NAME, DESCRIPTION); |
52 |
|
53 |
dynClass.extend(dynman.get(FeatureStore.DYNCLASS_NAME)); |
54 |
DYNCLASS = dynClass; |
55 |
} |
56 |
} |
57 |
|
58 |
/* (non-Javadoc)
|
59 |
* @see org.gvsig.fmap.dal.store.jdbc.JDBCStoreProvider#doDispose()
|
60 |
*/
|
61 |
protected void doDispose() throws BaseException { |
62 |
super.doDispose();
|
63 |
((H2SpatialServerExplorer)getExplorer()).remove(getParameters()); |
64 |
|
65 |
} |
66 |
public H2SpatialStoreProvider(JDBCStoreParameters params,
|
67 |
DataStoreProviderServices storeServices) throws InitializeException {
|
68 |
super(params, storeServices, ToolsLocator.getDynObjectManager()
|
69 |
.createDynObject(DYNCLASS)); |
70 |
} |
71 |
|
72 |
private H2SpatialStoreParameters getH2SpatialParamters() {
|
73 |
return (H2SpatialStoreParameters) this.getParameters(); |
74 |
} |
75 |
|
76 |
/* (non-Javadoc)
|
77 |
* @see org.gvsig.fmap.dal.store.jdbc.JDBCStoreProvider#createHelper()
|
78 |
*/
|
79 |
protected JDBCHelper createHelper() throws InitializeException { |
80 |
return new H2SpatialHelper(this, getH2SpatialParamters()); |
81 |
} |
82 |
|
83 |
/* (non-Javadoc)
|
84 |
* @see org.gvsig.fmap.dal.store.jdbc.JDBCStoreProvider#fixFilter(java.lang.String)
|
85 |
*/
|
86 |
protected String fixFilter(String filter) { |
87 |
if (filter == null) { |
88 |
return null; |
89 |
} |
90 |
|
91 |
// Transform SRS to code
|
92 |
// GeomFromText\s*\(\s*'[^']*'\s*,\s*('[^']*')\s*\)
|
93 |
Pattern pattern = Pattern |
94 |
.compile("GeomFromText\\s*\\(\\s*'[^']*'\\s*,\\s*'([^']*)'\\s*\\)");
|
95 |
Matcher matcher = pattern.matcher(filter);
|
96 |
StringBuilder strb = new StringBuilder(); |
97 |
int pos = 0; |
98 |
String srsCode;
|
99 |
while (matcher.find(pos)) {
|
100 |
strb.append(filter.substring(pos, matcher.start(1)));
|
101 |
srsCode = matcher.group(1).trim();
|
102 |
if (srsCode.startsWith("'")) { |
103 |
srsCode = srsCode.substring(1);
|
104 |
} |
105 |
if (srsCode.endsWith("'")) { |
106 |
srsCode = srsCode.substring(0, srsCode.length() - 1); |
107 |
} |
108 |
// strb.append(helper.getProviderSRID(srsCode));
|
109 |
strb.append(filter.substring(matcher.end(1), matcher.end()));
|
110 |
pos = matcher.end(); |
111 |
|
112 |
} |
113 |
strb.append(filter.substring(pos)); |
114 |
|
115 |
return strb.toString();
|
116 |
} |
117 |
|
118 |
/* (non-Javadoc)
|
119 |
* @see org.gvsig.fmap.dal.store.jdbc.JDBCStoreProvider#getName()
|
120 |
*/
|
121 |
public String getName() { |
122 |
return NAME;
|
123 |
} |
124 |
|
125 |
/* (non-Javadoc)
|
126 |
* @see org.gvsig.fmap.dal.store.jdbc.JDBCStoreProvider#createSet(org.gvsig.fmap.dal.feature.FeatureQuery, org.gvsig.fmap.dal.feature.FeatureType)
|
127 |
*/
|
128 |
public FeatureSetProvider createSet(FeatureQuery query,
|
129 |
FeatureType featureType) throws DataException {
|
130 |
return new H2SpatialSetProvider(this, query, featureType); |
131 |
} |
132 |
|
133 |
/* (non-Javadoc)
|
134 |
* @see org.gvsig.fmap.dal.store.jdbc.JDBCStoreProvider#getExplorer()
|
135 |
*/
|
136 |
public DataServerExplorer getExplorer() throws ReadException { |
137 |
DataManager manager = DALLocator.getDataManager(); |
138 |
H2SpatialServerExplorerParameters exParams; |
139 |
H2SpatialStoreParameters params = getH2SpatialParamters(); |
140 |
try {
|
141 |
exParams = (H2SpatialServerExplorerParameters) manager |
142 |
.createServerExplorerParameters(H2SpatialServerExplorer.NAME); |
143 |
exParams.setUrl(params.getUrl()); |
144 |
exParams.setHost(params.getHost()); |
145 |
exParams.setPort(params.getPort()); |
146 |
exParams.setDBName(params.getDBName()); |
147 |
exParams.setUser(params.getUser()); |
148 |
exParams.setPassword(params.getPassword()); |
149 |
exParams.setCatalog(params.getCatalog()); |
150 |
exParams.setSchema(params.getSchema()); |
151 |
exParams.setJDBCDriverClassName(params.getJDBCDriverClassName()); |
152 |
exParams.setUseSSL(params.getUseSSL()); |
153 |
|
154 |
return manager.createServerExplorer(exParams);
|
155 |
} catch (DataException e) {
|
156 |
throw new ReadException(this.getName(), e); |
157 |
} catch (ValidateDataParametersException e) {
|
158 |
throw new ReadException(this.getName(), e); |
159 |
} |
160 |
} |
161 |
|
162 |
/* (non-Javadoc)
|
163 |
* @see org.gvsig.fmap.dal.store.jdbc.JDBCStoreProvider#allowAutomaticValues()
|
164 |
*/
|
165 |
public boolean allowAutomaticValues() { |
166 |
return true; |
167 |
} |
168 |
|
169 |
/* (non-Javadoc)
|
170 |
* @see org.gvsig.fmap.dal.store.jdbc.JDBCStoreProvider#hasGeometrySupport()
|
171 |
*/
|
172 |
public boolean hasGeometrySupport() { |
173 |
return true; |
174 |
} |
175 |
|
176 |
/* (non-Javadoc)
|
177 |
* @see org.gvsig.fmap.dal.feature.spi.AbstractFeatureStoreProvider#canWriteGeometry(int, int)
|
178 |
*/
|
179 |
public boolean canWriteGeometry(int geometryType, int geometrySubtype) |
180 |
throws DataException {
|
181 |
FeatureType fType = this.getFeatureStore().getDefaultFeatureType();
|
182 |
FeatureAttributeDescriptor geomAttr = fType |
183 |
.getAttributeDescriptor(fType.getDefaultGeometryAttributeName()); |
184 |
if (geomAttr == null) { |
185 |
return false; |
186 |
} |
187 |
if (geometrySubtype != Geometry.SUBTYPES.GEOM2D) {
|
188 |
return false; |
189 |
} |
190 |
|
191 |
switch (geomAttr.getGeometryType()) {
|
192 |
case Geometry.TYPES.GEOMETRY:
|
193 |
return true; |
194 |
|
195 |
case Geometry.TYPES.MULTISURFACE:
|
196 |
return geometryType == Geometry.TYPES.MULTISURFACE
|
197 |
|| geometryType == Geometry.TYPES.SURFACE; |
198 |
|
199 |
case Geometry.TYPES.MULTIPOINT:
|
200 |
return geometryType == Geometry.TYPES.MULTIPOINT
|
201 |
|| geometryType == Geometry.TYPES.POINT; |
202 |
|
203 |
case Geometry.TYPES.MULTICURVE:
|
204 |
return geometryType == Geometry.TYPES.MULTICURVE
|
205 |
|| geometryType == Geometry.TYPES.CURVE; |
206 |
|
207 |
case Geometry.TYPES.MULTISOLID:
|
208 |
return geometryType == Geometry.TYPES.MULTISOLID
|
209 |
|| geometryType == Geometry.TYPES.SOLID; |
210 |
|
211 |
default:
|
212 |
return geometryType == geomAttr.getGeometryType();
|
213 |
} |
214 |
|
215 |
} |
216 |
|
217 |
/* (non-Javadoc)
|
218 |
* @see org.gvsig.fmap.dal.store.jdbc.JDBCStoreProviderWriter#getSqlStatementAlterField(org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor, org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor, java.util.List)
|
219 |
*/
|
220 |
protected List getSqlStatementAlterField( |
221 |
FeatureAttributeDescriptor attrOrg, |
222 |
FeatureAttributeDescriptor attrTrg, List additionalStatement)
|
223 |
throws DataException {
|
224 |
//
|
225 |
List actions = super.getSqlStatementAlterField(attrOrg, attrTrg, |
226 |
additionalStatement); |
227 |
StringBuilder strb;
|
228 |
if (attrOrg.getDataType() != attrTrg.getDataType()) {
|
229 |
if (attrOrg.getDataType() == DataTypes.GEOMETRY) {
|
230 |
// FIXME
|
231 |
//additionalStatement.add(getSqlGeometyFieldDrop(attrOrg));
|
232 |
} |
233 |
if (attrTrg.getDataType() == DataTypes.GEOMETRY) {
|
234 |
// FIXME
|
235 |
// additionalStatement.addAll(((MySQLHelper) helper)
|
236 |
// .getSqlGeometyFieldAdd(attrTrg, params.getTable(),
|
237 |
// params.getSchema()));
|
238 |
} |
239 |
} |
240 |
if (attrOrg.getDataType() == attrTrg.getDataType()
|
241 |
&& attrTrg.getDataType() == DataTypes.GEOMETRY) { |
242 |
// TODO Checks SRS and GeomType/Subtype
|
243 |
} |
244 |
|
245 |
return actions;
|
246 |
} |
247 |
|
248 |
} |