-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
If entry data is changed, file name should also be changed. #12024
Conversation
…it would be saved for subsequent runs
Hello, @HoussemNasri, good evening. I have implemented the changes. Please let me know if any changes are needed. |
src/main/java/org/jabref/gui/externalfiles/LinkedFileAutoRenamer.java
Outdated
Show resolved
Hide resolved
@@ -524,4 +524,66 @@ public static boolean detectBadFileName(String fileName) { | |||
public static boolean isCharLegal(char c) { | |||
return Arrays.binarySearch(ILLEGAL_CHARS, c) < 0; | |||
} | |||
|
|||
public static boolean renameLinkedFile(LinkedFile linkedFile, BibEntry entry, String fileNamePattern, List<Path> fileDirectories, FilePreferences filePreferences, BibDatabaseContext bibDatabaseContext) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this method duplicated from somewhere else in the codebase? I'm sure we have already implemented this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have another implementation at LinkedFileHandler
that differs slightly from this one. Can we refactor this to remove the duplication? Perhaps we can combine both implementations and change dependents of LinkedFileHandler and LinkedFileAutoRenamer to depend on the same implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did this as well!
|
||
if (renamed) { | ||
UiTaskExecutor.runInJavaFXThread(() -> { | ||
entry.setFiles(linkedFiles); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm uncertain whether we need to do this for every linked file. The typical user would have a handful or linked files per entry, but you never now, people can get very creative, so if it's possible to minimize calls to setFiles, we should do it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @HoussemNasri, do you have any suggestion on what should be done for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please checkout the comment below.
|
||
public class FileNameParser { | ||
|
||
private static final Pattern FIELD_PATTERN = Pattern.compile("\\[([^\\]]+)\\]"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm pretty sure this is already implemented. Just to clarify, it is possible to change the linked file name to follow a pattern by right-clicking on the linked file and choosing 'rename file to a defined pattern'.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed the code to use BracketedPattern instead and deleted this class!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The feature works great! Kudos on the fantastic code quality. Just a few notes on code duplication.
@HoussemNasri Thank you so much for the feedback. I will try my best to make these changes as soon as possible! |
Merge branch 'fix-for-issue-11316' of https://github.com/AdiAr11/jabref into fix-for-issue-11316
Hi @HoussemNasri, i have made the changes you requested. Hope it is fine now. |
Merge branch 'main' into fix-for-issue-11316
boolean renamed = FileUtil.renameLinkedFileToName(linkedFile, entry, newFileName, false, bibDatabaseContext, fileDirectories, preferences.getFilePreferences()); | ||
|
||
if (renamed) { | ||
UiTaskExecutor.runInJavaFXThread(() -> { | ||
entry.setFiles(linkedFiles); | ||
LOGGER.debug("Updated entry files after renaming."); | ||
}); | ||
} | ||
} catch (IOException e) { | ||
LOGGER.error("Failed to rename file '{}' to '{}'", linkedFile.getLink(), fileNamePattern, e); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
boolean renamed = FileUtil.renameLinkedFileToName(linkedFile, entry, newFileName, false, bibDatabaseContext, fileDirectories, preferences.getFilePreferences()); | |
if (renamed) { | |
UiTaskExecutor.runInJavaFXThread(() -> { | |
entry.setFiles(linkedFiles); | |
LOGGER.debug("Updated entry files after renaming."); | |
}); | |
} | |
} catch (IOException e) { | |
LOGGER.error("Failed to rename file '{}' to '{}'", linkedFile.getLink(), fileNamePattern, e); | |
} | |
} | |
FileUtil.renameLinkedFileToName(linkedFile, entry, newFileName, false, bibDatabaseContext, fileDirectories, preferences.getFilePreferences()); | |
} | |
} catch (IOException e) { | |
LOGGER.error("Failed to rename file '{}' to '{}'", linkedFile.getLink(), fileNamePattern, e); | |
} | |
} | |
entry.setFiles(linkedFiles); | |
LOGGER.debug("Updated entry files after renaming."); |
Scheduling the linked files update on the JavaFX thread will lead to glitches if you change the fields fast enough. Also, we only need to call setFiles
once we have renamed all the linked files that needed renaming.
String newFileName = FileUtil.createFileNameFromPattern(bibDatabaseContext.getDatabase(), entry, fileNamePattern); | ||
LOGGER.debug("Generated new file name: '{}'", newFileName); | ||
|
||
boolean renamed = FileUtil.renameLinkedFileToName(linkedFile, entry, newFileName, false, bibDatabaseContext, fileDirectories, preferences.getFilePreferences()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Passing the boolean false
like this is against best practices, please define a constant OVERWRITE_EXISTING_FILE = true, and pass !OVERWRITE_EXISTING_FILE
to the method.
@@ -588,4 +588,73 @@ public static String shortenFileName(String fileName, Integer maxLength) { | |||
public static boolean isCharLegal(char c) { | |||
return Arrays.binarySearch(ILLEGAL_CHARS, c) < 0; | |||
} | |||
|
|||
public static boolean renameLinkedFileToName(LinkedFile linkedFile, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is too much logic to go untested. Please add unit tests for it. You can refer to our testing guide in the devdocs, and reviewing other test classes may also provide useful insights.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can call the already existing RenamePdfCleanup which does exactly this
Noted! Will make these changes and write the test cases. Thank you! |
Closing this issue due to inactivity 💤 We discussed in the DevCall. The implementation should make use of the existing renaming cleanup. |
Closes #11316
Mandatory checks
CHANGELOG.md
described in a way that is understandable for the average user (if applicable)UI change: