svn-gvsig-desktop / trunk / libraries / libDataSourceDBBaseDrivers / src / org / gvsig / data / datastores / vectorial / db / jdbc / h2 / H2Store.java @ 20376
History | View | Annotate | Download (7.42 KB)
1 |
package org.gvsig.data.datastores.vectorial.db.jdbc.h2; |
---|---|
2 |
|
3 |
import java.lang.ref.WeakReference; |
4 |
import java.sql.Connection; |
5 |
import java.sql.ResultSet; |
6 |
import java.sql.SQLException; |
7 |
import java.util.Locale; |
8 |
|
9 |
import org.gvsig.data.DataManager; |
10 |
import org.gvsig.data.IDataCollection; |
11 |
import org.gvsig.data.IDataExplorer; |
12 |
import org.gvsig.data.IDataStoreParameters; |
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.WKBParser2; |
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 |
|
29 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
30 |
|
31 |
public class H2Store extends JDBCStore{ |
32 |
public static final String CONNECTION_STRING = "h2"; |
33 |
public static String DATASTORE_NAME = "H2Store"; |
34 |
private static WKBParser2 wkbParser = new WKBParser2(); |
35 |
|
36 |
protected static Locale ukLocale = new Locale("en", "UK"); // English, UK version |
37 |
public H2StoreParameters getParametersH2(){
|
38 |
return (H2StoreParameters)this.parameters; |
39 |
} |
40 |
|
41 |
|
42 |
public void init(IDataStoreParameters parameters) throws InitializeException { |
43 |
super.init(parameters);
|
44 |
|
45 |
|
46 |
|
47 |
this.initConnection();
|
48 |
this.initFeatureType();
|
49 |
this.initSqlProperties();
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
//writer.setCreateTable(false);
|
56 |
//writer.setWriteAll(false);
|
57 |
//writer.initialize(lyrDef);
|
58 |
|
59 |
} |
60 |
|
61 |
private void initFeatureType() throws InitializeException{ |
62 |
H2StoreParameters dParams = this.getParametersH2();
|
63 |
try {
|
64 |
this.featureType = H2Utils.getFeatureType(this.connection, dParams); |
65 |
} catch (ReadException e) {
|
66 |
throw new InitializeException(DATASTORE_NAME,e); |
67 |
} |
68 |
String[] fieldsID = dParams.getFieldsId(); |
69 |
if (fieldsID == null || fieldsID.length < 1){ |
70 |
throw new InitializeException("Field Id not set", |
71 |
this.getName());
|
72 |
} else if (fieldsID.length > 1){ |
73 |
//TODO: Falta por implementar soporte para multiples ID
|
74 |
throw new InitializeException("Multy fields id not supported yet", |
75 |
this.getName());
|
76 |
|
77 |
} else{
|
78 |
for (int i=0;i<fieldsID.length;i++){ |
79 |
if (this.featureType.getFieldIndex(fieldsID[i]) < 0) { |
80 |
|
81 |
throw new InitializeException("Field id '"+ fieldsID[i] +"' (id pos "+ (i+1) +") not Found", |
82 |
this.getName());
|
83 |
|
84 |
} |
85 |
} |
86 |
} |
87 |
|
88 |
|
89 |
} |
90 |
|
91 |
|
92 |
private void initConnection() throws InitializeException{ |
93 |
H2StoreParameters dParams = this.getParametersH2();
|
94 |
|
95 |
String dburl = dParams.getUrl();
|
96 |
String dbuser = dParams.getUser();
|
97 |
String dbpass = dParams.getPassw();
|
98 |
|
99 |
this.connection = H2Utils.getConnection(dburl, dbuser, dbpass);
|
100 |
|
101 |
} |
102 |
|
103 |
public IDataCollection getDataCollection(IFeatureType type, String filter, String order) throws ReadException { |
104 |
if (useSqlSource ){
|
105 |
if (filter != null || order != null){ |
106 |
throw new ReadException("Unsuported filter/order in sqlSource mode",this.getName()); |
107 |
} |
108 |
} |
109 |
if (type!=null && type != this.getDefaultFeatureType()){ |
110 |
throw new UnsupportedOperationException("Type != null"); |
111 |
}else{
|
112 |
type = this.getDefaultFeatureType();
|
113 |
} |
114 |
|
115 |
IFeatureCollection coll; |
116 |
if (featureManager == null){ |
117 |
coll=new H2FeatureCollection(this,(DBFeatureType)type,filter,order); |
118 |
}else{
|
119 |
if ((order != null && order != "")){ |
120 |
coll=new H2FeatureCollectionWithFeatureID(featureManager,this,type,filter,order); |
121 |
} else{
|
122 |
if (filter == null || filter == ""){ |
123 |
coll=new H2FeatureCollectionEditing(featureManager,this,type); |
124 |
} else {
|
125 |
coll=new H2FeatureCollectionEditingFiltered(featureManager,this,type,filter); |
126 |
} |
127 |
} |
128 |
|
129 |
|
130 |
} |
131 |
|
132 |
this.addObserver(new WeakReference(coll)); |
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 |
H2StoreParameters dParams = (H2StoreParameters)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 |
} |
188 |
|
189 |
public void close() throws CloseException { |
190 |
// FIXME: Resource Manager
|
191 |
try {
|
192 |
connection.close(); |
193 |
} catch (java.sql.SQLException e) {
|
194 |
throw new CloseException("H2",e); |
195 |
} |
196 |
} |
197 |
|
198 |
public void dispose() { |
199 |
// FIXME: Resource Manager
|
200 |
|
201 |
|
202 |
} |
203 |
|
204 |
public String getName() { |
205 |
return DATASTORE_NAME;
|
206 |
} |
207 |
|
208 |
protected JDBCFeaturesWriter getFeaturesWriter() throws InitializeWriterException { |
209 |
H2FeaturesWriter writer = new H2FeaturesWriter();
|
210 |
writer.init(this);
|
211 |
return writer;
|
212 |
} |
213 |
|
214 |
public IDataExplorer getExplorer() throws ReadException { |
215 |
DataManager dm = DataManager.getManager(); |
216 |
H2ExplorerParameters explorerParams = (H2ExplorerParameters)dm.createDataExplorerParameters(H2Explorer.DATAEXPLORER_NAME); |
217 |
explorerParams.loadFromStoreParameters(this.getParametersH2());
|
218 |
return dm.createDataExplorer(explorerParams);
|
219 |
} |
220 |
|
221 |
public IMetadata getMetadata() {
|
222 |
if (metadata==null){ |
223 |
IMetadata tmp=super.getMetadata();
|
224 |
|
225 |
return tmp;
|
226 |
}else{
|
227 |
return super.getMetadata(); |
228 |
} |
229 |
|
230 |
} |
231 |
|
232 |
static String getConnectionResourceID(String dbUrl,String dbUser){ |
233 |
return H2Store.CONNECTION_STRING+";"+dbUrl+";"+dbUser; |
234 |
|
235 |
} |
236 |
|
237 |
protected void loadValueFromResulset(ResultSet rs, IFeature feature, IFeatureAttributeDescriptor attr) throws ReadException { |
238 |
IGeometry geom; |
239 |
String name = attr.getName();
|
240 |
try{
|
241 |
if (attr.getDataType().equals(IFeatureAttributeDescriptor.TYPE_GEOMETRY)) {
|
242 |
byte[] data = rs.getBytes(name); |
243 |
|
244 |
if (data == null) { |
245 |
geom = null;
|
246 |
} else{
|
247 |
geom = wkbParser.parse(data); |
248 |
} |
249 |
// feature.setDefaultGeometry(geom);
|
250 |
feature.setGeometry(name,geom); |
251 |
} else {
|
252 |
feature.set(name, rs.getObject(name)); |
253 |
} |
254 |
} catch (SQLException e) { |
255 |
throw new ReadException(this.getName(),e); |
256 |
} |
257 |
|
258 |
|
259 |
} |
260 |
|
261 |
|
262 |
protected IFeature newFeatureInstance(DBFeatureType featureType, Object[] pk) { |
263 |
return new H2Feature(featureType,this,pk); |
264 |
} |
265 |
|
266 |
IFeature createFeatureFromResulset(ResultSet rs, DBFeatureType featureType2) throws ReadException { |
267 |
return this.createFeature(rs, featureType2); |
268 |
} |
269 |
|
270 |
} |