root / trunk / libraries / libFMap_dataDB / src / org / gvsig / data / datastores / vectorial / db / jdbc / postgresql / PostgresqlStore.java @ 20776
History | View | Annotate | Download (6.55 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.ResourceManager; |
13 |
import org.gvsig.data.datastores.vectorial.db.DBFeatureType; |
14 |
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCAttributeDescriptor; |
15 |
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCFeature; |
16 |
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCFeaturesWriter; |
17 |
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCStore; |
18 |
import org.gvsig.data.datastores.vectorial.db.jdbc.h2.H2Resource; |
19 |
import org.gvsig.data.datastores.vectorial.db.jdbc.h2.H2StoreParameters; |
20 |
import org.gvsig.data.exception.CloseException; |
21 |
import org.gvsig.data.exception.DataException; |
22 |
import org.gvsig.data.exception.InitializeException; |
23 |
import org.gvsig.data.exception.InitializeWriterException; |
24 |
import org.gvsig.data.exception.OpenException; |
25 |
import org.gvsig.data.exception.ReadException; |
26 |
import org.gvsig.data.spatialprovisional.IExtent; |
27 |
import org.gvsig.data.vectorial.IFeature; |
28 |
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor; |
29 |
import org.gvsig.data.vectorial.IFeatureCollection; |
30 |
import org.gvsig.data.vectorial.IFeatureType; |
31 |
import org.gvsig.data.vectorial.IsNotFeatureSettingException; |
32 |
import org.gvsig.metadata.IMetadata; |
33 |
import org.postgis.PGgeometry; |
34 |
|
35 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
36 |
|
37 |
public class PostgresqlStore extends JDBCStore{ |
38 |
public static final String CONNECTION_STRING = "postgresql"; |
39 |
public static String DATASTORE_NAME = "PostgresqlStore"; |
40 |
protected static Locale ukLocale = new Locale("en", "UK"); // English, UK version |
41 |
PostgresqlStoreParameters getParametersPostgresql(){ |
42 |
return (PostgresqlStoreParameters)this.parameters; |
43 |
} |
44 |
|
45 |
|
46 |
public void init(IDataStoreParameters parameters) throws InitializeException { |
47 |
// super.init(parameters);
|
48 |
//
|
49 |
//
|
50 |
//
|
51 |
// this.initConnection();
|
52 |
// this.initFeatureType();
|
53 |
// this.initSqlProperties();
|
54 |
|
55 |
|
56 |
PostgresqlResource tmpResource = new PostgresqlResource((PostgresqlStoreParameters)parameters);
|
57 |
PostgresqlResource theResource; |
58 |
ResourceManager resMan = ResourceManager.getResourceManager(); |
59 |
|
60 |
try {
|
61 |
theResource = (PostgresqlResource)resMan.addResource(tmpResource); |
62 |
} catch (DataException e1) {
|
63 |
throw new InitializeException(this.getName(),e1); |
64 |
} |
65 |
|
66 |
super.init(parameters,theResource);
|
67 |
|
68 |
|
69 |
this.initFeatureType();
|
70 |
this.initSqlProperties();
|
71 |
|
72 |
} |
73 |
|
74 |
private void initFeatureType() throws InitializeException{ |
75 |
PostgresqlStoreParameters dParams = this.getParametersPostgresql();
|
76 |
try {
|
77 |
this.featureType = PostgresqlStoreUtils.getFeatureType(this.getConnection(), dParams); |
78 |
} catch (ReadException e) {
|
79 |
throw new InitializeException(DATASTORE_NAME,e); |
80 |
} |
81 |
String[] fieldsID = dParams.getFieldsId(); |
82 |
if (fieldsID == null || fieldsID.length < 1){ |
83 |
throw new InitializeException( |
84 |
DATASTORE_NAME, |
85 |
new Exception("Field Id not set")); |
86 |
} else if (fieldsID.length > 1){ |
87 |
//TODO: Falta por implementar soporte para multiples ID
|
88 |
throw new InitializeException( |
89 |
DATASTORE_NAME, |
90 |
new Exception("Multy fields id not supported yet")); |
91 |
|
92 |
} else{
|
93 |
for (int i=0;i<fieldsID.length;i++){ |
94 |
if (this.featureType.getFieldIndex(fieldsID[i]) < 0) { |
95 |
|
96 |
throw new InitializeException( |
97 |
DATASTORE_NAME, |
98 |
new Exception("Field id '"+ fieldsID[i] +"' (id pos "+ (i+1) +") not Found")); |
99 |
|
100 |
} |
101 |
} |
102 |
} |
103 |
|
104 |
|
105 |
} |
106 |
|
107 |
public IDataCollection getDataCollection(IFeatureType type, String filter, String order) throws ReadException { |
108 |
if (useSqlSource ){
|
109 |
if (filter != null || order != null){ |
110 |
throw new ReadException(DATASTORE_NAME, |
111 |
new UnsupportedOperationException("Unsuported filter/order in sqlSource mode")); |
112 |
} |
113 |
} |
114 |
if (type==null){ |
115 |
type=getDefaultFeatureType(); |
116 |
} |
117 |
IFeatureCollection coll=null;
|
118 |
if (featureManager == null){ |
119 |
coll=new PostgresqlFeatureCollection(this,type,filter,order); |
120 |
}else{
|
121 |
if ((order != null && order != "")){ |
122 |
coll=new PostgresqlFeatureCollectionWithFeatureID(featureManager,this,type,filter,order); |
123 |
} else{
|
124 |
if (filter == null || filter == ""){ |
125 |
coll=new PostgresqlFeatureCollectionEditing(featureManager,this,type); |
126 |
} else {
|
127 |
coll=new PostgresqlFeatureCollectionEditingFiltered(featureManager,this,type,filter); |
128 |
} |
129 |
} |
130 |
|
131 |
|
132 |
} |
133 |
this.addObserver(new WeakReference(coll)); |
134 |
|
135 |
return coll;
|
136 |
} |
137 |
|
138 |
protected Connection getConnection() throws ReadException{ |
139 |
return super.getConnection(); |
140 |
|
141 |
} |
142 |
|
143 |
|
144 |
private void initSqlProperties() throws InitializeException{ |
145 |
PostgresqlStoreParameters dParams = (PostgresqlStoreParameters)this.getParameters();
|
146 |
if (dParams.getSqlSoure() != null){ |
147 |
this.sqlSource = dParams.getSqlSoure();
|
148 |
this.useSqlSource = true; |
149 |
this.sqlSelectPart = null; |
150 |
this.baseWhereClause = null; |
151 |
this.baseOrder = null; |
152 |
} else {
|
153 |
this.sqlSelectPart = "SELECT " + dParams.getFieldsString() +" FROM "+ dParams.tableID(); |
154 |
|
155 |
this.baseWhereClause = dParams.getBaseFilter();
|
156 |
|
157 |
if (dParams.getWorkingArea() != null){ |
158 |
String waWhere = getWorkingAreaWhere(dParams.getWorkingArea(), dParams.getSRISD());
|
159 |
if (waWhere != null){ |
160 |
this.baseWhereClause = "(("+this.baseWhereClause+") and "+waWhere +")"; |
161 |
} |
162 |
|
163 |
} |
164 |
|
165 |
this.baseOrder = dParams.getBaseOrder();
|
166 |
} |
167 |
} |
168 |
private String getWorkingAreaWhere(IExtent r, String strEPSG) { |
169 |
//TODO????
|
170 |
if (r==null){ |
171 |
return null; |
172 |
} |
173 |
return null; |
174 |
} |
175 |
|
176 |
|
177 |
public boolean canAlterFeatureType() { |
178 |
return true; |
179 |
} |
180 |
|
181 |
protected void doOpen() throws OpenException { |
182 |
// FIXME: Resource Manager
|
183 |
} |
184 |
|
185 |
|
186 |
protected void doDispose() throws CloseException{ |
187 |
super.doDispose();
|
188 |
// FIXME: Resource Manager
|
189 |
|
190 |
} |
191 |
|
192 |
|
193 |
public String getName() { |
194 |
return DATASTORE_NAME;
|
195 |
} |
196 |
|
197 |
|
198 |
protected JDBCFeaturesWriter getFeaturesWriter() throws InitializeWriterException { |
199 |
JDBCFeaturesWriter writer = new PostgresqlFeaturesWriter();
|
200 |
writer.init(this);
|
201 |
return writer;
|
202 |
} |
203 |
|
204 |
public IDataExplorer getExplorer() {
|
205 |
// TODO Auto-generated method stub
|
206 |
return null; |
207 |
} |
208 |
|
209 |
public IMetadata getMetadata() {
|
210 |
if (metadata==null){ |
211 |
IMetadata tmp=super.getMetadata();
|
212 |
|
213 |
return tmp;
|
214 |
}else{
|
215 |
return super.getMetadata(); |
216 |
} |
217 |
|
218 |
} |
219 |
|
220 |
protected IFeature createFeatureFromResulset(ResultSet rs, DBFeatureType featureType2) throws ReadException { |
221 |
return new PostgresqlFeature(featureType,this,rs); |
222 |
} |
223 |
|
224 |
|
225 |
/* (non-Javadoc)
|
226 |
* @see org.gvsig.data.vectorial.FeatureStore#doRefresh()
|
227 |
*/
|
228 |
protected void doRefresh() throws OpenException, InitializeException { |
229 |
this.initFeatureType();
|
230 |
this.initSqlProperties();
|
231 |
} |
232 |
|
233 |
|
234 |
} |