svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / DefaultFeatureReference.java @ 27264
History | View | Annotate | Download (4.73 KB)
1 | 24496 | jmvivo | package org.gvsig.fmap.dal.feature.impl; |
---|---|---|---|
2 | 24248 | jjdelcerro | |
3 | import java.lang.ref.WeakReference; |
||
4 | import java.util.ArrayList; |
||
5 | import java.util.Iterator; |
||
6 | |||
7 | 24505 | jmvivo | import org.gvsig.fmap.dal.exception.DataException; |
8 | 24496 | jmvivo | import org.gvsig.fmap.dal.feature.Feature; |
9 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
10 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
11 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
12 | import org.gvsig.fmap.dal.feature.spi.FeatureData; |
||
13 | import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
||
14 | 24248 | jjdelcerro | import org.gvsig.tools.persistence.AbstractPersistenceManager; |
15 | import org.gvsig.tools.persistence.PersistenceException; |
||
16 | import org.gvsig.tools.persistence.Persistent; |
||
17 | import org.gvsig.tools.persistence.PersistentState; |
||
18 | |||
19 | public class DefaultFeatureReference implements |
||
20 | FeatureReferenceProviderServices, Persistent { |
||
21 | |||
22 | private Object oid; |
||
23 | 25733 | jmvivo | private int fDataHashCode; |
24 | 24248 | jjdelcerro | private Object[] pk; |
25 | private WeakReference storeRef; |
||
26 | private boolean isNewFeature; |
||
27 | |||
28 | public DefaultFeatureReference(DefaultFeature feature) {
|
||
29 | 25691 | jmvivo | this(feature.getStore(), feature.getData());
|
30 | 24248 | jjdelcerro | } |
31 | |||
32 | public DefaultFeatureReference(FeatureStore store,
|
||
33 | 25691 | jmvivo | FeatureData fdata) { |
34 | this.isNewFeature = fdata.isNew();
|
||
35 | 24248 | jjdelcerro | this.oid = null; |
36 | this.pk = null; |
||
37 | this.storeRef = new WeakReference(store); |
||
38 | 25944 | vcaballero | |
39 | if (fdata.getType().hasOID()) {
|
||
40 | this.oid = fdata.getOID();
|
||
41 | if (this.oid == null) { |
||
42 | // FIXME: petar
|
||
43 | 24248 | jjdelcerro | } |
44 | 25944 | vcaballero | } else if (!isNewFeature) { |
45 | this.pk = this.calculatePK(fdata); |
||
46 | if (this.pk == null) { |
||
47 | // FIXME: pete
|
||
48 | } |
||
49 | 25971 | vcaballero | } else { // isNew && !hasOID |
50 | 25733 | jmvivo | fDataHashCode = fdata.hashCode(); |
51 | 24248 | jjdelcerro | } |
52 | 25971 | vcaballero | |
53 | 24248 | jjdelcerro | } |
54 | |||
55 | 25691 | jmvivo | /*
|
56 | * Use only for Persistent.setState
|
||
57 | */
|
||
58 | 24248 | jjdelcerro | public DefaultFeatureReference(FeatureStore store) {
|
59 | this.isNewFeature = false; |
||
60 | this.oid = null; |
||
61 | this.pk = null; |
||
62 | this.storeRef = new WeakReference(store); |
||
63 | } |
||
64 | |||
65 | public DefaultFeatureReference(FeatureStore store, Object oid) { |
||
66 | this.isNewFeature = false; |
||
67 | this.oid = oid;
|
||
68 | this.pk = null; |
||
69 | this.storeRef = new WeakReference(store); |
||
70 | } |
||
71 | |||
72 | public DefaultFeatureReference(FeatureStore store, Object[] pk) { |
||
73 | this.isNewFeature = false; |
||
74 | this.oid = null; |
||
75 | this.pk = pk;
|
||
76 | this.storeRef = new WeakReference(store); |
||
77 | } |
||
78 | |||
79 | |||
80 | private DefaultFeatureStore getStore() {
|
||
81 | return (DefaultFeatureStore) this.storeRef.get(); |
||
82 | } |
||
83 | |||
84 | private Object[] calculatePK(FeatureData fdata) { |
||
85 | ArrayList key = new ArrayList(); |
||
86 | FeatureType type = fdata.getType(); |
||
87 | Iterator it = type.iterator();
|
||
88 | while (it.hasNext()) {
|
||
89 | FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) it |
||
90 | .next(); |
||
91 | if (attr.isPrimaryKey()) {
|
||
92 | key.add(fdata.get(attr.getIndex())); |
||
93 | } |
||
94 | } |
||
95 | if (key.size() < 1) { |
||
96 | return null; |
||
97 | } |
||
98 | return key.toArray();
|
||
99 | } |
||
100 | |||
101 | public Feature getFeature() throws DataException { |
||
102 | return this.getStore().getFeatureByReference(this); |
||
103 | } |
||
104 | |||
105 | public Feature getFeature(FeatureType featureType) throws DataException { |
||
106 | return this.getStore().getFeatureByReference(this, featureType); |
||
107 | } |
||
108 | |||
109 | public Object[] getKey() { |
||
110 | return this.pk; |
||
111 | } |
||
112 | |||
113 | public Object getOID() { |
||
114 | return this.oid; |
||
115 | } |
||
116 | |||
117 | public boolean isNewFeature() { |
||
118 | return this.isNewFeature; |
||
119 | } |
||
120 | public PersistentState getState() throws PersistenceException { |
||
121 | return AbstractPersistenceManager.getState(this); |
||
122 | } |
||
123 | |||
124 | public void loadState(PersistentState state) throws PersistenceException { |
||
125 | state.set("oid", oid);
|
||
126 | state.set("isNewFeature", isNewFeature);
|
||
127 | 25666 | vcaballero | if (pk !=null){ |
128 | state.set("pk.elements", pk.length);
|
||
129 | for (int i = 0; i < pk.length; i++) { |
||
130 | state.set("pk.item" + i, pk[0]); |
||
131 | } |
||
132 | 24248 | jjdelcerro | } |
133 | |||
134 | } |
||
135 | |||
136 | public void setState(PersistentState state) throws PersistenceException { |
||
137 | this.oid = state.get("oid"); |
||
138 | this.isNewFeature = state.getBoolean("isNewFeature"); |
||
139 | 25666 | vcaballero | if (state.get("pk.elements")!=null){ |
140 | 24248 | jjdelcerro | this.pk = new Object[state.getInt("pk.elements")]; |
141 | 25666 | vcaballero | for (int i = 0; i < pk.length; i++) { |
142 | this.pk[i] = state.get("pk.item" + i); |
||
143 | } |
||
144 | 24248 | jjdelcerro | } |
145 | } |
||
146 | 25230 | jmvivo | |
147 | public boolean equals(Object obj) { |
||
148 | if (!(obj instanceof DefaultFeatureReference)) { |
||
149 | return false; |
||
150 | } |
||
151 | DefaultFeatureReference other = (DefaultFeatureReference) obj; |
||
152 | |||
153 | FeatureStore otherStore = (FeatureStore) other.storeRef.get(); |
||
154 | FeatureStore myrStore = (FeatureStore) this.storeRef.get();
|
||
155 | if (otherStore == null || myrStore == null) { |
||
156 | return false; |
||
157 | } |
||
158 | if (!myrStore.equals(otherStore)) {
|
||
159 | return false; |
||
160 | } |
||
161 | if (this.oid != null) { |
||
162 | return this.oid.equals(other.oid); |
||
163 | } |
||
164 | |||
165 | for (int i = 0; i < this.pk.length; i++) { |
||
166 | if (!this.pk[i].equals(other.pk[i])) { |
||
167 | return false; |
||
168 | } |
||
169 | } |
||
170 | return true; |
||
171 | } |
||
172 | |||
173 | public int hashCode() { |
||
174 | if (this.oid != null) { |
||
175 | return this.oid.hashCode(); |
||
176 | } else {
|
||
177 | 25971 | vcaballero | if (this.isNewFeature){ |
178 | return fDataHashCode;
|
||
179 | } |
||
180 | |||
181 | 25230 | jmvivo | StringBuffer buff = new StringBuffer(); |
182 | |||
183 | for (int i = 0; i < this.pk.length; i++) { |
||
184 | buff.append(this.pk[i].hashCode());
|
||
185 | buff.append("$");
|
||
186 | } |
||
187 | return buff.toString().hashCode();
|
||
188 | |||
189 | } |
||
190 | } |
||
191 | |||
192 | |||
193 | 24248 | jjdelcerro | } |