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

If entry data is changed, file name should also be changed. #12024

Closed
wants to merge 22 commits into from

Conversation

AdiAr11
Copy link

@AdiAr11 AdiAr11 commented Oct 19, 2024

Closes #11316

Mandatory checks

  • I own the copyright of the code submitted and I licence it under the MIT license
  • Change in CHANGELOG.md described in a way that is understandable for the average user (if applicable)
  • Tests created for changes (if applicable)
  • Manually tested changed features in running JabRef (always required)
  • Screenshots added in PR description (for UI changes)
  • Checked developer's documentation: Is the information available and up to date? If not, I outlined it in this pull request.
  • Checked documentation: Is the information available and up to date? If not, I created an issue at https://github.com/JabRef/user-documentation/issues or, even better, I submitted a pull request to the documentation repository.

UI change:
Screenshot 2024-10-22 at 7 22 24 PM

@HoussemNasri HoussemNasri changed the title Started working on task- If entry data is changed, file name should also be changed. [WIP] If entry data is changed, file name should also be changed. Oct 19, 2024
@AdiAr11 AdiAr11 marked this pull request as ready for review October 22, 2024 08:25
@AdiAr11
Copy link
Author

AdiAr11 commented Oct 22, 2024

Hello, @HoussemNasri, good evening. I have implemented the changes. Please let me know if any changes are needed.
Thank you!

@HoussemNasri HoussemNasri changed the title [WIP] If entry data is changed, file name should also be changed. If entry data is changed, file name should also be changed. Oct 22, 2024
@@ -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) {
Copy link
Member

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.

Copy link
Member

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.

Copy link
Author

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);
Copy link
Member

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.

Copy link
Author

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?

Copy link
Member

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("\\[([^\\]]+)\\]");
Copy link
Member

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'.

Copy link
Author

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!!

Copy link
Member

@HoussemNasri HoussemNasri left a 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.

@AdiAr11
Copy link
Author

AdiAr11 commented Oct 24, 2024

@HoussemNasri Thank you so much for the feedback. I will try my best to make these changes as soon as possible!

@AdiAr11
Copy link
Author

AdiAr11 commented Oct 26, 2024

Hi @HoussemNasri, i have made the changes you requested. Hope it is fine now.
Thank you!

Merge branch 'main' into fix-for-issue-11316
Comment on lines +94 to +105
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);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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());
Copy link
Member

@HoussemNasri HoussemNasri Oct 27, 2024

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,
Copy link
Member

@HoussemNasri HoussemNasri Oct 27, 2024

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.

Copy link
Member

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

@AdiAr11
Copy link
Author

AdiAr11 commented Oct 28, 2024

Noted! Will make these changes and write the test cases. Thank you!

@koppor koppor added the status: changes required Pull requests that are not yet complete label Oct 31, 2024
@koppor
Copy link
Member

koppor commented Dec 9, 2024

Closing this issue due to inactivity 💤

We discussed in the DevCall. The implementation should make use of the existing renaming cleanup.

@koppor koppor closed this Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: changes required Pull requests that are not yet complete
Projects
None yet
Development

Successfully merging this pull request may close these issues.

If entry data is changed, file name should also be changed
4 participants