Skip to content

Commit

Permalink
Fix clear year and month field when converting to biblatex (#6434)
Browse files Browse the repository at this point in the history
* ISSUE :: 6224 - allow merging of year and date if identical

* fix comment

* Updated Changelog.md file with fixed issue

* Fix changelog.md file

* Check If date field is filled and is equal to year it should be removed the year and month fields

* Added unit test: Cleanup With Date Already Present And Equals To Year Removes Year And Month

* ISSUE :: 6224 - Removed Unused import - java.util.Optional

* ISSUE :: 6224 - Separated org.jabref.model.FieldChange from previous imports

* remove checkstyle warning

* Change variable newYear name

* Update Changelog.md

* ISSUE :: 6224 - Added month validation and tests

* ISSUE :: 6224 - For inconsistent data: Do not overwrite/move

* ISSUE :: 6224 - Fix Test

* ISSUE :: 6224 - Added month checking Test

* ISSUE :: 6224 - Fix Test

* ISSUE :: 6224 - Change Date.parse

* ISSUE :: 6224 - Update Date class

* ISSUE :: 6224 - Fix Date

* ISSUE :: 6224 - Added new test
  • Loading branch information
catarinagomespt committed May 14, 2020
1 parent 16d4938 commit 09111be
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We fixed an issue where long directory names created from patterns could create an exception. [#3915](https://github.com/JabRef/jabref/issues/3915)

- We fixed an issue where sort on numeric cases was broken. [#6349](https://github.com/JabRef/jabref/issues/6349)
- We fixed an issue where year and month fields were not cleared when converting to biblatex [#6224](https://github.com/JabRef/jabref/issues/6224)
- We fixed an issue where an "Not on FX thread" exception occured when saving on linux [#6453](https://github.com/JabRef/jabref/issues/6453)
- We fixed an issue where the library sort order was lost. [#6091](https://github.com/JabRef/jabref/issues/6091)
- We fixed an issue where brackets in regular expressions were not working. [6469](https://github.com/JabRef/jabref/pull/6469)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.jabref.model.FieldChange;
import org.jabref.model.cleanup.CleanupJob;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.Date;
import org.jabref.model.entry.EntryConverter;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.StandardField;
Expand Down Expand Up @@ -39,6 +41,18 @@ public List<FieldChange> cleanup(BibEntry entry) {
entry.clearField(StandardField.YEAR).ifPresent(changes::add);
entry.clearField(StandardField.MONTH).ifPresent(changes::add);
});
} else {
// If the year from date field is filled and equal to year it should be removed the year field
entry.getFieldOrAlias(StandardField.DATE).ifPresent(date -> {
Optional<Date> newDate = Date.parse(date);
Optional<Date> checkDate = Date.parse(entry.getFieldOrAlias(StandardField.YEAR),
entry.getFieldOrAlias(StandardField.MONTH), Optional.empty());

if (checkDate.equals(newDate)) {
entry.clearField(StandardField.YEAR).ifPresent(changes::add);
entry.clearField(StandardField.MONTH).ifPresent(changes::add);
}
});
}
return changes;
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/jabref/model/entry/Date.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public Date(TemporalAccessor date) {
* - "dd-MM-yyyy" (covers 15-1-2009)
* - "d.M.uuuu" (covers 15.1.2015)
* - "uuuu.M.d" (covers 2015.1.15)
* - "MMM, uuuu" (covers Jan, 2020)
* The code is essentially taken from http://stackoverflow.com/questions/4024544/how-to-parse-dates-in-multiple-formats-using-simpledateformat.
*/
public static Optional<Date> parse(String dateString) {
Expand All @@ -54,7 +55,8 @@ public static Optional<Date> parse(String dateString) {
"MMMM d, uuuu",
"MMMM, uuuu",
"d.M.uuuu",
"uuuu.M.d", "uuuu");
"uuuu.M.d", "uuuu",
"MMM, uuuu");

for (String formatString : formatStrings) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,59 @@ public void cleanupMovesYearMonthToDate() {
}

@Test
public void cleanupWithDateAlreadyPresentDoesNothing() {
public void cleanupWithDateAlreadyPresentAndDifferentFromYearDoesNothing() {
BibEntry entry = new BibEntry();
entry.setField(StandardField.YEAR, "2011");
entry.setField(StandardField.MONTH, "#jan#");
entry.setField(StandardField.DATE, "2012");
entry.setField(StandardField.DATE, "2012-01");

worker.cleanup(entry);

assertEquals(Optional.of("2011"), entry.getField(StandardField.YEAR));
assertEquals(Optional.of("#jan#"), entry.getField(StandardField.MONTH));
assertEquals(Optional.of("2012"), entry.getField(StandardField.DATE));
assertEquals(Optional.of("2012-01"), entry.getField(StandardField.DATE));
}

@Test
public void cleanupWithDateAlreadyPresentAndDifferentFromMonthDoesNothing() {
BibEntry entry = new BibEntry();
entry.setField(StandardField.YEAR, "2011");
entry.setField(StandardField.MONTH, "#jan#");
entry.setField(StandardField.DATE, "2011-02");

worker.cleanup(entry);

assertEquals(Optional.of("2011"), entry.getField(StandardField.YEAR));
assertEquals(Optional.of("#jan#"), entry.getField(StandardField.MONTH));
assertEquals(Optional.of("2011-02"), entry.getField(StandardField.DATE));
}

@Test
public void cleanupWithEmptyDateDoesNothing() {
BibEntry entry = new BibEntry();
entry.setField(StandardField.YEAR, "");
entry.setField(StandardField.MONTH, "");
entry.setField(StandardField.DATE, "");

worker.cleanup(entry);

assertEquals(Optional.empty(), entry.getField(StandardField.YEAR));
assertEquals(Optional.empty(), entry.getField(StandardField.MONTH));
assertEquals(Optional.empty(), entry.getField(StandardField.DATE));
}

@Test
public void cleanupWithDateAlreadyPresentAndEqualsToYearAndMonth() {
BibEntry entry = new BibEntry();
entry.setField(StandardField.YEAR, "2011");
entry.setField(StandardField.MONTH, "#jan#");
entry.setField(StandardField.DATE, "2011-01");

worker.cleanup(entry);

assertEquals(Optional.empty(), entry.getField(StandardField.YEAR));
assertEquals(Optional.empty(), entry.getField(StandardField.MONTH));
assertEquals(Optional.of("2011-01"), entry.getField(StandardField.DATE));
}

@Test
Expand All @@ -55,4 +97,5 @@ public void cleanupMovesJournalToJournaltitle() {
assertEquals(Optional.empty(), entry.getField(StandardField.JOURNAL));
assertEquals(Optional.of("Best of JabRef"), entry.getField(StandardField.JOURNALTITLE));
}

}

0 comments on commit 09111be

Please sign in to comment.