Statistics
| Revision:

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

History | View | Annotate | Download (5.6 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

    
7
import org.gvsig.data.CloseException;
8
import org.gvsig.data.IDataCollection;
9
import org.gvsig.data.IDataExplorer;
10
import org.gvsig.data.InitializeException;
11
import org.gvsig.data.OpenException;
12
import org.gvsig.data.ReadException;
13
import org.gvsig.data.datastores.vectorial.InitializeWriterException;
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.JDBCResource;
17
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCStore;
18
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCStoreParameters;
19
import org.gvsig.data.spatialprovisional.IExtent;
20
import org.gvsig.data.vectorial.IFeature;
21
import org.gvsig.data.vectorial.IFeatureCollection;
22
import org.gvsig.data.vectorial.IFeatureType;
23
import org.gvsig.metadata.IMetadata;
24

    
25
public class PostgresqlStore extends JDBCStore{
26
        public static final String CONNECTION_STRING = "postgresql";
27
        public static String DATASTORE_NAME = "PostgresqlStore";
28
    PostgresqlStoreParameters getParametersPostgresql(){
29
                return (PostgresqlStoreParameters)this.parameters;
30
        }
31

    
32
        /* (non-Javadoc)
33
         * @see org.gvsig.data.datastores.vectorial.db.jdbc.JDBCStore#createResource(org.gvsig.data.datastores.vectorial.file.dbf.DBFStoreParameters)
34
         */
35
        protected JDBCResource createResource(JDBCStoreParameters params) {
36
                return new PostgresqlResource((PostgresqlStoreParameters)params);
37
        }
38

    
39

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

    
58
                } else{
59
                        for (int i=0;i<fieldsID.length;i++){
60
                                if (this.getDefaultFeatureType().getFieldIndex(fieldsID[i]) < 0) {
61

    
62
                                throw new InitializeException(
63
                                                DATASTORE_NAME,
64
                                                new Exception("Field id '"+ fieldsID[i] +"' (id pos "+ (i+1) +") not Found"));
65

    
66
                                }
67
                        }
68
                }
69

    
70

    
71
        }
72

    
73
        public IDataCollection getDataCollection(IFeatureType type, String filter, String order) throws ReadException {
74
                if (useSqlSource ){
75
                        if (filter != null || order != null){
76
                                throw new ReadException(DATASTORE_NAME,
77
                                                new UnsupportedOperationException("Unsuported filter/order in sqlSource mode"));
78
                        }
79
                }
80
                if (type==null){
81
                        type=getDefaultFeatureType();
82
                }
83
                IFeatureCollection coll=null;
84
                if (featureManager == null){
85
                        coll=new PostgresqlFeatureCollection(this,type,filter,order);
86
                }else{
87
                        if ((order != null && order != "")){
88
                                coll=new PostgresqlFeatureCollectionWithFeatureID(featureManager,this,type,filter,order);
89
                        } else{
90
                                if (filter == null || filter == ""){
91
                                        coll=new PostgresqlFeatureCollectionEditing(featureManager,this,type);
92
                                } else {
93
                                        coll=new PostgresqlFeatureCollectionEditingFiltered(featureManager,this,type,filter);
94
                                }
95
                        }
96

    
97

    
98
                }
99
                this.addObserver(new WeakReference(coll));
100

    
101
                return coll;
102
        }
103

    
104
        protected Connection getConnection() throws ReadException{
105
                return super.getConnection();
106

    
107
        }
108

    
109

    
110
        protected void initSqlProperties() throws InitializeException{
111
                PostgresqlStoreParameters dParams = (PostgresqlStoreParameters)this.getParameters();
112
                if (dParams.getSqlSoure() != null){
113
                        this.sqlSource = dParams.getSqlSoure();
114
                        this.useSqlSource = true;
115
                        this.sqlSelectPart = null;
116
                        this.baseWhereClause = null;
117
                        this.baseOrder = null;
118
                } else {
119
                        this.sqlSelectPart = "SELECT " + dParams.getFieldsString() +" FROM "+ dParams.tableID();
120

    
121
                        this.baseWhereClause = dParams.getBaseFilter();
122

    
123
                        if (dParams.getWorkingArea() != null){
124
                                String waWhere = getWorkingAreaWhere(dParams.getWorkingArea(), dParams.getSRISD());
125
                                if (waWhere != null){
126
                                        this.baseWhereClause = "(("+this.baseWhereClause+") and "+waWhere +")";
127
                                }
128

    
129
                        }
130

    
131
                        this.baseOrder = dParams.getBaseOrder();
132
                }
133
        }
134
        private String getWorkingAreaWhere(IExtent r, String strEPSG) {
135
                //TODO????
136
                if (r==null){
137
                        return null;
138
                }
139
                return null;
140
        }
141

    
142

    
143
        public boolean canAlterFeatureType() {
144
                return true;
145
        }
146

    
147

    
148
        protected void doDispose() throws CloseException{
149
                super.doDispose();
150
        }
151

    
152

    
153
        public String getName() {
154
                return DATASTORE_NAME;
155
        }
156

    
157

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

    
164
        public IDataExplorer getExplorer() {
165
                // TODO Auto-generated method stub
166
                return null;
167
        }
168

    
169
        public IMetadata getMetadata() {
170
                if (metadata==null){
171
                        IMetadata tmp=super.getMetadata();
172

    
173
                        return tmp;
174
                }else{
175
                        return super.getMetadata();
176
                }
177

    
178
        }
179

    
180
        protected IFeature createFeatureFromResulset(ResultSet rs, DBFeatureType featureType2) throws ReadException {
181
                if (featureType2== null){
182
                        return new PostgresqlFeature(this.getDefaultFeatureType(),this,rs);
183
                }else{
184
                        return new PostgresqlFeature(featureType2,this,rs);
185
                }
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

    
198
}