Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / DefaultFeatureIndex.java @ 33331

History | View | Annotate | Download (7.33 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
*
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
* MA  02110-1301, USA.
20
*
21
*/
22

    
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2008 {{Company}}   {{Task}}
26
*/
27

    
28

    
29
package org.gvsig.fmap.dal.feature.impl;
30

    
31
import java.io.File;
32
import java.util.ArrayList;
33
import java.util.Iterator;
34
import java.util.List;
35

    
36
import org.gvsig.fmap.dal.DataTypes;
37
import org.gvsig.fmap.dal.exception.DataException;
38
import org.gvsig.fmap.dal.exception.InitializeException;
39
import org.gvsig.fmap.dal.feature.Feature;
40
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
41
import org.gvsig.fmap.dal.feature.FeatureReference;
42
import org.gvsig.fmap.dal.feature.FeatureSet;
43
import org.gvsig.fmap.dal.feature.FeatureType;
44
import org.gvsig.fmap.dal.feature.exception.FeatureIndexException;
45
import org.gvsig.fmap.dal.feature.spi.DefaultLongList;
46
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices;
47
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices;
48
import org.gvsig.fmap.dal.feature.spi.index.FeatureIndexProvider;
49
import org.gvsig.fmap.dal.feature.spi.index.FeatureIndexProviderServices;
50
import org.gvsig.tools.dispose.DisposableIterator;
51
import org.gvsig.tools.dispose.DisposeUtils;
52

    
53
/**
54
 * Default feature index provider services.
55
 *
56
 * @author jyarza
57
 *
58
 */
59
public class DefaultFeatureIndex implements FeatureIndexProviderServices {
60

    
61
        private final FeatureStoreProviderServices featureStore;
62
        private final FeatureType featureType;
63
        private final String attributeName;
64
        private final String indexName;
65
        private final int dataType;
66
        private final FeatureIndexProvider indexProvider;
67
        private List attributeNames;
68

    
69
        public DefaultFeatureIndex(FeatureStoreProviderServices featureStore, FeatureType featureType, FeatureIndexProvider indexProvider, String attributeName, String indexName) {
70
                if (featureStore == null) {
71
                        throw new IllegalArgumentException("featureStore cannot be null.");
72
                }
73
                if (featureType == null) {
74
                        throw new IllegalArgumentException("featureType cannot be null.");
75
                }
76
                if (attributeName == null) {
77
                        throw new IllegalArgumentException("attributeName cannot be null.");
78
                }
79
                if (indexName == null) {
80
                        throw new IllegalArgumentException("indexName cannot be null.");
81
                }
82

    
83
                // FIXME Esto debe ir al provider
84
                if (featureStore.getProvider().getOIDType() != DataTypes.INT &&
85
                                featureStore.getProvider().getOIDType() != DataTypes.LONG) {
86
                        throw new IllegalArgumentException();
87
                }
88

    
89
                FeatureAttributeDescriptor attr = featureType.getAttributeDescriptor(attributeName);
90
                if (attr == null) {
91
                        throw new IllegalArgumentException("Attribute " + attributeName +" not found in FeatureType " + featureType.toString());
92
                }
93

    
94
                this.featureStore = featureStore;
95
                this.featureType = featureType;
96
                this.attributeName = attributeName;
97
                this.indexName = indexName;
98
                this.dataType = attr.getType();
99
                this.indexProvider = indexProvider;
100

    
101
                attributeNames = new ArrayList();
102
                attributeNames.add(attributeName);
103
        }
104

    
105
        public final FeatureAttributeDescriptor getFeatureAttributeDescriptor() {
106
                return featureType.getAttributeDescriptor(attributeName);
107
        }
108

    
109
        public final FeatureStoreProviderServices getFeatureStoreProviderServices() {
110
                return featureStore;
111
        }
112

    
113
        public final FeatureType getFeatureType() {
114
                return featureType;
115
        }
116

    
117
        public final String getName() {
118
                return indexName;
119
        }
120

    
121
        public final String getAttributeName() {
122
                return attributeName;
123
        }
124

    
125
        public final int getDataType() {
126
                return dataType;
127
        }
128

    
129
        /**
130
         * Fills this index with all the data in its FeatureStore
131
         */
132
        public final void fill() throws FeatureIndexException {
133
                FeatureSet set = null;
134
                try {
135
                        set =
136
                                        getFeatureStoreProviderServices().getFeatureStore()
137
                                                        .getFeatureSet();
138
                        insert(set);
139
                } catch (DataException e) {
140
                        throw new FeatureIndexException(e);
141
                } finally {
142
                        DisposeUtils.dispose(set);
143
                }
144
        }
145

    
146
        public final void insert(FeatureSet data) throws DataException {
147
                DisposableIterator it = null;
148

    
149
                try {
150
                        it = data.iterator();
151
                        while (it.hasNext()) {
152
                                Feature feat = (Feature) it.next();
153
                                insert(feat);
154
                        }
155
                } finally {
156
                        DisposeUtils.dispose(it);
157
                }
158
        }
159

    
160
        public void insert(Feature feat) {
161
                try {
162
                        indexProvider.insert(feat.get(attributeName),
163
                                        (FeatureReferenceProviderServices) feat.getReference());
164
                } catch (NullPointerException e) {
165
                        throw new IllegalArgumentException("Feature does not contain a column with name " + attributeName);
166
                } catch (ClassCastException e) {
167
                        throw new IllegalArgumentException("Attribute data type is not valid.");
168
                }
169
        }
170

    
171
        public FeatureSet getMatchFeatureSet(Object value)
172
                        throws FeatureIndexException {
173
                return new IndexFeatureSet(this, new DefaultLongList(indexProvider
174
                                .match(value)));
175
        }
176

    
177
        public FeatureSet getRangeFeatureSet(Object value1, Object value2)
178
                        throws FeatureIndexException {
179
                return new IndexFeatureSet(this, new DefaultLongList(indexProvider
180
                                .range(value1, value2)));
181
        }
182

    
183
        public FeatureSet getNearestFeatureSet(int count, Object value)
184
                        throws FeatureIndexException {
185
                return new IndexFeatureSet(this, new DefaultLongList(indexProvider
186
                                .nearest(count, value)));
187
        }
188

    
189
        public FeatureSet getNearestFeatureSet(int count, Object value,
190
                        Object tolerance) throws FeatureIndexException {
191
                return new IndexFeatureSet(this, new DefaultLongList(indexProvider
192
                                .nearest(count, value, tolerance)));
193
        }
194

    
195
        public void initialize() throws InitializeException {
196
                indexProvider.setFeatureIndexProviderServices(this);
197
                indexProvider.initialize();
198
        }
199

    
200
        public void delete(Object value, FeatureReference fref) {
201
                indexProvider.delete(value, (FeatureReferenceProviderServices) fref);
202
        }
203

    
204
        public void delete(Feature feat) {
205
                indexProvider.delete(feat.get(this.attributeName),
206
                                (FeatureReferenceProviderServices) feat.getReference());
207
        }
208

    
209
        public void delete(FeatureSet data) throws FeatureIndexException {
210
                DisposableIterator it = null;
211
                try {
212
                        it = data.iterator();
213
                        while (it.hasNext()) {
214
                                Feature feat = (Feature) it.next();
215
                                delete(feat);
216
                        }
217
                } catch (DataException e) {
218
                        throw new FeatureIndexException(e);
219
                } finally {
220
                        DisposeUtils.dispose(it);
221
                }
222
        }
223

    
224
        public List getAttributeNames() {
225
                return attributeNames;
226
        }
227

    
228
        public String getNewFileName(String prefix, String sufix) {
229
                int n=0;
230
                File file = new File(prefix + getName(), sufix);
231
                while (file.exists()) {
232
                        n++;
233
                        file = new File(prefix + getName() + n, sufix);
234
                }
235
                return file.getAbsolutePath();
236
        }
237

    
238
        public String getFileName() {
239
                // TODO Auto-generated method stub
240
                return null;
241
        }
242

    
243
        public String getTemporaryFileName() {
244
                // TODO Auto-generated method stub
245
                return null;
246
        }
247

    
248
        public FeatureIndexProvider getFeatureIndexProvider() {
249
                return this.indexProvider;
250
        }
251

    
252
}
253