Search in sources :

Example 1 with RecordLayerErrors

use of com.revolsys.swing.map.layer.record.table.model.RecordLayerErrors in project com.revolsys.open by revolsys.

the class AbstractUpdateField method finish.

private void finish() {
    if (this.recordCount > 100) {
        final int confirm = JOptionPane.showConfirmDialog(this, "<html>Update <b style='color:#32CD32'>" + this.recordCountString + "</b> records? This may take a long time or fail if there are many records.</html>", "Update Records?", JOptionPane.OK_CANCEL_OPTION);
        if (confirm != JOptionPane.OK_OPTION) {
            setVisible(false);
            return;
        }
    }
    final ProgressMonitor progressMonitor = new ProgressMonitor(this, getProgressMonitorTitle(), "Updated 0 of " + this.recordCountString, 0, 100);
    progressMonitor.setProgress(0);
    setVisible(false);
    final AtomicInteger progress = new AtomicInteger();
    final SwingWorker<?, ?> task = new SwingWorker<Void, Void>() {

        @Override
        protected Void doInBackground() throws Exception {
            final Set<String> fieldNames = new LinkedHashSet<>();
            fieldNames.add(AbstractUpdateField.this.fieldDefinition.getName());
            fieldNames.addAll(AbstractUpdateField.this.layer.getFieldNames());
            final RecordLayerErrors errors = new RecordLayerErrors("Setting Field Values", AbstractUpdateField.this.layer, fieldNames);
            AbstractUpdateField.this.tableModel.forEachRecord((record) -> {
                if (progressMonitor.isCanceled()) {
                    throw new CancellationException();
                } else {
                    try {
                        updateRecord(record);
                    } catch (final WrappedException e) {
                        errors.addRecord(record, e.getCause());
                    } catch (final Throwable e) {
                        errors.addRecord(record, e);
                    }
                    final int updateCount = progress.incrementAndGet();
                    if (updateCount < AbstractUpdateField.this.recordCount) {
                        final int updatePercent = (int) Math.floor(updateCount * 100 / (double) AbstractUpdateField.this.recordCount);
                        if (updatePercent < 100) {
                            setProgress(updatePercent);
                        }
                    }
                }
            });
            setProgress(100);
            if (!isCancelled()) {
                errors.showErrorDialog();
            }
            return null;
        }

        @Override
        protected void done() {
        }
    };
    task.addPropertyChangeListener((event) -> {
        if ("progress" == event.getPropertyName()) {
            final int percent = (Integer) event.getNewValue();
            progressMonitor.setProgress(percent);
            progressMonitor.setNote("Updated " + progress + " of " + this.recordCountString);
        }
    });
    Invoke.worker(task);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) WrappedException(com.revolsys.util.WrappedException) RecordLayerErrors(com.revolsys.swing.map.layer.record.table.model.RecordLayerErrors) ProgressMonitor(javax.swing.ProgressMonitor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CancellationException(java.util.concurrent.CancellationException) SwingWorker(javax.swing.SwingWorker)

Example 2 with RecordLayerErrors

use of com.revolsys.swing.map.layer.record.table.model.RecordLayerErrors in project com.revolsys.open by revolsys.

the class AbstractRecordLayer method saveChanges.

public final boolean saveChanges(final LayerRecord record) {
    // Includes two types of validation of record. The first checks field
    // types before interacting with the record store. The second is any
    // errors on the actual saving of data.
    final Set<Boolean> allSaved = new HashSet<>();
    // 
    RecordValidationDialog.validateRecords(// 
    "Save Changes", // 
    this, record, (validator) -> {
        // Success
        // Save the valid records
        final List<LayerRecord> validRecords = validator.getValidRecords();
        if (!validRecords.isEmpty()) {
            final RecordLayerErrors errors = new RecordLayerErrors("Saving Changes", this);
            try (BaseCloseable eventsEnabled = eventsDisabled()) {
                synchronized (this.getSync()) {
                    try {
                        final boolean saved = internalSaveChanges(errors, record);
                        if (!saved) {
                            errors.addRecord(record, "Unknown error");
                        }
                    } catch (final Throwable t) {
                        errors.addRecord(record, t);
                    }
                }
                cleanCachedRecords();
                record.fireRecordUpdated();
            } finally {
                if (!errors.showErrorDialog()) {
                    allSaved.add(false);
                }
            }
        }
        final List<LayerRecord> invalidRecords = validator.getInvalidRecords();
        if (!invalidRecords.isEmpty()) {
            allSaved.add(false);
        }
    }, (validator) -> {
        allSaved.add(false);
    });
    return allSaved.isEmpty();
}
Also used : BaseCloseable(com.revolsys.io.BaseCloseable) RecordLayerErrors(com.revolsys.swing.map.layer.record.table.model.RecordLayerErrors) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Example 3 with RecordLayerErrors

use of com.revolsys.swing.map.layer.record.table.model.RecordLayerErrors in project com.revolsys.open by revolsys.

the class AbstractRecordLayer method saveChanges.

public final boolean saveChanges(final Collection<? extends LayerRecord> records) {
    try {
        if (records.isEmpty()) {
            return true;
        } else {
            // Includes two types of validation of record. The first checks field
            // types before interacting with the record store. The second is any
            // errors on the actual saving of data.
            final Set<Boolean> allSaved = new HashSet<>();
            // 
            RecordValidationDialog.validateRecords(// 
            "Save Changes", // 
            this, records, (validator) -> {
                // Success
                // Save the valid records
                final List<LayerRecord> validRecords = validator.getValidRecords();
                if (!validRecords.isEmpty()) {
                    final RecordLayerErrors errors = new RecordLayerErrors("Saving Changes", this);
                    try (BaseCloseable eventsEnabled = eventsDisabled()) {
                        for (final LayerRecord record : validRecords) {
                            synchronized (this.getSync()) {
                                try {
                                    final boolean saved = internalSaveChanges(errors, record);
                                    if (!saved) {
                                        errors.addRecord(record, "Unknown error");
                                    }
                                } catch (final Throwable t) {
                                    errors.addRecord(record, t);
                                }
                            }
                        }
                        cleanCachedRecords();
                    } finally {
                        if (!errors.showErrorDialog()) {
                            allSaved.add(false);
                        }
                    }
                    fireRecordsChanged();
                }
                final List<LayerRecord> invalidRecords = validator.getInvalidRecords();
                if (!invalidRecords.isEmpty()) {
                    allSaved.add(false);
                }
            }, (validator) -> {
                allSaved.add(false);
            });
            return allSaved.isEmpty();
        }
    } finally {
        fireSelected();
        fireHasChangedRecords();
    }
}
Also used : BaseCloseable(com.revolsys.io.BaseCloseable) RecordLayerErrors(com.revolsys.swing.map.layer.record.table.model.RecordLayerErrors) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Aggregations

RecordLayerErrors (com.revolsys.swing.map.layer.record.table.model.RecordLayerErrors)3 LinkedHashSet (java.util.LinkedHashSet)3 BaseCloseable (com.revolsys.io.BaseCloseable)2 HashSet (java.util.HashSet)2 WrappedException (com.revolsys.util.WrappedException)1 CancellationException (java.util.concurrent.CancellationException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ProgressMonitor (javax.swing.ProgressMonitor)1 SwingWorker (javax.swing.SwingWorker)1