Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extDalTransformJoin / src / org / gvsig / app / join / dal / feature / JoinTransform.java @ 36450

History | View | Annotate | Download (14.4 KB)

1
package org.gvsig.app.join.dal.feature;
2

    
3
import java.util.ArrayList;
4
import java.util.Arrays;
5
import java.util.HashMap;
6
import java.util.Iterator;
7
import java.util.List;
8
import java.util.Map;
9
import java.util.Map.Entry;
10

    
11
import org.gvsig.fmap.dal.exception.DataException;
12
import org.gvsig.fmap.dal.feature.AbstractFeatureStoreTransform;
13
import org.gvsig.fmap.dal.feature.EditableFeature;
14
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
15
import org.gvsig.fmap.dal.feature.EditableFeatureType;
16
import org.gvsig.fmap.dal.feature.Feature;
17
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
18
import org.gvsig.fmap.dal.feature.FeatureQuery;
19
import org.gvsig.fmap.dal.feature.FeatureSet;
20
import org.gvsig.fmap.dal.feature.FeatureStore;
21
import org.gvsig.fmap.dal.feature.FeatureType;
22
import org.gvsig.tools.ToolsLocator;
23
import org.gvsig.tools.dispose.DisposableIterator;
24
import org.gvsig.tools.dynobject.DynStruct;
25
import org.gvsig.tools.evaluator.Evaluator;
26
import org.gvsig.tools.evaluator.EvaluatorData;
27
import org.gvsig.tools.evaluator.EvaluatorException;
28
import org.gvsig.tools.evaluator.EvaluatorFieldsInfo;
29
import org.gvsig.tools.persistence.PersistenceManager;
30
import org.gvsig.tools.persistence.PersistentState;
31
import org.gvsig.tools.persistence.exception.PersistenceException;
32

    
33
public class JoinTransform extends AbstractFeatureStoreTransform {
34

    
35
    public static final String PERSISTENCE_DEFINITION_NAME = "JoinTransform";
36
    
37
    /**
38
     * Store from which the join transform will get the additional attributes
39
     */
40
    private FeatureStore store2;
41

    
42
    /**
43
     * name of the key attr in store1 that will be used to match features in
44
     * store2
45
     */
46
    private String keyAttr1;
47

    
48
    /**
49
     * name of the key attr in store2 that will be used to match features in
50
     * store1
51
     */
52
    private String keyAttr2;
53

    
54
    /**
55
     * names of the attributes to join from store2 to store1
56
     */
57
    private String[] attrs;
58

    
59
    /**
60
     * Attribute names may change after transformation a prefix is applied.
61
     * This map keeps correspondence between store1 original names
62
     * and their transformed counterparts.
63
     */
64
    private Map<String,String> store1NamesMap;
65

    
66
    /**
67
     * Attribute names may change after transformation if they are repeated in
68
     * both stores or if a prefix is applied. This map keeps correspondence
69
     * between store2 original names and their transformed counterparts.
70
     */
71
    private Map<String,String> store2NamesMap;
72

    
73
    private JoinTransformEvaluator evaluator = null;
74

    
75
    private FeatureType originalFeatureType;
76

    
77
    private String[] attrsForQuery;
78

    
79
    private String prefix1;
80

    
81
    private String prefix2;
82

    
83
    /**
84
     * A default constructor
85
     */
86
    public JoinTransform() {
87
        store1NamesMap = new HashMap<String,String>();
88
        store2NamesMap = new HashMap<String,String>();
89
    }
90

    
91
    /**
92
     * Initializes all the necessary data for this transform
93
     *
94
     * @param store1
95
     *            store whose default feature type is the target of this
96
     *            transform
97
     *
98
     * @param store2
99
     *            store whose default feature type will provide the new
100
     *            attributes to join
101
     *
102
     * @param keyAttr1
103
     *            key attribute in store1 that matches keyAttr2 in store2
104
     *            (foreign key), used for joining both stores.
105
     *
106
     * @param keyAttr2
107
     *            key attribute in store2 that matches keyAttr1 in store2
108
     *            (foreign key), used for joining both stores.
109
     *
110
     * @param attrs
111
     *            names of the attributes in store2 that will be joined to
112
     *            store1.
113
     */
114
    public void initialize(FeatureStore store1, FeatureStore store2,
115
        String keyAttr1, String keyAttr2, String prefix1, String prefix2,
116
        String[] attrs)
117
    throws DataException {
118

    
119
        if (store1 == store2) {
120
            throw new IllegalArgumentException("store1 == store2");
121
        }
122

    
123
        // Initialize needed data
124
        this.setFeatureStore(store1);
125
        this.store2 = store2;
126
        this.keyAttr1 = keyAttr1;
127
        this.keyAttr2 = keyAttr2;
128
        this.prefix1 = prefix1; // TODO
129
        this.prefix2 = prefix2; // TODO
130
        this.attrs = attrs;
131

    
132
        // calculate this transform resulting feature type
133
        // by adding all specified attrs from store2 to store1's default
134
        // feature type
135
        // FIXME for more than one FTypes ??
136
        this.originalFeatureType = this.getFeatureStore()
137
        .getDefaultFeatureType();
138

    
139
        // Create the feature type and copy the store 1 type        
140
        EditableFeatureType editableFeatureType = this.getFeatureStore().getDefaultFeatureType().getEditable();
141
        FeatureAttributeDescriptor[] featureAttributeDescriptors = editableFeatureType.getAttributeDescriptors();
142
        for (int i=0 ; i<featureAttributeDescriptors.length ; i++){ 
143
            editableFeatureType.remove(featureAttributeDescriptors[i].getName());           
144
        }    
145
        addFeatureType(editableFeatureType, featureAttributeDescriptors, prefix1, store1NamesMap);
146

    
147
        // Add the store 2 fields    
148
        FeatureType featureType2 = store2.getDefaultFeatureType();
149

    
150
        // Add the fields       
151
        for (int i = 0; i < attrs.length; i++) {
152
            addFeatureType(editableFeatureType, featureType2.getAttributeDescriptor(attrs[i]), prefix2, store2NamesMap);        
153
        }
154

    
155
        if (this.store2NamesMap.containsKey(keyAttr2)) {
156
            this.attrsForQuery = this.attrs;
157
        } else {
158
            List<String> list = new ArrayList<String>(this.attrs.length + 1);
159
            list.addAll(Arrays.asList(this.attrs));
160
            list.add(keyAttr2);
161
            this.attrsForQuery = (String[]) list.toArray(new String[] {});
162
        }
163

    
164
        // assign calculated feature type as this transform's feature type
165
        FeatureType[] types = new FeatureType[] { editableFeatureType.getNotEditableCopy() };
166
        setFeatureTypes(Arrays.asList(types), types[0]);
167
    }
168

    
169
    private void addFeatureType(EditableFeatureType targetFeatureType, FeatureAttributeDescriptor[] featureAttributeDescriptors,
170
        String prefix, Map<String, String> storeMap) throws DataException{
171

    
172
        for (int i=0 ; i<featureAttributeDescriptors.length ; i++){         
173
            addFeatureType(targetFeatureType, featureAttributeDescriptors[i], prefix, storeMap);
174
        }
175
    }
176

    
177
    private void addFeatureType(EditableFeatureType targetFeatureType, FeatureAttributeDescriptor featureAttributeDescriptor,
178
        String prefix, Map<String, String> storeMap) throws DataException{
179

    
180
        String attName = featureAttributeDescriptor.getName();
181
        if ((prefix != null) && (!prefix.equals(""))){
182
            attName = prefix + "_" + attName;
183
        }
184

    
185
        // If an attribute already exists, calculate an alternate name and add it to our type
186
        int j = 0;
187
        while (targetFeatureType.getIndex(attName) >= 0) {
188
            attName = targetFeatureType.getAttributeDescriptor(attName).getName() + "_" + ++j;
189
        }
190

    
191
        EditableFeatureAttributeDescriptor editableFeatureAttributeDescriptor = 
192
            targetFeatureType.add(attName, featureAttributeDescriptor.getType(),
193
                featureAttributeDescriptor.getSize());
194
        editableFeatureAttributeDescriptor.setPrecision(featureAttributeDescriptor.getPrecision());
195

    
196
        // keep correspondence between original name and transformed name
197
        storeMap.put(featureAttributeDescriptor.getName(), attName);
198
    } 
199

    
200
    /**
201
     *
202
     *
203
     * @param source
204
     *
205
     * @param target
206
     *
207
     * @throws DataException
208
     */
209
    public void applyTransform(Feature source, EditableFeature target)
210
    throws DataException {
211

    
212
        // copy the data from store1 into the resulting feature
213
        this.copySourceToTarget(source, target);
214

    
215
        // ask store2 for the specified attributes, filtering by the key
216
        // attribute value
217
        // from the source feature
218
        JoinTransformEvaluator eval = this.getEvaluator();
219
        eval.updateValue(source.get(this.keyAttr1));
220

    
221
        FeatureQuery query = store2.createFeatureQuery();
222
        query.setAttributeNames(attrsForQuery);
223
        query.setFilter(eval);
224
        FeatureSet set = null;
225
        DisposableIterator itFeat = null;
226

    
227
        try {
228

    
229
            set = store2.getFeatureSet(query);
230
            // In this join implementation, we will take only the first matching
231
            // feature found in store2
232

    
233
            Feature feat;
234

    
235

    
236
            itFeat = set.iterator();
237
            if (itFeat.hasNext()) {
238
                feat = (Feature) itFeat.next();
239

    
240
                // copy all attributes from joined feature to target
241
                this.copyJoinToTarget(feat, target);
242
            }
243
        } finally {
244
            if (itFeat != null) {
245
                itFeat.dispose();
246
            }
247
            if (set != null) {
248
                set.dispose();
249
            }
250
        }
251
    }
252

    
253
    /**
254
     * @param feat
255
     * @param target
256
     */
257
    private void copyJoinToTarget(Feature join, EditableFeature target) {
258
        Iterator<Entry<String, String>> iter = store2NamesMap.entrySet()
259
        .iterator();
260
        Entry<String, String> entry;
261
        FeatureType trgType = target.getType();
262
        FeatureAttributeDescriptor attr;
263
        while (iter.hasNext()) {
264
            entry = iter.next();
265
            attr = trgType.getAttributeDescriptor((String) entry.getValue());
266
            if (attr != null) {
267
                target.set(attr.getIndex(), join.get((String) entry.getKey()));
268
            }
269
        }
270
    }
271

    
272
    /**
273
     * @param source
274
     * @param target
275
     */
276
    private void copySourceToTarget(Feature source, EditableFeature target) {
277
        FeatureAttributeDescriptor attr, attrTrg;
278
        FeatureType ftSrc = source.getType();
279
        FeatureType ftTrg = target.getType();
280

    
281

    
282
        for (int i = 0; i < source.getType().size(); i++) {
283
            attr = ftSrc.getAttributeDescriptor(i);
284
            attrTrg = ftTrg.getAttributeDescriptor(store1NamesMap.get(attr.getName()));
285
            if (attrTrg != null) {
286
                try {
287
                    target.set(attrTrg.getIndex(), source.get(i));
288
                } catch (IllegalArgumentException e) {
289
                    attrTrg = ftTrg.getAttributeDescriptor(attr.getName());
290
                    target.set(attrTrg.getIndex(), attrTrg.getDefaultValue());
291
                }
292

    
293
            }
294
        }
295

    
296
    }
297

    
298
    private JoinTransformEvaluator getEvaluator() {
299
        if (this.evaluator == null){
300
            this.evaluator = new JoinTransformEvaluator(keyAttr2);
301
        }
302
        return evaluator;
303

    
304
    }
305

    
306
    private class JoinTransformEvaluator implements Evaluator {
307

    
308
        private String attribute;
309
        private Object value;
310
        private String sql;
311
        private EvaluatorFieldsInfo info = null;
312

    
313
        //                private int attributeIndex;
314

    
315
        public JoinTransformEvaluator(String attribute) {
316
            this.attribute = attribute;
317
            this.value = null;
318
            this.info = new EvaluatorFieldsInfo();
319

    
320
            //                        this.attributeIndex = attrIndex;
321
        }
322

    
323
        public void updateValue(Object value) {
324
            this.value = value;
325
            this.sql = this.attribute + "= '" + this.value + "'";
326
            this.info = new EvaluatorFieldsInfo();
327
            this.info.addMatchFieldValue(this.attribute, value);
328
        }
329

    
330
        public Object evaluate(EvaluatorData arg0) throws EvaluatorException {
331
            Object curValue = arg0.getDataValue(attribute);
332
            if (curValue == null) {
333
                return value == null;
334
            }
335
            return curValue.equals(value);
336
        }
337

    
338
        public String getSQL() {
339
            return this.sql;
340
        }
341

    
342
        public String getDescription() {
343
            return "Evaluates join transform match";
344
        }
345

    
346
        public String getName() {
347
            return "JoinTransformEvaluator";
348
        }
349

    
350
        public EvaluatorFieldsInfo getFieldsInfo() {
351
            return this.info;
352
        }
353

    
354
    }        
355

    
356
    @SuppressWarnings("unchecked")
357
    public FeatureType getSourceFeatureTypeFrom(FeatureType arg0) {
358
        return originalFeatureType;
359
    }
360

    
361
    public boolean isTransformsOriginalValues() {
362
        return false;
363
    }
364

    
365
    public static void registerPersistent() {
366
        PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager();
367

    
368
        if( persistenceManager.getDefinition(AbstractFeatureStoreTransform.class) == null ) {
369
            AbstractFeatureStoreTransform.registerPersistent();
370
        }
371

    
372
        DynStruct definition = persistenceManager.getDefinition(PERSISTENCE_DEFINITION_NAME);
373

    
374
        if (definition == null){  
375
            definition = persistenceManager.addDefinition(
376
                JoinTransform.class,
377
                PERSISTENCE_DEFINITION_NAME,
378
                "JoinTransform Persistence definition",
379
                null, 
380
                null
381
            );
382
            definition.extend(PersistenceManager.PERSISTENCE_NAMESPACE,
383
                ABSTRACT_FEATURESTORE_DYNCLASS_NAME);
384

    
385
            definition.addDynFieldObject("store2").setClassOfValue(FeatureStore.class).setMandatory(true);
386
            definition.addDynFieldString("keyAttr1").setMandatory(true);
387
            definition.addDynFieldString("keyAttr2").setMandatory(true);
388
            definition.addDynFieldString("prefix1").setMandatory(true);
389
            definition.addDynFieldString("prefix2").setMandatory(true);
390
            definition.addDynFieldList("attrs").setClassOfItems(String.class).setMandatory(true);
391
        }
392
    }
393

    
394
    public void saveToState(PersistentState state) throws PersistenceException {
395
        super.saveToState(state);
396
        state.set("store2", this.store2);
397
        state.set("keyAttr1", this.keyAttr1);
398
        state.set("keyAttr2", this.keyAttr2);
399
        state.set("prefix1", this.prefix1);
400
        state.set("prefix2", this.prefix2);                
401
        state.set("attrs", this.attrs);
402
    }
403

    
404
    public void loadFromState(PersistentState state) throws PersistenceException {
405
        super.loadFromState(state);
406
        FeatureStore store2 = (FeatureStore) state.get("store2");
407
        String keyAttr1 = state.getString("keyAttr1");
408
        String keyAttr2 = state.getString("keyAttr2");
409
        String prefix2 =  state.getString("prefix2");
410
        String[] attrs = (String[]) state.getArray("attrs", String.class);
411
        try {
412
            initialize(getFeatureStore(), store2, keyAttr1, keyAttr2, prefix1, prefix2, attrs);
413
        } catch (DataException e) {
414
            throw new PersistenceException("Impossible to create the transform", e);
415
        }
416
    }
417

    
418
}