Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dal / src-test / org / gvsig / fmap / dal / feature / impl / JoinTransform.java @ 25587

History | View | Annotate | Download (5.02 KB)

1
package org.gvsig.fmap.dal.feature.impl;
2

    
3
import java.util.HashMap;
4
import java.util.Iterator;
5
import java.util.Map;
6

    
7
import org.gvsig.fmap.dal.DALLocator;
8
import org.gvsig.fmap.dal.exception.DataException;
9
import org.gvsig.fmap.dal.feature.AbstractFeatureStoreTransform;
10
import org.gvsig.fmap.dal.feature.EditableFeature;
11
import org.gvsig.fmap.dal.feature.EditableFeatureType;
12
import org.gvsig.fmap.dal.feature.Feature;
13
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
14
import org.gvsig.fmap.dal.feature.FeatureQuery;
15
import org.gvsig.fmap.dal.feature.FeatureSet;
16
import org.gvsig.fmap.dal.feature.FeatureStore;
17
import org.gvsig.tools.evaluator.Evaluator;
18
import org.gvsig.tools.persistence.PersistenceException;
19
import org.gvsig.tools.persistence.PersistentState;
20

    
21
public class JoinTransform extends AbstractFeatureStoreTransform {
22

    
23
        /**
24
         * Store whose default feature type will be transformed
25
         */
26
        private FeatureStore store1;
27

    
28
        /**
29
         * Store from which the join transform will get the additional attributes
30
         */
31
        private FeatureStore store2;
32

    
33
        /**
34
         * name of the key attr in store1 that will be used to match features in
35
         * store2
36
         */
37
        private String keyAttr1;
38

    
39
        /**
40
         * name of the key attr in store2 that will be used to match features in
41
         * store1
42
         */
43
        private String keyAttr2;
44

    
45
        /**
46
         * names of the attributes to join from store2 to store1
47
         */
48
        private String[] attrs;
49

    
50
        /**
51
         * Attribute names may change after transformation if they are repeated in
52
         * both stores. This map keeps correspondence between store2 original names
53
         * and their transformed counterparts.
54
         */
55
        private Map targetNamesMap;
56

    
57
        /**
58
         * A default constructor
59
         */
60
        public JoinTransform() {
61
                targetNamesMap = new HashMap();
62
        }
63

    
64
        /**
65
         * Initializes all the necessary data for this transform
66
         *
67
         * @param store1
68
         *            store whose default feature type is the target of this
69
         *            transform
70
         *
71
         * @param store2
72
         *            store whose default feature type will provide the new
73
         *            attributes to join
74
         *
75
         * @param keyAttr1
76
         *            key attribute in store1 that matches keyAttr2 in store2
77
         *            (foreign key), used for joining both stores.
78
         *
79
         * @param keyAttr2
80
         *            key attribute in store2 that matches keyAttr1 in store2
81
         *            (foreign key), used for joining both stores.
82
         *
83
         * @param attrs
84
         *            names of the attributes in store2 that will be joined to
85
         *            store1.
86
         */
87
        public void initialize(FeatureStore store1, FeatureStore store2,
88
                        String keyAttr1, String keyAttr2, String[] attrs)
89
                        throws DataException {
90

    
91
                // Initialize needed data
92
                this.store1 = store1;
93
                this.store2 = store2;
94
                this.keyAttr1 = keyAttr1;
95
                this.keyAttr2 = keyAttr2;
96
                this.attrs = attrs;
97

    
98
                // calculate this transform resulting feature type
99
                // by adding all specified attrs from store2 to store1's default
100
                // feature type
101
                EditableFeatureType type = this.store1.getDefaultFeatureType().getEditable();
102

    
103
                for (int i = 0; i < attrs.length; i++) {
104
                        String name = attrs[i];
105

    
106
                        // If an attribute already exists with the same name in store1's
107
                        // default feature type,
108
                        // calculate an alternate name and add it to our type
109
                        int j = 0;
110
                        while (type.get(name) != null) {
111
                                name = attrs[i] + "_" + ++j;
112
                        }
113
                        type.add(name, store2.getDefaultFeatureType()
114
                                        .getAttributeDescriptor(attrs[i]).getDataType());
115

    
116
                        // keep correspondence between original name and transformed name
117
                        this.targetNamesMap.put(attrs[i], name);
118
                }
119

    
120
                // assign calculated feature type as this transform's feature type
121
                setDefaultFeatureType(type.getNotEditableCopy());
122
        }
123

    
124
        /**
125
         *
126
         *
127
         * @param source
128
         *
129
         * @param target
130
         *
131
         * @throws DataException
132
         */
133
        public void applyTransform(Feature source, EditableFeature target)
134
                        throws DataException {
135

    
136
                // copy the data from store1 into the resulting feature
137
                target.copyFrom(source);
138

    
139
                // ask store2 for the specified attributes, filtering by the key
140
                // attribute value
141
                // from the source feature
142
                Evaluator eval = DALLocator.getDataManager().createExpresion(
143
                                keyAttr2 + "=" + source.get(keyAttr1));
144
                FeatureQuery query = new FeatureQuery(attrs, eval);
145
                FeatureSet set = store2.getFeatureSet(query);
146

    
147
                // In this join implementation, we will take only the first matching
148
                // feature found in store2
149
                Iterator it = set.iterator();
150
                if (it.hasNext()) {
151
                        Feature feat = (Feature) it.next();
152

    
153
                        // copy all attributes from joined feature to target
154
                        Iterator it2 = feat.getType().iterator();
155
                        while (it2.hasNext()) {
156
                                FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) it2
157
                                                .next();
158
                                // find original attribute name
159
                                String targetName = (String) this.targetNamesMap.get(attr
160
                                                .getName());
161
                                // copy its value to target feature attribute
162
                                target.set(targetName, feat.get(attr.getName()));
163
                        }
164
                }
165
                set.dispose();
166
        }
167

    
168
        public void loadState(PersistentState state) throws PersistenceException {
169
                // TODO Auto-generated method stub
170

    
171
        }
172

    
173
        public void setState(PersistentState state) throws PersistenceException {
174
                // TODO Auto-generated method stub
175

    
176
        }
177

    
178
}