Revision 42574 trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.file/org.gvsig.fmap.dal.file.shp/src/main/java/org/gvsig/fmap/dal/store/shp/SHPStoreProvider.java

View differences:

SHPStoreProvider.java
76 76
import org.gvsig.tools.exception.BaseException;
77 77

  
78 78
/**
79
 * @author fdiaz
80 79
 *
81 80
 */
82 81
public class SHPStoreProvider extends DBFStoreProvider {
......
97 96

  
98 97
    private boolean loTengoEnUso;
99 98

  
100
    public SHPStoreProvider(SHPStoreParameters params,
101
            DataStoreProviderServices storeServices)
102
            throws InitializeException {
103
        super(
104
                params,
105
                storeServices,
106
                FileHelper.newMetadataContainer(METADATA_DEFINITION_NAME)
107
        );
99
    /**
100
     * @param params
101
     * @param storeServices
102
     * @throws InitializeException
103
     */
104
    public SHPStoreProvider(SHPStoreParameters params, DataStoreProviderServices storeServices)
105
        throws InitializeException {
106
        super(params, storeServices, FileHelper.newMetadataContainer(METADATA_DEFINITION_NAME));
108 107
    }
109 108

  
110
    protected void init(DBFStoreParameters params,
111
            DataStoreProviderServices storeServices) throws InitializeException {
109
    protected void init(DBFStoreParameters params, DataStoreProviderServices storeServices) throws InitializeException {
112 110

  
113 111
        this.shpFile = new SHPFile2((SHPStoreParameters) params);
114 112
        super.init(params, storeServices);
......
130 128
        return super.getDynValue(name);
131 129
    }
132 130

  
133
    protected void initResource(DBFStoreParameters params,
134
            DataStoreProviderServices storeServices) throws InitializeException {
131
    protected void initResource(DBFStoreParameters params, DataStoreProviderServices storeServices)
132
        throws InitializeException {
135 133

  
136 134
        SHPStoreParameters shpParams = (SHPStoreParameters) params;
137
        resource
138
                = (MultiResource) createResource(MultiResource.TYPE_NAME,
139
                        new Object[]{shpParams.getSHPFileName()});
135
        resource = (MultiResource) createResource(MultiResource.TYPE_NAME, new Object[] { shpParams.getSHPFileName() });
140 136

  
141
        resource.addResource(FileResource.NAME,
142
                new Object[]{shpParams.getSHPFileName()}, true);
143
        resource.addResource(FileResource.NAME,
144
                new Object[]{shpParams.getSHXFileName()}, true);
145
        resource.addResource(FileResource.NAME,
146
                new Object[]{shpParams.getDBFFileName()}, true);
137
        resource.addResource(FileResource.NAME, new Object[] { shpParams.getSHPFileName() }, true);
138
        resource.addResource(FileResource.NAME, new Object[] { shpParams.getSHXFileName() }, true);
139
        resource.addResource(FileResource.NAME, new Object[] { shpParams.getDBFFileName() }, true);
147 140

  
148 141
        resource.frozen();
149 142
        resource.addMultiResourceConsumer(this);
......
151 144

  
152 145
    ;
153 146

  
154
	public ResourceProvider getResource() {
147
    public ResourceProvider getResource() {
155 148
        return resource;
156 149
    }
157 150

  
......
159 152
     *
160 153
     * @throws ResourceNotifyChangesException
161 154
     */
162
    protected void resourcesNotifyChanges()
163
            throws ResourceNotifyChangesException {
164
		// super.resourcesNotifyChanges();
165
        // this.shpResource.notifyChanges();
166
        // this.shxResource.notifyChanges();
155
    protected void resourcesNotifyChanges() throws ResourceNotifyChangesException {
167 156
        getResource().notifyChanges();
168 157
        // TODO .prj
169 158

  
......
174 163
     *
175 164
     */
176 165
    protected void resourcesNotifyClose() throws ResourceNotifyCloseException {
177
//		super.resourcesNotifyClose();
178
//		this.shpResource.notifyClose();
179
//		this.shxResource.notifyClose();
180 166
        getResource().notifyClose();
181 167
        // TODO .prj
182 168

  
......
199 185
     *
200 186
     */
201 187
    protected void resourcesOpen() throws ResourceNotifyOpenException {
202
		// super.resourcesOpen();
203
        // this.shpResource.notifyOpen();
204
        // this.shxResource.notifyOpen();
205 188
        getResource().notifyOpen();
206 189
    }
207 190

  
208
    protected static EditableFeatureAttributeDescriptor addGeometryColumn(
209
            EditableFeatureType fType) {
191
    protected static EditableFeatureAttributeDescriptor addGeometryColumn(EditableFeatureType fType) {
210 192

  
211 193
        EditableFeatureAttributeDescriptor attrTmp = null;
212 194
        EditableFeatureAttributeDescriptor attr = null;
213
        Iterator iter = fType.iterator();
195
        Iterator<?> iter = fType.iterator();
214 196
        while (iter.hasNext()) {
215 197
            attrTmp = (EditableFeatureAttributeDescriptor) iter.next();
216 198
            if (attrTmp.getType() == DataTypes.GEOMETRY) {
......
219 201
                    fType.remove(attrTmp.getName());
220 202
                } else {
221 203
                    attr = attrTmp;
222
                    // attr.setName(GEOMETRY_ATTIBUTE_NAME);
223 204
                }
224 205
            }
225 206
        }
......
251 232
        return candidate;
252 233
    }
253 234

  
254
    protected static FeatureType removeGeometryColumn(
255
            EditableFeatureType fType) {
256
        Iterator iter = fType.iterator();
235
    protected static FeatureType removeGeometryColumn(EditableFeatureType fType) {
236
        Iterator<?> iter = fType.iterator();
257 237
        FeatureAttributeDescriptor attr;
258 238
        while (iter.hasNext()) {
259 239
            attr = (FeatureAttributeDescriptor) iter.next();
......
265 245
        return fType.getNotEditableCopy();
266 246
    }
267 247

  
268
    protected EditableFeatureType getTheFeatureType()
269
            throws InitializeException, OpenException {
248
    protected EditableFeatureType getTheFeatureType() throws InitializeException, OpenException {
270 249
        final EditableFeatureType fType = super.getTheFeatureType();
271 250
        this.open();
272
		// try {
273
        // this.resourcesBegin();
274
        // } catch (DataException e) {
275
        // throw new InitializeException(this.getName(), e);
276
        // }
277 251
        try {
278 252
            getResource().execute(new ResourceAction() {
253

  
279 254
                public Object run() throws Exception {
280
                    EditableFeatureAttributeDescriptor attr
281
                            = addGeometryColumn(fType);
282
                    attr.setGeometryType(shpFile.getGeometryType());
283
                    attr.setGeometrySubType(shpFile.getGeometrySubType());
255
                    EditableFeatureAttributeDescriptor attr = addGeometryColumn(fType);
284 256

  
257
                    attr.setGeometryType(geomManager.getGeometryType(shpFile.getGeometryType(),
258
                        shpFile.getGeometrySubType()));
259

  
285 260
                    IProjection srs = getShpParameters().getCRS();
286 261
                    attr.setSRS(srs);
287 262

  
......
291 266
            return fType;
292 267
        } catch (ResourceExecuteException e) {
293 268
            throw new InitializeException(e);
294
			// } finally {
295
            // this.resourcesEnd();
296 269
        }
297 270
    }
298 271

  
299
//	private String getSRSFromPrj(String srsParameters) {
300
//		// TODO identificar que SRS hay que usar, ya sea
301
//		// el que se recibe de los parametros o el que
302
//		// conicida con el que se ha encontrado en el
303
//		// prg... y si ninguna de las dos que?
304
//		return null;
305
//	}
306 272
    protected SHPStoreParameters getShpParameters() {
307 273
        return (SHPStoreParameters) getParameters();
308 274
    }
......
322 288
     * @return
323 289
     * @throws ReadException
324 290
     */
325
    protected FeatureProvider getFeatureProviderByIndex(long index,
326
            FeatureType featureType) throws DataException {
327
		 this.open();
328
//         this.resourcesBegin();
291
    protected FeatureProvider getFeatureProviderByIndex(long index, FeatureType featureType) throws DataException {
292
        this.open();
329 293
        try {
330 294

  
331
            FeatureProvider featureProvider = super.getFeatureProviderByIndex(index,
332
                    featureType);
295
            FeatureProvider featureProvider = super.getFeatureProviderByIndex(index, featureType);
333 296
            featureProvider.setDefaultEnvelope(this.shpFile.getBoundingBox(index));
334 297
            return featureProvider;
335 298
        } catch (DataException e) {
......
342 305

  
343 306
    }
344 307

  
345
    protected void initFeatureProviderByIndex(FeatureProvider featureProvider,
346
            long index, FeatureType featureType) throws DataException {
347
		// this.open();
348
        // this.resourcesBegin();
308
    protected void initFeatureProviderByIndex(FeatureProvider featureProvider, long index, FeatureType featureType)
309
        throws DataException {
349 310
        try {
350 311
            super.initFeatureProviderByIndex(featureProvider, index, featureType);
351 312
            featureProvider.setDefaultEnvelope(this.shpFile.getBoundingBox(index));
......
361 322
     * @param featureProvider
362 323
     * @throws DataException
363 324
     */
364
    protected void loadFeatureProviderByIndex(FeatureProvider featureProvider)
365
            throws DataException {
325
    protected void loadFeatureProviderByIndex(FeatureProvider featureProvider) throws DataException {
366 326

  
367 327
        FeatureType featureType = featureProvider.getType();
368 328
        long index = ((Long) featureProvider.getOID()).longValue();
......
385 345

  
386 346
    }
387 347

  
388
    private boolean hasDBFAttributes(FeatureType featureType,
389
            boolean hasGeometry) {
390
        FeatureAttributeDescriptor[] attributes
391
                = featureType.getAttributeDescriptors();
348
    private boolean hasDBFAttributes(FeatureType featureType, boolean hasGeometry) {
349
        FeatureAttributeDescriptor[] attributes = featureType.getAttributeDescriptors();
392 350
        // If there aren't any attributes, nor has any DBF attributes
393 351
        if (attributes == null || attributes.length == 0) {
394 352
            return false;
......
401 359
        return true;
402 360
    }
403 361

  
404
    protected void loadValue(FeatureProvider featureProvider, int rowIndex,
405
            FeatureAttributeDescriptor descriptor) throws ReadException {
362
    protected void loadValue(FeatureProvider featureProvider, int rowIndex, FeatureAttributeDescriptor descriptor)
363
        throws ReadException {
406 364
        if (descriptor.getType() == DataTypes.GEOMETRY) {
407 365
            return;
408 366
        } else {
......
429 387
        this.shpFile.close();
430 388
    }
431 389

  
432
    public boolean canWriteGeometry(final int geometryType, int geometrySubType)
433
            throws DataException {
390
    public boolean canWriteGeometry(final int geometryType, int geometrySubType) throws DataException {
434 391
        this.open();
435 392
        return ((Boolean) getResource().execute(new ResourceAction() {
393

  
436 394
            public Object run() throws Exception {
437 395
                boolean value = shpFile.canWriteGeometry(geometryType);
438 396
                return value ? Boolean.TRUE : Boolean.FALSE;
439 397
            }
440 398
        })).booleanValue();
441
//		this.resourcesBegin();
442
//		try {
443
//			return this.shpFile.canWriteGeometry(geometryType);
444
//
445
//		} finally {
446
//			this.resourcesEnd();
447
//		}
448 399
    }
449 400

  
450
    public void performChanges(Iterator deleteds, Iterator inserteds,
451
            Iterator updateds, Iterator originalFeatureTypesUpdated)
452
            throws PerformEditingException {
401
    @SuppressWarnings("rawtypes")
402
    public void performChanges(Iterator deleteds, Iterator inserteds, Iterator updateds,
403
        Iterator originalFeatureTypesUpdated) throws PerformEditingException {
453 404

  
454 405
        /*
455 406
         * This will throw an exception if there are new fields
......
469 420

  
470 421
        removeGeometryColumn(dbfFtype);
471 422

  
472
		// try {
473
        // this.resourcesBegin();
474
        // } catch (ResourceExecuteException e1) {
475
        // throw new PerformEditingException(this.getName(), e1);
476
        // }
477 423
        try {
478 424
            // TODO repasar el concepto de enUso de un recurso.
479 425
            loTengoEnUso = true;
480 426
            resourceCloseRequest();
481 427

  
482 428
            getResource().execute(new ResourceAction() {
429

  
483 430
                public Object run() throws Exception {
484 431
                    FeatureSet set = null;
485 432
                    DisposableIterator iter = null;
......
488 435
                        writer = new SHPFeatureWriter(getProviderName());
489 436

  
490 437
                        SHPStoreParameters shpParams = getShpParameters();
491
                        SHPStoreParameters tmpParams
492
                                = (SHPStoreParameters) shpParams.getCopy();
438
                        SHPStoreParameters tmpParams = (SHPStoreParameters) shpParams.getCopy();
493 439

  
494
                        File tmp_base = File.createTempFile(
495
                                "tmp_" + System.currentTimeMillis(), null);
440
                        File tmp_base = File.createTempFile("tmp_" + System.currentTimeMillis(), null);
496 441
                        String str_base = tmp_base.getCanonicalPath();
497 442

  
498 443
                        tmpParams.setDBFFile(str_base + ".dbf");
......
511 456
                        loTengoEnUso = false;
512 457
                        close();
513 458

  
514

  
515 459
                        File tmpPrjFile = SHP.getPrjFile(tmpParams.getSHPFile());
516 460
                        try {
517
                        FileUtils.writeStringToFile(tmpPrjFile, tmpParams.getCRS().export(ICRSFactory.FORMAT_WKT_ESRI));
518
                        } catch(Exception e) {
519
                            logger.info("Can't write prj file '"+tmpPrjFile.getAbsolutePath()+"'.");
461
                            FileUtils.writeStringToFile(tmpPrjFile,
462
                                tmpParams.getCRS().export(ICRSFactory.FORMAT_WKT_ESRI));
463
                        } catch (Exception e) {
464
                            logger.info("Can't write prj file '" + tmpPrjFile.getAbsolutePath() + "'.");
520 465
                        }
521 466

  
522 467
                        if (!shpParams.getDBFFile().delete()) {
523
                            logger.debug("Can't delete dbf file '"+shpParams.getDBFFile()+"'.");
524
                            throw new IOException("Can't delete dbf '"+FilenameUtils.getBaseName(shpParams.getDBFFileName())+"' file to replace with the new dbf.\nThe new dbf is in temporary file '"+str_base+"'");
468
                            logger.debug("Can't delete dbf file '" + shpParams.getDBFFile() + "'.");
469
                            throw new IOException("Can't delete dbf '"
470
                                + FilenameUtils.getBaseName(shpParams.getDBFFileName())
471
                                + "' file to replace with the new dbf.\nThe new dbf is in temporary file '" + str_base
472
                                + "'");
525 473
                        }
526 474
                        if (!shpParams.getSHPFile().delete()) {
527
                            logger.debug("Can't delete dbf file '"+shpParams.getSHPFile()+"'.");
528
                            throw new IOException("Can't delete shp '"+FilenameUtils.getBaseName(shpParams.getSHPFileName())+"' file to replace with the new shp.\nThe new shp is in temporary file '"+str_base+"'");
475
                            logger.debug("Can't delete dbf file '" + shpParams.getSHPFile() + "'.");
476
                            throw new IOException("Can't delete shp '"
477
                                + FilenameUtils.getBaseName(shpParams.getSHPFileName())
478
                                + "' file to replace with the new shp.\nThe new shp is in temporary file '" + str_base
479
                                + "'");
529 480
                        }
530 481
                        if (!shpParams.getSHXFile().delete()) {
531
                            logger.debug("Can't delete dbf file '"+shpParams.getSHXFile()+"'.");
532
                            throw new IOException("Can't delete shx '"+FilenameUtils.getBaseName(shpParams.getSHXFileName())+"' file to replace with the new shx.\nThe new shx is in temporary file '"+str_base+"'");
482
                            logger.debug("Can't delete dbf file '" + shpParams.getSHXFile() + "'.");
483
                            throw new IOException("Can't delete shx '"
484
                                + FilenameUtils.getBaseName(shpParams.getSHXFileName())
485
                                + "' file to replace with the new shx.\nThe new shx is in temporary file '" + str_base
486
                                + "'");
533 487
                        }
534 488

  
535 489
                        File prjFile = SHP.getPrjFile(shpParams.getSHPFile());
......
542 496
                                    + str_base + "'");
543 497
                            }
544 498
                        }
545
                        FileUtils.moveFile(
546
                                tmpParams.getDBFFile(),
547
                                shpParams.getDBFFile());
548
                        FileUtils.moveFile(
549
                                tmpParams.getSHPFile(),
550
                                shpParams.getSHPFile());
551
                        FileUtils.moveFile(
552
                                tmpParams.getSHXFile(),
553
                                shpParams.getSHXFile());
499
                        FileUtils.moveFile(tmpParams.getDBFFile(), shpParams.getDBFFile());
500
                        FileUtils.moveFile(tmpParams.getSHPFile(), shpParams.getSHPFile());
501
                        FileUtils.moveFile(tmpParams.getSHXFile(), shpParams.getSHXFile());
554 502

  
555
                        FileUtils.moveFile(
556
                            tmpPrjFile,
557
                            SHP.getPrjFile(shpParams.getSHPFile()));
503
                        FileUtils.moveFile(tmpPrjFile, SHP.getPrjFile(shpParams.getSHPFile()));
558 504

  
559 505
                        resourcesNotifyChanges();
560 506
                        initFeatureType();
......
573 519

  
574 520
    }
575 521

  
576

  
577 522
    protected void resourceCloseRequest() throws ResourceException {
578
		// super.resourceCloseRequest();
579
        // this.shpResource.closeRequest();
580
        // this.shxResource.closeRequest();
581 523
        getResource().closeRequest();
582 524
    }
583 525

  
......
587 529
    }
588 530

  
589 531
    public void append(final FeatureProvider featureProvider) throws DataException {
590
//		this.resourcesBegin();
591
//		try {
592

  
593 532
        getResource().execute(new ResourceAction() {
533

  
594 534
            public Object run() throws Exception {
595 535
                writer.append(getStoreServices().createFeature(featureProvider));
596 536
                return null;
597 537
            }
598 538
        });
599
//		} finally {
600
//			this.resourcesEnd();
601
//		}
602

  
603 539
    }
604 540

  
605 541
    public void beginAppend() throws DataException {
606
		// this.resourcesBegin();
607
        // try {
608

  
609 542
        getResource().execute(new ResourceAction() {
543

  
610 544
            public Object run() throws Exception {
611 545
                FeatureStore store = getFeatureStore();
612 546
                FeatureType fType = store.getDefaultFeatureType();
613 547

  
614
				// TODO Comprobar el campo de geometria
548
                // TODO Comprobar el campo de geometria
615 549
                EditableFeatureType dbfFtype = fType.getEditable();
616 550

  
617 551
                removeGeometryColumn(dbfFtype);
......
623 557
                return null;
624 558
            }
625 559
        });
626
		// } finally {
627
        // this.resourcesEnd();
628
        // }
629

  
630 560
    }
631 561

  
632 562
    public void endAppend() throws DataException {
633
//		this.resourcesBegin();
634
//		try {
635 563
        getResource().execute(new ResourceAction() {
564

  
636 565
            public Object run() throws Exception {
637 566
                writer.end();
638 567
                close();
......
640 569
                SHPStoreParameters shpParameters = SHPStoreProvider.this.getShpParameters();
641 570
                File prjFile = SHP.getPrjFile(shpParameters.getFile());
642 571
                try {
643
                FileUtils.writeStringToFile(prjFile, shpParameters.getCRS().export(ICRSFactory.FORMAT_WKT_ESRI));
644
                } catch(Exception e) {
645
                    logger.info("Can't write prj file '"+prjFile.getAbsolutePath()+"'.");
572
                    FileUtils.writeStringToFile(prjFile, shpParameters.getCRS().export(ICRSFactory.FORMAT_WKT_ESRI));
573
                } catch (Exception e) {
574
                    logger.info("Can't write prj file '" + prjFile.getAbsolutePath() + "'.");
646 575
                }
647 576

  
648 577
                resourcesNotifyChanges();
649 578
                return null;
650 579
            }
651 580
        });
652
//		} finally {
653
//			this.resourcesEnd();
654
//		}
655

  
656 581
    }
657 582

  
658 583
    public Object getSourceId() {

Also available in: Unified diff