Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dataDB / src / org / gvsig / fmap / data / feature / db / jdbc / h2 / H2Store.java @ 23303

History | View | Annotate | Download (8.12 KB)

1
package org.gvsig.fmap.data.feature.db.jdbc.h2;
2

    
3
import java.lang.ref.WeakReference;
4
import java.sql.Connection;
5
import java.sql.ResultSet;
6
import java.util.Iterator;
7
import java.util.Locale;
8

    
9
import org.gvsig.fmap.data.CloseException;
10
import org.gvsig.fmap.data.DataCollection;
11
import org.gvsig.fmap.data.DataException;
12
import org.gvsig.fmap.data.DataExplorer;
13
import org.gvsig.fmap.data.DataManager;
14
import org.gvsig.fmap.data.InitializeException;
15
import org.gvsig.fmap.data.OpenException;
16
import org.gvsig.fmap.data.ReadException;
17
import org.gvsig.fmap.data.feature.Feature;
18
import org.gvsig.fmap.data.feature.FeatureCollection;
19
import org.gvsig.fmap.data.feature.FeatureType;
20
import org.gvsig.fmap.data.feature.InitializeWriterException;
21
import org.gvsig.fmap.data.feature.db.DBFeatureType;
22
import org.gvsig.fmap.data.feature.db.jdbc.JDBCFeaturesWriter;
23
import org.gvsig.fmap.data.feature.db.jdbc.JDBCResource;
24
import org.gvsig.fmap.data.feature.db.jdbc.JDBCStore;
25
import org.gvsig.fmap.data.feature.db.jdbc.JDBCStoreParameters;
26
import org.gvsig.fmap.geom.Geometry;
27
import org.gvsig.fmap.geom.primitive.DefaultEnvelope;
28
import org.gvsig.fmap.geom.primitive.Envelope;
29
import org.gvsig.metadata.IMetadata;
30
import org.gvsig.metadata.IMetadataManager;
31
import org.gvsig.metadata.MetadataManager;
32
import org.gvsig.tools.exception.BaseException;
33

    
34
public class H2Store extends JDBCStore{
35
        public static final String CONNECTION_STRING = "h2";
36
        public static String DATASTORE_NAME = "H2Store";
37

    
38

    
39
        protected static Locale ukLocale = new Locale("en", "UK"); // English, UK version
40
    public H2StoreParameters getParametersH2(){
41
                return (H2StoreParameters)this.parameters;
42
        }
43

    
44

    
45
        /* (non-Javadoc)
46
         * @see org.gvsig.fmap.data.feature.db.jdbc.JDBCStore#createResource(org.gvsig.fmap.data.feature.file.dbf.DBFStoreParameters)
47
         */
48
        protected JDBCResource createResource(JDBCStoreParameters params) {
49
                return new H2Resource(params);
50
        }
51

    
52
        protected void initFeatureType() throws InitializeException{
53
                H2StoreParameters dParams = this.getParametersH2();
54
                try {
55
                        this.defaultFeatureType = H2Utils.getFeatureType(this.getConnection(), dParams);
56
                } catch (ReadException e) {
57
                        throw new InitializeException(DATASTORE_NAME,e);
58
                }
59
                String[] fieldsID = dParams.getFieldsId();
60
                if (fieldsID == null || fieldsID.length < 1){
61
                        throw new InitializeException("Field Id not set",
62
                                        this.getName());
63
                } else if (fieldsID.length > 1){
64
                        //TODO: Falta por implementar soporte para multiples ID
65
                        throw new InitializeException("Multy fields id not supported yet",
66
                                        this.getName());
67

    
68
                } else{
69
                        for (int i=0;i<fieldsID.length;i++){
70
                                if (this.getDefaultFeatureType().getFieldIndex(fieldsID[i]) < 0) {
71

    
72
                                throw new InitializeException("Field id '"+ fieldsID[i] +"' (id pos "+ (i+1) +") not Found",
73
                                                this.getName());
74

    
75
                                }
76
                        }
77
                }
78

    
79

    
80
        }
81

    
82

    
83
        public DataCollection getDataCollection(FeatureType type, String filter, String order) throws ReadException {
84
                try {
85
                        type = this.checkFeatureTypeForCollection(type);
86
                } catch (DataException e) {
87
                        throw new ReadException(this.getName(), e);
88
                }
89

    
90
                if (useSqlSource ){
91
                        if (filter != null || order != null){
92
                                throw new ReadException("Unsuported filter/order in sqlSource mode",this.getName());
93
                        }
94
                }
95

    
96
                FeatureCollection coll;
97
                if (featureManager == null){
98
                        coll=new H2FeatureCollection(this,(DBFeatureType)type,filter,order);
99
                }else{
100
                        if ((order != null && order != "")){
101
                                coll=new H2FeatureCollectionWithFeatureID(featureManager,this,type,filter,order);
102
                        } else{
103
                                if (filter == null || filter == ""){
104
                                        coll=new H2FeatureCollectionEditing(featureManager,this,type);
105
                                } else {
106
                                        coll=new H2FeatureCollectionEditingFiltered(featureManager,this,type,filter);
107
                                }
108
                        }
109

    
110

    
111
                }
112

    
113
                this.addObserver(new WeakReference(coll));
114
                return coll;
115
        }
116

    
117
        protected Connection getConnection() throws ReadException{
118
                return ((H2Resource)this.resource).getConnection();
119

    
120
        }
121

    
122
        protected void initSqlProperties() throws InitializeException{
123
                H2StoreParameters dParams = (H2StoreParameters)this.getParameters();
124
                if (dParams.getSqlSoure() != null){
125
                        this.sqlSource = dParams.getSqlSoure();
126
                        this.useSqlSource = true;
127
                        this.sqlSelectPart = null;
128
                        this.baseWhereClause = null;
129
                        this.baseOrder = null;
130
                } else {
131
                        this.sqlSelectPart = "SELECT " + dParams.getFieldsString() +" FROM "+ dParams.tableID();
132

    
133
                        this.baseWhereClause = dParams.getBaseFilter();
134

    
135
                        if (dParams.getWorkingArea() != null){
136
                                String waWhere = getWorkingAreaWhere(dParams.getWorkingArea(), dParams.getSRISD());
137
                                if (waWhere != null){
138
                                        this.baseWhereClause = "(("+this.baseWhereClause+") and "+waWhere +")";
139
                                }
140

    
141
                        }
142

    
143
                        this.baseOrder = dParams.getBaseOrder();
144
                }
145
        }
146
        private String getWorkingAreaWhere(Envelope r, String strEPSG) {
147
                //TODO????
148
                if (r==null){
149
                        return null;
150
                }
151
                return null;
152
        }
153

    
154

    
155
        public boolean canAlterFeatureType() {
156
                return true;
157
        }
158

    
159
        protected void doDispose() throws CloseException {
160
                super.doDispose();
161
        }
162

    
163
        public String getName() {
164
                return DATASTORE_NAME;
165
        }
166

    
167
        protected JDBCFeaturesWriter getFeaturesWriter() throws InitializeWriterException {
168
                H2FeaturesWriter writer = new H2FeaturesWriter();
169
                writer.init(this);
170
                return writer;
171
        }
172

    
173
        public DataExplorer getExplorer() throws ReadException {
174
                DataManager dm = DataManager.getManager();
175
                H2ExplorerParameters explorerParams = (H2ExplorerParameters)dm.createDataExplorerParameters(H2Explorer.DATAEXPLORER_NAME);
176
                explorerParams.loadFromStoreParameters(this.getParametersH2());
177
                return dm.createDataExplorer(explorerParams);
178
        }
179

    
180
        public IMetadata getMetadata() throws BaseException{
181

    
182
                if (metadata==null){
183
                        IMetadataManager manager=MetadataManager.getManager();
184
                        metadata=manager.create(DATASTORE_NAME);
185
                        Envelope extent=this.getFullExtent();
186
                        metadata.set("extent",extent);
187
//                        String srs=this.getSRS();
188
//                        metadata.set("srs",srs);
189
                }
190
                if (this.alterMode){
191
                        Envelope extent=(Envelope)metadata.get("extent");
192
                        FeatureCollection featureCollection=(FeatureCollection)getDataCollection();
193
                        if (spatialManager.isFullExtentDirty()){
194
                                if (!featureCollection.isEmpty()){
195
                                        Iterator featureIterator=featureCollection.iterator();
196
                                        extent = ((Feature)featureIterator.next()).getExtent();
197
                                        while(featureIterator.hasNext()){
198
                                                Feature feature=(Feature)featureIterator.next();
199
                                                Envelope boundExtent=feature.getExtent();
200
                                                if (boundExtent!=null) {
201
                                                        extent.add(boundExtent);
202
                                                }
203
                                        }
204
                                }
205
                        }
206
                        metadata.set("extent",extent);
207
                }
208
                return metadata;
209

    
210

    
211
//
212
//
213
//                if (metadata==null){
214
//                        IMetadata tmp=super.getMetadata();
215
//
216
//                        return tmp;
217
//                }else{
218
//                        return super.getMetadata();
219
//                }
220

    
221
        }
222

    
223

    
224
        private Envelope getFullExtent() throws ReadException {
225
                Envelope result = new DefaultEnvelope(2);
226

    
227
                String geomField = this.getParametersH2().getDefaultGeometryField();
228
                FeatureCollection coll = (FeatureCollection) this.getDataCollection(new String[] {geomField},null,null);
229
                Iterator iter = coll.iterator();
230
                Feature feature;
231
                Geometry geom;
232

    
233
                while (iter.hasNext()) {
234
                        feature = (Feature) iter.next();
235
                        geom = (Geometry) feature.getDefaultGeometry();
236
                        if (geom != null) {
237
                                result.add(geom.getEnvelope());
238
                        }
239

    
240
                }
241

    
242
                geom = null;
243
                feature = null;
244
                iter = null;
245
                coll.dispose();
246

    
247
                return result;
248
        }
249

    
250

    
251
        protected Feature createFeatureFromResulset(ResultSet rs, DBFeatureType featureType2) throws ReadException{
252
                return new H2Feature(featureType2,this,rs);
253
        }
254

    
255

    
256
        /* (non-Javadoc)
257
         * @see org.gvsig.fmap.data.feature.FeatureStore#doRefresh()
258
         */
259
        protected void doRefresh() throws OpenException, InitializeException {
260
                this.initFeatureType();
261
                this.initSqlProperties();
262
        }
263

    
264

    
265
        public boolean canWriteGeometry(int gvSIGgeometryType) {
266
                try {
267
                        return getFeaturesWriter().canWriteGeometry(gvSIGgeometryType);
268
                } catch (InitializeWriterException e) {
269
                        e.printStackTrace();
270
                }
271
                return false;
272
        }
273

    
274

    
275
        public Feature getByIndex(long index) throws ReadException {
276
                // TODO Auto-generated method stub
277
                return null;
278
        }
279

    
280
}