Search in sources :

Example 1 with DuplicateResolverResult

use of org.jabref.gui.DuplicateResolverDialog.DuplicateResolverResult in project jabref by JabRef.

the class DuplicateSearch method run.

@Override
public void run() {
    panel.output(Localization.lang("Searching for duplicates..."));
    bes = panel.getDatabase().getEntries();
    if (bes.size() < 2) {
        return;
    }
    SearcherRunnable st = new SearcherRunnable();
    JabRefExecutorService.INSTANCE.executeInterruptableTask(st, "DuplicateSearcher");
    int current = 0;
    final List<BibEntry> toRemove = new ArrayList<>();
    final List<BibEntry> toAdd = new ArrayList<>();
    int duplicateCounter = 0;
    boolean autoRemoveExactDuplicates = false;
    synchronized (duplicates) {
        while (!st.finished() || (current < duplicates.size())) {
            if (current >= duplicates.size()) {
                try {
                    duplicates.wait();
                } catch (InterruptedException ignored) {
                // Ignore
                }
            } else {
                // duplicates found
                List<BibEntry> be = duplicates.get(current);
                current++;
                if (!toRemove.contains(be.get(0)) && !toRemove.contains(be.get(1))) {
                    // Check if they are exact duplicates:
                    boolean askAboutExact = false;
                    if (DuplicateCheck.compareEntriesStrictly(be.get(0), be.get(1)) > 1) {
                        if (autoRemoveExactDuplicates) {
                            toRemove.add(be.get(1));
                            duplicateCounter++;
                            continue;
                        }
                        askAboutExact = true;
                    }
                    DuplicateCallBack cb = new DuplicateCallBack(JabRefGUI.getMainFrame(), be.get(0), be.get(1), askAboutExact ? DuplicateResolverType.DUPLICATE_SEARCH_WITH_EXACT : DuplicateResolverType.DUPLICATE_SEARCH);
                    ((CallBack) Spin.over(cb)).update();
                    duplicateCounter++;
                    DuplicateResolverResult answer = cb.getSelected();
                    if ((answer == DuplicateResolverResult.KEEP_LEFT) || (answer == DuplicateResolverResult.AUTOREMOVE_EXACT)) {
                        toRemove.add(be.get(1));
                        if (answer == DuplicateResolverResult.AUTOREMOVE_EXACT) {
                            // Remember choice
                            autoRemoveExactDuplicates = true;
                        }
                    } else if (answer == DuplicateResolverResult.KEEP_RIGHT) {
                        toRemove.add(be.get(0));
                    } else if (answer == DuplicateResolverResult.BREAK) {
                        // thread killing
                        st.setFinished();
                        current = Integer.MAX_VALUE;
                        // correct counter
                        duplicateCounter--;
                    } else if (answer == DuplicateResolverResult.KEEP_MERGE) {
                        toRemove.addAll(be);
                        toAdd.add(cb.getMergedEntry());
                    }
                }
            }
        }
    }
    final NamedCompound ce = new NamedCompound(Localization.lang("duplicate removal"));
    final int dupliC = duplicateCounter;
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            // Now, do the actual removal:
            if (!toRemove.isEmpty()) {
                for (BibEntry entry : toRemove) {
                    panel.getDatabase().removeEntry(entry);
                    ce.addEdit(new UndoableRemoveEntry(panel.getDatabase(), entry, panel));
                }
                panel.markBaseChanged();
            }
            // and adding merged entries:
            if (!toAdd.isEmpty()) {
                for (BibEntry entry : toAdd) {
                    panel.getDatabase().insertEntry(entry);
                    ce.addEdit(new UndoableInsertEntry(panel.getDatabase(), entry, panel));
                }
                panel.markBaseChanged();
            }
            synchronized (duplicates) {
                panel.output(Localization.lang("Duplicates found") + ": " + duplicates.size() + ' ' + Localization.lang("pairs processed") + ": " + dupliC);
            }
            ce.end();
            panel.getUndoManager().addEdit(ce);
        }
    });
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) CallBack(org.jabref.gui.worker.CallBack) ArrayList(java.util.ArrayList) UndoableRemoveEntry(org.jabref.gui.undo.UndoableRemoveEntry) UndoableInsertEntry(org.jabref.gui.undo.UndoableInsertEntry) NamedCompound(org.jabref.gui.undo.NamedCompound) DuplicateResolverResult(org.jabref.gui.DuplicateResolverDialog.DuplicateResolverResult)

Aggregations

ArrayList (java.util.ArrayList)1 DuplicateResolverResult (org.jabref.gui.DuplicateResolverDialog.DuplicateResolverResult)1 NamedCompound (org.jabref.gui.undo.NamedCompound)1 UndoableInsertEntry (org.jabref.gui.undo.UndoableInsertEntry)1 UndoableRemoveEntry (org.jabref.gui.undo.UndoableRemoveEntry)1 CallBack (org.jabref.gui.worker.CallBack)1 BibEntry (org.jabref.model.entry.BibEntry)1