svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.lib / src / main / java / org / gvsig / fmap / dal / feature / spi / simpleprovider / SimpleSequentialReaderStoreProvider.java @ 44321
History | View | Annotate | Download (48.8 KB)
1 | 44057 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2013 gvSIG Association.
|
||
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 3
|
||
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 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | */
|
||
24 | package org.gvsig.fmap.dal.feature.spi.simpleprovider; |
||
25 | |||
26 | import java.io.File; |
||
27 | import java.io.IOException; |
||
28 | import java.util.ArrayList; |
||
29 | import java.util.HashMap; |
||
30 | import java.util.Iterator; |
||
31 | import java.util.List; |
||
32 | import java.util.Locale; |
||
33 | 44251 | jjdelcerro | import java.util.Map; |
34 | 44057 | jjdelcerro | |
35 | import org.apache.commons.lang3.StringUtils; |
||
36 | import org.cresques.cts.IProjection; |
||
37 | 44307 | jjdelcerro | import org.gvsig.fmap.dal.BaseStoresRepository; |
38 | 44057 | jjdelcerro | import org.gvsig.fmap.dal.DALLocator; |
39 | import org.gvsig.fmap.dal.DataManager; |
||
40 | import org.gvsig.fmap.dal.DataServerExplorer; |
||
41 | import org.gvsig.fmap.dal.DataStore; |
||
42 | import org.gvsig.fmap.dal.DataStoreNotification; |
||
43 | import org.gvsig.fmap.dal.DataTypes; |
||
44 | import org.gvsig.fmap.dal.FileHelper; |
||
45 | 44304 | jjdelcerro | import org.gvsig.fmap.dal.StoresRepository; |
46 | 44057 | jjdelcerro | import org.gvsig.fmap.dal.exception.DataException; |
47 | import org.gvsig.fmap.dal.exception.InitializeException; |
||
48 | import org.gvsig.fmap.dal.exception.OpenException; |
||
49 | import org.gvsig.fmap.dal.exception.ReadException; |
||
50 | import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
||
51 | import org.gvsig.fmap.dal.feature.EditableFeature; |
||
52 | import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
||
53 | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
||
54 | import org.gvsig.fmap.dal.feature.Feature; |
||
55 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
56 | import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator; |
||
57 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
58 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
59 | 44307 | jjdelcerro | import org.gvsig.fmap.dal.feature.OpenFeatureStoreParameters; |
60 | 44057 | jjdelcerro | import org.gvsig.fmap.dal.feature.exception.PerformEditingException; |
61 | import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
||
62 | import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices; |
||
63 | import org.gvsig.fmap.dal.feature.spi.memory.AbstractMemoryStoreProvider; |
||
64 | import org.gvsig.fmap.dal.resource.file.FileResource; |
||
65 | import org.gvsig.fmap.dal.resource.spi.ResourceConsumer; |
||
66 | import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
||
67 | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
||
68 | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
||
69 | 44251 | jjdelcerro | import org.gvsig.fmap.dal.spi.DALSPILocator; |
70 | import org.gvsig.fmap.dal.spi.DataManagerProviderServices; |
||
71 | 44057 | jjdelcerro | import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
72 | import org.gvsig.fmap.geom.Geometry; |
||
73 | import org.gvsig.fmap.geom.GeometryLocator; |
||
74 | import org.gvsig.fmap.geom.GeometryManager; |
||
75 | 44242 | jjdelcerro | import org.gvsig.fmap.geom.GeometryUtils; |
76 | 44057 | jjdelcerro | import org.gvsig.fmap.geom.aggregate.MultiPoint; |
77 | import org.gvsig.fmap.geom.primitive.Envelope; |
||
78 | import org.gvsig.fmap.geom.primitive.Point; |
||
79 | import org.gvsig.fmap.geom.type.GeometryType; |
||
80 | 44251 | jjdelcerro | import org.gvsig.timesupport.Interval; |
81 | 44057 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
82 | import org.gvsig.tools.dataTypes.CoercionException; |
||
83 | 44307 | jjdelcerro | import org.gvsig.tools.dataTypes.DataTypeUtils; |
84 | 44057 | jjdelcerro | import org.gvsig.tools.dataTypes.DataTypesManager; |
85 | import org.gvsig.tools.dataTypes.DataTypesManager.Coercion; |
||
86 | import org.gvsig.tools.dataTypes.DataTypesManager.CoercionWithLocale; |
||
87 | 44307 | jjdelcerro | import org.gvsig.tools.dispose.Disposable; |
88 | import org.gvsig.tools.dispose.DisposeUtils; |
||
89 | 44251 | jjdelcerro | import org.gvsig.tools.dynobject.Tags; |
90 | 44057 | jjdelcerro | import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException; |
91 | import org.gvsig.tools.exception.BaseException; |
||
92 | import org.gvsig.tools.task.SimpleTaskStatus; |
||
93 | import org.gvsig.tools.task.TaskStatusManager; |
||
94 | 44259 | jjdelcerro | import org.gvsig.tools.util.UnmodifiableBasicMap; |
95 | import org.gvsig.tools.util.UnmodifiableBasicMapToMapAdapter; |
||
96 | import org.gvsig.tools.util.UnmodifiableBasicSet; |
||
97 | 44057 | jjdelcerro | import org.gvsig.tools.visitor.VisitCanceledException; |
98 | import org.gvsig.tools.visitor.Visitor; |
||
99 | import org.slf4j.Logger; |
||
100 | import org.slf4j.LoggerFactory; |
||
101 | |||
102 | 44242 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
103 | public class SimpleSequentialReaderStoreProvider extends AbstractMemoryStoreProvider implements |
||
104 | 44057 | jjdelcerro | ResourceConsumer { |
105 | |||
106 | 44242 | jjdelcerro | private static final Logger LOGGER = LoggerFactory.getLogger(SimpleSequentialReaderStoreProvider.class); |
107 | 44057 | jjdelcerro | |
108 | 44307 | jjdelcerro | private class StoresRepositoryWithChildren extends BaseStoresRepository { |
109 | |||
110 | public StoresRepositoryWithChildren() {
|
||
111 | super(SimpleSequentialReaderStoreProvider.this.getName());
|
||
112 | } |
||
113 | |||
114 | @Override
|
||
115 | public DataStore getStore(String name) { |
||
116 | if( StringUtils.equalsIgnoreCase(name, readerData.getName()) ||
|
||
117 | StringUtils.equalsIgnoreCase(name, readerData.getAlias()) ) { |
||
118 | return readerData.getStore();
|
||
119 | } |
||
120 | for (ReaderData child : childrenData) {
|
||
121 | if( StringUtils.equalsIgnoreCase(name, child.getName()) ||
|
||
122 | StringUtils.equalsIgnoreCase(name, child.getAlias()) ) { |
||
123 | return child.getStore();
|
||
124 | } |
||
125 | } |
||
126 | return super.getStore(name); |
||
127 | } |
||
128 | |||
129 | } |
||
130 | |||
131 | private class ReaderData implements Disposable { |
||
132 | 44251 | jjdelcerro | private FeatureType defaultFeatureType;
|
133 | private List<FeatureType> featureTypes; |
||
134 | 44307 | jjdelcerro | private List<FeatureProvider> features; |
135 | 44251 | jjdelcerro | private boolean needCalculateEnvelope; |
136 | private String name; |
||
137 | 44307 | jjdelcerro | private String alias; |
138 | 44251 | jjdelcerro | private long OIDcounter; |
139 | private DataStore store;
|
||
140 | 44304 | jjdelcerro | private StoresRepository storesRepository;
|
141 | 44307 | jjdelcerro | private OpenFeatureStoreParameters parameters;
|
142 | 44057 | jjdelcerro | |
143 | 44251 | jjdelcerro | public ReaderData() {
|
144 | this.needCalculateEnvelope = false; |
||
145 | this.features = new ArrayList<>(); |
||
146 | this.OIDcounter = 0; |
||
147 | this.store = null; |
||
148 | } |
||
149 | 44307 | jjdelcerro | |
150 | @Override
|
||
151 | public void dispose() { |
||
152 | DisposeUtils.disposeQuietly(store); |
||
153 | this.store = null; |
||
154 | this.features = null; |
||
155 | this.OIDcounter = 0; |
||
156 | this.defaultFeatureType = null; |
||
157 | this.featureTypes = null; |
||
158 | this.parameters = null; |
||
159 | this.storesRepository = null; |
||
160 | } |
||
161 | 44251 | jjdelcerro | |
162 | public void addFeatureProvider(FeatureProvider feature) { |
||
163 | feature.setOID(this.OIDcounter++);
|
||
164 | this.features.add(feature);
|
||
165 | } |
||
166 | |||
167 | public void setFeatureTypes(List<FeatureType> featureTypes, FeatureType defaultFeatureType) { |
||
168 | this.featureTypes = featureTypes;
|
||
169 | this.defaultFeatureType = defaultFeatureType;
|
||
170 | } |
||
171 | |||
172 | public void setNeedCalculateEnvelope(boolean needCalculateEnvelope) { |
||
173 | this.needCalculateEnvelope = needCalculateEnvelope;
|
||
174 | } |
||
175 | |||
176 | public boolean getNeedCalculateEnvelope() { |
||
177 | return this.needCalculateEnvelope; |
||
178 | } |
||
179 | |||
180 | public String getName() { |
||
181 | return name;
|
||
182 | } |
||
183 | |||
184 | public void setName(String name) { |
||
185 | this.name = name;
|
||
186 | } |
||
187 | |||
188 | public List<FeatureType> getFeatureTypes() { |
||
189 | return this.featureTypes; |
||
190 | } |
||
191 | |||
192 | public FeatureType getDefaultFeatureType() {
|
||
193 | return this.defaultFeatureType; |
||
194 | } |
||
195 | |||
196 | public List<FeatureProvider> getFeatures() { |
||
197 | return this.features; |
||
198 | } |
||
199 | |||
200 | 44307 | jjdelcerro | public DataStore getStore() {
|
201 | 44251 | jjdelcerro | if( this.store == null ) { |
202 | 44307 | jjdelcerro | try {
|
203 | SimpleSequentialReaderStoreProvider provider = new SimpleSequentialReaderStoreProvider(
|
||
204 | readerFactory, |
||
205 | (SimpleSequentialReaderStoreParameters) getParameters(), |
||
206 | null,
|
||
207 | childrenData, |
||
208 | this
|
||
209 | ); |
||
210 | DataManagerProviderServices manager = DALSPILocator.getDataManagerProviderServices(); |
||
211 | this.store = manager.openStore(
|
||
212 | getParameters(), |
||
213 | provider |
||
214 | ); |
||
215 | provider.setStoreServices((FeatureStoreProviderServices) this.store);
|
||
216 | provider.name = this.name;
|
||
217 | DisposeUtils.bind(this.store);
|
||
218 | } catch(Exception ex) { |
||
219 | LOGGER.warn("Can't build store form child '"+name+"'.",ex); |
||
220 | return null; |
||
221 | } |
||
222 | 44251 | jjdelcerro | } |
223 | 44307 | jjdelcerro | DisposeUtils.bind(this.store);
|
224 | 44251 | jjdelcerro | return this.store; |
225 | } |
||
226 | 44304 | jjdelcerro | |
227 | public void setStoresRepository(StoresRepository storesRepository) { |
||
228 | this.storesRepository = storesRepository;
|
||
229 | } |
||
230 | |||
231 | public StoresRepository getStoresRepository() {
|
||
232 | return storesRepository;
|
||
233 | } |
||
234 | 44307 | jjdelcerro | |
235 | public void setParameters(OpenFeatureStoreParameters parameters) { |
||
236 | if( parameters==null ) { |
||
237 | LOGGER.warn("Can't set parameters to null");
|
||
238 | return;
|
||
239 | } |
||
240 | this.parameters = parameters;
|
||
241 | } |
||
242 | |||
243 | public OpenFeatureStoreParameters getParameters() {
|
||
244 | return parameters;
|
||
245 | } |
||
246 | |||
247 | public String getAlias() { |
||
248 | return this.alias; |
||
249 | } |
||
250 | |||
251 | public void setAlias(String alias) { |
||
252 | this.alias = alias;
|
||
253 | } |
||
254 | 44304 | jjdelcerro | |
255 | 44307 | jjdelcerro | |
256 | 44251 | jjdelcerro | } |
257 | 44259 | jjdelcerro | |
258 | class Children implements UnmodifiableBasicMap<String, DataStore> { |
||
259 | // Con esta clase se pospone la creacion de los stores hasta que se
|
||
260 | // pide cada uno de ellos, y no cuando se pide el Map de estos, ya
|
||
261 | // que el map se puede pedir solo para saber si hay o cuantos hay.
|
||
262 | 44251 | jjdelcerro | |
263 | 44259 | jjdelcerro | @Override
|
264 | public DataStore get(String key) { |
||
265 | for (ReaderData child : childrenData) {
|
||
266 | if( StringUtils.equalsIgnoreCase(child.getName(), key) ) {
|
||
267 | 44307 | jjdelcerro | return child.getStore();
|
268 | 44259 | jjdelcerro | } |
269 | } |
||
270 | return null; |
||
271 | } |
||
272 | |||
273 | @Override
|
||
274 | public boolean isEmpty() { |
||
275 | return childrenData.isEmpty();
|
||
276 | } |
||
277 | |||
278 | @Override
|
||
279 | public boolean containsKey(String key) { |
||
280 | for (ReaderData child : childrenData) {
|
||
281 | if( StringUtils.equalsIgnoreCase(child.getName(), key) ) {
|
||
282 | return true; |
||
283 | } |
||
284 | } |
||
285 | return false; |
||
286 | } |
||
287 | |||
288 | @Override
|
||
289 | public Map<String, DataStore> toMap() { |
||
290 | return new UnmodifiableBasicMapToMapAdapter<>(this); |
||
291 | } |
||
292 | |||
293 | @Override
|
||
294 | public int size() { |
||
295 | return childrenData.size();
|
||
296 | } |
||
297 | |||
298 | @Override
|
||
299 | public UnmodifiableBasicSet<String> keySet() { |
||
300 | return new UnmodifiableBasicSet<String>() { |
||
301 | |||
302 | @Override
|
||
303 | public boolean isEmpty() { |
||
304 | return childrenData.isEmpty();
|
||
305 | } |
||
306 | |||
307 | @Override
|
||
308 | public int size() { |
||
309 | return childrenData.size();
|
||
310 | } |
||
311 | |||
312 | @Override
|
||
313 | public Iterator<String> iterator() { |
||
314 | final Iterator<ReaderData> it = childrenData.iterator(); |
||
315 | return new Iterator<String>() { |
||
316 | @Override
|
||
317 | public boolean hasNext() { |
||
318 | return it.hasNext();
|
||
319 | } |
||
320 | |||
321 | @Override
|
||
322 | public String next() { |
||
323 | ReaderData theReaderData = it.next(); |
||
324 | return theReaderData.getName();
|
||
325 | } |
||
326 | }; |
||
327 | } |
||
328 | }; |
||
329 | } |
||
330 | |||
331 | @Override
|
||
332 | public Iterator<DataStore> iterator() { |
||
333 | final Iterator<String> it = this.keySet().iterator(); |
||
334 | return new Iterator<DataStore>() { |
||
335 | @Override
|
||
336 | public boolean hasNext() { |
||
337 | return it.hasNext();
|
||
338 | } |
||
339 | |||
340 | @Override
|
||
341 | public DataStore next() {
|
||
342 | String name = it.next();
|
||
343 | return get(name);
|
||
344 | } |
||
345 | }; |
||
346 | } |
||
347 | |||
348 | } |
||
349 | |||
350 | 44057 | jjdelcerro | private final ResourceProvider resource; |
351 | |||
352 | private Envelope envelope;
|
||
353 | private boolean need_calculate_envelope = false; |
||
354 | private String name = ""; |
||
355 | 44242 | jjdelcerro | private final SimpleSequentialReaderFactory readerFactory; |
356 | 44259 | jjdelcerro | private List<ReaderData> childrenData; |
357 | 44251 | jjdelcerro | private ReaderData readerData;
|
358 | 44307 | jjdelcerro | private final boolean isAChild; |
359 | 44057 | jjdelcerro | |
360 | 44242 | jjdelcerro | public SimpleSequentialReaderStoreProvider(
|
361 | SimpleSequentialReaderFactory readerFactory, |
||
362 | SimpleSequentialReaderStoreParameters parameters, |
||
363 | 44057 | jjdelcerro | DataStoreProviderServices storeServices |
364 | ) throws InitializeException {
|
||
365 | 44251 | jjdelcerro | this(readerFactory, parameters, storeServices, new ArrayList<ReaderData>(), null); |
366 | } |
||
367 | |||
368 | private SimpleSequentialReaderStoreProvider(
|
||
369 | SimpleSequentialReaderFactory readerFactory, |
||
370 | SimpleSequentialReaderStoreParameters parameters, |
||
371 | DataStoreProviderServices storeServices, |
||
372 | List<ReaderData> childrenData,
|
||
373 | ReaderData readerData |
||
374 | |||
375 | ) throws InitializeException {
|
||
376 | 44057 | jjdelcerro | super(
|
377 | parameters, |
||
378 | storeServices, |
||
379 | FileHelper.newMetadataContainer(readerFactory.getName()) |
||
380 | ); |
||
381 | 44251 | jjdelcerro | this.childrenData = childrenData;
|
382 | this.readerData = readerData;
|
||
383 | 44057 | jjdelcerro | this.readerFactory = readerFactory;
|
384 | 44307 | jjdelcerro | this.isAChild = this.readerData!=null; |
385 | |||
386 | 44057 | jjdelcerro | File file = getParameters().getFile();
|
387 | resource = this.createResource(
|
||
388 | FileResource.NAME, |
||
389 | new Object[]{file.getAbsolutePath()} |
||
390 | ); |
||
391 | |||
392 | resource.addConsumer(this);
|
||
393 | 44259 | jjdelcerro | if( this.readerData!=null ) { |
394 | this.name = this.readerData.getName(); |
||
395 | } |
||
396 | 44251 | jjdelcerro | if( storeServices != null ) { |
397 | initializeFeatureTypes(); |
||
398 | } |
||
399 | } |
||
400 | 44307 | jjdelcerro | |
401 | private boolean isChild() { |
||
402 | return this.isAChild; |
||
403 | } |
||
404 | |||
405 | 44251 | jjdelcerro | private void setStoreServices(FeatureStoreProviderServices storeServices) throws InitializeException { |
406 | this.store = storeServices;
|
||
407 | 44057 | jjdelcerro | initializeFeatureTypes(); |
408 | } |
||
409 | 44307 | jjdelcerro | |
410 | @Override
|
||
411 | public FeatureStoreProviderServices getStoreServices() {
|
||
412 | if( this.store==null ) { |
||
413 | // Por aqui no deberia de pasar.
|
||
414 | // Si pasa es que algo va mal.
|
||
415 | LOGGER.warn("Algo no anda bien, el store es null");
|
||
416 | DataStore theStore = this.readerData.getStore();
|
||
417 | if( theStore==null ) { |
||
418 | return null; |
||
419 | } |
||
420 | this.store = (FeatureStoreProviderServices) theStore ;
|
||
421 | } |
||
422 | return this.store; |
||
423 | } |
||
424 | 44251 | jjdelcerro | |
425 | 44057 | jjdelcerro | @Override
|
426 | 44242 | jjdelcerro | public SimpleSequentialReaderStoreParameters getParameters() {
|
427 | return (SimpleSequentialReaderStoreParameters) super.getParameters(); |
||
428 | 44057 | jjdelcerro | } |
429 | |||
430 | @Override
|
||
431 | public String getProviderName() { |
||
432 | 44242 | jjdelcerro | if( this.readerFactory==null ) { |
433 | return "unknown"; |
||
434 | } |
||
435 | return this.readerFactory.getName(); |
||
436 | 44057 | jjdelcerro | } |
437 | |||
438 | @Override
|
||
439 | public boolean allowWrite() { |
||
440 | return false; |
||
441 | } |
||
442 | |||
443 | private String getFullFileName() { |
||
444 | // Usar solo para mostrar mensajes en el logger.
|
||
445 | String s;
|
||
446 | try {
|
||
447 | s = getParameters().getFile().getAbsolutePath(); |
||
448 | } catch (Exception e2) { |
||
449 | s = "(unknow)";
|
||
450 | } |
||
451 | return s;
|
||
452 | } |
||
453 | |||
454 | @Override
|
||
455 | public void open() throws OpenException { |
||
456 | if ( this.data != null ) { |
||
457 | return;
|
||
458 | } |
||
459 | this.data = new ArrayList<>(); |
||
460 | resource.setData(new HashMap()); |
||
461 | try {
|
||
462 | loadFeatures(); |
||
463 | } catch (RuntimeException e) { |
||
464 | 44242 | jjdelcerro | LOGGER.warn("Can't load features from '"+getProviderName()+"' file '" + getFullFileName() + "'.", e); |
465 | 44057 | jjdelcerro | throw e;
|
466 | } catch (Exception e) { |
||
467 | 44242 | jjdelcerro | LOGGER.warn("Can't load features from '"+getProviderName()+"' file '" + getFullFileName() + "'.", e); |
468 | 44057 | jjdelcerro | throw new RuntimeException(e); |
469 | } |
||
470 | } |
||
471 | |||
472 | @Override
|
||
473 | public DataServerExplorer getExplorer() throws ReadException { |
||
474 | DataManager manager = DALLocator.getDataManager(); |
||
475 | FilesystemServerExplorerParameters params; |
||
476 | try {
|
||
477 | params = (FilesystemServerExplorerParameters) manager |
||
478 | .createServerExplorerParameters(FilesystemServerExplorer.NAME); |
||
479 | params.setRoot(this.getParameters().getFile().getParent());
|
||
480 | return manager.openServerExplorer(FilesystemServerExplorer.NAME, params);
|
||
481 | } catch (DataException | ValidateDataParametersException e) {
|
||
482 | throw new ReadException(this.getProviderName(), e); |
||
483 | } |
||
484 | |||
485 | } |
||
486 | |||
487 | @Override
|
||
488 | public void performChanges(Iterator deleteds, Iterator inserteds, Iterator updateds, Iterator originalFeatureTypesUpdated) throws PerformEditingException { |
||
489 | throw new UnsupportedOperationException(); |
||
490 | } |
||
491 | |||
492 | @Override
|
||
493 | public boolean closeResourceRequested(ResourceProvider resource) { |
||
494 | return true; |
||
495 | } |
||
496 | |||
497 | @Override
|
||
498 | public int getOIDType() { |
||
499 | return DataTypes.LONG;
|
||
500 | } |
||
501 | |||
502 | @Override
|
||
503 | public boolean supportsAppendMode() { |
||
504 | return false; |
||
505 | } |
||
506 | |||
507 | @Override
|
||
508 | public void append(FeatureProvider featureProvider) { |
||
509 | throw new UnsupportedOperationException(); |
||
510 | } |
||
511 | |||
512 | @Override
|
||
513 | public void beginAppend() { |
||
514 | throw new UnsupportedOperationException(); |
||
515 | } |
||
516 | |||
517 | @Override
|
||
518 | public void endAppend() { |
||
519 | throw new UnsupportedOperationException(); |
||
520 | } |
||
521 | |||
522 | @Override
|
||
523 | public Object createNewOID() { |
||
524 | 44251 | jjdelcerro | throw new UnsupportedOperationException(); |
525 | 44057 | jjdelcerro | } |
526 | |||
527 | 44307 | jjdelcerro | @Override
|
528 | protected void doDispose() throws BaseException { |
||
529 | for (ReaderData theReaderData : this.childrenData) { |
||
530 | DisposeUtils.disposeQuietly(theReaderData); |
||
531 | } |
||
532 | this.childrenData = null; |
||
533 | DisposeUtils.disposeQuietly(this.readerData);
|
||
534 | this.readerData = null; |
||
535 | // this.resource = null;
|
||
536 | // this.readerFactory = null;
|
||
537 | this.envelope = null; |
||
538 | super.doDispose();
|
||
539 | } |
||
540 | |||
541 | |||
542 | private void initializeFeatureTypes() throws InitializeException { |
||
543 | 44057 | jjdelcerro | try {
|
544 | this.open();
|
||
545 | } catch (OpenException e) {
|
||
546 | throw new InitializeException(this.getProviderName(), e); |
||
547 | } |
||
548 | } |
||
549 | |||
550 | @Override
|
||
551 | public Envelope getEnvelope() throws DataException { |
||
552 | this.open();
|
||
553 | if ( this.envelope != null ) { |
||
554 | return this.envelope; |
||
555 | } |
||
556 | if ( !this.need_calculate_envelope ) { |
||
557 | return null; |
||
558 | } |
||
559 | FeatureStore fs = this.getFeatureStore();
|
||
560 | FeatureType ft = fs.getDefaultFeatureType(); |
||
561 | FeatureAttributeDescriptor fad = ft.getAttributeDescriptor(ft.getDefaultGeometryAttributeIndex()); |
||
562 | |||
563 | try {
|
||
564 | this.envelope = GeometryLocator.getGeometryManager().createEnvelope(fad.getGeomType().getSubType());
|
||
565 | fs.accept(new Visitor() {
|
||
566 | @Override
|
||
567 | public void visit(Object obj) throws VisitCanceledException, BaseException { |
||
568 | Feature f = (Feature) obj; |
||
569 | Geometry geom = f.getDefaultGeometry(); |
||
570 | if ( geom != null ) { |
||
571 | envelope.add(geom.getEnvelope()); |
||
572 | } |
||
573 | } |
||
574 | }); |
||
575 | } catch (BaseException e) {
|
||
576 | 44242 | jjdelcerro | LOGGER.warn("Can't calculate the envelope of '"+getProviderName()+"' file '" + this.getFullName() + "'.", e); |
577 | 44057 | jjdelcerro | this.envelope = null; |
578 | } |
||
579 | |||
580 | this.need_calculate_envelope = false; |
||
581 | return this.envelope; |
||
582 | } |
||
583 | |||
584 | @Override
|
||
585 | public Object getDynValue(String name) throws DynFieldNotFoundException { |
||
586 | if ( DataStore.METADATA_ENVELOPE.equalsIgnoreCase(name) ) {
|
||
587 | try {
|
||
588 | return this.getEnvelope(); |
||
589 | } catch (DataException e) {
|
||
590 | return null; |
||
591 | } |
||
592 | } else {
|
||
593 | if ( DataStore.METADATA_CRS.equalsIgnoreCase(name) ) {
|
||
594 | IProjection pro = this.getParameters().getCRS();
|
||
595 | if ( pro != null ) { |
||
596 | return pro;
|
||
597 | } |
||
598 | 44242 | jjdelcerro | FeatureType type; |
599 | try {
|
||
600 | type = this.getStoreServices().getDefaultFeatureType();
|
||
601 | pro = type.getDefaultSRS(); |
||
602 | if( pro!=null ) { |
||
603 | return pro;
|
||
604 | } |
||
605 | } catch (DataException ex) {
|
||
606 | } |
||
607 | 44057 | jjdelcerro | } |
608 | } |
||
609 | return super.getDynValue(name); |
||
610 | } |
||
611 | |||
612 | @Override
|
||
613 | public void resourceChanged(ResourceProvider resource) { |
||
614 | this.getStoreServices().notifyChange(
|
||
615 | DataStoreNotification.RESOURCE_CHANGED, |
||
616 | resource); |
||
617 | } |
||
618 | |||
619 | @Override
|
||
620 | public Object getSourceId() { |
||
621 | return this.getParameters().getFile(); |
||
622 | } |
||
623 | |||
624 | @Override
|
||
625 | public String getName() { |
||
626 | return this.name; |
||
627 | } |
||
628 | |||
629 | @Override
|
||
630 | public String getFullName() { |
||
631 | return this.getParameters().getFile().getAbsolutePath(); |
||
632 | } |
||
633 | |||
634 | @Override
|
||
635 | public ResourceProvider getResource() {
|
||
636 | return resource;
|
||
637 | } |
||
638 | |||
639 | 44251 | jjdelcerro | private String[] split(String value, String separators) { |
640 | int firstSeparatorPosition = 1000000; |
||
641 | Character sep = null; |
||
642 | for (char ch : separators.toCharArray()) { |
||
643 | int pos = value.indexOf(ch);
|
||
644 | if( pos>0 && pos<firstSeparatorPosition ) { |
||
645 | sep = ch; |
||
646 | firstSeparatorPosition = pos; |
||
647 | } |
||
648 | } |
||
649 | if( sep == null ) { |
||
650 | return new String[] { value }; |
||
651 | } |
||
652 | return value.split("["+sep+"]"); |
||
653 | } |
||
654 | |||
655 | 44057 | jjdelcerro | private class FieldTypeParser { |
656 | |||
657 | public String name = null; |
||
658 | public int type = DataTypes.STRING; |
||
659 | public int size = 0; |
||
660 | 44242 | jjdelcerro | public int geomType = Geometry.TYPES.GEOMETRY; |
661 | public int geomSubtype = Geometry.SUBTYPES.GEOM2D; |
||
662 | 44251 | jjdelcerro | public Map<String,String> tags = new HashMap<>(); |
663 | public Map<String,String> assignments = new HashMap<>(); |
||
664 | 44057 | jjdelcerro | |
665 | private String typename = "string"; |
||
666 | |||
667 | FieldTypeParser() { |
||
668 | } |
||
669 | |||
670 | private int getType(String value) { |
||
671 | DataTypesManager dataTypesManager = ToolsLocator.getDataTypesManager(); |
||
672 | return dataTypesManager.getType(typename);
|
||
673 | } |
||
674 | |||
675 | @SuppressWarnings("UseSpecificCatch") |
||
676 | public boolean parse(String value) { |
||
677 | 44242 | jjdelcerro | String[] args; |
678 | 44251 | jjdelcerro | if ( value.contains("__") ) { |
679 | 44242 | jjdelcerro | args = value.split("__");
|
680 | } else {
|
||
681 | 44251 | jjdelcerro | args = split(value, ":/#@!;-");
|
682 | if( args.length == 1 ) { |
||
683 | this.name = value;
|
||
684 | return true; |
||
685 | } |
||
686 | } |
||
687 | 44242 | jjdelcerro | int n = 0; |
688 | this.name = args[n++];
|
||
689 | if( n >= args.length ) {
|
||
690 | return true; |
||
691 | } |
||
692 | this.typename = args[n++];
|
||
693 | this.type = this.getType(this.typename); |
||
694 | if ( this.type == DataTypes.INVALID ) { |
||
695 | this.geomType = GeometryUtils.getGeometryType(this.typename); |
||
696 | if( this.geomType==Geometry.TYPES.UNKNOWN ) { |
||
697 | this.type = DataTypes.STRING;
|
||
698 | LOGGER.info("Type '" + this.typename + "' not valid for attribute '" + value + "' in '"+getProviderName()+"' file '" + getFullFileName() + "'."); |
||
699 | } else {
|
||
700 | this.typename = "GEOMETRY"; |
||
701 | this.type = DataTypes.GEOMETRY;
|
||
702 | } |
||
703 | } |
||
704 | switch(this.type) { |
||
705 | case DataTypes.STRING:
|
||
706 | this.size = 50; |
||
707 | break;
|
||
708 | case DataTypes.INT:
|
||
709 | this.size = 10; |
||
710 | break;
|
||
711 | case DataTypes.LONG:
|
||
712 | this.size = 20; |
||
713 | break;
|
||
714 | case DataTypes.FLOAT:
|
||
715 | this.size = 10; |
||
716 | break;
|
||
717 | case DataTypes.DOUBLE:
|
||
718 | this.size = 20; |
||
719 | break;
|
||
720 | default:
|
||
721 | this.size = 0; |
||
722 | } |
||
723 | while (n < args.length) {
|
||
724 | String option = args[n++].toLowerCase();
|
||
725 | switch (option) {
|
||
726 | case "size": |
||
727 | try {
|
||
728 | this.size = Integer.parseInt(args[n++]); |
||
729 | } catch (Exception ex) { |
||
730 | LOGGER.warn("Ignore incorrect field size for field " + value + " in '"+getProviderName()+"' file '" + getFullFileName() + "'.", ex); |
||
731 | } |
||
732 | break;
|
||
733 | 44251 | jjdelcerro | case "tag": { |
734 | String x = args[n++];
|
||
735 | int pos = x.indexOf("="); |
||
736 | if( pos < 0 ) { |
||
737 | this.tags.put(x, null); |
||
738 | } else {
|
||
739 | this.tags.put(
|
||
740 | StringUtils.substring(x, 0, pos),
|
||
741 | StringUtils.substring(x, pos+1)
|
||
742 | ); |
||
743 | } |
||
744 | break;
|
||
745 | } |
||
746 | case "set": { |
||
747 | String x = args[n++];
|
||
748 | int pos = x.indexOf("="); |
||
749 | if( pos < 0 ) { |
||
750 | this.assignments.put(x, null); |
||
751 | } else {
|
||
752 | this.assignments.put(
|
||
753 | StringUtils.substring(x, 0, pos),
|
||
754 | StringUtils.substring(x, pos+1)
|
||
755 | ); |
||
756 | } |
||
757 | break;
|
||
758 | } |
||
759 | 44242 | jjdelcerro | default:
|
760 | 44259 | jjdelcerro | LOGGER.warn("Illegal argumente '"+option+"' for field '"+this.name+"' in '"+getProviderName()+"' file '" + getFullFileName() + "' ("+value+")."); |
761 | 44057 | jjdelcerro | } |
762 | } |
||
763 | return true; |
||
764 | } |
||
765 | |||
766 | } |
||
767 | |||
768 | private EditableFeatureType getFeatureType(List<String> headers, int automaticTypes[]) { |
||
769 | EditableFeatureType fType = getStoreServices().createFeatureType(this.getName());
|
||
770 | fType.setHasOID(true);
|
||
771 | 44242 | jjdelcerro | // DataTypesManager dataTypesManager = ToolsLocator.getDataTypesManager();
|
772 | 44057 | jjdelcerro | |
773 | FieldTypeParser[] fieldTypes = new FieldTypeParser[headers.size()]; |
||
774 | //
|
||
775 | // Calculamos cuales pueden ser los tipos de datos
|
||
776 | //
|
||
777 | for ( int i = 0; i < fieldTypes.length; i++ ) { |
||
778 | fieldTypes[i] = new FieldTypeParser();
|
||
779 | } |
||
780 | |||
781 | // Asuminos los tipos pasados por parametro, que se supone
|
||
782 | // son los detectados automaticamente.
|
||
783 | if ( automaticTypes != null ) { |
||
784 | for ( int i = 0; i < fieldTypes.length && i < automaticTypes.length; i++ ) { |
||
785 | fieldTypes[i].type = automaticTypes[i]; |
||
786 | } |
||
787 | } |
||
788 | 44242 | jjdelcerro | // Luego probamos con lo que diga las cabezeras, sobreescribiendo
|
789 | 44057 | jjdelcerro | // los tipos anteriores en caso de definirse en la cabezara.
|
790 | for ( int i = 0; i < fieldTypes.length; i++ ) { |
||
791 | 44242 | jjdelcerro | fieldTypes[i].parse(headers.get(i)); |
792 | 44057 | jjdelcerro | } |
793 | |||
794 | // Y por ultimo hacemos caso a lo que se haya especificado en los parametros
|
||
795 | // de apertura del reader, teniendo esto prioridad sobre todo.
|
||
796 | int[] param_types = this.getParameters().getFieldTypes(); |
||
797 | if ( param_types != null ) { |
||
798 | for ( int i = 0; i < fieldTypes.length && i < param_types.length; i++ ) { |
||
799 | fieldTypes[i].type = param_types[i]; |
||
800 | } |
||
801 | } |
||
802 | |||
803 | int[] param_sizes = this.getParameters().getFieldSizes(); |
||
804 | if ( param_sizes != null ) { |
||
805 | for ( int i = 0; i < param_sizes.length; i++ ) { |
||
806 | if ( param_sizes[i] > 0 ) { |
||
807 | fieldTypes[i].size = param_sizes[i]; |
||
808 | } |
||
809 | } |
||
810 | } |
||
811 | //
|
||
812 | // Una vez ya sabemos los tipos de datos rellenamos el feature-type
|
||
813 | //
|
||
814 | 44251 | jjdelcerro | DataTypesManager dataTypesManager = ToolsLocator.getDataTypesManager(); |
815 | 44057 | jjdelcerro | for (FieldTypeParser fieldType : fieldTypes) {
|
816 | EditableFeatureAttributeDescriptor fad = fType.add(fieldType.name, fieldType.type); |
||
817 | fad.setSize(fieldType.size); |
||
818 | 44242 | jjdelcerro | if (fieldType.type == DataTypes.GEOMETRY ) {
|
819 | fad.setGeometryType(fieldType.geomType, fieldType.geomSubtype); |
||
820 | if( fType.getDefaultGeometryAttributeName() == null ) { |
||
821 | fType.setDefaultGeometryAttributeName(fieldType.name); |
||
822 | } |
||
823 | 44057 | jjdelcerro | } |
824 | 44251 | jjdelcerro | for (Map.Entry<String, String> entry : fieldType.assignments.entrySet()) { |
825 | try {
|
||
826 | switch(entry.getKey().toLowerCase()) {
|
||
827 | case "hidden": |
||
828 | 44307 | jjdelcerro | fad.setHidden(DataTypeUtils.toBoolean(entry.getValue(), false));
|
829 | 44251 | jjdelcerro | break;
|
830 | case "allownull": |
||
831 | 44307 | jjdelcerro | fad.setAllowNull(DataTypeUtils.toBoolean(entry.getValue(), false));
|
832 | 44251 | jjdelcerro | break;
|
833 | 44262 | jjdelcerro | case "pk": |
834 | case "ispk": |
||
835 | case "primarykey": |
||
836 | case "isprimarykey": |
||
837 | 44307 | jjdelcerro | fad.setIsPrimaryKey(DataTypeUtils.toBoolean(entry.getValue(), false));
|
838 | 44262 | jjdelcerro | break;
|
839 | 44251 | jjdelcerro | case "isautomatic": |
840 | case "automatic": |
||
841 | 44307 | jjdelcerro | fad.setIsAutomatic(DataTypeUtils.toBoolean(entry.getValue(), false));
|
842 | 44251 | jjdelcerro | break;
|
843 | case "time": |
||
844 | case "istime": |
||
845 | 44307 | jjdelcerro | fad.setIsTime(DataTypeUtils.toBoolean(entry.getValue(), false));
|
846 | 44251 | jjdelcerro | break;
|
847 | case "profile": |
||
848 | 44307 | jjdelcerro | fad.setDataProfileName(DataTypeUtils.toString(entry.getValue(), ""));
|
849 | 44251 | jjdelcerro | break;
|
850 | case "group": |
||
851 | 44307 | jjdelcerro | fad.setGroup(DataTypeUtils.toString(entry.getValue(), ""));
|
852 | 44251 | jjdelcerro | break;
|
853 | case "description": |
||
854 | 44307 | jjdelcerro | fad.setDescription(DataTypeUtils.toString(entry.getValue(), ""));
|
855 | 44251 | jjdelcerro | break;
|
856 | case "label": |
||
857 | 44307 | jjdelcerro | fad.setLabel(DataTypeUtils.toString(entry.getValue(), ""));
|
858 | 44251 | jjdelcerro | break;
|
859 | case "expression": |
||
860 | // Los campos calculados los procesamos en una segunda
|
||
861 | // pasada, cuando ya estan definidos el resto de los campos
|
||
862 | // ya que pueden requerir campos que aun no se han definido.
|
||
863 | break;
|
||
864 | case "size": |
||
865 | 44307 | jjdelcerro | fad.setSize(DataTypeUtils.toInteger(entry.getValue(), 50));
|
866 | 44251 | jjdelcerro | break;
|
867 | case "precision": |
||
868 | 44307 | jjdelcerro | fad.setPrecision(DataTypeUtils.toInteger(entry.getValue(), 10));
|
869 | 44251 | jjdelcerro | break;
|
870 | case "order": |
||
871 | 44307 | jjdelcerro | fad.setOrder(DataTypeUtils.toInteger(entry.getValue(), 0));
|
872 | 44251 | jjdelcerro | break;
|
873 | 44307 | jjdelcerro | case "foreingkey": |
874 | fad.getForeingKey().setForeingKey(DataTypeUtils.toBoolean(entry.getValue(), false));
|
||
875 | break;
|
||
876 | case "foreingkey.code": |
||
877 | fad.getForeingKey().setCodeName(DataTypeUtils.toString(entry.getValue(), ""));
|
||
878 | break;
|
||
879 | case "foreingkey.label": |
||
880 | fad.getForeingKey().setLabelFormula(DataTypeUtils.toString(entry.getValue(), ""));
|
||
881 | break;
|
||
882 | case "foreingkey.selectable": |
||
883 | fad.getForeingKey().setSelectable(DataTypeUtils.toBoolean(entry.getValue(), false));
|
||
884 | break;
|
||
885 | case "foreingkey.table": |
||
886 | fad.getForeingKey().setTableName(DataTypeUtils.toString(entry.getValue(), ""));
|
||
887 | break;
|
||
888 | 44251 | jjdelcerro | case "interval": |
889 | fad.setInterval((Interval) dataTypesManager.coerce(DataTypes.INTERVAL, entry.getValue())); |
||
890 | break;
|
||
891 | case "geomtype": |
||
892 | case "geometrytype": |
||
893 | fad.setGeometryType(entry.getValue()); |
||
894 | break;
|
||
895 | case "srs": |
||
896 | fad.setSRS(entry.getValue()); |
||
897 | break;
|
||
898 | } |
||
899 | } catch (Exception ex) { |
||
900 | LOGGER.warn("Can't set property '"+entry.getKey()+"' of '"+fad.getName()+"'.", ex); |
||
901 | } |
||
902 | } |
||
903 | Tags tags = fad.getTags(); |
||
904 | for (Map.Entry<String, String> entry : fieldType.tags.entrySet()) { |
||
905 | tags.set(entry.getKey(), entry.getValue()); |
||
906 | } |
||
907 | 44057 | jjdelcerro | } |
908 | 44251 | jjdelcerro | // Processamos ahora los campos calculados
|
909 | for (FieldTypeParser fieldType : fieldTypes) {
|
||
910 | EditableFeatureAttributeDescriptor fad = fType.getEditableAttributeDescriptor(fieldType.name); |
||
911 | for (Map.Entry<String, String> entry : fieldType.assignments.entrySet()) { |
||
912 | try {
|
||
913 | switch(entry.getKey().toLowerCase()) {
|
||
914 | case "expression": |
||
915 | fad.setFeatureAttributeEmulator((String) dataTypesManager.coerce(DataTypes.STRING, entry.getValue()));
|
||
916 | break;
|
||
917 | } |
||
918 | } catch (Exception ex) { |
||
919 | LOGGER.warn("Can't set property '"+entry.getKey()+"' in '"+fad.getName()+"' of '"+getFullFileName()+"'.", ex); |
||
920 | } |
||
921 | } |
||
922 | } |
||
923 | |||
924 | 44057 | jjdelcerro | String[] pointDimensionNames = this.getParameters().getPointDimensionNames(); |
925 | if ( pointDimensionNames != null ) { |
||
926 | PointAttributeEmulator emulator = new PointAttributeEmulator(pointDimensionNames);
|
||
927 | EditableFeatureAttributeDescriptor attr = fType.add("the_geom", DataTypes.GEOMETRY, emulator);
|
||
928 | GeometryManager geommgr = GeometryLocator.getGeometryManager(); |
||
929 | GeometryType gt; |
||
930 | try {
|
||
931 | if ( emulator.fieldNames != null && emulator.fieldNames.length <= 2 ) { |
||
932 | gt = geommgr.getGeometryType(Geometry.TYPES.GEOMETRY, Geometry.SUBTYPES.GEOM2D); |
||
933 | } else {
|
||
934 | gt = geommgr.getGeometryType(Geometry.TYPES.GEOMETRY, Geometry.SUBTYPES.GEOM3D); |
||
935 | } |
||
936 | attr.setGeometryType(gt); |
||
937 | } catch (Exception e) { |
||
938 | 44242 | jjdelcerro | LOGGER.warn("Can't set geometry type for the calculated field in '"+getProviderName()+"' file '" + getFullFileName() + "'.", e); |
939 | 44057 | jjdelcerro | } |
940 | } |
||
941 | return fType;
|
||
942 | } |
||
943 | |||
944 | 44242 | jjdelcerro | class PointAttributeEmulator implements FeatureAttributeEmulator { |
945 | 44057 | jjdelcerro | |
946 | private static final int XNAME = 0; |
||
947 | private static final int YNAME = 1; |
||
948 | private static final int ZNAME = 2; |
||
949 | |||
950 | private final GeometryManager geommgr; |
||
951 | private final String[] fieldNames; |
||
952 | private final Coercion toDouble; |
||
953 | private int errorcount = 0; |
||
954 | |||
955 | public PointAttributeEmulator(String[] pointDimensionNames) { |
||
956 | if ( pointDimensionNames.length > 2 ) { |
||
957 | this.fieldNames = new String[3]; |
||
958 | this.fieldNames[ZNAME] = pointDimensionNames[2]; |
||
959 | } else {
|
||
960 | this.fieldNames = new String[2]; |
||
961 | } |
||
962 | this.fieldNames[XNAME] = pointDimensionNames[0]; |
||
963 | this.fieldNames[YNAME] = pointDimensionNames[1]; |
||
964 | this.geommgr = GeometryLocator.getGeometryManager();
|
||
965 | DataTypesManager datatypeManager = ToolsLocator.getDataTypesManager(); |
||
966 | |||
967 | this.toDouble = datatypeManager.getCoercion(DataTypes.DOUBLE);
|
||
968 | } |
||
969 | |||
970 | @Override
|
||
971 | @SuppressWarnings("UseSpecificCatch") |
||
972 | public Object get(Feature feature) { |
||
973 | try {
|
||
974 | Object valueX = feature.get(this.fieldNames[XNAME]); |
||
975 | valueX = toDouble.coerce(valueX); |
||
976 | if ( valueX == null ) { |
||
977 | return null; |
||
978 | } |
||
979 | Object valueY = feature.get(this.fieldNames[YNAME]); |
||
980 | valueY = toDouble.coerce(valueY); |
||
981 | if ( valueY == null ) { |
||
982 | return null; |
||
983 | } |
||
984 | Object valueZ = null; |
||
985 | if ( this.fieldNames.length > 2 ) { |
||
986 | valueZ = toDouble.coerce(feature.get(this.fieldNames[ZNAME]));
|
||
987 | if ( valueZ == null ) { |
||
988 | return null; |
||
989 | } |
||
990 | } |
||
991 | |||
992 | double x = ((Double) valueX); |
||
993 | double y = ((Double) valueY); |
||
994 | Point point;
|
||
995 | if ( this.fieldNames.length > 2 ) { |
||
996 | point = geommgr.createPoint(x, y, Geometry.SUBTYPES.GEOM3D); |
||
997 | double z = ((Double) valueZ); |
||
998 | point.setCoordinateAt(2, z);
|
||
999 | } else {
|
||
1000 | point = geommgr.createPoint(x, y, Geometry.SUBTYPES.GEOM2D); |
||
1001 | } |
||
1002 | return point;
|
||
1003 | } catch (Exception ex) { |
||
1004 | if ( ++errorcount < 5 ) { |
||
1005 | 44242 | jjdelcerro | LOGGER.warn("[" + errorcount + "] Can't create point from '"+getProviderName()+"' file."+ |
1006 | 44057 | jjdelcerro | "XNAME='" + this.fieldNames[XNAME] + |
1007 | "', YNAME='" + this.fieldNames[YNAME] + |
||
1008 | "', ZNAME='" + ((this.fieldNames.length > 2)? this.fieldNames[ZNAME]: "(2D)") + |
||
1009 | "' feature=" + feature.toString(), ex);
|
||
1010 | } |
||
1011 | return null; |
||
1012 | } |
||
1013 | } |
||
1014 | |||
1015 | @Override
|
||
1016 | public void set(EditableFeature feature, Object value) { |
||
1017 | if ( value == null ) { |
||
1018 | return;
|
||
1019 | } |
||
1020 | Point point;
|
||
1021 | if ( value instanceof MultiPoint ) { |
||
1022 | point = (Point) ((MultiPoint) value).getPrimitiveAt(0); |
||
1023 | } else {
|
||
1024 | point = (Point) value;
|
||
1025 | } |
||
1026 | feature.set(this.fieldNames[XNAME], point.getX());
|
||
1027 | feature.set(this.fieldNames[YNAME], point.getY());
|
||
1028 | if ( this.fieldNames.length > 2 ) { |
||
1029 | feature.set(this.fieldNames[ZNAME], point.getCoordinateAt(2)); |
||
1030 | } |
||
1031 | } |
||
1032 | |||
1033 | @Override
|
||
1034 | public boolean allowSetting() { |
||
1035 | return true; |
||
1036 | } |
||
1037 | |||
1038 | @Override
|
||
1039 | public String[] getRequiredFieldNames() { |
||
1040 | return this.fieldNames; |
||
1041 | } |
||
1042 | |||
1043 | } |
||
1044 | |||
1045 | private void loadFeatures() throws IOException, DataException, |
||
1046 | CoercionException, CloneNotSupportedException {
|
||
1047 | |||
1048 | 44251 | jjdelcerro | ReaderData theReaderData = this.readerData;
|
1049 | if( theReaderData==null ) { |
||
1050 | theReaderData = new ReaderData();
|
||
1051 | SimpleSequentialReader reader = this.readerFactory.createReader(this.getParameters()); |
||
1052 | try {
|
||
1053 | loadFeatures(reader, theReaderData); |
||
1054 | 44307 | jjdelcerro | StoresRepositoryWithChildren repoWithChildren = new StoresRepositoryWithChildren();
|
1055 | StoresRepository repo = reader.getStoresRepository(); |
||
1056 | if( repo!=null ) { |
||
1057 | repoWithChildren.addRepository(repo); |
||
1058 | } |
||
1059 | 44259 | jjdelcerro | this.childrenData = new ArrayList<>(); |
1060 | 44307 | jjdelcerro | List<SimpleSequentialReader> children = reader.getChildren();
|
1061 | for(SimpleSequentialReader childReader : children ) {
|
||
1062 | 44251 | jjdelcerro | ReaderData childData = new ReaderData();
|
1063 | loadFeatures(childReader, childData); |
||
1064 | this.childrenData.add(childData);
|
||
1065 | 44307 | jjdelcerro | repoWithChildren.add(childReader.getName(), childData.getParameters()); |
1066 | if( !StringUtils.isBlank(childReader.getAlias()) ) {
|
||
1067 | repoWithChildren.add(childReader.getAlias(), childData.getParameters()); |
||
1068 | } |
||
1069 | childData.setStoresRepository(repoWithChildren); |
||
1070 | 44251 | jjdelcerro | } |
1071 | 44307 | jjdelcerro | repoWithChildren.add(reader.getName(), theReaderData.getParameters()); |
1072 | if( !StringUtils.isBlank(reader.getAlias()) ) {
|
||
1073 | repoWithChildren.add(reader.getAlias(), theReaderData.getParameters()); |
||
1074 | } |
||
1075 | theReaderData.setStoresRepository(repoWithChildren); |
||
1076 | 44251 | jjdelcerro | this.readerData = theReaderData;
|
1077 | } finally {
|
||
1078 | reader.close(); |
||
1079 | } |
||
1080 | } |
||
1081 | this.name = theReaderData.getName();
|
||
1082 | 44259 | jjdelcerro | FeatureStoreProviderServices theStore = this.getStoreServices();
|
1083 | theStore.setFeatureTypes(theReaderData.getFeatureTypes(), theReaderData.getDefaultFeatureType()); |
||
1084 | 44251 | jjdelcerro | this.need_calculate_envelope = theReaderData.getNeedCalculateEnvelope();
|
1085 | this.data = theReaderData.getFeatures();
|
||
1086 | |||
1087 | } |
||
1088 | |||
1089 | private void loadFeatures(SimpleSequentialReader reader, ReaderData readerData) throws IOException, DataException, |
||
1090 | CoercionException, CloneNotSupportedException {
|
||
1091 | |||
1092 | 44262 | jjdelcerro | TaskStatusManager manager = ToolsLocator.getTaskStatusManager(); |
1093 | SimpleTaskStatus taskStatus = manager.createDefaultSimpleTaskStatus(reader.getName()); |
||
1094 | taskStatus.setAutoremove(true);
|
||
1095 | taskStatus.setIndeterminate(); |
||
1096 | taskStatus.add(); |
||
1097 | 44057 | jjdelcerro | try {
|
1098 | List<String> headers; |
||
1099 | |||
1100 | 44262 | jjdelcerro | taskStatus.message("_preparing");
|
1101 | 44251 | jjdelcerro | readerData.setName(reader.getName()); |
1102 | 44304 | jjdelcerro | readerData.setStoresRepository(reader.getStoresRepository()); |
1103 | 44307 | jjdelcerro | readerData.setParameters(reader.getParameters()); |
1104 | readerData.setAlias(reader.getAlias()); |
||
1105 | 44251 | jjdelcerro | |
1106 | 44057 | jjdelcerro | boolean ignore_errors = getParameters().getIgnoreErrors();
|
1107 | |||
1108 | headers = getParameters().getFieldNames(); |
||
1109 | if ( headers == null ) { |
||
1110 | 44321 | jjdelcerro | // Esto es por si el getFieldNames devuelbe algo que no es
|
1111 | // un String pero su toString hace lo que toca.
|
||
1112 | headers = new ArrayList<>(); |
||
1113 | for (Object fielddesc : reader.getFieldNames()) { |
||
1114 | headers.add(fielddesc.toString()); |
||
1115 | } |
||
1116 | 44057 | jjdelcerro | } |
1117 | |||
1118 | // Initialize the feature types
|
||
1119 | EditableFeatureType edftype = this.getFeatureType(headers, automaticDetectionOfTypes(reader));
|
||
1120 | 44251 | jjdelcerro | |
1121 | edftype.setLabel(reader.getLabel()); |
||
1122 | edftype.setDescription(reader.getDescription()); |
||
1123 | Map<String, String> tagsReader = reader.getTags(); |
||
1124 | if( tagsReader!=null ) { |
||
1125 | Tags tagsType = edftype.getTags(); |
||
1126 | for (Map.Entry<String, String> tag : tagsReader.entrySet()) { |
||
1127 | tagsType.set(tag.getKey(), tag.getValue()); |
||
1128 | } |
||
1129 | } |
||
1130 | |||
1131 | 44057 | jjdelcerro | FeatureType ftype = edftype.getNotEditableCopy(); |
1132 | List<FeatureType> ftypes = new ArrayList<>(); |
||
1133 | ftypes.add(ftype); |
||
1134 | 44251 | jjdelcerro | readerData.setFeatureTypes(ftypes, ftype); |
1135 | 44057 | jjdelcerro | |
1136 | Coercion coercion[] = new Coercion[ftype.size()]; |
||
1137 | int sizes[] = new int[ftype.size()]; |
||
1138 | for ( int i = 0; i < ftype.size(); i++ ) { |
||
1139 | sizes[i] = -1;
|
||
1140 | FeatureAttributeDescriptor ad = ftype.getAttributeDescriptor(i); |
||
1141 | coercion[i] = ad.getDataType().getCoercion(); |
||
1142 | if ( ad.getDataType().getType() == DataTypes.STRING ) {
|
||
1143 | if ( ad.getSize() == 0 ) { |
||
1144 | // Es un string y no tiene un size asignado.
|
||
1145 | // Lo ponemos a cero para calcularlo.
|
||
1146 | sizes[i] = 0;
|
||
1147 | } |
||
1148 | } |
||
1149 | } |
||
1150 | 44251 | jjdelcerro | |
1151 | 44057 | jjdelcerro | if ( ftype.getDefaultGeometryAttributeName() != null ) { |
1152 | 44251 | jjdelcerro | readerData.setNeedCalculateEnvelope(true);
|
1153 | 44057 | jjdelcerro | } |
1154 | |||
1155 | Locale locale = getParameters().getLocale();
|
||
1156 | taskStatus.message("_loading");
|
||
1157 | 44262 | jjdelcerro | long rowCount = reader.getRowCount();
|
1158 | if( rowCount >0 ) { |
||
1159 | taskStatus.setRangeOfValues(0, rowCount);
|
||
1160 | } |
||
1161 | |||
1162 | 44057 | jjdelcerro | int count = 0; |
1163 | |||
1164 | int count_errors = 0; |
||
1165 | |||
1166 | reader.rewind(); |
||
1167 | List<Object> row = reader.read(); |
||
1168 | |||
1169 | Object rawvalue;
|
||
1170 | while ( row != null ) { |
||
1171 | taskStatus.setCurValue(++count); |
||
1172 | FeatureProvider feature = this.createFeatureProvider(ftype);
|
||
1173 | for ( int i = 0; i < row.size(); i++ ) { |
||
1174 | try {
|
||
1175 | 44251 | jjdelcerro | if( ftype.get(i).isComputed() ) {
|
1176 | continue;
|
||
1177 | } |
||
1178 | rawvalue = row.get(i); |
||
1179 | if( rawvalue instanceof String && StringUtils.isBlank((String)rawvalue) ) { |
||
1180 | rawvalue = null;
|
||
1181 | } |
||
1182 | 44057 | jjdelcerro | Object value;
|
1183 | if ( locale != null && coercion[i] instanceof CoercionWithLocale ) { |
||
1184 | value = ((CoercionWithLocale) (coercion[i])).coerce(rawvalue, locale); |
||
1185 | } else {
|
||
1186 | value = coercion[i].coerce(rawvalue); |
||
1187 | } |
||
1188 | feature.set(i, value); |
||
1189 | if ( sizes[i] >= 0 && value != null ) { |
||
1190 | int x = ((String) value).length(); |
||
1191 | if ( sizes[i] < x ) {
|
||
1192 | sizes[i] = x; |
||
1193 | } |
||
1194 | } |
||
1195 | 44251 | jjdelcerro | } catch (Exception ex) { |
1196 | 44057 | jjdelcerro | if ( !ignore_errors ) {
|
1197 | throw ex;
|
||
1198 | } |
||
1199 | if ( count_errors++ < 10 ) { |
||
1200 | 44251 | jjdelcerro | LOGGER.warn("Can't load value of attribute " + i+"/"+row.size()+"/"+ftype.size()+ " in row " + count + " in " +readerData.getName()+ ".", ex); |
1201 | 44057 | jjdelcerro | } |
1202 | if ( count_errors == 10 ) { |
||
1203 | LOGGER.info("Too many errors, suppress messages.");
|
||
1204 | } |
||
1205 | } |
||
1206 | } |
||
1207 | 44251 | jjdelcerro | readerData.addFeatureProvider(feature); |
1208 | 44057 | jjdelcerro | row = reader.read(); |
1209 | } |
||
1210 | for ( int i = 0; i < ftype.size(); i++ ) { |
||
1211 | if ( sizes[i] > 0 ) { |
||
1212 | EditableFeatureAttributeDescriptor efad = ((EditableFeatureAttributeDescriptor) edftype.getAttributeDescriptor(i)); |
||
1213 | 44251 | jjdelcerro | if( efad.getSize()<sizes[i] ) {
|
1214 | efad.setSize(sizes[i]); |
||
1215 | } |
||
1216 | 44057 | jjdelcerro | } |
1217 | } |
||
1218 | // Volvemos a asignar al store el featuretype, ya que puede
|
||
1219 | // haber cambiado.
|
||
1220 | ftype = edftype.getNotEditableCopy(); |
||
1221 | ftypes = new ArrayList<>(); |
||
1222 | ftypes.add(ftype); |
||
1223 | 44251 | jjdelcerro | readerData.setFeatureTypes(ftypes, ftype); |
1224 | 44057 | jjdelcerro | |
1225 | taskStatus.terminate(); |
||
1226 | } catch (Exception ex) { |
||
1227 | 44262 | jjdelcerro | taskStatus.abort(); |
1228 | 44242 | jjdelcerro | LOGGER.warn("Can't load features from '"+getProviderName()+"' file '" + getFullFileName() + "'.", ex); |
1229 | 44057 | jjdelcerro | } |
1230 | } |
||
1231 | |||
1232 | 44242 | jjdelcerro | private int[] automaticDetectionOfTypes(SimpleSequentialReader reader) throws IOException { |
1233 | 44057 | jjdelcerro | boolean automatic_types_detection = getParameters().getAutomaticTypesDetection();
|
1234 | if ( !automatic_types_detection ) {
|
||
1235 | return null; |
||
1236 | } |
||
1237 | int[] types = null; |
||
1238 | try {
|
||
1239 | reader.rewind(); |
||
1240 | List<String> fieldNames = reader.getFieldNames(); |
||
1241 | if ( fieldNames == null ) { |
||
1242 | fieldNames = getParameters().getFieldNames(); |
||
1243 | } |
||
1244 | |||
1245 | AutomaticDetectionOfTypes x = new AutomaticDetectionOfTypes(
|
||
1246 | this.getFullFileName()
|
||
1247 | ); |
||
1248 | types = x.detect( |
||
1249 | fieldNames.size(), |
||
1250 | reader, |
||
1251 | false,
|
||
1252 | getParameters().getLocale() |
||
1253 | ); |
||
1254 | } catch (Exception ex) { |
||
1255 | 44242 | jjdelcerro | throw new RuntimeException("Problems reading '"+getProviderName()+"' file '" + getFullFileName() + "'.", ex); |
1256 | 44057 | jjdelcerro | } |
1257 | return types;
|
||
1258 | } |
||
1259 | 44232 | jjdelcerro | |
1260 | @Override
|
||
1261 | public boolean hasDynValue(String name) { |
||
1262 | if ( DataStore.METADATA_ENVELOPE.equalsIgnoreCase(name) ) {
|
||
1263 | return true; |
||
1264 | } else if ( DataStore.METADATA_CRS.equalsIgnoreCase(name) ) { |
||
1265 | return true; |
||
1266 | } |
||
1267 | return super.hasDynValue(name); |
||
1268 | } |
||
1269 | |||
1270 | 44251 | jjdelcerro | @Override
|
1271 | 44259 | jjdelcerro | public UnmodifiableBasicMap<String, DataStore> getChildren() { |
1272 | 44307 | jjdelcerro | if( this.isChild() ) { |
1273 | return null; |
||
1274 | } |
||
1275 | 44259 | jjdelcerro | return new Children(); |
1276 | 44251 | jjdelcerro | } |
1277 | |||
1278 | 44304 | jjdelcerro | @Override
|
1279 | public StoresRepository getStoresRepository() {
|
||
1280 | StoresRepository repo = this.readerData.getStoresRepository();
|
||
1281 | return repo;
|
||
1282 | } |
||
1283 | |||
1284 | 44057 | jjdelcerro | } |