Statistics
| Revision:

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

History | View | Annotate | Download (6.94 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.SQLException;
7
import java.util.Locale;
8

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

    
30
import com.iver.cit.gvsig.fmap.core.IGeometry;
31

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

    
36

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

    
42

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

    
46

    
47

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

    
52

    
53

    
54

    
55

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

    
60
        }
61

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

    
78
                } else{
79
                        for (int i=0;i<fieldsID.length;i++){
80
                                if (this.featureType.getFieldIndex(fieldsID[i]) < 0) {
81

    
82
                                throw new InitializeException("Field id '"+ fieldsID[i] +"' (id pos "+ (i+1) +") not Found",
83
                                                this.getName());
84

    
85
                                }
86
                        }
87
                }
88

    
89

    
90
        }
91

    
92

    
93
        private void initConnection() throws InitializeException{
94
                H2StoreParameters dParams = this.getParametersH2();
95

    
96
                String dburl = dParams.getUrl();
97
                String dbuser = dParams.getUser();
98
                String dbpass = dParams.getPassw();
99

    
100
                this.connection = H2Utils.getConnection(dburl, dbuser, dbpass);
101

    
102
        }
103

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

    
116
                IFeatureCollection coll;
117
                if (featureManager == null){
118
                        coll=new H2FeatureCollection(this,(DBFeatureType)type,filter,order);
119
                }else{
120
                        if ((order != null && order != "")){
121
                                coll=new H2FeatureCollectionWithFeatureID(featureManager,this,type,filter,order);
122
                        } else{
123
                                if (filter == null || filter == ""){
124
                                        coll=new H2FeatureCollectionEditing(featureManager,this,type);
125
                                } else {
126
                                        coll=new H2FeatureCollectionEditingFiltered(featureManager,this,type,filter);
127
                                }
128
                        }
129

    
130

    
131
                }
132

    
133
                this.addObserver(new WeakReference(coll));
134
                return coll;
135
        }
136

    
137
        protected Connection getConnection(){
138
                // FIXME: OJO REsource manager
139
                return this.connection;
140

    
141
        }
142

    
143
        Connection getCurrentConnection(){
144
                return this.getConnection();
145
        }
146

    
147
        private void initSqlProperties() throws InitializeException{
148
                H2StoreParameters dParams = (H2StoreParameters)this.getParameters();
149
                if (dParams.getSqlSoure() != null){
150
                        this.sqlSource = dParams.getSqlSoure();
151
                        this.useSqlSource = true;
152
                        this.sqlSelectPart = null;
153
                        this.baseWhereClause = null;
154
                        this.baseOrder = null;
155
                } else {
156
                        this.sqlSelectPart = "SELECT " + dParams.getFieldsString() +" FROM "+ dParams.tableID();
157

    
158
                        this.baseWhereClause = dParams.getBaseFilter();
159

    
160
                        if (dParams.getWorkingArea() != null){
161
                                String waWhere = getWorkingAreaWhere(dParams.getWorkingArea(), dParams.getSRISD());
162
                                if (waWhere != null){
163
                                        this.baseWhereClause = "(("+this.baseWhereClause+") and "+waWhere +")";
164
                                }
165

    
166
                        }
167

    
168
                        this.baseOrder = dParams.getBaseOrder();
169
                }
170
        }
171
        private String getWorkingAreaWhere(IExtent r, String strEPSG) {
172
                //TODO????
173
                if (r==null){
174
                        return null;
175
                }
176
                return null;
177
        }
178

    
179

    
180
        public boolean canAlterFeatureType() {
181
                return true;
182
        }
183

    
184
        protected void doOpen() throws OpenException {
185
                // FIXME: Resource Manager
186

    
187

    
188
        }
189

    
190
        protected void doClose() throws CloseException {
191
                // FIXME: Resource Manager
192
                try {
193
                        connection.close();
194
                } catch (java.sql.SQLException e) {
195
                        throw new CloseException("H2",e);
196
                }
197
        }
198

    
199
        protected void doDispose() throws CloseException {
200
                super.doDispose();
201
                //TODO: FALTA!!!
202

    
203
        }
204

    
205
        public String getName() {
206
                return DATASTORE_NAME;
207
        }
208

    
209
        protected JDBCFeaturesWriter getFeaturesWriter() throws InitializeWriterException {
210
                H2FeaturesWriter writer = new H2FeaturesWriter();
211
                writer.init(this);
212
                return writer;
213
        }
214

    
215
        public IDataExplorer getExplorer() throws ReadException {
216
                DataManager dm = DataManager.getManager();
217
                H2ExplorerParameters explorerParams = (H2ExplorerParameters)dm.createDataExplorerParameters(H2Explorer.DATAEXPLORER_NAME);
218
                explorerParams.loadFromStoreParameters(this.getParametersH2());
219
                return dm.createDataExplorer(explorerParams);
220
        }
221

    
222
        public IMetadata getMetadata() {
223
                if (metadata==null){
224
                        IMetadata tmp=super.getMetadata();
225

    
226
                        return tmp;
227
                }else{
228
                        return super.getMetadata();
229
                }
230

    
231
        }
232

    
233
        static String getConnectionResourceID(String dbUrl,String dbUser){
234
                return H2Store.CONNECTION_STRING+";"+dbUrl+";"+dbUser;
235

    
236
        }
237

    
238

    
239
        protected IFeature createFeatureFromResulset(ResultSet rs, DBFeatureType featureType2) throws ReadException{
240
                return new H2Feature(featureType2,this,rs);
241
        }
242

    
243

    
244
        /* (non-Javadoc)
245
         * @see org.gvsig.data.vectorial.FeatureStore#doRefresh()
246
         */
247
        protected void doRefresh() throws OpenException, InitializeException {
248
                this.initFeatureType();
249
                this.initSqlProperties();
250
        }
251

    
252
}