Statistics
| Revision:

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.csv / src / main / java / org / gvsig / fmap / dal / store / csv / CSVStoreProvider.java @ 45817

History | View | Annotate | Download (29.9 KB)

1
/**
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 modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.fmap.dal.store.csv;
24

    
25
import java.io.File;
26
import java.io.InputStreamReader;
27
import java.net.URI;
28
import java.net.URL;
29
import java.util.ArrayList;
30
import java.util.HashMap;
31
import java.util.Iterator;
32
import java.util.List;
33
import java.util.Locale;
34

    
35
import org.apache.commons.io.FilenameUtils;
36
import org.apache.commons.lang3.StringUtils;
37
import org.cresques.cts.IProjection;
38
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
import org.gvsig.fmap.dal.exception.DataException;
46
import org.gvsig.fmap.dal.exception.InitializeException;
47
import org.gvsig.fmap.dal.exception.OpenException;
48
import org.gvsig.fmap.dal.exception.ReadException;
49
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
50
import org.gvsig.fmap.dal.feature.EditableFeatureType;
51
import org.gvsig.fmap.dal.feature.Feature;
52
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
53
import org.gvsig.fmap.dal.feature.FeatureQuery;
54
import org.gvsig.fmap.dal.feature.FeatureSet;
55
import org.gvsig.fmap.dal.feature.FeatureStore;
56
import org.gvsig.fmap.dal.feature.FeatureType;
57
import org.gvsig.fmap.dal.feature.exception.PerformEditingException;
58
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
59
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider;
60
import org.gvsig.fmap.dal.feature.spi.memory.AbstractMemoryStoreProvider;
61
import org.gvsig.fmap.dal.resource.ResourceAction;
62
import org.gvsig.fmap.dal.resource.file.FileResource;
63
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer;
64
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
65
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer;
66
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters;
67
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
68
import org.gvsig.fmap.dal.store.csv.simplereaders.AbstractSimpleReader;
69
import org.gvsig.fmap.dal.store.csv.simplereaders.CSVReaderSuperCSV;
70
import org.gvsig.fmap.dal.store.csv.simplereaders.SimpleReader;
71
import org.gvsig.fmap.geom.Geometry;
72
import org.gvsig.fmap.geom.GeometryLocator;
73
import org.gvsig.fmap.geom.GeometryManager;
74
import org.gvsig.fmap.geom.primitive.Envelope;
75
import org.gvsig.fmap.geom.primitive.Point;
76
import org.gvsig.tools.ToolsLocator;
77
import org.gvsig.tools.dataTypes.Coercion;
78
import org.gvsig.tools.dispose.DisposableIterator;
79
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
80
import org.gvsig.tools.evaluator.AbstractEvaluator;
81
import org.gvsig.tools.evaluator.EvaluatorData;
82
import org.gvsig.tools.evaluator.EvaluatorException;
83
import org.gvsig.tools.exception.BaseException;
84
import org.gvsig.tools.exception.NotYetImplemented;
85
import org.gvsig.tools.persistence.PersistentState;
86
import org.gvsig.tools.persistence.exception.PersistenceException;
87
import org.gvsig.tools.task.SimpleTaskStatus;
88
import org.gvsig.tools.task.TaskStatusManager;
89
import org.gvsig.tools.visitor.VisitCanceledException;
90
import org.gvsig.tools.visitor.Visitor;
91
import org.slf4j.Logger;
92
import org.slf4j.LoggerFactory;
93
import org.supercsv.prefs.CsvPreference;
94
import org.gvsig.tools.dataTypes.CoercionContext;
95
import org.gvsig.tools.util.GetItemWithSize64;
96

    
97
@SuppressWarnings("UseSpecificCatch")
98
public class CSVStoreProvider extends AbstractMemoryStoreProvider implements
99
        ResourceConsumer {
100

    
101
    private static final Logger LOGGER = LoggerFactory.getLogger(CSVStoreProvider.class);
102

    
103
    public static final String NAME = DataStore.CSV_PROVIDER_NAME;
104
    public static final String DESCRIPTION = "CSV file";
105

    
106
    public static final String METADATA_DEFINITION_NAME = NAME;
107

    
108
    private final ResourceProvider resource;
109

    
110
    private long counterNewsOIDs = 0;
111
    private Envelope envelope;
112
    private boolean need_calculate_envelope = false;
113
    private final SimpleTaskStatus taskStatus;
114
    private final CSVFeatureWriter writer;
115
    private FeatureType featureType;
116
    private GetItemWithSize64<List<String>> virtualrows;
117
    private RowToFeatureTranslator  rowToFeatureTranslator;
118

    
119
    @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"})
120
    public CSVStoreProvider(CSVStoreParameters parameters,
121
            DataStoreProviderServices storeServices) throws InitializeException {
122
        super(
123
                parameters,
124
                storeServices,
125
                FileHelper.newMetadataContainer(METADATA_DEFINITION_NAME)
126
        );
127
        this.writer = new CSVFeatureWriter();
128
        TaskStatusManager manager = ToolsLocator.getTaskStatusManager();
129
        this.taskStatus = manager.createDefaultSimpleTaskStatus("CSV");
130

    
131
        counterNewsOIDs = 0;
132

    
133
        File file = getCSVParameters().getFile();
134
        resource = this.createResource(
135
                FileResource.NAME,
136
                new Object[]{file.getAbsolutePath()}
137
        );
138

    
139
        resource.addConsumer(this);
140

    
141
        initializeFeatureTypes();
142
    }
143

    
144
    private CSVStoreParameters getCSVParameters() {
145
        return (CSVStoreParameters) this.getParameters();
146
    }
147

    
148
    @Override
149
    public String getProviderName() {
150
        return NAME;
151
    }
152

    
153
    @Override
154
    public boolean allowWrite() {
155
        return true;
156
    }
157

    
158
    private String getFullFileName() {
159
        // Usar solo para mostrar mensajes en el logger.
160
        String s;
161
        try {
162
            s = getCSVParameters().getFile().getAbsolutePath();
163
        } catch (Exception e2) {
164
            s = "(unknow)";
165
        }
166
        return s;
167
    }
168

    
169
    @Override
170
    public void open() throws OpenException {
171
        if (this.data != null) {
172
            return;
173
        }
174
        this.data = new ArrayList<>();
175
        resource.setData(new HashMap());
176
        counterNewsOIDs = 0;
177
        try {
178
            loadFeatures();
179
        } catch (RuntimeException e) {
180
            LOGGER.debug("Can't load features from CSV '" + getFullFileName() + "'.", e);
181
            throw e;
182
        } catch (Exception e) {
183
            LOGGER.debug("Can't load features from CSV '" + getFullFileName() + "'.", e);
184
            throw new RuntimeException(e);
185
        }
186
    }
187

    
188
    @Override
189
    public DataServerExplorer getExplorer() throws ReadException {
190
        DataManager manager = DALLocator.getDataManager();
191
        FilesystemServerExplorerParameters params;
192
        try {
193
            params = (FilesystemServerExplorerParameters) manager
194
                    .createServerExplorerParameters(FilesystemServerExplorer.NAME);
195
            params.setRoot(this.getCSVParameters().getFile().getParent());
196
            return manager.openServerExplorer(FilesystemServerExplorer.NAME, params);
197
        } catch (Exception e) {
198
            throw new ReadException(this.getProviderName(), e);
199
        }
200

    
201
    }
202

    
203
    @Override
204
    @SuppressWarnings("Convert2Lambda")
205
    public void performChanges(Iterator deleteds, Iterator inserteds, Iterator updateds, Iterator originalFeatureTypesUpdated) throws PerformEditingException {
206

    
207
        try {
208
            this.taskStatus.add();
209
            taskStatus.message("_preparing");
210
            this.getResource().closeRequest();
211
            getResource().execute(new ResourceAction() {
212
                @Override
213
                public Object run() throws Exception {
214
                    FeatureSet features = null;
215
                    DisposableIterator it = null;
216
                    try {
217
                        File file = (File) resource.get();
218

    
219
                        features
220
                                = getStoreServices().getFeatureStore()
221
                                        .getFeatureSet();
222
                        List<FeatureProvider> newdata = new ArrayList<>();
223
                        FeatureType ftype = getStoreServices().getDefaultFeatureType();
224
                        writer.initialize(getCSVParameters(), file, ftype, getCSVPreferences());
225
                        writer.begin();
226
                        it = features.fastIterator();
227
                        taskStatus.setRangeOfValues(0, 0);
228
                        long counter = 0;
229
                        while (it.hasNext()) {
230
                            taskStatus.setCurValue(counter++);
231
                            FeatureProvider feature = getStoreServices().getFeatureProviderFromFeature(
232
                                    (org.gvsig.fmap.dal.feature.Feature) it.next());
233
                            writer.add(feature);
234
                            if (feature.getOID() == null) {
235
                                LOGGER.warn("feature without OID");
236
                                feature.setOID(createNewOID());
237
                            }
238
                            newdata.add(feature);
239
                        }
240
                        data = newdata;
241
                        if (writer.getEnvelope() != null) {
242
                            envelope = writer.getEnvelope().getGeometry().getEnvelope();
243
                        }
244
                        resource.notifyChanges();
245
                        writer.end();
246
                    } finally {
247
                        if (it != null) {
248
                            it.dispose();
249
                        }
250
                        if (features != null) {
251
                            features.dispose();
252
                        }
253
                    }
254
                    return null;
255
                }
256

    
257
            });
258
            this.taskStatus.terminate();
259
        } catch (Exception e) {
260
            this.taskStatus.abort();
261
            throw new PerformEditingException(getResource().toString(), e);
262
        } finally {
263
            this.taskStatus.remove();
264
        }
265
    }
266

    
267
    private CsvPreference getCSVPreferences() {
268
        CSVReaderSuperCSV reader = new CSVReaderSuperCSV(getCSVParameters());
269
        return reader.getCSVPreferences();
270
    }
271

    
272
    @Override
273
    public boolean closeResourceRequested(ResourceProvider resource) {
274
        return true;
275
    }
276

    
277
    @Override
278
    public int getOIDType() {
279
        return DataTypes.LONG;
280
    }
281

    
282
    @Override
283
    public boolean supportsAppendMode() {
284
        return true;
285
    }
286

    
287
    @Override
288
    @SuppressWarnings("Convert2Lambda")
289
    public void append(final FeatureProvider featureProvider) {
290
        //todo
291
        getResource().execute(new ResourceAction() {
292
            @Override
293
            public Object run() throws Exception {
294
                //writer.append(getStoreServices().createFeature(featureProvider));
295
                writer.add(featureProvider);
296
                return null;
297
            }
298
        });
299
    }
300

    
301
    @Override
302
    @SuppressWarnings("Convert2Lambda")
303
    public void beginAppend() throws DataException {
304
        this.close();
305
        getResource().execute(new ResourceAction() {
306
            @Override
307
            public Object run() throws Exception {
308
                writer.initialize(
309
                        getCSVParameters(),
310
                        getCSVParameters().getFile(),
311
                        getFeatureStore().getDefaultFeatureType(),
312
                        getCSVPreferences()
313
                );
314
                writer.beginAppend();
315
                return null;
316
            }
317
        });
318

    
319
    }
320

    
321
    @Override
322
    @SuppressWarnings("Convert2Lambda")
323
    public void endAppend() {
324
        try {
325
            getResource().execute(new ResourceAction() {
326
                @Override
327
                public Object run() throws Exception {
328
                    writer.end();
329
                    resource.notifyChanges(); //resourcesNotifyChanges();
330
                    counterNewsOIDs = -1;
331
                    return null;
332
                }
333
            });
334
            writer.end();
335
        } catch (PerformEditingException ex) {
336
            LOGGER.warn("Not been able to end append '" + this.getFullName() + "'.", ex);
337
        }
338
    }
339

    
340
    public void saveToState(PersistentState state) throws PersistenceException {
341
        throw new NotYetImplemented();
342
    }
343

    
344
    public void loadFromState(PersistentState state) throws PersistenceException {
345
        throw new NotYetImplemented();
346
    }
347

    
348
    @Override
349
    public Object createNewOID() {
350
        return counterNewsOIDs++;
351
    }
352

    
353
    protected void initializeFeatureTypes() throws InitializeException {
354
        try {
355
            this.open();
356
        } catch (OpenException e) {
357
            throw new InitializeException(this.getProviderName(), e);
358
        }
359
    }
360

    
361
    @Override
362
    @SuppressWarnings("Convert2Lambda")
363
    public Envelope getEnvelope() throws DataException {
364
        this.open();
365
        if (this.envelope != null) {
366
            return this.envelope;
367
        }
368
        if (!this.need_calculate_envelope) {
369
            return null;
370
        }
371
        FeatureStore fs = this.getFeatureStore();
372
        FeatureType ft = fs.getDefaultFeatureType();
373
        FeatureAttributeDescriptor fad = ft.getAttributeDescriptor(ft.getDefaultGeometryAttributeIndex());
374

    
375
        try {
376
            this.envelope = GeometryLocator.getGeometryManager().createEnvelope(fad.getGeomType().getSubType());
377
            fs.accept(new Visitor() {
378
                @Override
379
                public void visit(Object obj) throws VisitCanceledException, BaseException {
380
                    Feature f = (Feature) obj;
381
                    Geometry geom = f.getDefaultGeometry();
382
                    if (geom != null) {
383
                        envelope.add(geom.getEnvelope());
384
                    }
385
                }
386
            });
387
        } catch (BaseException e) {
388
            LOGGER.warn("Can't calculate the envelope of CSV file '" + this.getFullName() + "'.", e);
389
            this.envelope = null;
390
        }
391

    
392
        this.need_calculate_envelope = false;
393
        return this.envelope;
394
    }
395

    
396
    @Override
397
    public Object getDynValue(String name) throws DynFieldNotFoundException {
398
        if (DataStore.METADATA_ENVELOPE.equalsIgnoreCase(name)) {
399
            try {
400
                return this.getEnvelope();
401
            } catch (DataException e) {
402
                return null;
403
            }
404
        } else {
405
            if (DataStore.METADATA_CRS.equalsIgnoreCase(name)) {
406
                IProjection pro = CSVStoreParameters.getCRS(this.getCSVParameters());
407
                if (pro != null) {
408
                    return pro;
409
                }
410
            }
411
        }
412
        return super.getDynValue(name);
413
    }
414

    
415
    @Override
416
    public void resourceChanged(ResourceProvider resource) {
417
        this.getStoreServices().notifyChange(
418
                DataStoreNotification.RESOURCE_CHANGED,
419
                resource);
420
    }
421

    
422
    @Override
423
    public Object getSourceId() {
424
        return this.getCSVParameters().getFile();
425
    }
426

    
427
    @Override
428
    public String getName() {
429
        String name = this.getCSVParameters().getFile().getName();
430
        return FilenameUtils.getBaseName(name);
431
    }
432

    
433
    @Override
434
    public String getFullName() {
435
        return this.getCSVParameters().getFile().getAbsolutePath();
436
    }
437

    
438
    @Override
439
    public ResourceProvider getResource() {
440
        return resource;
441
    }
442

    
443
    private boolean isEmpty(String s) {
444
        if (s == null) {
445
            return true;
446
        }
447
        return s.trim().length() == 0;
448
    }
449

    
450
    private void init(CSVStoreParameters parameters, DataStoreProviderServices storeServices) {
451
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
452
    }
453
    
454

    
455

    
456
    static class ToPointEvaluaror extends AbstractEvaluator {
457

    
458
        private static final Logger logger = LoggerFactory.getLogger(ToPointEvaluaror.class);
459

    
460
        private GeometryManager geommgr = null;
461
        private String xname = null;
462
        private String yname = null;
463
        private String zname = null;
464
        private final Coercion toDouble;
465
        private int errorcount = 0;
466

    
467
        ToPointEvaluaror(String[] pointDimensionNames) {
468
            this.xname = pointDimensionNames[0];
469
            this.yname = pointDimensionNames[1];
470
            if (pointDimensionNames.length > 2) {
471
                this.zname = pointDimensionNames[2];
472
            }
473
            this.geommgr = GeometryLocator.getGeometryManager();
474
            this.toDouble = ToolsLocator.getDataTypesManager().getCoercion(DataTypes.DOUBLE);
475
        }
476

    
477
        @Override
478
        public Object evaluate(EvaluatorData data) throws EvaluatorException {
479
            try {
480
                double x = ((Double) toDouble.coerce(data.getDataValue(xname)));
481
                double y = ((Double) toDouble.coerce(data.getDataValue(yname)));
482
                Point point = geommgr.createPoint(x, y, Geometry.SUBTYPES.GEOM3D);
483
                if (zname != null) {
484
                    double z = ((Double) toDouble.coerce(data.getDataValue(zname)));
485
                    point.setCoordinateAt(2, z);
486
                }
487
                return point;
488
            } catch (Exception ex) {
489
                if (++errorcount < 5) {
490
                    logger.warn("[" + errorcount + "] Can't create point in CSV provider. XNAME='"
491
                            + xname + "', YNAME='" + yname + "', ZNAME='" + zname + "', data=" + data.toString());
492
                }
493
                return null;
494
            }
495
        }
496

    
497
        @Override
498
        public String getName() {
499
            return "ToPointEvaluaror";
500
        }
501

    
502
    }
503

    
504
    public static class RowToFeatureTranslator {
505
        private Coercion coercion[];
506
        private CoercionContext coercionContext[];
507
        private int sizes[];
508
        private final boolean ignore_errors;
509
        private long count_errors;
510

    
511
        public RowToFeatureTranslator(boolean ignore_errors) {
512
            this.ignore_errors = ignore_errors;
513
            this.count_errors = 0;
514
        }
515

    
516
        public int getColumnSize(int column) {
517
            return this.sizes[column];
518
        }
519
        
520
        public void initialize(FeatureType ftype) {
521
            int columns = ftype.size();
522
            this.coercion = new Coercion[columns];
523
            this.coercionContext = new CoercionContext[columns];
524
            this.sizes = new int[columns];
525
            for (int i = 0; i < ftype.size(); i++) {
526
                FeatureAttributeDescriptor ad = ftype.getAttributeDescriptor(i);
527
                coercion[i] = ad.getCoercion();
528
                coercionContext[i] = ad.getCoercionContext();
529
                sizes[i] = ad.getSize();
530
//                sizes[i] = -1;
531
//                switch (ad.getDataType().getType()) {
532
//                    case DataTypes.INT:
533
//                        sizes[i] = 0;
534
//                        break;
535
//                    case DataTypes.LONG:
536
//                        sizes[i] = 0;
537
//                        break;
538
//                    case DataTypes.STRING:
539
//                        if (ad.getSize() == 0) {
540
//                            // Es un string y no tiene un size asignado.
541
//                            // Lo ponemos a cero para calcularlo.
542
//                            sizes[i] = 0;
543
//                        }
544
//                        break;
545
//                    case DataTypes.URL:
546
//                    case DataTypes.URI:
547
//                    case DataTypes.FILE:
548
//                        sizes[i] = 0;
549
//                }
550
            }
551
        }
552
        
553
        public void translate(long rowindex, List<String> row, FeatureProvider feature) throws Exception {
554

    
555
            int columns = coercion.length;
556
            if( row.size()<columns ) {
557
                columns = row.size();
558
            }
559
            
560
            for (int i = 0; i < columns; i++) {
561
                Object rawvalue = row.get(i);
562
                try {
563
                    Object value = null;
564
                    if (coercion[i] != null) {
565
                        value = coercion[i].coerce(rawvalue, coercionContext[i]);
566
                    }
567
                    feature.set(i, value);
568
                    if (sizes[i] >= 0
569
                            && (value instanceof String || value instanceof URL
570
                            || value instanceof URI || value instanceof File)) {
571
                        int x = value.toString().length();
572
                        if (sizes[i] < x) {
573
                            sizes[i] = x;
574
                        }
575
                    }
576
                } catch (Exception ex) {
577
                    if (!ignore_errors) {
578
                        throw ex;
579
                    }
580
                    if (count_errors++ < 10) {
581
                        LOGGER.warn("Can't load value of attribute " + i + " in row " + rowindex + ".", ex);
582
                    }
583
                    if (count_errors == 10) {
584
                        LOGGER.info("Too many errors, suppress messages.");
585
                    }
586
                }
587
            }
588
        }
589
    }
590

    
591
    private void loadFeatures() {
592
        InputStreamReader in = null;
593
        SimpleReader reader = null;
594
        try {
595
            taskStatus.add();
596
//            boolean ignore_errors = CSVStoreParameters.getIgnoreErrors(getCSVParameters());
597

    
598
            // Initialize the feature types
599
            EditableFeatureType edftype = getStoreServices().createFeatureType(this.getName());
600
            CSVUtils.loadFeatureType(getCSVParameters(), edftype, true);
601
            FeatureType ftype = edftype.getNotEditableCopy();
602
            this.setFeatureType(ftype);
603

    
604
            in = CSVUtils.openFile(
605
                    this.getCSVParameters().getFile(),
606
                    CSVStoreParameters.getCharset(this.getCSVParameters())
607
            );
608
            reader = CSVUtils.getSimpleReader(getCSVParameters(), in);
609
            if (CSVStoreParameters.isFirstLineHeader(getCSVParameters())) {
610
                reader.getHeader(); // Skip and ignore the header of file
611
            }
612
            this.rowToFeatureTranslator = new RowToFeatureTranslator(
613
                    CSVStoreParameters.getIgnoreErrors(getCSVParameters())
614
            );
615
            this.rowToFeatureTranslator.initialize(ftype);
616
//            Coercion coercion[] = new Coercion[ftype.size()];
617
//            CoercionContext coercionContext[] = new CoercionContext[ftype.size()];
618
//            int sizes[] = new int[ftype.size()];
619
//            for (int i = 0; i < ftype.size(); i++) {
620
//                sizes[i] = -1;
621
//                FeatureAttributeDescriptor ad = ftype.getAttributeDescriptor(i);
622
//                coercion[i] = ad.getCoercion();
623
//                coercionContext[i] = ad.getCoercionContext();
624
//                switch (ad.getDataType().getType()) {
625
//                    case DataTypes.INT:
626
//                        sizes[i] = 0;
627
//                        break;
628
//                    case DataTypes.LONG:
629
//                        sizes[i] = 0;
630
//                        break;
631
//                    case DataTypes.STRING:
632
//                        if (ad.getSize() == 0) {
633
//                            // Es un string y no tiene un size asignado.
634
//                            // Lo ponemos a cero para calcularlo.
635
//                            sizes[i] = 0;
636
//                        }
637
//                        break;
638
//                    case DataTypes.URL:
639
//                    case DataTypes.URI:
640
//                    case DataTypes.FILE:
641
//                        sizes[i] = 0;
642
//                }
643
//            }
644
            if (ftype.getDefaultGeometryAttributeName() != null) {
645
                this.need_calculate_envelope = true;
646
            }
647

    
648
            Locale locale = CSVStoreParameters.getLocale(getCSVParameters());
649
            taskStatus.message("_loading");
650
            int count = 0;
651

    
652
            this.virtualrows = ((AbstractSimpleReader)reader).getVirtualRows();
653
            if( this.virtualrows==null ) {
654

    
655
                List<String> row = reader.read();
656

    
657
                int skipLines = CSVStoreParameters.getSkipLines(getCSVParameters());
658
                if (skipLines > 0) {
659
                    row = reader.skip(skipLines);
660
                }
661
                int limit = CSVStoreParameters.getLimit(getCSVParameters());
662
                while (row != null) {
663
                    taskStatus.setCurValue(++count);
664
                    FeatureProvider feature = this.createFeatureProvider(ftype);
665
                    this.rowToFeatureTranslator.translate(count, row, feature);
666
//                    for (int i = 0; i < row.size(); i++) {
667
//                        Object rawvalue = row.get(i);
668
//                        try {
669
//                            Object value = null;
670
//                            if( coercion[i]!=null ) {
671
//                              value = coercion[i].coerce(rawvalue, coercionContext[i]);
672
//                            }
673
//                            feature.set(i, value);
674
//                            if (sizes[i] >= 0
675
//                                    && (value instanceof String || value instanceof URL
676
//                                    || value instanceof URI || value instanceof File)) {
677
//                                int x = value.toString().length();
678
//                                if (sizes[i] < x) {
679
//                                    sizes[i] = x;
680
//                                }
681
//                            }
682
//                        } catch (Exception ex) {
683
//                            if (!ignore_errors) {
684
//                                throw ex;
685
//                            }
686
//                            if (count_errors++ < 10) {
687
//                                LOGGER.warn("Can't load value of attribute " + i + " in row " + count + ".", ex);
688
//                            }
689
//                            if (count_errors == 10) {
690
//                                LOGGER.info("Too many errors, suppress messages.");
691
//                            }
692
//                        }
693
//                    }
694
                    
695
                    this.addFeatureProvider(feature);
696
                    if (limit > 0) {
697
                        if (limit < this.data.size()) {
698
                            break;
699
                        }
700
                    }
701
                    row = reader.read();
702
                }
703
                for (int i = 0; i < ftype.size(); i++) {
704
                    if( this.rowToFeatureTranslator.getColumnSize(i)>0 ) {
705
//                    if (sizes[i] > 0) {
706
                        EditableFeatureAttributeDescriptor efad = ((EditableFeatureAttributeDescriptor) edftype.getAttributeDescriptor(i));
707
//                        efad.setSize(sizes[i]);
708
                        efad.setSize(this.rowToFeatureTranslator.getColumnSize(i));
709
                    }
710
                }
711
                // Volvemos a asignar al store el featuretype, ya que puede
712
                // haber cambiado.
713
                ftype = edftype.getNotEditableCopy();
714
                this.setFeatureType(ftype);
715
            }
716
            
717
            taskStatus.terminate();
718
        } catch (Throwable ex) {
719
            taskStatus.abort();
720
            int lineno = 0;
721
            if (reader != null) {
722
                lineno = reader.getLine();
723
            }
724
            throw new RuntimeException("Problems reading file '" + getFullFileName() + "' near line " + lineno + ".", ex);
725

    
726
        } finally {
727
            if (reader != null) {
728
                try {
729
                    reader.close();
730
                } catch (Exception ex) {
731
                    // Do nothing
732
                }
733
//                reader = null;
734
            }
735
            if (in != null) {
736
                try {
737
                    in.close();
738
                } catch (Exception ex) {
739
                    // Do nothing
740
                }
741
//                in = null;
742
            }
743
        }
744
    }
745

    
746
    
747
    @Override
748
    public void fixFeatureTypeFromParameters() {
749
        String param_types_def = CSVStoreParameters.getRawFieldTypes(this.getParameters());
750
        String[] pointDimensionNames = CSVStoreParameters.getPointDimensionNames(this.getParameters());
751
        String geometry_column = CSVStoreParameters.getGeometryColumn(this.getParameters());
752
        if (StringUtils.isNotBlank(param_types_def) || 
753
                pointDimensionNames != null || 
754
                !StringUtils.isBlank(geometry_column)) {
755
            this.setFeatureType(featureType);
756
        }
757
    }
758
    
759
    private void setFeatureType(FeatureType ftype) {
760
        List<FeatureType> ftypes = new ArrayList<>();
761
        ftypes.add(ftype);
762
        this.featureType = ftype;
763
        this.getStoreServices().setFeatureTypes(ftypes, ftype);
764
    }
765

    
766
    @Override
767
    public FeatureSetProvider createSet(FeatureQuery query, FeatureType featureType)
768
            throws DataException {
769
        if( this.virtualrows==null ) {
770
            return super.createSet(query, featureType);
771
        }
772
        return new CSVSetProvider(this, query, featureType);
773
    }    
774

    
775
    public List<String> getRowByIndex(long index) {
776
        if( this.virtualrows==null ) {
777
            return null;
778
        }
779
        return this.virtualrows.get64(index);
780
    }
781
    
782
    public RowToFeatureTranslator getRowToFeatureTranslator() {
783
        if( this.rowToFeatureTranslator==null ) {
784
            boolean ignore_errors = CSVStoreParameters.getIgnoreErrors(getCSVParameters());
785
            this.rowToFeatureTranslator = new RowToFeatureTranslator(ignore_errors);
786
            this.rowToFeatureTranslator.initialize(featureType);
787
        }
788
        return this.rowToFeatureTranslator;
789
    }
790

    
791
    @Override
792
    public long getFeatureCount() throws DataException {
793
        if( this.virtualrows==null ) {
794
            return super.getFeatureCount(); 
795
        }
796
        return this.virtualrows.size64();
797
    }
798
    
799
    
800
}