Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libDataSourceDBBaseDrivers / src / org / gvsig / data / datastores / vectorial / db / jdbc / postgresqlbin / PostgresqlBinStore.java @ 20029

History | View | Annotate | Download (5.13 KB)

1
package org.gvsig.data.datastores.vectorial.db.jdbc.postgresqlbin;
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.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.postgresql.PostgresqlFeatureID;
17
import org.gvsig.data.datastores.vectorial.db.jdbc.postgresql.PostgresqlStore;
18
import org.gvsig.data.datastores.vectorial.db.jdbc.postgresql.PostgresqlStoreParameters;
19
import org.gvsig.data.datastores.vectorial.db.jdbc.postgresql.PostgresqlStoreUtils;
20
import org.gvsig.data.exception.CloseException;
21
import org.gvsig.data.exception.InitializeException;
22
import org.gvsig.data.exception.OpenException;
23
import org.gvsig.data.exception.ReadException;
24
import org.gvsig.data.spatialprovisional.IExtent;
25
import org.gvsig.data.vectorial.IFeature;
26
import org.gvsig.data.vectorial.IFeatureCollection;
27
import org.gvsig.data.vectorial.IFeatureID;
28
import org.gvsig.data.vectorial.IFeatureType;
29
import org.gvsig.metadata.IMetadata;
30
import org.gvsig.metadata.IMetadataManager;
31
import org.gvsig.metadata.MetadataManager;
32

    
33
public class PostgresqlBinStore extends PostgresqlStore{
34
        public static final String CONNECTION_STRING = "postgresql";
35
        public static String DATASTORE_NAME = "PostgresqlStore";
36
        protected static Locale ukLocale = new Locale("en", "UK"); // English, UK version
37
    private DBFeatureType featureType;
38
        protected IMetadata metadata;
39

    
40
        PostgresqlBinStoreParameters getParametersPostgresql(){
41
                return (PostgresqlBinStoreParameters)this.parameters;
42
        }
43

    
44
        public String getName() {
45
                return DATASTORE_NAME;
46
        }
47

    
48
        protected void initConnection() throws InitializeException{
49
                PostgresqlStoreParameters dParams = this.getParametersPostgresql();
50

    
51
                String dburl = dParams.getUrl();
52
                String dbuser = dParams.getUser();
53
                String dbpass = dParams.getPassw();
54

    
55
                this.connection = PostgresqlBinStoreUtils.getConnection(dburl, dbuser, dbpass);
56

    
57
        }
58

    
59

    
60
        public IDataCollection getDataCollection(IFeatureType type, String filter, String order) throws ReadException {
61
                if (useSqlSource ){
62
                        if (filter != null || order != null){
63
                                throw new ReadException(DATASTORE_NAME,
64
                                                new UnsupportedOperationException("Unsuported filter/order in sqlSource mode"));
65
                        }
66
                }
67
                if (type==null){
68
                        type=getDefaultFeatureType();
69
                }
70
                IFeatureCollection coll=null;
71
                if (featureManager == null){
72
                        coll=new PostgresqlBinFeatureCollection(this,type,filter,order);
73
                }else{
74
//                        if ((order != null && order != "")){
75
//                                coll=new H2FeatureCollectionWithFeatureID(featureManager,this,type,filter,order);
76
//                        } else{
77
//                                if (filter == null || filter == ""){
78
//                                        coll=new H2FeatureCollectionEditing(featureManager,this,type);
79
//                                } else {
80
//                                        coll=new H2FeatureCollectionEditingFiltered(featureManager,this,type,filter);
81
//                                }
82
//                        }
83
//
84

    
85
                }
86
                this.addObserver(new WeakReference(coll));
87

    
88
                return coll;
89
        }
90

    
91

    
92
        public IFeature getFeatureByID(IFeatureType featureType2, Object[] featureKey) throws ReadException{
93
                if (useSqlSource){
94
                        throw new ReadException(this.getName(),
95
                                        new UnsupportedOperationException("Unsuported featureByID in sqlSource mode"));
96
                }
97
                ResultSet rs=null;
98
                try{
99
                        this.open();
100
                        Statement st=this.getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
101
                        String sql = this.getSqlSelectPart() + " WHERE "+ PostgresqlBinStoreUtils.getFliterForID(this.featureType, featureKey);
102
                        rs=st.executeQuery(sql);
103
                        if (rs.isLast()) {
104

    
105
                                return null;
106
                        }else{
107
                                if(rs.next()){
108
                                        return PostgresqlBinStoreUtils.createFeature(this, rs, this.featureType);
109
                                }
110

    
111
                        }
112

    
113
                } catch (java.sql.SQLException e) {
114
                        e.printStackTrace();
115
                        throw new ReadException(this.getName(), e);
116
                } finally{
117
                        if (rs != null)
118
                                try {
119
                                        rs.close();
120
                                } catch (java.sql.SQLException e) {
121
                                        // TODO ?????
122
                                        e.printStackTrace();
123
                                }
124
                }
125
                return null;
126
        }
127

    
128
        Connection getCurrentConnection(){
129
                return this.getConnection();
130
        }
131

    
132

    
133
        public boolean canAlterFeatureType() {
134
                return true;
135
        }
136

    
137
        public void open() throws OpenException {
138
                // FIXME: Resource Manager
139
        }
140

    
141
        public void close() throws CloseException {
142
                // FIXME: Resource Manager
143
                try {
144
                        connection.close();
145
                } catch (java.sql.SQLException e) {
146
                        throw new CloseException(this.getName(),e);
147
                }
148
        }
149

    
150
        public void dispose() {
151
                // FIXME: Resource Manager
152

    
153
        }
154

    
155

    
156
        public JDBCFeaturesWriter getFeaturesWriter() {
157
//                IFeaturesWriter writer = new H2FeaturesWriter();
158
//                writer.init(this);
159
//                return writer;
160
                return null;
161
        }
162

    
163
        public IMetadata getMetadata() {
164
                if (metadata==null){
165
                        IMetadataManager manager=MetadataManager.getManager();
166
                        metadata=manager.create(DATASTORE_NAME);
167
                        //TODO: Apadir los meteadatos
168
                }
169
                return metadata;
170
        }
171

    
172

    
173
        public IDataExplorer getExplorer() {
174
                // TODO Auto-generated method stub
175
                return null;
176
        }
177

    
178
        public boolean isEditable() {
179
                return false;
180

    
181
//                return super.isEditable();
182
        }
183

    
184

    
185
}