Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libDataSourceDBBaseDrivers / src / org / gvsig / data / datastores / vectorial / db / jdbc / postgresql / PostgresqlStore.java @ 20014

History | View | Annotate | Download (7.64 KB)

1
package org.gvsig.data.datastores.vectorial.db.jdbc.postgresql;
2

    
3
import java.lang.ref.WeakReference;
4
import java.sql.Connection;
5
import java.sql.ResultSet;
6
import java.sql.Statement;
7
import java.util.ArrayList;
8
import java.util.List;
9
import java.util.Locale;
10

    
11
import org.gvsig.data.IDataCollection;
12
import org.gvsig.data.IDataExplorer;
13
import org.gvsig.data.IDataStoreParameters;
14
import org.gvsig.data.datastores.vectorial.db.DBFeatureType;
15
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCFeaturesWriter;
16
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCStore;
17
import org.gvsig.data.exception.CloseException;
18
import org.gvsig.data.exception.InitializeException;
19
import org.gvsig.data.exception.OpenException;
20
import org.gvsig.data.exception.ReadException;
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.IFeatureID;
25
import org.gvsig.data.vectorial.IFeatureType;
26
import org.gvsig.metadata.IMetadata;
27
import org.gvsig.metadata.IMetadataManager;
28
import org.gvsig.metadata.MetadataManager;
29

    
30
public class PostgresqlStore extends JDBCStore{
31
        public static final String CONNECTION_STRING = "postgresql";
32
        public static String DATASTORE_NAME = "PostgresqlStore";
33
        protected static Locale ukLocale = new Locale("en", "UK"); // English, UK version
34
    private DBFeatureType featureType;
35
        protected IMetadata metadata;
36
        PostgresqlStoreParameters getParametersPostgresql(){
37
                return (PostgresqlStoreParameters)this.parameters;
38
        }
39

    
40

    
41
        public void init(IDataStoreParameters parameters) throws InitializeException {
42
                super.init(parameters);
43

    
44

    
45

    
46
                this.initConnection();
47
                this.initFeatureType();
48
                this.initSqlProperties();
49

    
50

    
51

    
52

    
53

    
54
                        //writer.setCreateTable(false);
55
                        //writer.setWriteAll(false);
56
                        //writer.initialize(lyrDef);
57

    
58
        }
59

    
60
        private void initFeatureType() throws InitializeException{
61
                PostgresqlStoreParameters dParams = this.getParametersPostgresql();
62
                try {
63
                        this.featureType = PostgresqlStoreUtils.getFeatureType(this.connection, dParams);
64
                } catch (ReadException e) {
65
                        throw new InitializeException(DATASTORE_NAME,e);
66
                }
67
                String[] fieldsID = dParams.getFieldsId();
68
                if (fieldsID == null || fieldsID.length < 1){
69
                        throw new InitializeException(
70
                                        DATASTORE_NAME,
71
                                        new Exception("Field Id not set"));
72
                } else if (fieldsID.length > 1){
73
                        //TODO: Falta por implementar soporte para multiples ID
74
                        throw new InitializeException(
75
                                        DATASTORE_NAME,
76
                                        new Exception("Multy fields id not supported yet"));
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(
83
                                                DATASTORE_NAME,
84
                                                new Exception("Field id '"+ fieldsID[i] +"' (id pos "+ (i+1) +") not Found"));
85

    
86
                                }
87
                        }
88
                }
89

    
90

    
91
        }
92

    
93

    
94
        protected void initConnection() throws InitializeException{
95
                PostgresqlStoreParameters dParams = this.getParametersPostgresql();
96

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

    
101
                this.connection = PostgresqlStoreUtils.getConnection(dburl, dbuser, dbpass);
102

    
103
        }
104

    
105
        public IDataCollection getDataCollection(IFeatureType type, String filter, String order) throws ReadException {
106
                if (useSqlSource ){
107
                        if (filter != null || order != null){
108
                                throw new ReadException(DATASTORE_NAME,
109
                                                new UnsupportedOperationException("Unsuported filter/order in sqlSource mode"));
110
                        }
111
                }
112
                if (type==null){
113
                        type=getDefaultFeatureType();
114
                }
115
                IFeatureCollection coll=null;
116
                if (featureManager == null){
117
                        coll=new PostgresqlFeatureCollection(this,type,filter,order);
118
                }else{
119
                        if ((order != null && order != "")){
120
                                coll=new PostgresqlFeatureCollectionWithFeatureID(featureManager,this,type,filter,order);
121
                        } else{
122
                                if (filter == null || filter == ""){
123
                                        coll=new PostgresqlFeatureCollectionEditing(featureManager,this,type);
124
                                } else {
125
                                        coll=new PostgresqlFeatureCollectionEditingFiltered(featureManager,this,type,filter);
126
                                }
127
                        }
128

    
129

    
130
                }
131
                this.addObserver(new WeakReference(coll));
132

    
133
                return coll;
134
        }
135

    
136
        public IFeature getFeatureByID(IFeatureID id) throws ReadException {
137
                return getFeatureByID(featureType,((PostgresqlFeatureID)id).getKey());
138
        }
139
        public IFeature getFeatureByID(IFeatureType featureType2, Object[] featureKey) throws ReadException{
140
                if (useSqlSource){
141
                        throw new ReadException(this.getName(),
142
                                        new UnsupportedOperationException("Unsuported featureByID in sqlSource mode"));
143
                }
144
                ResultSet rs=null;
145
                try{
146
                        this.open();
147
                        Statement st=this.getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
148
                        String sql = this.getSqlSelectPart() + " WHERE "+ PostgresqlStoreUtils.getFliterForID(this.featureType, featureKey);
149
                        rs=st.executeQuery(sql);
150
                        if (rs.isLast()) {
151

    
152
                                return null;
153
                        }else{
154
                                if(rs.next()){
155
                                        return PostgresqlStoreUtils.createFeature(this, rs, this.featureType);
156
                                }
157

    
158
                        }
159

    
160
                } catch (java.sql.SQLException e) {
161
                        e.printStackTrace();
162
                        throw new ReadException(this.getName(), e);
163
                } finally{
164
                        if (rs != null)
165
                                try {
166
                                        rs.close();
167
                                } catch (java.sql.SQLException e) {
168
                                        // TODO ?????
169
                                        e.printStackTrace();
170
                                }
171
                }
172
                return null;
173
        }
174
        protected Connection getConnection(){
175
                // FIXME: OJO REsource manager
176
                return this.connection;
177

    
178
        }
179

    
180
        Connection getCurrentConnection(){
181
                return this.getConnection();
182
        }
183

    
184
        private void initSqlProperties() throws InitializeException{
185
                PostgresqlStoreParameters dParams = (PostgresqlStoreParameters)this.getParameters();
186
                if (dParams.getSqlSoure() != null){
187
                        this.sqlSource = dParams.getSqlSoure();
188
                        this.useSqlSource = true;
189
                        this.sqlSelectPart = null;
190
                        this.baseWhereClause = null;
191
                        this.baseOrder = null;
192
                } else {
193
                        this.sqlSelectPart = "SELECT " + dParams.getFieldsString() +" FROM "+ dParams.tableID();
194

    
195
                        this.baseWhereClause = dParams.getBaseFilter();
196

    
197
                        if (dParams.getWorkingArea() != null){
198
                                String waWhere = getWorkingAreaWhere(dParams.getWorkingArea(), dParams.getSRISD());
199
                                if (waWhere != null){
200
                                        this.baseWhereClause = "(("+this.baseWhereClause+") and "+waWhere +")";
201
                                }
202

    
203
                        }
204

    
205
                        this.baseOrder = dParams.getBaseOrder();
206
                }
207
        }
208
        private String getWorkingAreaWhere(IExtent r, String strEPSG) {
209
                //TODO????
210
                if (r==null){
211
                        return null;
212
                }
213
                return null;
214
        }
215

    
216

    
217
        public boolean canAlterFeatureType() {
218
                return true;
219
        }
220

    
221
        public void open() throws OpenException {
222
                // FIXME: Resource Manager
223
        }
224

    
225
        public void close() throws CloseException {
226
                // FIXME: Resource Manager
227
                try {
228
                        connection.close();
229
                } catch (java.sql.SQLException e) {
230
                        throw new CloseException("H2",e);
231
                }
232
        }
233

    
234
        public void dispose() {
235
                // FIXME: Resource Manager
236

    
237
        }
238

    
239

    
240
        public String getName() {
241
                return DATASTORE_NAME;
242
        }
243

    
244

    
245
        public JDBCFeaturesWriter getFeaturesWriter() {
246
//                IFeaturesWriter writer = new H2FeaturesWriter();
247
//                writer.init(this);
248
//                return writer;
249
                return null;
250
        }
251

    
252
        public IMetadata getMetadata() {
253
                if (metadata==null){
254
                        IMetadataManager manager=MetadataManager.getManager();
255
                        metadata=manager.create(DATASTORE_NAME);
256
                        //TODO: Apadir los meteadatos
257
                }
258
                return metadata;
259
        }
260

    
261

    
262
        public IFeatureType getDefaultFeatureType() {
263
                // TODO Auto-generated method stub
264
                return this.featureType;
265
        }
266

    
267

    
268
        public Object getDefaultLabelingStrategy() {
269
                // TODO Auto-generated method stub
270
                return null;
271
        }
272

    
273

    
274
        public Object getDefaultLegend() {
275
                // TODO Auto-generated method stub
276
                return null;
277
        }
278

    
279

    
280
        public List getFeatureTypes() {
281
                ArrayList list = new ArrayList();
282
                list.add(this.featureType);
283
                return list;
284
        }
285

    
286

    
287
        public boolean isWithDefaultLegend() {
288
                // TODO Auto-generated method stub
289
                return false;
290
        }
291

    
292

    
293
        public IDataExplorer getExplorer() {
294
                // TODO Auto-generated method stub
295
                return null;
296
        }
297

    
298
}