Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dataDB / src / org / gvsig / data / datastores / vectorial / db / jdbc / h2 / H2Store.java @ 20973

History | View | Annotate | Download (5.97 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.util.Locale;
7

    
8
import org.gvsig.data.CloseException;
9
import org.gvsig.data.DataManager;
10
import org.gvsig.data.IDataCollection;
11
import org.gvsig.data.IDataExplorer;
12
import org.gvsig.data.InitializeException;
13
import org.gvsig.data.OpenException;
14
import org.gvsig.data.ReadException;
15
import org.gvsig.data.datastores.vectorial.InitializeWriterException;
16
import org.gvsig.data.datastores.vectorial.db.DBFeatureType;
17
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCFeaturesWriter;
18
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCResource;
19
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCStore;
20
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCStoreParameters;
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.IFeatureType;
25
import org.gvsig.metadata.IMetadata;
26

    
27
public class H2Store extends JDBCStore{
28
        public static final String CONNECTION_STRING = "h2";
29
        public static String DATASTORE_NAME = "H2Store";
30

    
31

    
32
        protected static Locale ukLocale = new Locale("en", "UK"); // English, UK version
33
    public H2StoreParameters getParametersH2(){
34
                return (H2StoreParameters)this.parameters;
35
        }
36

    
37

    
38
        /* (non-Javadoc)
39
         * @see org.gvsig.data.datastores.vectorial.db.jdbc.JDBCStore#createResource(org.gvsig.data.datastores.vectorial.file.dbf.DBFStoreParameters)
40
         */
41
        protected JDBCResource createResource(JDBCStoreParameters params) {
42
                return new H2Resource((H2StoreParameters)params);
43
        }
44

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

    
61
                } else{
62
                        for (int i=0;i<fieldsID.length;i++){
63
                                if (this.getDefaultFeatureType().getFieldIndex(fieldsID[i]) < 0) {
64

    
65
                                throw new InitializeException("Field id '"+ fieldsID[i] +"' (id pos "+ (i+1) +") not Found",
66
                                                this.getName());
67

    
68
                                }
69
                        }
70
                }
71

    
72

    
73
        }
74

    
75

    
76
        public IDataCollection getDataCollection(IFeatureType type, String filter, String order) throws ReadException {
77
                if (useSqlSource ){
78
                        if (filter != null || order != null){
79
                                throw new ReadException("Unsuported filter/order in sqlSource mode",this.getName());
80
                        }
81
                }
82
//                if (type!=null && type != this.getDefaultFeatureType()){
83
//                        throw new UnsupportedOperationException("Type != null");
84
//                }else{
85
                        type = this.getDefaultFeatureType();
86
//                }
87

    
88
                IFeatureCollection coll;
89
                if (featureManager == null){
90
                        coll=new H2FeatureCollection(this,(DBFeatureType)type,filter,order);
91
                }else{
92
                        if ((order != null && order != "")){
93
                                coll=new H2FeatureCollectionWithFeatureID(featureManager,this,type,filter,order);
94
                        } else{
95
                                if (filter == null || filter == ""){
96
                                        coll=new H2FeatureCollectionEditing(featureManager,this,type);
97
                                } else {
98
                                        coll=new H2FeatureCollectionEditingFiltered(featureManager,this,type,filter);
99
                                }
100
                        }
101

    
102

    
103
                }
104

    
105
                this.addObserver(new WeakReference(coll));
106
                return coll;
107
        }
108

    
109
        protected Connection getConnection() throws ReadException{
110
                return ((H2Resource)this.resource).getConnection();
111

    
112
        }
113

    
114
        protected void initSqlProperties() throws InitializeException{
115
                H2StoreParameters dParams = (H2StoreParameters)this.getParameters();
116
                if (dParams.getSqlSoure() != null){
117
                        this.sqlSource = dParams.getSqlSoure();
118
                        this.useSqlSource = true;
119
                        this.sqlSelectPart = null;
120
                        this.baseWhereClause = null;
121
                        this.baseOrder = null;
122
                } else {
123
                        this.sqlSelectPart = "SELECT " + dParams.getFieldsString() +" FROM "+ dParams.tableID();
124

    
125
                        this.baseWhereClause = dParams.getBaseFilter();
126

    
127
                        if (dParams.getWorkingArea() != null){
128
                                String waWhere = getWorkingAreaWhere(dParams.getWorkingArea(), dParams.getSRISD());
129
                                if (waWhere != null){
130
                                        this.baseWhereClause = "(("+this.baseWhereClause+") and "+waWhere +")";
131
                                }
132

    
133
                        }
134

    
135
                        this.baseOrder = dParams.getBaseOrder();
136
                }
137
        }
138
        private String getWorkingAreaWhere(IExtent r, String strEPSG) {
139
                //TODO????
140
                if (r==null){
141
                        return null;
142
                }
143
                return null;
144
        }
145

    
146

    
147
        public boolean canAlterFeatureType() {
148
                return true;
149
        }
150

    
151
        protected void doDispose() throws CloseException {
152
                super.doDispose();
153
        }
154

    
155
        public String getName() {
156
                return DATASTORE_NAME;
157
        }
158

    
159
        protected JDBCFeaturesWriter getFeaturesWriter() throws InitializeWriterException {
160
                H2FeaturesWriter writer = new H2FeaturesWriter();
161
                writer.init(this);
162
                return writer;
163
        }
164

    
165
        public IDataExplorer getExplorer() throws ReadException {
166
                DataManager dm = DataManager.getManager();
167
                H2ExplorerParameters explorerParams = (H2ExplorerParameters)dm.createDataExplorerParameters(H2Explorer.DATAEXPLORER_NAME);
168
                explorerParams.loadFromStoreParameters(this.getParametersH2());
169
                return dm.createDataExplorer(explorerParams);
170
        }
171

    
172
        public IMetadata getMetadata() {
173
                if (metadata==null){
174
                        IMetadata tmp=super.getMetadata();
175

    
176
                        return tmp;
177
                }else{
178
                        return super.getMetadata();
179
                }
180

    
181
        }
182

    
183

    
184
        protected IFeature createFeatureFromResulset(ResultSet rs, DBFeatureType featureType2) throws ReadException{
185
                return new H2Feature(featureType2,this,rs);
186
        }
187

    
188

    
189
        /* (non-Javadoc)
190
         * @see org.gvsig.data.vectorial.FeatureStore#doRefresh()
191
         */
192
        protected void doRefresh() throws OpenException, InitializeException {
193
                this.initFeatureType();
194
                this.initSqlProperties();
195
        }
196

    
197
}