svn-gvsig-desktop / trunk / libraries / libDataSourceDBBaseDrivers / src / org / gvsig / data / datastores / vectorial / db / jdbc / h2 / H2Store.java @ 20058
History | View | Annotate | Download (7.15 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.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.JDBCFeaturesWriter; |
14 |
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCStore; |
15 |
import org.gvsig.data.datastores.vectorial.db.jdbc.WKBParser2; |
16 |
import org.gvsig.data.exception.CloseException; |
17 |
import org.gvsig.data.exception.InitializeException; |
18 |
import org.gvsig.data.exception.OpenException; |
19 |
import org.gvsig.data.exception.ReadException; |
20 |
import org.gvsig.data.spatialprovisional.IExtent; |
21 |
import org.gvsig.data.vectorial.IFeature; |
22 |
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor; |
23 |
import org.gvsig.data.vectorial.IFeatureCollection; |
24 |
import org.gvsig.data.vectorial.IFeatureType; |
25 |
import org.gvsig.metadata.IMetadata; |
26 |
|
27 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
28 |
|
29 |
public class H2Store extends JDBCStore{ |
30 |
public static final String CONNECTION_STRING = "h2"; |
31 |
public static String DATASTORE_NAME = "H2Store"; |
32 |
private static WKBParser2 wkbParser = new WKBParser2(); |
33 |
|
34 |
protected static Locale ukLocale = new Locale("en", "UK"); // English, UK version |
35 |
public H2StoreParameters getParametersH2(){
|
36 |
return (H2StoreParameters)this.parameters; |
37 |
} |
38 |
|
39 |
|
40 |
public void init(IDataStoreParameters parameters) throws InitializeException { |
41 |
super.init(parameters);
|
42 |
|
43 |
|
44 |
|
45 |
this.initConnection();
|
46 |
this.initFeatureType();
|
47 |
this.initSqlProperties();
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
//writer.setCreateTable(false);
|
54 |
//writer.setWriteAll(false);
|
55 |
//writer.initialize(lyrDef);
|
56 |
|
57 |
} |
58 |
|
59 |
private void initFeatureType() throws InitializeException{ |
60 |
H2StoreParameters dParams = this.getParametersH2();
|
61 |
try {
|
62 |
this.featureType = H2Utils.getFeatureType(this.connection, dParams); |
63 |
} catch (ReadException e) {
|
64 |
throw new InitializeException(DATASTORE_NAME,e); |
65 |
} |
66 |
String[] fieldsID = dParams.getFieldsId(); |
67 |
if (fieldsID == null || fieldsID.length < 1){ |
68 |
throw new InitializeException( |
69 |
DATASTORE_NAME, |
70 |
new Exception("Field Id not set")); |
71 |
} else if (fieldsID.length > 1){ |
72 |
//TODO: Falta por implementar soporte para multiples ID
|
73 |
throw new InitializeException( |
74 |
DATASTORE_NAME, |
75 |
new Exception("Multy fields id not supported yet")); |
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( |
82 |
DATASTORE_NAME, |
83 |
new Exception("Field id '"+ fieldsID[i] +"' (id pos "+ (i+1) +") not Found")); |
84 |
|
85 |
} |
86 |
} |
87 |
} |
88 |
|
89 |
|
90 |
} |
91 |
|
92 |
|
93 |
private void initConnection() throws InitializeException{ |
94 |
H2StoreParameters dParams = this.getParametersH2();
|
95 |
|
96 |
String dburl = dParams.getUrl();
|
97 |
String dbuser = dParams.getUser();
|
98 |
String dbpass = dParams.getPassw();
|
99 |
|
100 |
this.connection = H2Utils.getConnection(dburl, dbuser, dbpass);
|
101 |
|
102 |
} |
103 |
|
104 |
public IDataCollection getDataCollection(IFeatureType type, String filter, String order) throws ReadException { |
105 |
if (useSqlSource ){
|
106 |
if (filter != null || order != null){ |
107 |
throw new ReadException(DATASTORE_NAME, |
108 |
new UnsupportedOperationException("Unsuported filter/order in sqlSource mode")); |
109 |
} |
110 |
} |
111 |
if (type!=null && type != this.getDefaultFeatureType()){ |
112 |
throw new UnsupportedOperationException("Type != null"); |
113 |
}else{
|
114 |
type = this.getDefaultFeatureType();
|
115 |
} |
116 |
|
117 |
IFeatureCollection coll; |
118 |
if (featureManager == null){ |
119 |
coll=new H2FeatureCollection(this,(DBFeatureType)type,filter,order); |
120 |
}else{
|
121 |
if ((order != null && order != "")){ |
122 |
coll=new H2FeatureCollectionWithFeatureID(featureManager,this,type,filter,order); |
123 |
} else{
|
124 |
if (filter == null || filter == ""){ |
125 |
coll=new H2FeatureCollectionEditing(featureManager,this,type); |
126 |
} else {
|
127 |
coll=new H2FeatureCollectionEditingFiltered(featureManager,this,type,filter); |
128 |
} |
129 |
} |
130 |
|
131 |
|
132 |
} |
133 |
|
134 |
this.addObserver(new WeakReference(coll)); |
135 |
return coll;
|
136 |
} |
137 |
|
138 |
protected Connection getConnection(){ |
139 |
// FIXME: OJO REsource manager
|
140 |
return this.connection; |
141 |
|
142 |
} |
143 |
|
144 |
Connection getCurrentConnection(){
|
145 |
return this.getConnection(); |
146 |
} |
147 |
|
148 |
private void initSqlProperties() throws InitializeException{ |
149 |
H2StoreParameters dParams = (H2StoreParameters)this.getParameters();
|
150 |
if (dParams.getSqlSoure() != null){ |
151 |
this.sqlSource = dParams.getSqlSoure();
|
152 |
this.useSqlSource = true; |
153 |
this.sqlSelectPart = null; |
154 |
this.baseWhereClause = null; |
155 |
this.baseOrder = null; |
156 |
} else {
|
157 |
this.sqlSelectPart = "SELECT " + dParams.getFieldsString() +" FROM "+ dParams.tableID(); |
158 |
|
159 |
this.baseWhereClause = dParams.getBaseFilter();
|
160 |
|
161 |
if (dParams.getWorkingArea() != null){ |
162 |
String waWhere = getWorkingAreaWhere(dParams.getWorkingArea(), dParams.getSRISD());
|
163 |
if (waWhere != null){ |
164 |
this.baseWhereClause = "(("+this.baseWhereClause+") and "+waWhere +")"; |
165 |
} |
166 |
|
167 |
} |
168 |
|
169 |
this.baseOrder = dParams.getBaseOrder();
|
170 |
} |
171 |
} |
172 |
private String getWorkingAreaWhere(IExtent r, String strEPSG) { |
173 |
//TODO????
|
174 |
if (r==null){ |
175 |
return null; |
176 |
} |
177 |
return null; |
178 |
} |
179 |
|
180 |
|
181 |
public boolean canAlterFeatureType() { |
182 |
return true; |
183 |
} |
184 |
|
185 |
public void open() throws OpenException { |
186 |
// FIXME: Resource Manager
|
187 |
|
188 |
|
189 |
} |
190 |
|
191 |
public void close() throws CloseException { |
192 |
// FIXME: Resource Manager
|
193 |
try {
|
194 |
connection.close(); |
195 |
} catch (java.sql.SQLException e) {
|
196 |
throw new CloseException("H2",e); |
197 |
} |
198 |
} |
199 |
|
200 |
public void dispose() { |
201 |
// FIXME: Resource Manager
|
202 |
|
203 |
|
204 |
} |
205 |
|
206 |
public String getName() { |
207 |
return DATASTORE_NAME;
|
208 |
} |
209 |
|
210 |
protected JDBCFeaturesWriter getFeaturesWriter() {
|
211 |
H2FeaturesWriter writer = new H2FeaturesWriter();
|
212 |
writer.init(this);
|
213 |
return writer;
|
214 |
} |
215 |
|
216 |
public IDataExplorer getExplorer() {
|
217 |
// TODO Auto-generated method stub
|
218 |
return null; |
219 |
} |
220 |
public IMetadata getMetadata() {
|
221 |
if (metadata==null){ |
222 |
IMetadata tmp=super.getMetadata();
|
223 |
|
224 |
return tmp;
|
225 |
}else{
|
226 |
return super.getMetadata(); |
227 |
} |
228 |
|
229 |
} |
230 |
|
231 |
static String getConnectionResourceID(String dbUrl,String dbUser){ |
232 |
return H2Store.CONNECTION_STRING+";"+dbUrl+";"+dbUser; |
233 |
|
234 |
} |
235 |
|
236 |
protected void loadValueFromResulset(ResultSet rs, IFeature feature, IFeatureAttributeDescriptor attr) throws ReadException { |
237 |
IGeometry geom; |
238 |
String name = attr.getName();
|
239 |
try{
|
240 |
if (attr.getDataType().equals(IFeatureAttributeDescriptor.TYPE_GEOMETRY)) {
|
241 |
byte[] data = rs.getBytes(name); |
242 |
|
243 |
if (data == null) { |
244 |
geom = null;
|
245 |
} else{
|
246 |
geom = wkbParser.parse(data); |
247 |
} |
248 |
// feature.setDefaultGeometry(geom);
|
249 |
feature.setGeometry(name,geom); |
250 |
} else {
|
251 |
feature.set(name, rs.getObject(name)); |
252 |
} |
253 |
} catch (SQLException e) { |
254 |
throw new ReadException(this.getName(),e); |
255 |
} |
256 |
|
257 |
|
258 |
} |
259 |
|
260 |
|
261 |
protected IFeature newFeatureInstance(DBFeatureType featureType, Object[] pk) { |
262 |
return new H2Feature(featureType,this,pk); |
263 |
} |
264 |
|
265 |
IFeature createFeatureFromResulset(ResultSet rs, DBFeatureType featureType2) throws ReadException { |
266 |
return this.createFeature(rs, featureType2); |
267 |
} |
268 |
|
269 |
} |