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

Fix respecting overWriteOwner preferences #9896

Merged
merged 3 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We fixed an issue where an exception was thrown for the user after <kbd>Ctrl</kbd>+<kbd>Z</kbd> command [#9737](https://github.com/JabRef/jabref/issues/9737)
- We fixed the citation key generation for (`[authors]`, `[authshort]`, `[authorsAlpha]`, `authIniN`, `authEtAl`, `auth.etal`)[https://docs.jabref.org/setup/citationkeypatterns#special-field-markers] to handle `and others` properly. [koppor#626](https://github.com/koppor/jabref/issues/626)
- We fixed the Save/save as file type shows BIBTEX_DB instead of "Bibtex library" [#9372](https://github.com/JabRef/jabref/issues/9372)
- We fixed an issue when overwriting the owner was disabled. [#9896](https://github.com/JabRef/jabref/pull/9896)
- We fixed an issue regarding recording redundant prefixes in search history. [#9685](https://github.com/JabRef/jabref/issues/9685)
- We fixed an issue where passing a URL containing a DOI led to a "No entry found" notification. [#9821](https://github.com/JabRef/jabref/issues/9821)

Expand Down
10 changes: 3 additions & 7 deletions src/main/java/org/jabref/gui/LibraryTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -469,13 +469,9 @@ public void insertEntries(final List<BibEntry> entries) {
bibDatabaseContext.getDatabase().insertEntries(entries);

// Set owner and timestamp
for (BibEntry entry : entries) {
UpdateField.setAutomaticFields(entry,
true,
true,
preferencesService.getOwnerPreferences(),
preferencesService.getTimestampPreferences());
}
UpdateField.setAutomaticFields(entries,
preferencesService.getOwnerPreferences(),
preferencesService.getTimestampPreferences());
// Create an UndoableInsertEntries object.
getUndoManager().addEdit(new UndoableInsertEntries(bibDatabaseContext.getDatabase(), entries));

Expand Down
47 changes: 3 additions & 44 deletions src/main/java/org/jabref/logic/util/UpdateField.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,13 @@ private UpdateField() {

/**
* Updating a field will result in the entry being reformatted on save
*
* @param be BibEntry
* @param field Field name
* @param newValue New field value
*/
public static Optional<FieldChange> updateField(BibEntry be, Field field, String newValue) {
return updateField(be, field, newValue, false);
}

/**
* Updating a non-displayable field does not result in the entry being reformatted on save
*
* @param be BibEntry
* @param field Field name
* @param newValue New field value
*/
public static Optional<FieldChange> updateNonDisplayableField(BibEntry be, Field field, String newValue) {
boolean changed = be.hasChanged();
Expand All @@ -43,9 +35,6 @@ public static Optional<FieldChange> updateNonDisplayableField(BibEntry be, Field
/**
* Undoable change of field value
*
* @param be BibEntry
* @param field Field name
* @param newValue New field value
* @param nullFieldIfValueIsTheSame If true the field value is removed when the current value is equals to newValue
*/
public static Optional<FieldChange> updateField(BibEntry be, Field field, String newValue,
Expand Down Expand Up @@ -80,24 +69,6 @@ public static Optional<FieldChange> updateField(BibEntry be, Field field, String
return Optional.of(new FieldChange(be, field, oldValue, writtenValue));
}

/**
* Sets empty or non-existing owner fields of a bibtex entry to a specified default value. Timestamp field is also
* set. Preferences are checked to see if these options are enabled.
*
* @param entry The entry to set fields for.
* @param overwriteOwner Indicates whether owner should be set if it is already set.
* @param overwriteTimestamp Indicates whether timestamp should be set if it is already set.
*/
public static void setAutomaticFields(BibEntry entry, boolean overwriteOwner, boolean overwriteTimestamp,
OwnerPreferences ownerPreferences, TimestampPreferences timestampPreferences) {
String defaultOwner = ownerPreferences.getDefaultOwner();
String timestamp = timestampPreferences.now();
boolean setOwner = ownerPreferences.isUseOwner() && (overwriteOwner || (!entry.hasField(StandardField.OWNER)));
boolean setTimeStamp = timestampPreferences.shouldAddCreationDate();

setAutomaticFields(entry, setOwner, defaultOwner, setTimeStamp, timestamp);
}

private static void setAutomaticFields(BibEntry entry, boolean setOwner, String owner, boolean setTimeStamp, String timeStamp) {
// Set owner field if this option is enabled:
if (setOwner) {
Expand All @@ -113,12 +84,8 @@ private static void setAutomaticFields(BibEntry entry, boolean setOwner, String
/**
* Sets empty or non-existing owner fields of bibtex entries inside a List to a specified default value. Timestamp
* field is also set. Preferences are checked to see if these options are enabled.
*
* @param bibs List of bibtex entries
*/
public static void setAutomaticFields(Collection<BibEntry> bibs, boolean overwriteOwner,
OwnerPreferences ownerPreferences, TimestampPreferences timestampPreferences) {

public static void setAutomaticFields(Collection<BibEntry> entries, OwnerPreferences ownerPreferences, TimestampPreferences timestampPreferences) {
boolean globalSetOwner = ownerPreferences.isUseOwner();
boolean setTimeStamp = timestampPreferences.shouldAddCreationDate();

Expand All @@ -129,19 +96,11 @@ public static void setAutomaticFields(Collection<BibEntry> bibs, boolean overwri

String defaultOwner = ownerPreferences.getDefaultOwner();
String timestamp = timestampPreferences.now();
boolean overwriteOwner = ownerPreferences.isOverwriteOwner();

// Iterate through all entries
for (BibEntry curEntry : bibs) {
for (BibEntry curEntry : entries) {
boolean setOwner = globalSetOwner && (overwriteOwner || (!curEntry.hasField(StandardField.OWNER)));
setAutomaticFields(curEntry, setOwner, defaultOwner, setTimeStamp, timestamp);
}
}

public static void setAutomaticFields(Collection<BibEntry> bibs,
OwnerPreferences ownerPreferences, TimestampPreferences timestampPreferences) {
UpdateField.setAutomaticFields(bibs,
ownerPreferences.isOverwriteOwner(),
ownerPreferences,
timestampPreferences);
}
}
19 changes: 10 additions & 9 deletions src/test/java/org/jabref/logic/util/UpdateFieldTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

import org.jabref.logic.preferences.OwnerPreferences;
Expand Down Expand Up @@ -179,7 +180,7 @@ public void emptyOwnerFieldNowPresentAfterAutomaticSet() {

OwnerPreferences ownerPreferences = createOwnerPreference(true, true);
TimestampPreferences timestampPreferences = createTimestampPreference();
UpdateField.setAutomaticFields(entry, false, false, ownerPreferences, timestampPreferences);
UpdateField.setAutomaticFields(List.of(entry), ownerPreferences, timestampPreferences);

assertEquals(Optional.of("testDefaultOwner"), entry.getField(StandardField.OWNER), "No owner exists");
}
Expand All @@ -188,7 +189,7 @@ public void emptyOwnerFieldNowPresentAfterAutomaticSet() {
public void ownerAssignedCorrectlyAfterAutomaticSet() {
OwnerPreferences ownerPreferences = createOwnerPreference(true, true);
TimestampPreferences timestampPreferences = createTimestampPreference();
UpdateField.setAutomaticFields(entry, false, false, ownerPreferences, timestampPreferences);
UpdateField.setAutomaticFields(List.of(entry), ownerPreferences, timestampPreferences);

assertEquals(Optional.of("testDefaultOwner"), entry.getField(StandardField.OWNER));
}
Expand All @@ -202,30 +203,30 @@ public void ownerIsNotResetAfterAutomaticSetIfOverwriteOwnerFalse() {

OwnerPreferences ownerPreferences = createOwnerPreference(true, false);
TimestampPreferences timestampPreferences = createTimestampPreference();
UpdateField.setAutomaticFields(entry, false, false, ownerPreferences, timestampPreferences);
UpdateField.setAutomaticFields(List.of(entry), ownerPreferences, timestampPreferences);

assertNotEquals(Optional.of("testDefaultOwner"), entry.getField(StandardField.OWNER), "Owner has changed");
assertEquals(Optional.of(alreadySetOwner), entry.getField(StandardField.OWNER), "Owner has not changed");
}

@Test
public void emptyCreationdateFieldNowPresentAfterAutomaticSet() {
public void emptyCreationDateFieldNowPresentAfterAutomaticSet() {
assertEquals(Optional.empty(), entry.getField(StandardField.CREATIONDATE), "CreationDate is present");

OwnerPreferences ownerPreferences = createOwnerPreference(true, true);
TimestampPreferences timestampPreferences = createTimestampPreference();
UpdateField.setAutomaticFields(entry, false, false, ownerPreferences, timestampPreferences);
UpdateField.setAutomaticFields(List.of(entry), ownerPreferences, timestampPreferences);

String creationDate = timestampPreferences.now();

assertEquals(Optional.of(creationDate), entry.getField(StandardField.CREATIONDATE), "No CreationDate exists");
}

@Test
public void creationdateAssignedCorrectlyAfterAutomaticSet() {
public void creationDateAssignedCorrectlyAfterAutomaticSet() {
OwnerPreferences ownerPreferences = createOwnerPreference(true, true);
TimestampPreferences timestampPreferences = createTimestampPreference();
UpdateField.setAutomaticFields(entry, false, false, ownerPreferences, timestampPreferences);
UpdateField.setAutomaticFields(List.of(entry), ownerPreferences, timestampPreferences);

String creationDate = timestampPreferences.now();

Expand All @@ -245,7 +246,7 @@ public void ownerSetToDefaultValueForCollectionOfBibEntries() {

OwnerPreferences ownerPreferences = createOwnerPreference(true, true);
TimestampPreferences timestampPreferences = createTimestampPreference();
UpdateField.setAutomaticFields(bibs, false, ownerPreferences, timestampPreferences);
UpdateField.setAutomaticFields(bibs, ownerPreferences, timestampPreferences);

String defaultOwner = "testDefaultOwner";

Expand All @@ -269,7 +270,7 @@ public void ownerNotChangedForCollectionOfBibEntriesIfOptionsDisabled() {

OwnerPreferences ownerPreferences = createOwnerPreference(true, false);
TimestampPreferences timestampPreferences = createTimestampPreference();
UpdateField.setAutomaticFields(bibs, false, ownerPreferences, timestampPreferences);
UpdateField.setAutomaticFields(bibs, ownerPreferences, timestampPreferences);

assertEquals(Optional.of(initialOwner), entry.getField(StandardField.OWNER), "entry has new value for owner field");
assertEquals(Optional.of(initialOwner), entry2.getField(StandardField.OWNER), "entry2 has new value for owner field");
Expand Down