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