svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / DefaultFeatureReference.java @ 37603
History | View | Annotate | Download (7.62 KB)
1 | 24496 | jmvivo | package org.gvsig.fmap.dal.feature.impl; |
---|---|---|---|
2 | 24248 | jjdelcerro | |
3 | import java.lang.ref.WeakReference; |
||
4 | 32900 | cordinyana | import java.text.MessageFormat; |
5 | 24248 | jjdelcerro | import java.util.ArrayList; |
6 | 30208 | jmvivo | import java.util.Arrays; |
7 | 24248 | jjdelcerro | import java.util.Iterator; |
8 | 30208 | jmvivo | import java.util.List; |
9 | 24248 | jjdelcerro | |
10 | 24505 | jmvivo | import org.gvsig.fmap.dal.exception.DataException; |
11 | 24496 | jmvivo | 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 | 29289 | jmvivo | import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
16 | 24496 | jmvivo | import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
17 | 30154 | cordinyana | import org.gvsig.tools.ToolsLocator; |
18 | 32880 | jjdelcerro | import org.gvsig.tools.dynobject.DynStruct; |
19 | 24248 | jjdelcerro | import org.gvsig.tools.persistence.Persistent; |
20 | import org.gvsig.tools.persistence.PersistentState; |
||
21 | 32880 | jjdelcerro | import org.gvsig.tools.persistence.exception.PersistenceException; |
22 | 24248 | jjdelcerro | |
23 | public class DefaultFeatureReference implements |
||
24 | FeatureReferenceProviderServices, Persistent { |
||
25 | |||
26 | private Object oid; |
||
27 | 28423 | jmvivo | private Integer myHashCode = null; |
28 | 24248 | jjdelcerro | private Object[] pk; |
29 | 27672 | jmvivo | private String[] pkNames; |
30 | 24248 | jjdelcerro | private WeakReference storeRef; |
31 | private boolean isNewFeature; |
||
32 | 28660 | jmvivo | private String featureTypeId; |
33 | 31272 | jpiera | |
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 | 24248 | jjdelcerro | public DefaultFeatureReference(DefaultFeature feature) {
|
45 | 25691 | jmvivo | this(feature.getStore(), feature.getData());
|
46 | 24248 | jjdelcerro | } |
47 | |||
48 | public DefaultFeatureReference(FeatureStore store,
|
||
49 | 29289 | jmvivo | FeatureProvider fdata) { |
50 | 25691 | jmvivo | this.isNewFeature = fdata.isNew();
|
51 | 24248 | jjdelcerro | this.oid = null; |
52 | this.pk = null; |
||
53 | this.storeRef = new WeakReference(store); |
||
54 | 28660 | jmvivo | this.featureTypeId = fdata.getType().getId();
|
55 | 25944 | vcaballero | |
56 | 28423 | jmvivo | if (fdata.getType().hasOID() || isNewFeature) {
|
57 | 25944 | vcaballero | this.oid = fdata.getOID();
|
58 | if (this.oid == null) { |
||
59 | 28423 | jmvivo | // FIXME Exception
|
60 | throw new RuntimeException("Missing OID"); |
||
61 | 24248 | jjdelcerro | } |
62 | 28423 | jmvivo | } else {
|
63 | 27570 | jmvivo | this.calculatePK(fdata);
|
64 | 25944 | vcaballero | if (this.pk == null) { |
65 | 28423 | jmvivo | // FIXME Exception
|
66 | throw new RuntimeException("Missing pk attributes"); |
||
67 | 25944 | vcaballero | } |
68 | 24248 | jjdelcerro | } |
69 | 25971 | vcaballero | |
70 | 24248 | jjdelcerro | } |
71 | |||
72 | 25691 | jmvivo | /*
|
73 | * Use only for Persistent.setState
|
||
74 | */
|
||
75 | 24248 | jjdelcerro | 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 | 28660 | jmvivo | // FIXME featureTypeId is needed !!!
|
84 | 24248 | jjdelcerro | 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 | 29289 | jmvivo | private void calculatePK(FeatureProvider fdata) { |
95 | 27570 | jmvivo | ArrayList keys = new ArrayList(); |
96 | ArrayList keyNames = new ArrayList(); |
||
97 | 24248 | jjdelcerro | 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 | 27570 | jmvivo | keys.add(fdata.get(attr.getIndex())); |
104 | keyNames.add(attr.getName()); |
||
105 | 24248 | jjdelcerro | } |
106 | } |
||
107 | 27570 | jmvivo | if (keys.size() < 1) { |
108 | pk = null;
|
||
109 | pkNames = null;
|
||
110 | } else {
|
||
111 | pk = keys.toArray(); |
||
112 | 33744 | cordinyana | pkNames = (String[]) keyNames.toArray(new String[keyNames.size()]); |
113 | 24248 | jjdelcerro | } |
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 | 28423 | jmvivo | |
133 | 25230 | jmvivo | 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 | 28423 | jmvivo | if (myHashCode != null && other.myHashCode != null) { |
148 | return myHashCode.equals(other.myHashCode);
|
||
149 | } |
||
150 | 25230 | jmvivo | 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 | 28423 | jmvivo | } |
165 | if (myHashCode == null) { |
||
166 | StringBuffer buff = new StringBuffer(); |
||
167 | 25971 | vcaballero | |
168 | 28423 | jmvivo | for (int i = 0; i < this.pk.length; i++) { |
169 | buff.append(this.pk[i].hashCode());
|
||
170 | buff.append("##");
|
||
171 | 25230 | jmvivo | } |
172 | 28423 | jmvivo | myHashCode = new Integer(buff.toString().hashCode()); |
173 | 25230 | jmvivo | } |
174 | 28423 | jmvivo | return myHashCode.intValue();
|
175 | 25230 | jmvivo | } |
176 | |||
177 | 27570 | jmvivo | public String[] getKeyNames() { |
178 | 27672 | jmvivo | return pkNames;
|
179 | 27570 | jmvivo | } |
180 | 25230 | jmvivo | |
181 | 27570 | jmvivo | public Object getKeyValue(String name) { |
182 | 27672 | jmvivo | for (int i = 0; i < pkNames.length; i++) { |
183 | if (pkNames[i].equalsIgnoreCase(name)) {
|
||
184 | return pk[i];
|
||
185 | } |
||
186 | } |
||
187 | // FIXME exception????
|
||
188 | 27570 | jmvivo | return null; |
189 | } |
||
190 | |||
191 | 28660 | jmvivo | public String getFeatureTypeId() { |
192 | return featureTypeId;
|
||
193 | } |
||
194 | 27570 | jmvivo | |
195 | 30208 | jmvivo | // *** Persistence ***
|
196 | 28660 | jmvivo | |
197 | 30208 | jmvivo | 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 | 33744 | cordinyana | this.pkNames =
|
212 | (String[]) pkNamesList.toArray(new String[pkList.size()]); |
||
213 | 30208 | jmvivo | } else {
|
214 | this.pk = null; |
||
215 | this.pkNames = null; |
||
216 | } |
||
217 | } |
||
218 | |||
219 | public void saveToState(PersistentState state) throws PersistenceException { |
||
220 | state.set("oid", oid);
|
||
221 | state.set("myHashCode", myHashCode);
|
||
222 | state.set("isNewFeature", isNewFeature);
|
||
223 | state.set("store", (Persistent) storeRef.get());
|
||
224 | state.set("featureTypeId", featureTypeId);
|
||
225 | if (pk == null) { |
||
226 | 33281 | jjdelcerro | state.setNull("pk");
|
227 | state.setNull("pkNames");
|
||
228 | 30208 | jmvivo | } else {
|
229 | 33281 | jjdelcerro | state.set("pk", pk);
|
230 | state.set("pkNames", pkNames);
|
||
231 | 30208 | jmvivo | } |
232 | |||
233 | } |
||
234 | |||
235 | public static void registerPersistent() { |
||
236 | 32880 | jjdelcerro | DynStruct definition = ToolsLocator.getPersistenceManager().addDefinition( |
237 | DefaultFeatureReference.class, |
||
238 | "Reference",
|
||
239 | "DefaultFeatureReference Persistent definition",
|
||
240 | null,
|
||
241 | null
|
||
242 | ); |
||
243 | 30208 | jmvivo | |
244 | 33281 | jjdelcerro | definition.addDynFieldObject("oid")
|
245 | .setClassOfValue(Object.class)
|
||
246 | 33739 | fdiaz | .setMandatory(false)
|
247 | 32880 | jjdelcerro | .setPersistent(true);
|
248 | |||
249 | 33281 | jjdelcerro | definition.addDynFieldBoolean("isNewFeature")
|
250 | 32880 | jjdelcerro | .setMandatory(true)
|
251 | .setPersistent(true);
|
||
252 | |||
253 | 33281 | jjdelcerro | definition.addDynFieldObject("store")
|
254 | 32880 | jjdelcerro | .setClassOfValue(FeatureStore.class) |
255 | .setMandatory(true)
|
||
256 | .setPersistent(true);
|
||
257 | 30208 | jmvivo | |
258 | 33281 | jjdelcerro | definition.addDynFieldInt("myHashCode")
|
259 | 33659 | fdiaz | .setMandatory(false)
|
260 | 32880 | jjdelcerro | .setPersistent(true);
|
261 | |||
262 | 33281 | jjdelcerro | definition.addDynFieldString("featureTypeId")
|
263 | 32880 | jjdelcerro | .setMandatory(true)
|
264 | .setPersistent(true);
|
||
265 | 30208 | jmvivo | |
266 | 33659 | fdiaz | definition.addDynFieldArray("pk")
|
267 | 33281 | jjdelcerro | .setClassOfItems(Object.class)
|
268 | 32880 | jjdelcerro | .setMandatory(false)
|
269 | .setPersistent(true);
|
||
270 | 30208 | jmvivo | |
271 | 33659 | fdiaz | definition.addDynFieldArray("pkNames")
|
272 | 33281 | jjdelcerro | .setClassOfItems(String.class)
|
273 | 33659 | fdiaz | .setMandatory(false)
|
274 | 32880 | jjdelcerro | .setPersistent(true);
|
275 | |||
276 | 30208 | jmvivo | } |
277 | |||
278 | 32900 | cordinyana | public String toString() { |
279 | return MessageFormat.format( |
||
280 | "FeatureReference: oid = {0}, myHashCode = {1}, "
|
||
281 | + "pks = {2}, pkNames = {3}, "
|
||
282 | + "isNewFeature = {4}, featureTypeId = {5}", new Object[] { |
||
283 | oid, myHashCode, (pk == null ? null : Arrays.asList(pk)), |
||
284 | (pkNames == null ? null : Arrays.asList(pkNames)), |
||
285 | new Boolean(isNewFeature), |
||
286 | featureTypeId }); |
||
287 | } |
||
288 | 30208 | jmvivo | |
289 | 24248 | jjdelcerro | } |