Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dataDB / src / org / gvsig / data / datastores / vectorial / db / jdbc / JDBCStore.java @ 20973

History | View | Annotate | Download (10.2 KB)

1
package org.gvsig.data.datastores.vectorial.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.data.CloseException;
12
import org.gvsig.data.DataException;
13
import org.gvsig.data.IDataExplorer;
14
import org.gvsig.data.IDataStoreParameters;
15
import org.gvsig.data.InitializeException;
16
import org.gvsig.data.OpenException;
17
import org.gvsig.data.ReadException;
18
import org.gvsig.data.Resource;
19
import org.gvsig.data.ResourceManager;
20
import org.gvsig.data.WriteException;
21
import org.gvsig.data.commands.implementation.AttributeCommand;
22
import org.gvsig.data.commands.implementation.FeatureCommand;
23
import org.gvsig.data.commands.implementation.UpdateAttributeCommand;
24
import org.gvsig.data.commands.implementation.UpdateFeatureCommand;
25
import org.gvsig.data.datastores.vectorial.InitializeWriterException;
26
import org.gvsig.data.datastores.vectorial.db.DBFeatureID;
27
import org.gvsig.data.datastores.vectorial.db.DBFeatureType;
28
import org.gvsig.data.datastores.vectorial.db.DBStore;
29
import org.gvsig.data.datastores.vectorial.file.dbf.DBFStoreParameters;
30
import org.gvsig.data.vectorial.CreatedFeature;
31
import org.gvsig.data.vectorial.IFeature;
32
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor;
33
import org.gvsig.data.vectorial.IFeatureID;
34
import org.gvsig.data.vectorial.IFeatureType;
35
import org.gvsig.metadata.IMetadata;
36
import org.gvsig.metadata.IMetadataManager;
37
import org.gvsig.metadata.MetadataManager;
38

    
39
public abstract class JDBCStore extends DBStore {
40

    
41
        protected JDBCResource resource = null;
42
        protected String sqlSelectPart;
43
        protected String baseWhereClause = null;
44
        protected String baseOrder = null;
45
        protected String sqlSource = null;
46
        protected boolean useSqlSource = false;
47
        protected IMetadata metadata;
48

    
49

    
50
        public void init(IDataStoreParameters parameters) throws InitializeException {
51

    
52
                JDBCResource tmpResource = this.createResource((JDBCStoreParameters)parameters);
53
                ResourceManager resMan = ResourceManager.getResourceManager();
54

    
55
                try {
56
                        this.resource = (JDBCResource)resMan.addResource(tmpResource);
57
                } catch (DataException e1) {
58
                        throw new InitializeException(this.getName(),e1);
59
                }
60

    
61
                super.init(parameters,this.resource);
62

    
63

    
64
                this.initFeatureType();
65
                this.initSqlProperties();
66
        }
67

    
68

    
69
        protected abstract JDBCResource createResource(JDBCStoreParameters params);
70
        protected abstract void initFeatureType() throws InitializeException;
71
        protected abstract void initSqlProperties() throws InitializeException;
72

    
73

    
74
        /* (non-Javadoc)
75
         * @see org.gvsig.data.vectorial.FeatureStore#doClose()
76
         */
77
        protected void doClose() throws CloseException {
78
                this.resource.close();
79

    
80
        }
81

    
82
        /* (non-Javadoc)
83
         * @see org.gvsig.data.vectorial.FeatureStore#doDispose()
84
         */
85
        protected void doDispose() throws CloseException {
86
                ResourceManager resMan = ResourceManager.getResourceManager();
87

    
88
            try {
89
                        resMan.remove(this.resource);
90
                } catch (DataException e1) {
91
                        throw new CloseException(this.getName(),e1);
92
                } catch (KeyException e) {
93
                        // TODO Auto-generated catch block
94
                        throw new CloseException(this.getName(),e);
95
                }
96
                this.metadata = null;
97
                super.doDispose();
98
        }
99

    
100
        /* (non-Javadoc)
101
         * @see org.gvsig.data.vectorial.FeatureStore#doOpen()
102
         */
103
        protected void doOpen() throws OpenException {
104
                //No Operation
105

    
106
        }
107

    
108

    
109
        /* (non-Javadoc)
110
         * @see org.gvsig.data.vectorial.IFeatureStore#canAlterFeatureType()
111
         */
112
        public boolean canAlterFeatureType() {
113
                return false;
114
        }
115

    
116
        /* (non-Javadoc)
117
         * @see org.gvsig.data.IDataStore#getExplorer()
118
         */
119
        public IDataExplorer getExplorer() throws ReadException {
120
                return null;
121
        }
122

    
123

    
124
        /* (non-Javadoc)
125
         * @see org.gvsig.data.vectorial.FeatureStore#init(org.gvsig.data.IDataStoreParameters, org.gvsig.data.Resource)
126
         */
127
        public void init(IDataStoreParameters parameters, Resource resource) throws InitializeException {
128
                super.init(parameters, resource);
129
                this.resource=(JDBCResource)resource;
130
        }
131

    
132
        protected Connection getConnection() throws ReadException{
133
                return this.resource.getConnection();
134
        }
135

    
136
        protected Connection getWriterConnection() throws ReadException{
137
                return this.resource.getWriterConnection();
138
        }
139

    
140
        public boolean isEditable() {
141
                return super.isEditable() && !this.useSqlSource;
142
        }
143

    
144

    
145
        public String getSqlSelectPart() {
146
                return sqlSelectPart;
147
        }
148

    
149
        public String getBaseOrder() {
150
                return this.baseOrder;
151
        }
152

    
153
        public String getBaseWhereClause() {
154
                return baseWhereClause;
155
        }
156

    
157
        public boolean isUseSqlSource() {
158
                return this.useSqlSource;
159
        }
160

    
161
        public String getSqlSource() {
162
                return this.sqlSource;
163
        }
164

    
165
        protected abstract JDBCFeaturesWriter getFeaturesWriter() throws InitializeWriterException;
166

    
167
        protected void doFinishEdition() throws WriteException, ReadException {
168
                Iterator commandsfeatures = null;
169
            JDBCFeaturesWriter selectiveWriter = getFeaturesWriter();
170
            IFeatureType type = getDefaultFeatureType();
171
            boolean needRefresh=false;
172

    
173
                IFeature feature;
174
                Object obj;
175
                IFeatureID featureId;
176

    
177

    
178
                selectiveWriter.updateFeatureType(type);
179
                selectiveWriter.preProcess();
180

    
181
                try{
182
                    commandsfeatures = commands.getCommandsAttributeDeleted().iterator();
183
                        while( commandsfeatures.hasNext() ) {
184
                                obj=commandsfeatures.next();
185
                                if (obj instanceof AttributeCommand){
186
                                        IFeatureAttributeDescriptor attribute = ((AttributeCommand)obj).getAttributeDescriptor();
187
                                        selectiveWriter.deleteAttribute(attribute);
188
                                        needRefresh=true;
189
                                }
190
                        }
191
                        commandsfeatures = commands.getCommandsAttributeUpdated().iterator();
192
                        while( commandsfeatures.hasNext() ) {
193
                                obj=commandsfeatures.next();
194
                                if (obj instanceof AttributeCommand){
195
                                        IFeatureAttributeDescriptor oldAttribute = ((UpdateAttributeCommand)obj).getOldAttributeDescriptor();
196
                                        IFeatureAttributeDescriptor attribute = ((UpdateAttributeCommand)obj).getAttributeDescriptor();
197
                                        selectiveWriter.updateAttribute(oldAttribute,attribute);
198
                                        needRefresh=true;
199
                                }
200
                        }
201
                        commandsfeatures = commands.getCommandsAttributeInserted().iterator();
202
                        while( commandsfeatures.hasNext() ) {
203
                                obj=commandsfeatures.next();
204
                                if (obj instanceof AttributeCommand){
205
                                        IFeatureAttributeDescriptor attribute = ((AttributeCommand)obj).getAttributeDescriptor();
206
                                        selectiveWriter.insertAttribute(attribute);
207
                                        needRefresh=true;
208
                                }
209
                        }
210

    
211

    
212
                        commandsfeatures = commands.getCommandsFeatureDeleted().iterator();
213
                        while( commandsfeatures.hasNext() ) {
214
                                obj=commandsfeatures.next();
215
                                if (obj instanceof FeatureCommand){
216
                                        feature = ((FeatureCommand)obj).getFeature();
217
                                        if (feature instanceof CreatedFeature)
218
                                                continue;
219

    
220
                                        selectiveWriter.deleteFeature(feature);
221
                                }
222
                        }
223

    
224
                        commandsfeatures = commands.getCommandsFeatureInserted().iterator();
225
                        while( commandsfeatures.hasNext() ) {
226
                                obj=commandsfeatures.next();
227
                                if (obj instanceof FeatureCommand){
228
                                        feature = ((FeatureCommand)obj).getFeature();
229
                                        if (featureManager.isDeleted(feature)){
230
                                                continue;
231
                                        }
232
                                        selectiveWriter.insertFeature(feature);
233
                                }
234
                        }
235

    
236
                        commandsfeatures = commands.getCommandsFeatureUpdated().iterator();
237
                        HashMap toUpdate = new HashMap();
238
                        while( commandsfeatures.hasNext() ) {
239
                                obj=commandsfeatures.next();
240
                                if (obj instanceof FeatureCommand){
241
//                                        IFeature oldFeature = ((UpdateFeatureCommand)obj).getOldFeature();
242
                                        feature = ((UpdateFeatureCommand)obj).getFeature();
243
                                        featureId =feature.getID();
244
                                        feature = featureManager.getFeature(featureId, this);
245
                                        if (feature != null){
246
                                                toUpdate.put(featureId, feature);
247
                                        }
248
                                }
249
                        }
250
                        Iterator toUpdateIter = toUpdate.values().iterator();
251
                        while (toUpdateIter.hasNext()){
252
                                feature = (IFeature) toUpdateIter.next();
253
                                selectiveWriter.updateFeature(feature);
254
                        }
255

    
256

    
257
                        selectiveWriter.postProcess();
258
                        this.resource.changed(this);
259
                        if (needRefresh){
260
                                this.refresh();
261
                        }
262

    
263

    
264
                }catch (ReadException e) {
265
                        selectiveWriter.cancelProcess();
266
                        throw e;
267
                } catch (WriteException e){
268
                        selectiveWriter.cancelProcess();
269
                        throw e;
270
                } catch (Exception e){
271
                        selectiveWriter.cancelProcess();
272
                        throw new WriteException(this.getName(),e);
273
                }
274
        }
275

    
276
        public String getFilterForID(DBFeatureType fType, Object[] featureKey) {
277
                if (fType.getFieldsId().length != 1)
278
                        throw new UnsupportedOperationException("ID fields > 1");
279
                String id =fType.getFieldsId()[0];
280
                return id + " = " + objectToSqlString(featureKey[0]);
281
        }
282

    
283
        protected String objectToSqlString(Object obj) {
284
                if (obj instanceof String){
285
                        return "'"+ scapeString((String)obj) +"'";
286
                } else if (obj == null){
287
                        return "null";
288
                }else{
289
                        // OJO con otros tipos!!
290
                        return obj.toString();
291
                }
292

    
293
        }
294

    
295
        protected String scapeString(String str) {
296
                return str.replace("'", "''");
297
        }
298

    
299
        public IFeature getFeatureByID(IFeatureID id) throws ReadException{
300
                return getFeatureByID((DBFeatureType)this.getDefaultFeatureType(),((DBFeatureID)id).getKey());
301
        }
302

    
303
        public IMetadata getMetadata() {
304
                if (metadata==null){
305
                        IMetadataManager manager=MetadataManager.getManager();
306
                        metadata=manager.create(this.getName());
307
                        //TODO: Apadir los meteadatos
308
                }
309
                return metadata;
310
        }
311

    
312

    
313
        public boolean isWithDefaultLegend() {
314
                return false;
315
        }
316

    
317
        public Object getDefaultLabelingStrategy() {
318
                return null;
319
        }
320

    
321
        public Object getDefaultLegend() {
322
                return null;
323
        }
324

    
325
        public IFeature getFeatureByID(IFeatureType featureType2, Object[] featureKey) throws ReadException{
326
                //TODO: Tener en cuenta el FeatureType por si es distinto
327
                if (useSqlSource){
328
                        throw new ReadException("Unsuported featureByID in sqlSource mode",this.getName());
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)this.getDefaultFeatureType());
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 IFeature createFeatureFromResulset(ResultSet rs, DBFeatureType featureType2) throws ReadException;
357
}