svn-gvsig-desktop / trunk / libraries / libDataSourceDBBaseDrivers / src / org / gvsig / data / datastores / vectorial / db / jdbc / JDBCStore.java @ 20376
History | View | Annotate | Download (8.82 KB)
1 |
package org.gvsig.data.datastores.vectorial.db.jdbc; |
---|---|
2 |
|
3 |
import java.sql.Connection; |
4 |
import java.sql.ResultSet; |
5 |
import java.sql.Statement; |
6 |
import java.util.ArrayList; |
7 |
import java.util.Iterator; |
8 |
import java.util.List; |
9 |
|
10 |
import org.gvsig.data.commands.implementation.AbstractAttributeCommand; |
11 |
import org.gvsig.data.commands.implementation.AbstractFeatureCommand; |
12 |
import org.gvsig.data.commands.implementation.UpdateAttributeCommand; |
13 |
import org.gvsig.data.commands.implementation.UpdateFeatureCommand; |
14 |
import org.gvsig.data.datastores.vectorial.ISelectiveWriter; |
15 |
import org.gvsig.data.datastores.vectorial.db.DBFeatureID; |
16 |
import org.gvsig.data.datastores.vectorial.db.DBFeatureType; |
17 |
import org.gvsig.data.datastores.vectorial.db.jdbc.exception.SQLException; |
18 |
import org.gvsig.data.datastores.vectorial.db.jdbc.h2.H2Utils; |
19 |
import org.gvsig.data.exception.DataException; |
20 |
import org.gvsig.data.exception.InitializeWriterException; |
21 |
import org.gvsig.data.exception.ReadException; |
22 |
import org.gvsig.data.exception.WriteException; |
23 |
import org.gvsig.data.vectorial.AbstractFeatureStore; |
24 |
import org.gvsig.data.vectorial.CreatedFeature; |
25 |
import org.gvsig.data.vectorial.IFeature; |
26 |
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor; |
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 abstract class JDBCStore extends AbstractFeatureStore { |
34 |
|
35 |
protected Connection connection = null; |
36 |
protected String sqlSelectPart; |
37 |
protected String baseWhereClause = null; |
38 |
protected String baseOrder = null; |
39 |
protected String sqlSource = null; |
40 |
protected boolean useSqlSource = false; |
41 |
protected DBFeatureType featureType;
|
42 |
protected IMetadata metadata;
|
43 |
|
44 |
|
45 |
protected abstract Connection getConnection(); |
46 |
|
47 |
protected abstract IFeature newFeatureInstance(DBFeatureType featureType,Object[] pk); |
48 |
|
49 |
protected abstract void loadValueFromResulset(ResultSet rs,IFeature feature,IFeatureAttributeDescriptor attr) throws ReadException; |
50 |
|
51 |
public boolean isEditable() { |
52 |
return !this.useSqlSource; |
53 |
} |
54 |
|
55 |
|
56 |
public String getSqlSelectPart() { |
57 |
return sqlSelectPart;
|
58 |
} |
59 |
|
60 |
public String getBaseOrder() { |
61 |
return this.baseOrder; |
62 |
} |
63 |
|
64 |
public String getBaseWhereClause() { |
65 |
return baseWhereClause;
|
66 |
} |
67 |
|
68 |
public boolean isUseSqlSource() { |
69 |
return this.useSqlSource; |
70 |
} |
71 |
|
72 |
public String getSqlSource() { |
73 |
return this.sqlSource; |
74 |
} |
75 |
|
76 |
protected abstract JDBCFeaturesWriter getFeaturesWriter() throws InitializeWriterException; |
77 |
|
78 |
protected void doFinishEdition() throws WriteException, ReadException { |
79 |
Iterator commandsfeatures = null; |
80 |
JDBCFeaturesWriter selectiveWriter = getFeaturesWriter(); |
81 |
IFeatureType type = getDefaultFeatureType(); |
82 |
|
83 |
|
84 |
selectiveWriter.updateFeatureType(type); |
85 |
selectiveWriter.preProcess(); |
86 |
|
87 |
try{
|
88 |
commandsfeatures = commands.getCommandsAttributeDeleted().iterator(); |
89 |
while( commandsfeatures.hasNext() ) {
|
90 |
Object obj=commandsfeatures.next();
|
91 |
if (obj instanceof AbstractAttributeCommand){ |
92 |
IFeatureAttributeDescriptor attribute = ((AbstractAttributeCommand)obj).getAttributeDescriptor(); |
93 |
selectiveWriter.deleteAttribute(attribute); |
94 |
} |
95 |
} |
96 |
commandsfeatures = commands.getCommandsAttributeUpdated().iterator(); |
97 |
while( commandsfeatures.hasNext() ) {
|
98 |
Object obj=commandsfeatures.next();
|
99 |
if (obj instanceof AbstractAttributeCommand){ |
100 |
IFeatureAttributeDescriptor oldAttribute = ((UpdateAttributeCommand)obj).getOldAttributeDescriptor(); |
101 |
IFeatureAttributeDescriptor attribute = ((UpdateAttributeCommand)obj).getAttributeDescriptor(); |
102 |
selectiveWriter.updateAttribute(oldAttribute,attribute); |
103 |
} |
104 |
} |
105 |
commandsfeatures = commands.getCommandsAttributeInserted().iterator(); |
106 |
while( commandsfeatures.hasNext() ) {
|
107 |
Object obj=commandsfeatures.next();
|
108 |
if (obj instanceof AbstractAttributeCommand){ |
109 |
IFeatureAttributeDescriptor attribute = ((AbstractAttributeCommand)obj).getAttributeDescriptor(); |
110 |
selectiveWriter.insertAttribute(attribute); |
111 |
} |
112 |
} |
113 |
|
114 |
|
115 |
commandsfeatures = commands.getCommandsFeatureDeleted().iterator(); |
116 |
while( commandsfeatures.hasNext() ) {
|
117 |
Object obj=commandsfeatures.next();
|
118 |
if (obj instanceof AbstractFeatureCommand){ |
119 |
IFeature feature = ((AbstractFeatureCommand)obj).getFeature(); |
120 |
if (feature instanceof CreatedFeature) |
121 |
continue;
|
122 |
|
123 |
selectiveWriter.deleteFeature(feature); |
124 |
} |
125 |
} |
126 |
commandsfeatures = commands.getCommandsFeatureUpdated().iterator(); |
127 |
while( commandsfeatures.hasNext() ) {
|
128 |
Object obj=commandsfeatures.next();
|
129 |
if (obj instanceof AbstractFeatureCommand){ |
130 |
IFeature oldFeature = ((UpdateFeatureCommand)obj).getOldFeature(); |
131 |
IFeature feature = ((UpdateFeatureCommand)obj).getFeature(); |
132 |
if (featureManager.isDeleted(feature)){
|
133 |
continue;
|
134 |
} |
135 |
selectiveWriter.updateFeature(oldFeature,feature); |
136 |
} |
137 |
} |
138 |
commandsfeatures = commands.getCommandsFeatureInserted().iterator(); |
139 |
while( commandsfeatures.hasNext() ) {
|
140 |
Object obj=commandsfeatures.next();
|
141 |
if (obj instanceof AbstractFeatureCommand){ |
142 |
IFeature feature = ((AbstractFeatureCommand)obj).getFeature(); |
143 |
if (featureManager.isDeleted(feature)){
|
144 |
continue;
|
145 |
} |
146 |
selectiveWriter.insertFeature(feature); |
147 |
} |
148 |
} |
149 |
selectiveWriter.postProcess(); |
150 |
|
151 |
}catch (ReadException e) {
|
152 |
selectiveWriter.cancelProcess(); |
153 |
throw e;
|
154 |
} catch (WriteException e){
|
155 |
selectiveWriter.cancelProcess(); |
156 |
throw e;
|
157 |
} catch (Exception e){ |
158 |
selectiveWriter.cancelProcess(); |
159 |
throw new WriteException(this.getName(),e); |
160 |
} |
161 |
} |
162 |
|
163 |
public String getFilterForID(DBFeatureType fType, Object[] featureKey) { |
164 |
if (fType.getFieldsId().length != 1) |
165 |
throw new UnsupportedOperationException("ID fields > 1"); |
166 |
String id =fType.getFieldsId()[0]; |
167 |
return id + " = " + objectToSqlString(featureKey[0]); |
168 |
} |
169 |
|
170 |
protected String objectToSqlString(Object obj) { |
171 |
if (obj instanceof String){ |
172 |
return "'"+ scapeString((String)obj) +"'"; |
173 |
} else if (obj == null){ |
174 |
return "null"; |
175 |
}else{
|
176 |
// OJO con otros tipos!!
|
177 |
return obj.toString();
|
178 |
} |
179 |
|
180 |
} |
181 |
|
182 |
protected String scapeString(String str) { |
183 |
return str.replace("'", "''"); |
184 |
} |
185 |
|
186 |
public IFeature getFeatureByID(IFeatureID id) throws ReadException { |
187 |
return getFeatureByID((DBFeatureType)this.getDefaultFeatureType(),((DBFeatureID)id).getKey()); |
188 |
} |
189 |
|
190 |
public IFeatureType getDefaultFeatureType() {
|
191 |
return this.featureType; |
192 |
} |
193 |
|
194 |
public IMetadata getMetadata() {
|
195 |
if (metadata==null){ |
196 |
IMetadataManager manager=MetadataManager.getManager(); |
197 |
metadata=manager.create(this.getName());
|
198 |
//TODO: Apadir los meteadatos
|
199 |
} |
200 |
return metadata;
|
201 |
} |
202 |
|
203 |
public List getFeatureTypes() { |
204 |
ArrayList list = new ArrayList(); |
205 |
list.add(this.featureType);
|
206 |
return list;
|
207 |
} |
208 |
|
209 |
public boolean isWithDefaultLegend() { |
210 |
// TODO Auto-generated method stub
|
211 |
return false; |
212 |
} |
213 |
|
214 |
public Object getDefaultLabelingStrategy() { |
215 |
// TODO Auto-generated method stub
|
216 |
return null; |
217 |
} |
218 |
|
219 |
public Object getDefaultLegend() { |
220 |
// TODO Auto-generated method stub
|
221 |
return null; |
222 |
} |
223 |
|
224 |
public IFeature getFeatureByID(IFeatureType featureType2, Object[] featureKey) throws ReadException { |
225 |
//TODO: Tener en cuenta el FeatureType por si es distinto
|
226 |
if (useSqlSource){
|
227 |
throw new ReadException("Unsuported featureByID in sqlSource mode",this.getName()); |
228 |
} |
229 |
ResultSet rs=null; |
230 |
try{
|
231 |
this.open();
|
232 |
Statement st=this.getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); |
233 |
String sql = this.getSqlSelectPart() + " WHERE "+ this.getFilterForID(this.featureType, featureKey); |
234 |
rs=st.executeQuery(sql); |
235 |
if(rs.next()){
|
236 |
return createFeature(rs, this.featureType); |
237 |
} |
238 |
|
239 |
} catch (java.sql.SQLException e) {
|
240 |
e.printStackTrace(); |
241 |
throw new ReadException(this.getName(), e); |
242 |
} finally{
|
243 |
if (rs != null) |
244 |
try {
|
245 |
rs.close(); |
246 |
} catch (java.sql.SQLException e) {
|
247 |
// TODO ?????
|
248 |
e.printStackTrace(); |
249 |
} |
250 |
} |
251 |
return null; |
252 |
} |
253 |
|
254 |
protected Object[] getPkFromResulset(ResultSet rs, DBFeatureType featureType) throws java.sql.SQLException{ |
255 |
String[] fieldsId = featureType.getFieldsId(); |
256 |
Object[] result = new Object[fieldsId.length]; |
257 |
for (int i=0;i<fieldsId.length;i++){ |
258 |
result[i] = rs.getObject(fieldsId[i]); |
259 |
} |
260 |
return result;
|
261 |
|
262 |
} |
263 |
|
264 |
protected IFeature createFeature(ResultSet rs, DBFeatureType featureType2) throws ReadException { |
265 |
JDBCFeature feature=null;
|
266 |
|
267 |
if (featureType2 == null){ |
268 |
featureType2 = (DBFeatureType)this.getDefaultFeatureType();
|
269 |
} |
270 |
|
271 |
|
272 |
Object[] pk; |
273 |
try {
|
274 |
pk = getPkFromResulset(rs, featureType2); |
275 |
|
276 |
feature=(JDBCFeature)this.newFeatureInstance(featureType2, pk);
|
277 |
|
278 |
Iterator iter = featureType2.iterator();
|
279 |
while (iter.hasNext()) {
|
280 |
IFeatureAttributeDescriptor fad=(IFeatureAttributeDescriptor)iter.next(); |
281 |
this.loadValueFromResulset(rs, feature, fad);
|
282 |
|
283 |
} |
284 |
return feature;
|
285 |
} catch (java.sql.SQLException e) {
|
286 |
throw new ReadException(this.getName(),e); |
287 |
} |
288 |
|
289 |
} |
290 |
} |