root / branches / v2_0_0_prep / libraries / libFMap_dataDB / src / org / gvsig / fmap / data / feature / db / jdbc / JDBCFeature.java @ 24250
History | View | Annotate | Download (5.01 KB)
1 |
package org.gvsig.fmap.data.feature.db.jdbc; |
---|---|
2 |
|
3 |
import java.sql.ResultSet; |
4 |
import java.sql.ResultSetMetaData; |
5 |
import java.sql.SQLException; |
6 |
import java.util.Iterator; |
7 |
import java.util.List; |
8 |
|
9 |
import org.gvsig.fmap.data.exceptions.DataException; |
10 |
import org.gvsig.fmap.data.exceptions.ReadException; |
11 |
import org.gvsig.fmap.data.feature.AbstractFeature; |
12 |
import org.gvsig.fmap.data.feature.AttributeDescriptor; |
13 |
import org.gvsig.fmap.data.feature.Feature; |
14 |
import org.gvsig.fmap.data.feature.FeatureAttributeDescriptor; |
15 |
import org.gvsig.fmap.data.feature.FeatureReference; |
16 |
import org.gvsig.fmap.data.feature.FeatureType; |
17 |
import org.gvsig.fmap.data.feature.db.DBFeature; |
18 |
import org.gvsig.fmap.data.feature.db.DBFeatureType; |
19 |
import org.gvsig.fmap.data.feature.exceptions.IsNotFeatureSettingException; |
20 |
import org.gvsig.fmap.geom.primitive.Envelope; |
21 |
|
22 |
public abstract class JDBCFeature extends DBFeature { |
23 |
|
24 |
protected JDBCStore store;
|
25 |
protected Object[] featureKey; |
26 |
|
27 |
public JDBCFeature(FeatureType featureType, JDBCStore store, ResultSet rs) throws ReadException { |
28 |
super((DBFeatureType)featureType);
|
29 |
this.store= store;
|
30 |
this.load(rs);
|
31 |
} |
32 |
|
33 |
protected JDBCFeature(JDBCFeature feature) throws ReadException { |
34 |
super((DBFeatureType)feature.featureType);
|
35 |
this.store= feature.store;
|
36 |
this.featureKey = new Object[feature.featureKey.length]; |
37 |
System.arraycopy(feature.featureKey, 0, this.featureKey, 0, feature.featureKey.length); |
38 |
Iterator iter=this.featureType.iterator(); |
39 |
|
40 |
Object value;
|
41 |
FeatureAttributeDescriptor featureAttribute; |
42 |
int column;
|
43 |
this.loading();
|
44 |
while(iter.hasNext()){
|
45 |
featureAttribute=(FeatureAttributeDescriptor)iter.next(); |
46 |
column=((AttributeDescriptor)featureAttribute).originalPosition(); |
47 |
value=feature.getAttribute(column); |
48 |
try{
|
49 |
if (featureAttribute.equals(FeatureAttributeDescriptor.GEOMETRY)){
|
50 |
//TODO: Falta clonar geometria
|
51 |
try {
|
52 |
this.setGeometry(column, value);
|
53 |
} catch (IsNotFeatureSettingException e) {
|
54 |
throw new ReadException(this.store.getName(),e); |
55 |
} |
56 |
|
57 |
}else if (featureAttribute.equals(FeatureAttributeDescriptor.OBJECT)){ |
58 |
//TODO falta clonar el objeto
|
59 |
// ((Cloneable)get(i)).clo
|
60 |
// set(i,.);
|
61 |
}else {
|
62 |
this.setAttribute(column, value);
|
63 |
} |
64 |
} catch (IsNotFeatureSettingException e){
|
65 |
throw new ReadException(this.store.getName(),e); |
66 |
} |
67 |
} |
68 |
this.stopLoading();
|
69 |
|
70 |
} |
71 |
|
72 |
|
73 |
protected void load(ResultSet rs) throws ReadException{ |
74 |
try {
|
75 |
this.loading();
|
76 |
this.featureKey = getPkFromResulset(rs, (DBFeatureType)this.featureType); |
77 |
|
78 |
Iterator iter = this.featureType.iterator(); |
79 |
ResultSetMetaData rsMeta = rs.getMetaData();
|
80 |
while (iter.hasNext()) {
|
81 |
AttributeDescriptor fad=(AttributeDescriptor)iter.next(); |
82 |
if (fad.isEvaluated()){
|
83 |
continue;
|
84 |
}else if (fad.originalPosition() > -1 && this.isResulsetColumns(rsMeta, fad)){ |
85 |
this.loadValueFromResulset(rs, fad);
|
86 |
} else{
|
87 |
try {
|
88 |
this.setAttribute(fad.ordinal(),fad.getDefaultValue());
|
89 |
} catch (IsNotFeatureSettingException e) {
|
90 |
throw new ReadException(this.store.getName(),e); |
91 |
} |
92 |
} |
93 |
|
94 |
} |
95 |
this.stopLoading();
|
96 |
} catch (java.sql.SQLException e) {
|
97 |
throw new ReadException(this.store.getName(),e); |
98 |
} |
99 |
} |
100 |
|
101 |
protected boolean isResulsetColumns(ResultSetMetaData rsMeta,FeatureAttributeDescriptor fad) throws SQLException{ |
102 |
String name = fad.getName();
|
103 |
for (int i=1;i<=rsMeta.getColumnCount();i++){ |
104 |
if (rsMeta.getColumnName(i).equalsIgnoreCase(name)){
|
105 |
return true; |
106 |
} |
107 |
} |
108 |
return false; |
109 |
} |
110 |
|
111 |
protected abstract void loadValueFromResulset(ResultSet rs, FeatureAttributeDescriptor attr) throws ReadException; |
112 |
|
113 |
|
114 |
protected Object[] getPkFromResulset(ResultSet rs, DBFeatureType featureType) throws java.sql.SQLException{ |
115 |
String[] fieldsId = featureType.getFieldsId(); |
116 |
Object[] result = new Object[fieldsId.length]; |
117 |
for (int i=0;i<fieldsId.length;i++){ |
118 |
result[i] = rs.getObject(fieldsId[i]); |
119 |
} |
120 |
return result;
|
121 |
|
122 |
} |
123 |
|
124 |
|
125 |
|
126 |
public void editing() throws DataException { |
127 |
if (!this.store.isEditing()){ |
128 |
throw new DataException("Read Only Feature"); |
129 |
} |
130 |
super.editing();
|
131 |
} |
132 |
|
133 |
protected Object[] getPkFromValues(ResultSet rs, DBFeatureType featureType) throws java.sql.SQLException{ |
134 |
String[] fieldsId = featureType.getFieldsId(); |
135 |
//TODO: optimizar esto usando el ordinal de los ids
|
136 |
Object[] result = new Object[fieldsId.length]; |
137 |
for (int i=0;i<fieldsId.length;i++){ |
138 |
result[i] = this.getAttribute(fieldsId[i]);
|
139 |
} |
140 |
return result;
|
141 |
|
142 |
} |
143 |
|
144 |
|
145 |
public abstract FeatureReference getID(); |
146 |
|
147 |
public Envelope getDefaultExtent() {
|
148 |
return null; |
149 |
} |
150 |
|
151 |
public List getSRSs() { |
152 |
return null; |
153 |
} |
154 |
|
155 |
public String getDefaultSRS() { |
156 |
return null; |
157 |
} |
158 |
|
159 |
public String getFilterForID() { |
160 |
return this.store.getFilterForID((DBFeatureType)this.featureType,this.featureKey); |
161 |
} |
162 |
|
163 |
protected String objectToSqlString(Object obj) { |
164 |
if (obj instanceof String){ |
165 |
return "'"+ scapeString((String)obj) +"'"; |
166 |
} else if (obj == null){ |
167 |
return "null"; |
168 |
}else{
|
169 |
// OJO con otros tipos!!
|
170 |
return obj.toString();
|
171 |
} |
172 |
|
173 |
} |
174 |
|
175 |
protected String scapeString(String str) { |
176 |
return str.replace("'", "''"); |
177 |
} |
178 |
|
179 |
|
180 |
} |