Statistics
| Revision:

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

History | View | Annotate | Download (8.3 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.ArrayList;
7
import java.util.Iterator;
8
import java.util.Locale;
9

    
10
import org.gvsig.fmap.data.DataSet;
11
import org.gvsig.fmap.data.DataExplorer;
12
import org.gvsig.fmap.data.DataManager;
13
import org.gvsig.fmap.data.exceptions.CloseException;
14
import org.gvsig.fmap.data.exceptions.DataException;
15
import org.gvsig.fmap.data.exceptions.InitializeException;
16
import org.gvsig.fmap.data.exceptions.OpenException;
17
import org.gvsig.fmap.data.exceptions.ReadException;
18
import org.gvsig.fmap.data.feature.Feature;
19
import org.gvsig.fmap.data.feature.FeatureSet;
20
import org.gvsig.fmap.data.feature.FeatureType;
21
import org.gvsig.fmap.data.feature.db.DBFeatureType;
22
import org.gvsig.fmap.data.feature.db.DBResource;
23
import org.gvsig.fmap.data.feature.db.jdbc.JDBCFeaturesWriter;
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.data.feature.exceptions.InitializeWriterException;
27
import org.gvsig.fmap.data.impl.DefaultDataManager;
28
import org.gvsig.fmap.geom.Geometry;
29
import org.gvsig.fmap.geom.primitive.DefaultEnvelope;
30
import org.gvsig.fmap.geom.primitive.Envelope;
31
import org.gvsig.metadata.Metadata;
32
import org.gvsig.metadata.MetadataManager;
33
import org.gvsig.metadata.DefaultMetadataManager;
34
import org.gvsig.tools.exception.BaseException;
35

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

    
40

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

    
46

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

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

    
70
                } else{
71
                        for (int i=0;i<fieldsID.length;i++){
72
                                if (this.getDefaultFeatureType().getIndex(fieldsID[i]) < 0) {
73

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

    
77
                                }
78
                        }
79
                }
80

    
81
                this.featureTypes = new ArrayList();
82
                this.featureTypes.add(defaultFeatureType);
83

    
84
        }
85

    
86

    
87
        public DataSet getDataCollection(FeatureType type, String filter, String order) throws ReadException {
88
                try {
89
                        type = this.checkFeatureTypeForCollection(type);
90
                } catch (DataException e) {
91
                        throw new ReadException(this.getName(), e);
92
                }
93

    
94
                if (useSqlSource ){
95
                        if (filter != null || order != null){
96
                                throw new ReadException("Unsuported filter/order in sqlSource mode",this.getName());
97
                        }
98
                }
99

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

    
114

    
115
                }
116

    
117
                this.addObserver(new WeakReference(coll));
118
                return coll;
119
        }
120

    
121
        protected Connection getConnection() throws ReadException{
122
                return ((H2Resource)this.resource).getConnection();
123

    
124
        }
125

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

    
137
                        this.baseWhereClause = dParams.getBaseFilter();
138

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

    
145
                        }
146

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

    
158

    
159
        public boolean canAlterFeatureType() {
160
                return true;
161
        }
162

    
163
        protected void doDispose() throws CloseException {
164
                super.doDispose();
165
        }
166

    
167
        public String getName() {
168
                return DATASTORE_NAME;
169
        }
170

    
171
        protected JDBCFeaturesWriter getFeaturesWriter() throws InitializeWriterException {
172
                H2FeaturesWriter writer = new H2FeaturesWriter();
173
                writer.init(this);
174
                return writer;
175
        }
176

    
177
        public DataExplorer getExplorer() throws ReadException {
178
                DataManager dm = DefaultDataManager.getManager();
179
                H2ExplorerParameters explorerParams = (H2ExplorerParameters)dm.createExplorerParameters(H2Explorer.DATAEXPLORER_NAME);
180
                explorerParams.putAllDefaultValues(this.getParametersH2());
181
                return dm.createExplorer(explorerParams);
182
        }
183

    
184
        public Metadata getMetadata() throws BaseException{
185

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

    
214

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

    
225
        }
226

    
227

    
228
        private Envelope getFullExtent() throws ReadException {
229
                Envelope result = new DefaultEnvelope(2);
230

    
231
                String geomField = this.getParametersH2().getDefaultGeometryField();
232
                FeatureSet coll = (FeatureSet) this.getDataCollection(new String[] {geomField},null,null);
233
                Iterator iter = coll.iterator();
234
                Feature feature;
235
                Geometry geom;
236

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

    
244
                }
245

    
246
                geom = null;
247
                feature = null;
248
                iter = null;
249
                coll.dispose();
250

    
251
                return result;
252
        }
253

    
254

    
255
        protected Feature createFeatureFromResulset(ResultSet rs, DBFeatureType featureType2) throws ReadException{
256
                return new H2Feature(featureType2,this,rs);
257
        }
258

    
259

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

    
268

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

    
278

    
279
        public Feature getByIndex(long index) throws ReadException {
280
                // TODO Auto-generated method stub
281
                return null;
282
        }
283

    
284
}