Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dataDB / src / org / gvsig / fmap / data / feature / db / jdbc / h2 / H2Store.java @ 22373

History | View | Annotate | Download (8.16 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.exceptions.BaseException;
10
import org.gvsig.fmap.data.CloseException;
11
import org.gvsig.fmap.data.DataCollection;
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

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

    
37

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

    
43

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

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

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

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

    
74
                                }
75
                        }
76
                }
77

    
78

    
79
        }
80

    
81

    
82
        public DataCollection getDataCollection(FeatureType type, String filter, String order) throws ReadException {
83
                if (type==null){
84
                        type=getDefaultFeatureType();
85
                } else {
86
                        if (!type.isSubtypeOf(this.getDefaultFeatureType())){
87
                                throw new ReadException("invalid type",this.getName());
88
                        }
89
                }
90

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

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

    
111

    
112
                }
113

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

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

    
121
        }
122

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

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

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

    
142
                        }
143

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

    
155

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

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

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

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

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

    
181
        public IMetadata getMetadata() throws BaseException{
182

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

    
211

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

    
222
        }
223

    
224

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

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

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

    
241
                }
242

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

    
248
                return result;
249
        }
250

    
251

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

    
256

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

    
265

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

    
275

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

    
281
}