Skip to content

Add auto-renaming of linked files on entry data change #13295

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

paudelritij
Copy link
Contributor

Closes #11316

This PR introduces an option to automatically rename linked files when the associated entry data is modified. The feature is configurable in Preferences under Linked files -> Linked file name conventions, with a default setting of false. It also includes handling for entries with multiple attached files and adds relevant test cases.

Steps to test

  1. Open JabRef and go to Preferences > Linked files.
  2. Enable [ ] Auto rename files if entry changes.
  3. Create a new entry and attach a file.
  4. Modify any field in the entry.
  5. Check that the linked file's name updates to match the new citation key.
  6. Test with multiple files to ensure all are renamed.
image

Mandatory checks

  • I own the copyright of the code submitted and I license it under the MIT license
  • Change in CHANGELOG.md described in a way that is understandable for the average user (if change is visible to the user)
  • Tests created for changes (if applicable)
  • Manually tested changed features in running JabRef (always required)
  • Screenshots added in PR description (if change is visible to the user)
  • 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.

- Implemented a preference option under Linked files -> Linked file name conventions to enable auto-renaming of files when entry data changes (default: false).
- Added functionality to listen for entry change events and rename files if the preference is enabled and the file name matches the defined pattern.
- Ensured that no action is taken if the pattern is empty, as the file name would not match.
- Considered scenarios where an entry has multiple files attached and handled them appropriately.
- Added test cases to verify the new functionality.
@Test
void multipleFilesRenameOnEntryChange() throws IOException {
// create multiple entries
List<String> fileNames = Arrays.asList(
Copy link

Choose a reason for hiding this comment

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

Modern Java data structures should be used. Instead of Arrays.asList, consider using List.of for better performance and readability.

@@ -106,6 +109,18 @@ public void setStoreFilesRelativeToBibFile(boolean shouldStoreFilesRelativeToBib
this.storeFilesRelativeToBibFile.set(shouldStoreFilesRelativeToBibFile);
}

public boolean shouldAutoRenameFilesOnChange() {
Copy link

Choose a reason for hiding this comment

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

The method should return an Optional instead of a primitive boolean to avoid returning null and to align with modern Java practices.

@paudelritij paudelritij marked this pull request as ready for review June 10, 2025 00:56
@@ -203,6 +204,7 @@ private void initializeComponentsAndListeners(boolean isDummyContext) {

bibDatabaseContext.getDatabase().registerListener(this);
bibDatabaseContext.getMetaData().registerListener(this);
new AutoRenameFileOnEntryChange(bibDatabaseContext, preferences);
Copy link

Choose a reason for hiding this comment

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

The instantiation of AutoRenameFileOnEntryChange should be moved to org.jabref.logic package as it involves non-GUI operations, adhering to the layered architecture principle.

}

private boolean relatesToFilePattern(String fileNamePattern, FieldChangedEvent event) {
Set<String> extractedFields = new HashSet<>();
Copy link

Choose a reason for hiding this comment

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

Using 'new HashSet<>()' is not optimal. Consider using 'Set.of()' for better readability and performance as per modern Java practices.

// change year only
entry.setField(StandardField.YEAR, "2082");
assertEquals("newKey2082.pdf", entry.getFiles().getFirst().getLink());
assertTrue(Files.exists(tempDir.resolve("newKey2082.pdf")));
Copy link

Choose a reason for hiding this comment

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

The assertion checks for a boolean condition instead of asserting the expected content of the object. Use assertEquals to compare expected and actual values.

Copy link

trag-bot bot commented Jun 11, 2025

@trag-bot didn't find any issues in the code! ✅✨

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

If entry data is changed, file name should also be changed
1 participant