Statistics
| Revision:

root / tags / v2_0_0_Build_2047 / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / DefaultFeatureReference.java @ 38284

History | View | Annotate | Download (7.71 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 38106 cordinyana
                if(pk != null) {
154
                        if(other.pk == null) {
155 25230 jmvivo
                                return false;
156
                        }
157 38106 cordinyana
                    for (int i = 0; i < this.pk.length; i++) {
158
                            if (!this.pk[i].equals(other.pk[i])) {
159
                                    return false;
160
                            }
161
                    }
162 25230 jmvivo
                }
163
                return true;
164
        }
165
166
        public int hashCode() {
167
                if (this.oid != null) {
168
                        return this.oid.hashCode();
169 28423 jmvivo
                }
170
                if (myHashCode == null) {
171
                        StringBuffer buff = new StringBuffer();
172 25971 vcaballero
173 28423 jmvivo
                        for (int i = 0; i < this.pk.length; i++) {
174
                                buff.append(this.pk[i].hashCode());
175
                                buff.append("##");
176 25230 jmvivo
                        }
177 28423 jmvivo
                        myHashCode = new Integer(buff.toString().hashCode());
178 25230 jmvivo
                }
179 28423 jmvivo
                return myHashCode.intValue();
180 25230 jmvivo
        }
181
182 27570 jmvivo
        public String[] getKeyNames() {
183 27672 jmvivo
                return pkNames;
184 27570 jmvivo
        }
185 25230 jmvivo
186 27570 jmvivo
        public Object getKeyValue(String name) {
187 27672 jmvivo
                for (int i = 0; i < pkNames.length; i++) {
188
                        if (pkNames[i].equalsIgnoreCase(name)) {
189
                                return pk[i];
190
                        }
191
                }
192
                // FIXME exception????
193 27570 jmvivo
                return null;
194
        }
195
196 28660 jmvivo
        public String getFeatureTypeId() {
197
                return featureTypeId;
198
        }
199 27570 jmvivo
200 30208 jmvivo
        // *** Persistence ***
201 28660 jmvivo
202 30208 jmvivo
        public void loadFromState(PersistentState state)
203
                        throws PersistenceException {
204
                this.oid = state.get("oid");
205
                this.myHashCode = (Integer) state.get("myHashCode");
206
                this.storeRef = new WeakReference(state.get("store"));
207
                this.isNewFeature = state.getBoolean("isNewFeature");
208
                this.featureTypeId = state.getString("featureTypeId");
209
                List pkList = (List) state.get("pk");
210
                if (pkList != null) {
211
                        List pkNamesList = (List) state.get("pkNames");
212
                        if (pkNamesList == null || pkList.size() != pkNamesList.size()) {
213
                                throw new PersistenceException("bad pkNames value");
214
                        }
215
                        this.pk = pkList.toArray();
216 33744 cordinyana
            this.pkNames =
217
                (String[]) pkNamesList.toArray(new String[pkList.size()]);
218 30208 jmvivo
                } else {
219
                        this.pk = null;
220
                        this.pkNames = null;
221
                }
222
        }
223
224
        public void saveToState(PersistentState state) throws PersistenceException {
225
                state.set("oid", oid);
226
                state.set("myHashCode", myHashCode);
227
                state.set("isNewFeature", isNewFeature);
228
                state.set("store", (Persistent) storeRef.get());
229
                state.set("featureTypeId", featureTypeId);
230
                if (pk == null) {
231 33281 jjdelcerro
                        state.setNull("pk");
232
                        state.setNull("pkNames");
233 30208 jmvivo
                } else {
234 33281 jjdelcerro
                        state.set("pk", pk);
235
                        state.set("pkNames", pkNames);
236 30208 jmvivo
                }
237
238
        }
239
240
        public static void registerPersistent() {
241 32880 jjdelcerro
                DynStruct definition = ToolsLocator.getPersistenceManager().addDefinition(
242
                                DefaultFeatureReference.class,
243
                                "Reference",
244
                                "DefaultFeatureReference Persistent definition",
245
                                null,
246
                                null
247
                        );
248 30208 jmvivo
249 33281 jjdelcerro
                definition.addDynFieldObject("oid")
250
                        .setClassOfValue(Object.class)
251 33739 fdiaz
                        .setMandatory(false)
252 32880 jjdelcerro
                        .setPersistent(true);
253
254 33281 jjdelcerro
                definition.addDynFieldBoolean("isNewFeature")
255 32880 jjdelcerro
                        .setMandatory(true)
256
                        .setPersistent(true);
257
258 33281 jjdelcerro
                definition.addDynFieldObject("store")
259 32880 jjdelcerro
                        .setClassOfValue(FeatureStore.class)
260
                        .setMandatory(true)
261
                        .setPersistent(true);
262 30208 jmvivo
263 33281 jjdelcerro
                definition.addDynFieldInt("myHashCode")
264 33659 fdiaz
                        .setMandatory(false)
265 32880 jjdelcerro
                        .setPersistent(true);
266
267 33281 jjdelcerro
                definition.addDynFieldString("featureTypeId")
268 32880 jjdelcerro
                        .setMandatory(true)
269
                        .setPersistent(true);
270 30208 jmvivo
271 33659 fdiaz
                definition.addDynFieldArray("pk")
272 33281 jjdelcerro
                        .setClassOfItems(Object.class)
273 32880 jjdelcerro
                        .setMandatory(false)
274
                        .setPersistent(true);
275 30208 jmvivo
276 33659 fdiaz
        definition.addDynFieldArray("pkNames")
277 33281 jjdelcerro
                        .setClassOfItems(String.class)
278 33659 fdiaz
                        .setMandatory(false)
279 32880 jjdelcerro
                        .setPersistent(true);
280
281 30208 jmvivo
        }
282
283 32900 cordinyana
        public String toString() {
284
                return MessageFormat.format(
285
                                "FeatureReference: oid = {0}, myHashCode = {1}, "
286
                                + "pks = {2}, pkNames = {3}, "
287
                                + "isNewFeature = {4}, featureTypeId = {5}", new Object[] {
288
                                oid, myHashCode, (pk == null ? null : Arrays.asList(pk)),
289
                                (pkNames == null ? null : Arrays.asList(pkNames)),
290
                                new Boolean(isNewFeature),
291
                                featureTypeId });
292
        }
293 30208 jmvivo
294 24248 jjdelcerro
}