svn-gvsig-desktop / trunk / libraries / libDataSourceDBBaseDrivers / src / org / gvsig / data / datastores / vectorial / db / jdbc / postgresql / PostgresqlStore.java @ 20376
History | View | Annotate | Download (6.96 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.util.Iterator; |
7 |
import java.util.Locale; |
8 |
|
9 |
import org.gvsig.data.IDataCollection; |
10 |
import org.gvsig.data.IDataExplorer; |
11 |
import org.gvsig.data.IDataStoreParameters; |
12 |
import org.gvsig.data.datastores.vectorial.db.DBFeatureType; |
13 |
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCAttributeDescriptor; |
14 |
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCFeature; |
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.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.metadata.IMetadata; |
28 |
import org.postgis.PGgeometry; |
29 |
|
30 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
31 |
|
32 |
public class PostgresqlStore extends JDBCStore{ |
33 |
public static final String CONNECTION_STRING = "postgresql"; |
34 |
public static String DATASTORE_NAME = "PostgresqlStore"; |
35 |
protected static Locale ukLocale = new Locale("en", "UK"); // English, UK version |
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 |
protected Connection getConnection(){ |
137 |
// FIXME: OJO REsource manager
|
138 |
return this.connection; |
139 |
|
140 |
} |
141 |
|
142 |
Connection getCurrentConnection(){
|
143 |
return this.getConnection(); |
144 |
} |
145 |
|
146 |
private void initSqlProperties() throws InitializeException{ |
147 |
PostgresqlStoreParameters dParams = (PostgresqlStoreParameters)this.getParameters();
|
148 |
if (dParams.getSqlSoure() != null){ |
149 |
this.sqlSource = dParams.getSqlSoure();
|
150 |
this.useSqlSource = true; |
151 |
this.sqlSelectPart = null; |
152 |
this.baseWhereClause = null; |
153 |
this.baseOrder = null; |
154 |
} else {
|
155 |
this.sqlSelectPart = "SELECT " + dParams.getFieldsString() +" FROM "+ dParams.tableID(); |
156 |
|
157 |
this.baseWhereClause = dParams.getBaseFilter();
|
158 |
|
159 |
if (dParams.getWorkingArea() != null){ |
160 |
String waWhere = getWorkingAreaWhere(dParams.getWorkingArea(), dParams.getSRISD());
|
161 |
if (waWhere != null){ |
162 |
this.baseWhereClause = "(("+this.baseWhereClause+") and "+waWhere +")"; |
163 |
} |
164 |
|
165 |
} |
166 |
|
167 |
this.baseOrder = dParams.getBaseOrder();
|
168 |
} |
169 |
} |
170 |
private String getWorkingAreaWhere(IExtent r, String strEPSG) { |
171 |
//TODO????
|
172 |
if (r==null){ |
173 |
return null; |
174 |
} |
175 |
return null; |
176 |
} |
177 |
|
178 |
|
179 |
public boolean canAlterFeatureType() { |
180 |
return true; |
181 |
} |
182 |
|
183 |
public void open() throws OpenException { |
184 |
// FIXME: Resource Manager
|
185 |
} |
186 |
|
187 |
public void close() throws CloseException { |
188 |
// FIXME: Resource Manager
|
189 |
try {
|
190 |
connection.close(); |
191 |
} catch (java.sql.SQLException e) {
|
192 |
throw new CloseException("H2",e); |
193 |
} |
194 |
} |
195 |
|
196 |
public void dispose() { |
197 |
// FIXME: Resource Manager
|
198 |
|
199 |
} |
200 |
|
201 |
|
202 |
public String getName() { |
203 |
return DATASTORE_NAME;
|
204 |
} |
205 |
|
206 |
|
207 |
protected JDBCFeaturesWriter getFeaturesWriter() throws InitializeWriterException { |
208 |
JDBCFeaturesWriter writer = new PostgresqlFeaturesWriter();
|
209 |
writer.init(this);
|
210 |
return writer;
|
211 |
} |
212 |
|
213 |
public IDataExplorer getExplorer() {
|
214 |
// TODO Auto-generated method stub
|
215 |
return null; |
216 |
} |
217 |
|
218 |
public IMetadata getMetadata() {
|
219 |
if (metadata==null){ |
220 |
IMetadata tmp=super.getMetadata();
|
221 |
|
222 |
return tmp;
|
223 |
}else{
|
224 |
return super.getMetadata(); |
225 |
} |
226 |
|
227 |
} |
228 |
|
229 |
|
230 |
protected void loadValueFromResulset(ResultSet rs, IFeature feature, IFeatureAttributeDescriptor attr) throws ReadException { |
231 |
IGeometry geom = null;
|
232 |
String name = attr.getName();
|
233 |
try {
|
234 |
if (attr.getDataType().equals(IFeatureAttributeDescriptor.TYPE_GEOMETRY)) {
|
235 |
|
236 |
PGgeometry data =(PGgeometry)rs.getObject(name); |
237 |
if (data == null) { |
238 |
geom = null;
|
239 |
} else{
|
240 |
geom = PostGIS2Geometry.getGeneralPath(data); |
241 |
} |
242 |
feature.setGeometry(name,geom); |
243 |
} else {
|
244 |
feature.set(name, rs.getObject(name)); |
245 |
} |
246 |
} catch (java.sql.SQLException e) {
|
247 |
throw new ReadException("CreateFeature",e); |
248 |
} |
249 |
} |
250 |
|
251 |
|
252 |
protected IFeature newFeatureInstance(DBFeatureType featureType, Object[] pk) { |
253 |
return new PostgresqlFeature(featureType,this,pk); |
254 |
} |
255 |
|
256 |
|
257 |
IFeature createFeatureFromResulset(ResultSet rs, DBFeatureType featureType2) throws ReadException { |
258 |
return this.createFeature(rs, featureType2); |
259 |
} |
260 |
|
261 |
|
262 |
} |