-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Rework AutoSetFileLinks * Only add first found file * Improve magic AutoFileLinking in entry editor by using same logic as manually triggering it * Don't use paths.get * add changelog entry * refactor TODO: remove duplicate findings * Use HashMap with BibEntry and LinkedFiles Only call setFiles when pressing F7 Fix javafx thread error * improve optional code use streams in externalfiletype * logging remove javadoc params * pass preferences to method * Return list of Linkedfiles and let caller handle bib entries Add test * Replace mocking of Jabrefs prefs with autolink prefs
- Loading branch information
1 parent
0235f63
commit 551cb91
Showing
7 changed files
with
204 additions
and
164 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
src/main/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.jabref.gui.externalfiles; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
import org.jabref.gui.externalfiletype.ExternalFileType; | ||
import org.jabref.gui.externalfiletype.ExternalFileTypes; | ||
import org.jabref.gui.externalfiletype.UnknownExternalFileType; | ||
import org.jabref.logic.util.io.AutoLinkPreferences; | ||
import org.jabref.logic.util.io.FileFinder; | ||
import org.jabref.logic.util.io.FileFinders; | ||
import org.jabref.model.database.BibDatabaseContext; | ||
import org.jabref.model.entry.BibEntry; | ||
import org.jabref.model.entry.LinkedFile; | ||
import org.jabref.model.metadata.FileDirectoryPreferences; | ||
import org.jabref.model.util.FileHelper; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
|
||
public class AutoSetFileLinksUtil { | ||
|
||
private static final Log LOGGER = LogFactory.getLog(AutoSetLinks.class); | ||
|
||
public List<LinkedFile> findassociatedNotLinkedFiles(BibEntry entry, BibDatabaseContext databaseContext, FileDirectoryPreferences fileDirPrefs, AutoLinkPreferences autoLinkPrefs, ExternalFileTypes externalFileTypes) { | ||
List<LinkedFile> linkedFiles = new ArrayList<>(); | ||
|
||
List<Path> dirs = databaseContext.getFileDirectoriesAsPaths(fileDirPrefs); | ||
List<String> extensions = externalFileTypes.getExternalFileTypeSelection().stream().map(ExternalFileType::getExtension).collect(Collectors.toList()); | ||
|
||
// Run the search operation: | ||
FileFinder fileFinder = FileFinders.constructFromConfiguration(autoLinkPrefs); | ||
List<Path> result = fileFinder.findAssociatedFiles(entry, dirs, extensions); | ||
|
||
// Iterate over the entries: | ||
|
||
for (Path foundFile : result) { | ||
boolean existingSameFile = entry.getFiles().stream() | ||
.map(file -> file.findIn(dirs)) | ||
.anyMatch(file -> { | ||
try { | ||
return file.isPresent() && Files.isSameFile(file.get(), foundFile); | ||
} catch (IOException e) { | ||
LOGGER.error("Problem with isSameFile", e); | ||
} | ||
return false; | ||
}); | ||
if (!existingSameFile) { | ||
|
||
Optional<ExternalFileType> type = FileHelper.getFileExtension(foundFile) | ||
.map(externalFileTypes::getExternalFileTypeByExt) | ||
.orElse(Optional.of(new UnknownExternalFileType(""))); | ||
|
||
String strType = type.isPresent() ? type.get().getName() : ""; | ||
|
||
LinkedFile linkedFile = new LinkedFile("", foundFile.toString(), strType); | ||
linkedFiles.add(linkedFile); | ||
} | ||
} | ||
|
||
return linkedFiles; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.