root / branches / v2_0_0_prep / libraries / libFMap_dataDB / src / org / gvsig / fmap / data / feature / db / jdbc / JDBCStore.java @ 23303
History | View | Annotate | Download (11.6 KB)
1 |
package org.gvsig.fmap.data.feature.db.jdbc; |
---|---|
2 |
|
3 |
|
4 |
import java.security.KeyException; |
5 |
import java.sql.Connection; |
6 |
import java.sql.ResultSet; |
7 |
import java.sql.Statement; |
8 |
import java.util.HashMap; |
9 |
import java.util.Iterator; |
10 |
|
11 |
import org.gvsig.fmap.data.CloseException; |
12 |
import org.gvsig.fmap.data.DataException; |
13 |
import org.gvsig.fmap.data.DataExplorer; |
14 |
import org.gvsig.fmap.data.DataStoreParameters; |
15 |
import org.gvsig.fmap.data.InitializeException; |
16 |
import org.gvsig.fmap.data.OpenException; |
17 |
import org.gvsig.fmap.data.ReadException; |
18 |
import org.gvsig.fmap.data.Resource; |
19 |
import org.gvsig.fmap.data.ResourceManager; |
20 |
import org.gvsig.fmap.data.WriteException; |
21 |
import org.gvsig.fmap.data.commands.implementation.AttributeCommand; |
22 |
import org.gvsig.fmap.data.commands.implementation.FeatureCommand; |
23 |
import org.gvsig.fmap.data.commands.implementation.UpdateAttributeCommand; |
24 |
import org.gvsig.fmap.data.commands.implementation.UpdateFeatureCommand; |
25 |
import org.gvsig.fmap.data.feature.AttributeDescriptor; |
26 |
import org.gvsig.fmap.data.feature.CreatedFeature; |
27 |
import org.gvsig.fmap.data.feature.Feature; |
28 |
import org.gvsig.fmap.data.feature.FeatureAttributeDescriptor; |
29 |
import org.gvsig.fmap.data.feature.FeatureID; |
30 |
import org.gvsig.fmap.data.feature.FeatureType; |
31 |
import org.gvsig.fmap.data.feature.InitializeWriterException; |
32 |
import org.gvsig.fmap.data.feature.db.DBFeatureID; |
33 |
import org.gvsig.fmap.data.feature.db.DBFeatureType; |
34 |
import org.gvsig.fmap.data.feature.db.DBStore; |
35 |
import org.gvsig.metadata.IMetadata; |
36 |
import org.gvsig.metadata.IMetadataManager; |
37 |
import org.gvsig.metadata.MetadataManager; |
38 |
import org.gvsig.tools.exception.BaseException; |
39 |
|
40 |
public abstract class JDBCStore extends DBStore { |
41 |
|
42 |
protected JDBCResource resource = null; |
43 |
protected String sqlSelectPart; |
44 |
protected String baseWhereClause = null; |
45 |
protected String baseOrder = null; |
46 |
protected String sqlSource = null; |
47 |
protected boolean useSqlSource = false; |
48 |
protected IMetadata metadata;
|
49 |
|
50 |
|
51 |
public void init(DataStoreParameters parameters) throws InitializeException { |
52 |
|
53 |
JDBCResource tmpResource = this.createResource((JDBCStoreParameters)parameters);
|
54 |
ResourceManager resMan = ResourceManager.getResourceManager(); |
55 |
|
56 |
try {
|
57 |
this.resource = (JDBCResource)resMan.addResource(tmpResource);
|
58 |
} catch (DataException e1) {
|
59 |
throw new InitializeException(this.getName(),e1); |
60 |
} |
61 |
|
62 |
super.init(parameters,this.resource); |
63 |
|
64 |
|
65 |
this.initFeatureType();
|
66 |
this.initSqlProperties();
|
67 |
} |
68 |
|
69 |
|
70 |
protected abstract JDBCResource createResource(JDBCStoreParameters params); |
71 |
protected abstract void initFeatureType() throws InitializeException; |
72 |
protected abstract void initSqlProperties() throws InitializeException; |
73 |
|
74 |
|
75 |
/* (non-Javadoc)
|
76 |
* @see org.gvsig.fmap.data.feature.FeatureStore#doClose()
|
77 |
*/
|
78 |
protected void doClose() throws CloseException { |
79 |
this.resource.close();
|
80 |
|
81 |
} |
82 |
|
83 |
/* (non-Javadoc)
|
84 |
* @see org.gvsig.fmap.data.feature.FeatureStore#doDispose()
|
85 |
*/
|
86 |
protected void doDispose() throws CloseException { |
87 |
ResourceManager resMan = ResourceManager.getResourceManager(); |
88 |
|
89 |
try {
|
90 |
resMan.remove(this.resource);
|
91 |
} catch (DataException e1) {
|
92 |
throw new CloseException(this.getName(),e1); |
93 |
} catch (KeyException e) { |
94 |
// TODO Auto-generated catch block
|
95 |
throw new CloseException(this.getName(),e); |
96 |
} |
97 |
this.metadata = null; |
98 |
super.doDispose();
|
99 |
} |
100 |
|
101 |
/* (non-Javadoc)
|
102 |
* @see org.gvsig.fmap.data.feature.FeatureStore#doOpen()
|
103 |
*/
|
104 |
protected void doOpen() throws OpenException { |
105 |
//No Operation
|
106 |
|
107 |
} |
108 |
|
109 |
|
110 |
/* (non-Javadoc)
|
111 |
* @see org.gvsig.fmap.data.feature.FeatureStore#canAlterFeatureType()
|
112 |
*/
|
113 |
public boolean canAlterFeatureType() { |
114 |
return false; |
115 |
} |
116 |
|
117 |
/* (non-Javadoc)
|
118 |
* @see org.gvsig.fmap.data.DataStore#getExplorer()
|
119 |
*/
|
120 |
public DataExplorer getExplorer() throws ReadException { |
121 |
return null; |
122 |
} |
123 |
|
124 |
|
125 |
/* (non-Javadoc)
|
126 |
* @see org.gvsig.fmap.data.feature.FeatureStore#init(org.gvsig.fmap.data.DataStoreParameters, org.gvsig.fmap.data.Resource)
|
127 |
*/
|
128 |
public void init(DataStoreParameters parameters, Resource resource) throws InitializeException { |
129 |
super.init(parameters, resource);
|
130 |
this.resource=(JDBCResource)resource;
|
131 |
} |
132 |
|
133 |
protected Connection getConnection() throws ReadException{ |
134 |
return this.resource.getConnection(); |
135 |
} |
136 |
|
137 |
protected Connection getWriterConnection() throws ReadException{ |
138 |
return this.resource.getWriterConnection(); |
139 |
} |
140 |
|
141 |
public boolean isEditable() { |
142 |
return super.isEditable() && !this.useSqlSource; |
143 |
} |
144 |
|
145 |
|
146 |
public String getSqlSelectPart() { |
147 |
return sqlSelectPart;
|
148 |
} |
149 |
|
150 |
public String getBaseOrder() { |
151 |
return this.baseOrder; |
152 |
} |
153 |
|
154 |
public String getBaseWhereClause() { |
155 |
return baseWhereClause;
|
156 |
} |
157 |
|
158 |
public boolean isUseSqlSource() { |
159 |
return this.useSqlSource; |
160 |
} |
161 |
|
162 |
public String getSqlSource() { |
163 |
return this.sqlSource; |
164 |
} |
165 |
|
166 |
protected abstract JDBCFeaturesWriter getFeaturesWriter() throws InitializeWriterException; |
167 |
|
168 |
protected void doFinishEdition() throws WriteException, ReadException { |
169 |
Iterator commandsfeatures = null; |
170 |
JDBCFeaturesWriter selectiveWriter = getFeaturesWriter(); |
171 |
FeatureType type = getDefaultFeatureType(); |
172 |
boolean needRefresh=false; |
173 |
|
174 |
Feature feature; |
175 |
Object obj;
|
176 |
FeatureID featureId; |
177 |
|
178 |
|
179 |
selectiveWriter.updateFeatureType(type); |
180 |
selectiveWriter.preProcess(); |
181 |
|
182 |
try{
|
183 |
commandsfeatures = commands.getCommandsAttributeDeleted().iterator(); |
184 |
while( commandsfeatures.hasNext() ) {
|
185 |
obj=commandsfeatures.next(); |
186 |
if (obj instanceof AttributeCommand){ |
187 |
FeatureAttributeDescriptor attribute = ((AttributeCommand)obj).getAttributeDescriptor(); |
188 |
selectiveWriter.deleteAttribute(attribute); |
189 |
needRefresh=true;
|
190 |
} |
191 |
} |
192 |
commandsfeatures = commands.getCommandsAttributeUpdated().iterator(); |
193 |
while( commandsfeatures.hasNext() ) {
|
194 |
obj=commandsfeatures.next(); |
195 |
if (obj instanceof AttributeCommand){ |
196 |
FeatureAttributeDescriptor oldAttribute = ((UpdateAttributeCommand)obj).getOldAttributeDescriptor(); |
197 |
FeatureAttributeDescriptor attribute = ((UpdateAttributeCommand)obj).getAttributeDescriptor(); |
198 |
selectiveWriter.updateAttribute(oldAttribute,attribute); |
199 |
needRefresh=true;
|
200 |
} |
201 |
} |
202 |
commandsfeatures = commands.getCommandsAttributeInserted().iterator(); |
203 |
while( commandsfeatures.hasNext() ) {
|
204 |
obj=commandsfeatures.next(); |
205 |
if (obj instanceof AttributeCommand){ |
206 |
FeatureAttributeDescriptor attribute = ((AttributeCommand)obj).getAttributeDescriptor(); |
207 |
selectiveWriter.insertAttribute(attribute); |
208 |
needRefresh=true;
|
209 |
} |
210 |
} |
211 |
|
212 |
|
213 |
commandsfeatures = commands.getCommandsFeatureDeleted().iterator(); |
214 |
while( commandsfeatures.hasNext() ) {
|
215 |
obj=commandsfeatures.next(); |
216 |
if (obj instanceof FeatureCommand){ |
217 |
feature = ((FeatureCommand)obj).getFeature(); |
218 |
if (feature instanceof CreatedFeature) { |
219 |
continue;
|
220 |
} |
221 |
|
222 |
selectiveWriter.deleteFeature(feature); |
223 |
} |
224 |
} |
225 |
|
226 |
commandsfeatures = commands.getCommandsFeatureInserted().iterator(); |
227 |
while( commandsfeatures.hasNext() ) {
|
228 |
obj=commandsfeatures.next(); |
229 |
if (obj instanceof FeatureCommand){ |
230 |
feature = ((FeatureCommand)obj).getFeature(); |
231 |
if (featureManager.isDeleted(feature)){
|
232 |
continue;
|
233 |
} |
234 |
selectiveWriter.insertFeature(feature); |
235 |
} |
236 |
} |
237 |
|
238 |
commandsfeatures = commands.getCommandsFeatureUpdated().iterator(); |
239 |
HashMap toUpdate = new HashMap(); |
240 |
while( commandsfeatures.hasNext() ) {
|
241 |
obj=commandsfeatures.next(); |
242 |
if (obj instanceof FeatureCommand){ |
243 |
// Feature oldFeature = ((UpdateFeatureCommand)obj).getOldFeature();
|
244 |
feature = ((UpdateFeatureCommand)obj).getFeature(); |
245 |
featureId =feature.getID(); |
246 |
feature = featureManager.getFeature(featureId, this,null); |
247 |
if (feature != null){ |
248 |
toUpdate.put(featureId, feature); |
249 |
} |
250 |
} |
251 |
} |
252 |
Iterator toUpdateIter = toUpdate.values().iterator();
|
253 |
while (toUpdateIter.hasNext()){
|
254 |
feature = (Feature) toUpdateIter.next(); |
255 |
selectiveWriter.updateFeature(feature); |
256 |
} |
257 |
|
258 |
|
259 |
selectiveWriter.postProcess(); |
260 |
this.resource.changed(this); |
261 |
if (needRefresh){
|
262 |
this.refresh();
|
263 |
} |
264 |
|
265 |
|
266 |
}catch (ReadException e) {
|
267 |
selectiveWriter.cancelProcess(); |
268 |
throw e;
|
269 |
} catch (WriteException e){
|
270 |
selectiveWriter.cancelProcess(); |
271 |
throw e;
|
272 |
} catch (Exception e){ |
273 |
selectiveWriter.cancelProcess(); |
274 |
throw new WriteException(this.getName(),e); |
275 |
} |
276 |
} |
277 |
|
278 |
public String getFilterForID(DBFeatureType fType, Object[] featureKey) { |
279 |
if (fType.getFieldsId().length != 1) { |
280 |
throw new UnsupportedOperationException("ID fields > 1"); |
281 |
} |
282 |
String id =fType.getFieldsId()[0]; |
283 |
return id + " = " + objectToSqlString(featureKey[0]); |
284 |
} |
285 |
|
286 |
protected String objectToSqlString(Object obj) { |
287 |
if (obj instanceof String){ |
288 |
return "'"+ scapeString((String)obj) +"'"; |
289 |
} else if (obj == null){ |
290 |
return "null"; |
291 |
}else{
|
292 |
// OJO con otros tipos!!
|
293 |
return obj.toString();
|
294 |
} |
295 |
|
296 |
} |
297 |
|
298 |
protected String scapeString(String str) { |
299 |
return str.replace("'", "''"); |
300 |
} |
301 |
|
302 |
public Feature getFeatureByID(FeatureID id,FeatureType featureType) throws ReadException{ |
303 |
return getFeatureByID(featureType,((DBFeatureID)id).getKey());
|
304 |
} |
305 |
|
306 |
public IMetadata getMetadata() throws BaseException { |
307 |
if (metadata==null){ |
308 |
IMetadataManager manager=MetadataManager.getManager(); |
309 |
metadata=manager.create(this.getName());
|
310 |
|
311 |
DBFeatureType fType = (DBFeatureType) this.getDefaultFeatureType();
|
312 |
metadata.set("srs",fType.getDefaultSRS());
|
313 |
} |
314 |
return metadata;
|
315 |
} |
316 |
|
317 |
|
318 |
public Feature getFeatureByID(FeatureType featureType, Object[] featureKey) throws ReadException{ |
319 |
//TODO: Tener en cuenta el FeatureType por si es distinto
|
320 |
if (useSqlSource){
|
321 |
throw new ReadException("Unsuported featureByID in sqlSource mode",this.getName()); |
322 |
} |
323 |
if (featureType==null){ |
324 |
featureType=getDefaultFeatureType(); |
325 |
}else{
|
326 |
if (!featureType.isSubtypeOf(this.getDefaultFeatureType())){ |
327 |
throw new ReadException("invalid type",this.getName()); |
328 |
} |
329 |
} |
330 |
ResultSet rs=null; |
331 |
try{
|
332 |
this.open();
|
333 |
Statement st=this.getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); |
334 |
String sql = this.getSqlSelectPart() + " WHERE "+ this.getFilterForID((DBFeatureType)this.getDefaultFeatureType(), featureKey); |
335 |
rs=st.executeQuery(sql); |
336 |
if(rs.next()){
|
337 |
return createFeatureFromResulset(rs, (DBFeatureType)featureType);
|
338 |
} |
339 |
|
340 |
} catch (java.sql.SQLException e) {
|
341 |
e.printStackTrace(); |
342 |
throw new ReadException(this.getName(), e); |
343 |
} finally{
|
344 |
if (rs != null){ |
345 |
try {
|
346 |
rs.close(); |
347 |
} catch (java.sql.SQLException e) {
|
348 |
throw new ReadException(this.getName(),e); |
349 |
} |
350 |
} |
351 |
} |
352 |
return null; |
353 |
} |
354 |
|
355 |
|
356 |
protected abstract Feature createFeatureFromResulset(ResultSet rs, DBFeatureType featureType2) throws ReadException; |
357 |
|
358 |
|
359 |
protected FeatureType checkFeatureTypeForCollection(FeatureType fType)
|
360 |
throws DataException {
|
361 |
if (!this.useSqlSource) { |
362 |
return super.checkFeatureTypeForCollection(fType); |
363 |
} |
364 |
FeatureType defType = this.getDefaultFeatureType();
|
365 |
if (fType == null || fType.equals(defType)) { |
366 |
return defType.getSubFeatureType(null); |
367 |
} |
368 |
if (!fType.isSubtypeOf(defType)) {
|
369 |
throw new ReadException("invalid type", this.getName()); |
370 |
} |
371 |
Iterator iterDef = defType.iterator();
|
372 |
Iterator iterType = fType.iterator();
|
373 |
|
374 |
AttributeDescriptor defAttr; |
375 |
AttributeDescriptor attr; |
376 |
|
377 |
while (iterDef.hasNext()) {
|
378 |
defAttr = (AttributeDescriptor) iterDef.next(); |
379 |
attr = (AttributeDescriptor) iterType.next(); |
380 |
if (!(
|
381 |
defAttr.getName().equals(attr.getName()) |
382 |
&& defAttr.getDataType().equals(attr.getDataType()) |
383 |
&& defAttr.getSize() == attr.getSize() |
384 |
&& defAttr.isReadOnly() == attr.isReadOnly() |
385 |
&& defAttr.isEvaluated() == attr.isEvaluated() |
386 |
)) { |
387 |
throw new ReadException("invalid type", this.getName()); |
388 |
} |
389 |
} |
390 |
while (iterType.hasNext()) {
|
391 |
attr = (AttributeDescriptor) iterType.next(); |
392 |
if (!attr.isEvaluated()) {
|
393 |
throw new ReadException("invalid type", this.getName()); |
394 |
} |
395 |
} |
396 |
|
397 |
return fType;
|
398 |
} |
399 |
|
400 |
} |