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

Refine code for BibEntry#replaceDownloadedFile #9118

Merged
merged 3 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -441,13 +441,12 @@ public void download() {
}

if (!isDuplicate) {
// we need to call LinkedFileViewModel#fromFile, because we need to make the path relative to the configured directories
LinkedFile newLinkedFile = LinkedFilesEditorViewModel.fromFile(
destination,
databaseContext.getFileDirectories(preferences.getFilePreferences()),
preferences.getFilePreferences());
List<LinkedFile> linkedFiles = entry.getFiles();

entry.addLinkedFile(entry, linkedFile, newLinkedFile, linkedFiles);
entry.replaceDownloadedFile(linkedFile.getLink(), newLinkedFile);

// Notify in bar when the file type is HTML.
if (newLinkedFile.getFileType().equals(StandardExternalFileType.URL.getName())) {
Expand Down
22 changes: 17 additions & 5 deletions src/main/java/org/jabref/model/entry/BibEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -1002,22 +1002,34 @@ public Observable[] getObservables() {
return new Observable[] {fields, type};
}

public void addLinkedFile(BibEntry entry, LinkedFile linkedFile, LinkedFile newLinkedFile, List<LinkedFile> linkedFiles) {
/**
* Helper method to add a downloaded file to the entry.
* <p>
* Use-case: URL is contained in the file, the file is downloaded and should then replace the url.
* This method. adds the given path (as file) to the entry and removes the url.
*
* @param linkToDownloadedFile the link to the file, which was downloaded
* @param downloadedFile the path to be added to the entry
*/
public void replaceDownloadedFile(String linkToDownloadedFile, LinkedFile downloadedFile) {
List<LinkedFile> linkedFiles = this.getFiles();

int oldFileIndex = -1;
int i = 0;
while ((i < linkedFiles.size()) && (oldFileIndex == -1)) {
LinkedFile file = linkedFiles.get(i);
// The file type changes as part of download process (see prepareDownloadTask), thus we only compare by link
if (file.getLink().equalsIgnoreCase(linkedFile.getLink())) {
if (file.getLink().equalsIgnoreCase(linkToDownloadedFile)) {
oldFileIndex = i;
}
i++;
}
if (oldFileIndex == -1) {
linkedFiles.add(0, newLinkedFile);
linkedFiles.add(0, downloadedFile);
} else {
linkedFiles.set(oldFileIndex, newLinkedFile);
linkedFiles.set(oldFileIndex, downloadedFile);
}
entry.setFiles(linkedFiles);

this.setFiles(linkedFiles);
}
}