svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / DefaultFeatureReference.java @ 33281
History | View | Annotate | Download (7.54 KB)
1 |
package org.gvsig.fmap.dal.feature.impl; |
---|---|
2 |
|
3 |
import java.lang.ref.WeakReference; |
4 |
import java.text.MessageFormat; |
5 |
import java.util.ArrayList; |
6 |
import java.util.Arrays; |
7 |
import java.util.Iterator; |
8 |
import java.util.List; |
9 |
|
10 |
import org.gvsig.fmap.dal.exception.DataException; |
11 |
import org.gvsig.fmap.dal.feature.Feature; |
12 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
13 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
14 |
import org.gvsig.fmap.dal.feature.FeatureType; |
15 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
16 |
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
17 |
import org.gvsig.tools.ToolsLocator; |
18 |
import org.gvsig.tools.dynobject.DynStruct; |
19 |
import org.gvsig.tools.persistence.Persistent; |
20 |
import org.gvsig.tools.persistence.PersistentState; |
21 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
22 |
|
23 |
public class DefaultFeatureReference implements |
24 |
FeatureReferenceProviderServices, Persistent { |
25 |
|
26 |
private Object oid; |
27 |
private Integer myHashCode = null; |
28 |
private Object[] pk; |
29 |
private String[] pkNames; |
30 |
private WeakReference storeRef; |
31 |
private boolean isNewFeature; |
32 |
private String featureTypeId; |
33 |
|
34 |
/**
|
35 |
* Constructor used by the persistence manager. Don't use directly.
|
36 |
* After to invoke this method, the persistence manager calls
|
37 |
* the the method {@link #loadFromState(PersistentState)} to set
|
38 |
* the values of the internal attributes that this class needs to work.
|
39 |
*/
|
40 |
public DefaultFeatureReference() {
|
41 |
super();
|
42 |
} |
43 |
|
44 |
public DefaultFeatureReference(DefaultFeature feature) {
|
45 |
this(feature.getStore(), feature.getData());
|
46 |
} |
47 |
|
48 |
public DefaultFeatureReference(FeatureStore store,
|
49 |
FeatureProvider fdata) { |
50 |
this.isNewFeature = fdata.isNew();
|
51 |
this.oid = null; |
52 |
this.pk = null; |
53 |
this.storeRef = new WeakReference(store); |
54 |
this.featureTypeId = fdata.getType().getId();
|
55 |
|
56 |
if (fdata.getType().hasOID() || isNewFeature) {
|
57 |
this.oid = fdata.getOID();
|
58 |
if (this.oid == null) { |
59 |
// FIXME Exception
|
60 |
throw new RuntimeException("Missing OID"); |
61 |
} |
62 |
} else {
|
63 |
this.calculatePK(fdata);
|
64 |
if (this.pk == null) { |
65 |
// FIXME Exception
|
66 |
throw new RuntimeException("Missing pk attributes"); |
67 |
} |
68 |
} |
69 |
|
70 |
} |
71 |
|
72 |
/*
|
73 |
* Use only for Persistent.setState
|
74 |
*/
|
75 |
public DefaultFeatureReference(FeatureStore store) {
|
76 |
this.isNewFeature = false; |
77 |
this.oid = null; |
78 |
this.pk = null; |
79 |
this.storeRef = new WeakReference(store); |
80 |
} |
81 |
|
82 |
public DefaultFeatureReference(FeatureStore store, Object oid) { |
83 |
// FIXME featureTypeId is needed !!!
|
84 |
this.isNewFeature = false; |
85 |
this.oid = oid;
|
86 |
this.pk = null; |
87 |
this.storeRef = new WeakReference(store); |
88 |
} |
89 |
|
90 |
private DefaultFeatureStore getStore() {
|
91 |
return (DefaultFeatureStore) this.storeRef.get(); |
92 |
} |
93 |
|
94 |
private void calculatePK(FeatureProvider fdata) { |
95 |
ArrayList keys = new ArrayList(); |
96 |
ArrayList keyNames = new ArrayList(); |
97 |
FeatureType type = fdata.getType(); |
98 |
Iterator it = type.iterator();
|
99 |
while (it.hasNext()) {
|
100 |
FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) it |
101 |
.next(); |
102 |
if (attr.isPrimaryKey()) {
|
103 |
keys.add(fdata.get(attr.getIndex())); |
104 |
keyNames.add(attr.getName()); |
105 |
} |
106 |
} |
107 |
if (keys.size() < 1) { |
108 |
pk = null;
|
109 |
pkNames = null;
|
110 |
} else {
|
111 |
pk = keys.toArray(); |
112 |
pkNames = (String[]) keyNames.toArray(new String[0]); |
113 |
} |
114 |
} |
115 |
|
116 |
public Feature getFeature() throws DataException { |
117 |
return this.getStore().getFeatureByReference(this); |
118 |
} |
119 |
|
120 |
public Feature getFeature(FeatureType featureType) throws DataException { |
121 |
return this.getStore().getFeatureByReference(this, featureType); |
122 |
} |
123 |
|
124 |
public Object getOID() { |
125 |
return this.oid; |
126 |
} |
127 |
|
128 |
public boolean isNewFeature() { |
129 |
return this.isNewFeature; |
130 |
} |
131 |
|
132 |
|
133 |
public boolean equals(Object obj) { |
134 |
if (!(obj instanceof DefaultFeatureReference)) { |
135 |
return false; |
136 |
} |
137 |
DefaultFeatureReference other = (DefaultFeatureReference) obj; |
138 |
|
139 |
FeatureStore otherStore = (FeatureStore) other.storeRef.get(); |
140 |
FeatureStore myrStore = (FeatureStore) this.storeRef.get();
|
141 |
if (otherStore == null || myrStore == null) { |
142 |
return false; |
143 |
} |
144 |
if (!myrStore.equals(otherStore)) {
|
145 |
return false; |
146 |
} |
147 |
if (myHashCode != null && other.myHashCode != null) { |
148 |
return myHashCode.equals(other.myHashCode);
|
149 |
} |
150 |
if (this.oid != null) { |
151 |
return this.oid.equals(other.oid); |
152 |
} |
153 |
for (int i = 0; i < this.pk.length; i++) { |
154 |
if (!this.pk[i].equals(other.pk[i])) { |
155 |
return false; |
156 |
} |
157 |
} |
158 |
return true; |
159 |
} |
160 |
|
161 |
public int hashCode() { |
162 |
if (this.oid != null) { |
163 |
return this.oid.hashCode(); |
164 |
} |
165 |
if (myHashCode == null) { |
166 |
StringBuffer buff = new StringBuffer(); |
167 |
|
168 |
for (int i = 0; i < this.pk.length; i++) { |
169 |
buff.append(this.pk[i].hashCode());
|
170 |
buff.append("##");
|
171 |
} |
172 |
myHashCode = new Integer(buff.toString().hashCode()); |
173 |
} |
174 |
return myHashCode.intValue();
|
175 |
} |
176 |
|
177 |
public String[] getKeyNames() { |
178 |
return pkNames;
|
179 |
} |
180 |
|
181 |
public Object getKeyValue(String name) { |
182 |
for (int i = 0; i < pkNames.length; i++) { |
183 |
if (pkNames[i].equalsIgnoreCase(name)) {
|
184 |
return pk[i];
|
185 |
} |
186 |
} |
187 |
// FIXME exception????
|
188 |
return null; |
189 |
} |
190 |
|
191 |
public String getFeatureTypeId() { |
192 |
return featureTypeId;
|
193 |
} |
194 |
|
195 |
// *** Persistence ***
|
196 |
|
197 |
public void loadFromState(PersistentState state) |
198 |
throws PersistenceException {
|
199 |
this.oid = state.get("oid"); |
200 |
this.myHashCode = (Integer) state.get("myHashCode"); |
201 |
this.storeRef = new WeakReference(state.get("store")); |
202 |
this.isNewFeature = state.getBoolean("isNewFeature"); |
203 |
this.featureTypeId = state.getString("featureTypeId"); |
204 |
List pkList = (List) state.get("pk"); |
205 |
if (pkList != null) { |
206 |
List pkNamesList = (List) state.get("pkNames"); |
207 |
if (pkNamesList == null || pkList.size() != pkNamesList.size()) { |
208 |
throw new PersistenceException("bad pkNames value"); |
209 |
} |
210 |
this.pk = pkList.toArray();
|
211 |
this.pkNames = (String[]) pkNamesList.toArray(); |
212 |
} else {
|
213 |
this.pk = null; |
214 |
this.pkNames = null; |
215 |
} |
216 |
} |
217 |
|
218 |
public void saveToState(PersistentState state) throws PersistenceException { |
219 |
state.set("oid", oid);
|
220 |
state.set("myHashCode", myHashCode);
|
221 |
state.set("isNewFeature", isNewFeature);
|
222 |
state.set("store", (Persistent) storeRef.get());
|
223 |
state.set("featureTypeId", featureTypeId);
|
224 |
if (pk == null) { |
225 |
state.setNull("pk");
|
226 |
state.setNull("pkNames");
|
227 |
} else {
|
228 |
state.set("pk", pk);
|
229 |
state.set("pkNames", pkNames);
|
230 |
} |
231 |
|
232 |
} |
233 |
|
234 |
public static void registerPersistent() { |
235 |
DynStruct definition = ToolsLocator.getPersistenceManager().addDefinition( |
236 |
DefaultFeatureReference.class, |
237 |
"Reference",
|
238 |
"DefaultFeatureReference Persistent definition",
|
239 |
null,
|
240 |
null
|
241 |
); |
242 |
|
243 |
definition.addDynFieldObject("oid")
|
244 |
.setClassOfValue(Object.class)
|
245 |
.setMandatory(true)
|
246 |
.setPersistent(true);
|
247 |
|
248 |
definition.addDynFieldBoolean("isNewFeature")
|
249 |
.setMandatory(true)
|
250 |
.setPersistent(true);
|
251 |
|
252 |
definition.addDynFieldObject("store")
|
253 |
.setClassOfValue(FeatureStore.class) |
254 |
.setMandatory(true)
|
255 |
.setPersistent(true);
|
256 |
|
257 |
definition.addDynFieldInt("myHashCode")
|
258 |
.setMandatory(true)
|
259 |
.setPersistent(true);
|
260 |
|
261 |
definition.addDynFieldString("featureTypeId")
|
262 |
.setMandatory(true)
|
263 |
.setPersistent(true);
|
264 |
|
265 |
definition.addDynFieldArray("ok")
|
266 |
.setClassOfItems(Object.class)
|
267 |
.setMandatory(false)
|
268 |
.setPersistent(true);
|
269 |
|
270 |
definition.addDynFieldArray("pkNames")
|
271 |
.setClassOfItems(String.class)
|
272 |
.setMandatory(true)
|
273 |
.setPersistent(true);
|
274 |
|
275 |
} |
276 |
|
277 |
public String toString() { |
278 |
return MessageFormat.format( |
279 |
"FeatureReference: oid = {0}, myHashCode = {1}, "
|
280 |
+ "pks = {2}, pkNames = {3}, "
|
281 |
+ "isNewFeature = {4}, featureTypeId = {5}", new Object[] { |
282 |
oid, myHashCode, (pk == null ? null : Arrays.asList(pk)), |
283 |
(pkNames == null ? null : Arrays.asList(pkNames)), |
284 |
new Boolean(isNewFeature), |
285 |
featureTypeId }); |
286 |
} |
287 |
|
288 |
} |