Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libDataSourceDBBaseDrivers / src / org / gvsig / data / datastores / vectorial / db / jdbc / h2 / H2Store.java @ 20029

History | View | Annotate | Download (7.85 KB)

1
package org.gvsig.data.datastores.vectorial.db.jdbc.h2;
2

    
3
import java.lang.ref.WeakReference;
4
import java.sql.Connection;
5
import java.sql.ResultSet;
6
import java.sql.Statement;
7
import java.util.ArrayList;
8
import java.util.List;
9
import java.util.Locale;
10

    
11
import org.gvsig.data.IDataCollection;
12
import org.gvsig.data.IDataExplorer;
13
import org.gvsig.data.IDataStoreParameters;
14
import org.gvsig.data.datastores.vectorial.db.DBFeatureType;
15
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCFeaturesWriter;
16
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCStore;
17
import org.gvsig.data.exception.CloseException;
18
import org.gvsig.data.exception.InitializeException;
19
import org.gvsig.data.exception.OpenException;
20
import org.gvsig.data.exception.ReadException;
21
import org.gvsig.data.spatialprovisional.IExtent;
22
import org.gvsig.data.vectorial.IFeature;
23
import org.gvsig.data.vectorial.IFeatureCollection;
24
import org.gvsig.data.vectorial.IFeatureID;
25
import org.gvsig.data.vectorial.IFeatureType;
26
import org.gvsig.metadata.IMetadata;
27
import org.gvsig.metadata.IMetadataManager;
28
import org.gvsig.metadata.MetadataManager;
29

    
30
public class H2Store extends JDBCStore{
31
        public static final String CONNECTION_STRING = "h2";
32
        public static String DATASTORE_NAME = "H2Store";
33
        protected static Locale ukLocale = new Locale("en", "UK"); // English, UK version
34
    public DBFeatureType featureType;
35
        protected IMetadata metadata;
36

    
37

    
38

    
39
        public H2StoreParameters getParametersH2(){
40
                return (H2StoreParameters)this.parameters;
41
        }
42

    
43

    
44
        public void init(IDataStoreParameters parameters) throws InitializeException {
45
                super.init(parameters);
46

    
47

    
48

    
49
                this.initConnection();
50
                this.initFeatureType();
51
                this.initSqlProperties();
52

    
53

    
54

    
55

    
56

    
57
                        //writer.setCreateTable(false);
58
                        //writer.setWriteAll(false);
59
                        //writer.initialize(lyrDef);
60

    
61
        }
62

    
63
        private void initFeatureType() throws InitializeException{
64
                H2StoreParameters dParams = this.getParametersH2();
65
                try {
66
                        this.featureType = H2Utils.getFeatureType(this.connection, dParams);
67
                } catch (ReadException e) {
68
                        throw new InitializeException(DATASTORE_NAME,e);
69
                }
70
                String[] fieldsID = dParams.getFieldsId();
71
                if (fieldsID == null || fieldsID.length < 1){
72
                        throw new InitializeException(
73
                                        DATASTORE_NAME,
74
                                        new Exception("Field Id not set"));
75
                } else if (fieldsID.length > 1){
76
                        //TODO: Falta por implementar soporte para multiples ID
77
                        throw new InitializeException(
78
                                        DATASTORE_NAME,
79
                                        new Exception("Multy fields id not supported yet"));
80

    
81
                } else{
82
                        for (int i=0;i<fieldsID.length;i++){
83
                                if (this.featureType.getFieldIndex(fieldsID[i]) < 0) {
84

    
85
                                throw new InitializeException(
86
                                                DATASTORE_NAME,
87
                                                new Exception("Field id '"+ fieldsID[i] +"' (id pos "+ (i+1) +") not Found"));
88

    
89
                                }
90
                        }
91
                }
92

    
93

    
94
        }
95

    
96

    
97
        private void initConnection() throws InitializeException{
98
                H2StoreParameters dParams = this.getParametersH2();
99

    
100
                String dburl = dParams.getUrl();
101
                String dbuser = dParams.getUser();
102
                String dbpass = dParams.getPassw();
103

    
104
                this.connection = H2Utils.getConnection(dburl, dbuser, dbpass);
105

    
106
        }
107

    
108
        public IDataCollection getDataCollection(IFeatureType type, String filter, String order) throws ReadException {
109
                if (useSqlSource ){
110
                        if (filter != null || order != null){
111
                                throw new ReadException(DATASTORE_NAME,
112
                                                new UnsupportedOperationException("Unsuported filter/order in sqlSource mode"));
113
                        }
114
                }
115
                if (type!=null && type != this.getDefaultFeatureType()){
116
                        throw new UnsupportedOperationException("Type != null");
117
                }else{
118
                        type = this.getDefaultFeatureType();
119
                }
120

    
121
                IFeatureCollection coll;
122
                if (featureManager == null){
123
                        coll=new H2FeatureCollection(this,type,filter,order);
124
                }else{
125
                        if ((order != null && order != "")){
126
                                coll=new H2FeatureCollectionWithFeatureID(featureManager,this,type,filter,order);
127
                        } else{
128
                                if (filter == null || filter == ""){
129
                                        coll=new H2FeatureCollectionEditing(featureManager,this,type);
130
                                } else {
131
                                        coll=new H2FeatureCollectionEditingFiltered(featureManager,this,type,filter);
132
                                }
133
                        }
134

    
135

    
136
                }
137

    
138
                this.addObserver(new WeakReference(coll));
139
                return coll;
140
        }
141

    
142
        public IFeature getFeatureByID(IFeatureID id) throws ReadException {
143
                return getFeatureByID(featureType,((H2FeatureID)id).getKey());
144
        }
145

    
146
        public IFeature getFeatureByID(IFeatureType featureType2, Object[] featureKey) throws ReadException {
147
                if (useSqlSource){
148
                        throw new ReadException(this.getName(),
149
                                        new UnsupportedOperationException("Unsuported featureByID in sqlSource mode"));
150
                }
151
                ResultSet rs=null;
152
                try{
153
                        this.open();
154
                        Statement st=this.getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
155
                        String sql = this.getSqlSelectPart() + " WHERE "+ H2Utils.getFilterForID(this.featureType, featureKey);
156
                        rs=st.executeQuery(sql);
157
                        if (rs.isLast()) {
158

    
159
                                return null;
160
                        }else{
161
                                if(rs.next()){
162
                                        return H2Utils.createFeature(this, rs, this.featureType);
163
                                }
164

    
165
                        }
166

    
167
                } catch (java.sql.SQLException e) {
168
                        e.printStackTrace();
169
                        throw new ReadException(DATASTORE_NAME, e);
170
                } finally{
171
                        if (rs != null)
172
                                try {
173
                                        rs.close();
174
                                } catch (java.sql.SQLException e) {
175
                                        // TODO ?????
176
                                        e.printStackTrace();
177
                                }
178
                }
179
                return null;
180
        }
181

    
182
        protected Connection getConnection(){
183
                // FIXME: OJO REsource manager
184
                return this.connection;
185

    
186
        }
187

    
188
        Connection getCurrentConnection(){
189
                return this.getConnection();
190
        }
191

    
192
        private void initSqlProperties() throws InitializeException{
193
                H2StoreParameters dParams = (H2StoreParameters)this.getParameters();
194
                if (dParams.getSqlSoure() != null){
195
                        this.sqlSource = dParams.getSqlSoure();
196
                        this.useSqlSource = true;
197
                        this.sqlSelectPart = null;
198
                        this.baseWhereClause = null;
199
                        this.baseOrder = null;
200
                } else {
201
                        this.sqlSelectPart = "SELECT " + dParams.getFieldsString() +" FROM "+ dParams.tableID();
202

    
203
                        this.baseWhereClause = dParams.getBaseFilter();
204

    
205
                        if (dParams.getWorkingArea() != null){
206
                                String waWhere = getWorkingAreaWhere(dParams.getWorkingArea(), dParams.getSRISD());
207
                                if (waWhere != null){
208
                                        this.baseWhereClause = "(("+this.baseWhereClause+") and "+waWhere +")";
209
                                }
210

    
211
                        }
212

    
213
                        this.baseOrder = dParams.getBaseOrder();
214
                }
215
        }
216
        private String getWorkingAreaWhere(IExtent r, String strEPSG) {
217
                //TODO????
218
                if (r==null){
219
                        return null;
220
                }
221
                return null;
222
        }
223

    
224

    
225
        public boolean canAlterFeatureType() {
226
                return true;
227
        }
228

    
229
        public void open() throws OpenException {
230
                // FIXME: Resource Manager
231

    
232

    
233
        }
234

    
235
        public void close() throws CloseException {
236
                // FIXME: Resource Manager
237
                try {
238
                        connection.close();
239
                } catch (java.sql.SQLException e) {
240
                        throw new CloseException("H2",e);
241
                }
242
        }
243

    
244
        public void dispose() {
245
                // FIXME: Resource Manager
246

    
247

    
248
        }
249

    
250
        public String getName() {
251
                return DATASTORE_NAME;
252
        }
253

    
254
        protected JDBCFeaturesWriter getFeaturesWriter() {
255
                H2FeaturesWriter writer = new H2FeaturesWriter();
256
                writer.init(this);
257
                return writer;
258
        }
259

    
260
        public IMetadata getMetadata() {
261
                if (metadata==null){
262
                        IMetadataManager manager=MetadataManager.getManager();
263
                        metadata=manager.create(DATASTORE_NAME);
264
                        //TODO: Apadir los meteadatos
265
                }
266
                return metadata;
267
        }
268

    
269

    
270
        public IFeatureType getDefaultFeatureType() {
271
                // TODO Auto-generated method stub
272
                return this.featureType;
273
        }
274

    
275

    
276
        public Object getDefaultLabelingStrategy() {
277
                // TODO Auto-generated method stub
278
                return null;
279
        }
280

    
281

    
282
        public Object getDefaultLegend() {
283
                // TODO Auto-generated method stub
284
                return null;
285
        }
286

    
287

    
288
        public List getFeatureTypes() {
289
                ArrayList list = new ArrayList();
290
                list.add(this.featureType);
291
                return list;
292
        }
293

    
294

    
295
        public boolean isWithDefaultLegend() {
296
                // TODO Auto-generated method stub
297
                return false;
298
        }
299

    
300
        public IDataExplorer getExplorer() {
301
                // TODO Auto-generated method stub
302
                return null;
303
        }
304
}