Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate check on importing in find unlinked files #12076

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/main/java/org/jabref/gui/externalfiles/ImportHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -417,4 +417,46 @@ public void importEntriesWithDuplicateCheck(BibDatabaseContext database, List<Bi
}
}
}

/**
* This method processes a list of file paths to extract BibEntry objects for import.
* It ensures that only unique entries are added to the import list by checking for duplicates
* against the existing database entries. If a file is unsupported or contains no valid entries,
* an empty entry with a link is created to maintain the integrity of the import process.
*
* @param files the list of file paths to process
* @return a list of BibEntry objects to be imported, ensuring uniqueness
*
*/
public List<BibEntry> getEntriesToImport(List<Path> files) {
List<BibEntry> entriesToImport = new ArrayList<>();
for (Path file : files) {
if (FileUtil.isPDFFile(file)) {
var pdfImporterResult = contentImporter.importPDFContent(file);
List<BibEntry> pdfEntriesInFile = pdfImporterResult.getDatabase().getEntries();
if (pdfImporterResult.hasWarnings()) {
LOGGER.warn("Warnings occurred while importing PDF content: {}", pdfImporterResult.getErrorMessage());
}
if (!pdfEntriesInFile.isEmpty()) {
entriesToImport.addAll(FileUtil.relativize(pdfEntriesInFile, bibDatabaseContext, preferences.getFilePreferences()));
} else {
entriesToImport.add(createEmptyEntryWithLink(file));
}
} else if (FileUtil.isBibFile(file)) {
try {
var bibtexParserResult = contentImporter.importFromBibFile(file, fileUpdateMonitor);
if (bibtexParserResult.hasWarnings()) {
LOGGER.warn("Warnings occurred while importing BibTeX file: {}", bibtexParserResult.getErrorMessage());
}
entriesToImport.addAll(bibtexParserResult.getDatabaseContext().getEntries());
} catch (IOException ex) {
LOGGER.error("Error importing from BibTeX file", ex);
}
} else {
LOGGER.warn("Unsupported file type: {}", file.toString());
entriesToImport.add(createEmptyEntryWithLink(file));
}
}
return entriesToImport;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -43,6 +45,8 @@
import org.jabref.logic.util.StandardFileType;
import org.jabref.logic.util.TaskExecutor;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.util.FileUpdateMonitor;

import de.saxsys.mvvmfx.utils.validation.FunctionBasedValidator;
Expand Down Expand Up @@ -83,6 +87,8 @@ public class UnlinkedFilesDialogViewModel {

private final FunctionBasedValidator<String> scanDirectoryValidator;

private List<BibEntry> entriesToImportWithoutDuplicates;

public UnlinkedFilesDialogViewModel(DialogService dialogService,
UndoManager undoManager,
FileUpdateMonitor fileUpdateMonitor,
Expand Down Expand Up @@ -155,6 +161,21 @@ public void startImport() {
}
resultList.clear();

List<BibEntry> entriesToImport = importHandler.getEntriesToImport(fileList);
entriesToImportWithoutDuplicates = new ArrayList<>();

for (BibEntry entry : entriesToImport) {
Optional<BibEntry> existingDuplicate = importHandler.findDuplicate(bibDatabase, entry);
if (existingDuplicate.isPresent()) {
LOGGER.info("Skipping duplicate entry: {}", entry);
} else {
entriesToImportWithoutDuplicates.add(entry);
}
}
if (entriesToImportWithoutDuplicates.isEmpty()) {
LOGGER.warn("No unique entries to import, skipping background task.");
return;
}
importFilesBackgroundTask = importHandler.importFilesInBackground(fileList, bibDatabase, preferences.getFilePreferences(), TransferMode.LINK)
.onRunning(() -> {
progressValueProperty.bind(importFilesBackgroundTask.workDonePercentageProperty());
Expand All @@ -166,10 +187,46 @@ public void startImport() {
progressTextProperty.unbind();
taskActiveProperty.setValue(false);
})
.onSuccess(resultList::addAll);
.onSuccess(importFilesResultItemViewModels -> {
if (entriesToImportWithoutDuplicates != null && !entriesToImportWithoutDuplicates.isEmpty()) {
List<ImportFilesResultItemViewModel> convertedEntries = entriesToImportWithoutDuplicates.stream()
.map(entry -> new ImportFilesResultItemViewModel(
Objects.requireNonNull(UnlinkedFilesDialogViewModel.this.getFilePathFromEntry(entry)),
true,
"Imported successfully"))
.toList();
resultList.addAll(convertedEntries);
} else {
LOGGER.warn("No entries to import or list is not initialized properly.");
}
});
importFilesBackgroundTask.executeWith(taskExecutor);
}

/**
* Extracts the file path from the given BibEntry.
*
* @param entry The BibEntry instance from which to extract the file path
* @return The file path if a valid file field exists in the BibEntry; otherwise, returns an empty path.
*
*/
private Path getFilePathFromEntry(BibEntry entry) {
Optional<String> fileField = entry.getField(StandardField.FILE);
if (fileField.isPresent()) {
String fileFieldValue = fileField.get();
String[] fileParts = fileFieldValue.split(":");

if (fileParts.length > 1) {
return Path.of(fileParts[1]);
} else {
LOGGER.warn("File field is not in the expected format: {}", fileFieldValue);
}
} else {
LOGGER.warn("No file associated with the entry: {}", entry);
}
return Path.of("");
}

/**
* This starts the export of all files of all selected nodes in the file tree view.
*/
Expand Down Expand Up @@ -202,7 +259,7 @@ public void startExport() {
} catch (IOException e) {
LOGGER.error("Error exporting", e);
}
}
}

public ObservableList<FileExtensionViewModel> getFileFilters() {
return this.fileFilterList;
Expand Down
Loading