Skip to content

Commit 09111be

Browse files
Fix clear year and month field when converting to biblatex (#6434)
* 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
1 parent 16d4938 commit 09111be

File tree

4 files changed

+64
-4
lines changed

4 files changed

+64
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
6161
- We fixed an issue where long directory names created from patterns could create an exception. [#3915](https://github.com/JabRef/jabref/issues/3915)
6262

6363
- We fixed an issue where sort on numeric cases was broken. [#6349](https://github.com/JabRef/jabref/issues/6349)
64+
- We fixed an issue where year and month fields were not cleared when converting to biblatex [#6224](https://github.com/JabRef/jabref/issues/6224)
6465
- We fixed an issue where an "Not on FX thread" exception occured when saving on linux [#6453](https://github.com/JabRef/jabref/issues/6453)
6566
- We fixed an issue where the library sort order was lost. [#6091](https://github.com/JabRef/jabref/issues/6091)
6667
- We fixed an issue where brackets in regular expressions were not working. [6469](https://github.com/JabRef/jabref/pull/6469)

src/main/java/org/jabref/logic/cleanup/ConvertToBiblatexCleanup.java

+14
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import java.util.ArrayList;
44
import java.util.List;
55
import java.util.Map;
6+
import java.util.Optional;
67

78
import org.jabref.model.FieldChange;
89
import org.jabref.model.cleanup.CleanupJob;
910
import org.jabref.model.entry.BibEntry;
11+
import org.jabref.model.entry.Date;
1012
import org.jabref.model.entry.EntryConverter;
1113
import org.jabref.model.entry.field.Field;
1214
import org.jabref.model.entry.field.StandardField;
@@ -39,6 +41,18 @@ public List<FieldChange> cleanup(BibEntry entry) {
3941
entry.clearField(StandardField.YEAR).ifPresent(changes::add);
4042
entry.clearField(StandardField.MONTH).ifPresent(changes::add);
4143
});
44+
} else {
45+
// If the year from date field is filled and equal to year it should be removed the year field
46+
entry.getFieldOrAlias(StandardField.DATE).ifPresent(date -> {
47+
Optional<Date> newDate = Date.parse(date);
48+
Optional<Date> checkDate = Date.parse(entry.getFieldOrAlias(StandardField.YEAR),
49+
entry.getFieldOrAlias(StandardField.MONTH), Optional.empty());
50+
51+
if (checkDate.equals(newDate)) {
52+
entry.clearField(StandardField.YEAR).ifPresent(changes::add);
53+
entry.clearField(StandardField.MONTH).ifPresent(changes::add);
54+
}
55+
});
4256
}
4357
return changes;
4458
}

src/main/java/org/jabref/model/entry/Date.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public Date(TemporalAccessor date) {
4040
* - "dd-MM-yyyy" (covers 15-1-2009)
4141
* - "d.M.uuuu" (covers 15.1.2015)
4242
* - "uuuu.M.d" (covers 2015.1.15)
43+
* - "MMM, uuuu" (covers Jan, 2020)
4344
* The code is essentially taken from http://stackoverflow.com/questions/4024544/how-to-parse-dates-in-multiple-formats-using-simpledateformat.
4445
*/
4546
public static Optional<Date> parse(String dateString) {
@@ -54,7 +55,8 @@ public static Optional<Date> parse(String dateString) {
5455
"MMMM d, uuuu",
5556
"MMMM, uuuu",
5657
"d.M.uuuu",
57-
"uuuu.M.d", "uuuu");
58+
"uuuu.M.d", "uuuu",
59+
"MMM, uuuu");
5860

5961
for (String formatString : formatStrings) {
6062
try {

src/test/java/org/jabref/logic/cleanup/ConvertToBiblatexCleanupTest.java

+46-3
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,59 @@ public void cleanupMovesYearMonthToDate() {
3333
}
3434

3535
@Test
36-
public void cleanupWithDateAlreadyPresentDoesNothing() {
36+
public void cleanupWithDateAlreadyPresentAndDifferentFromYearDoesNothing() {
3737
BibEntry entry = new BibEntry();
3838
entry.setField(StandardField.YEAR, "2011");
3939
entry.setField(StandardField.MONTH, "#jan#");
40-
entry.setField(StandardField.DATE, "2012");
40+
entry.setField(StandardField.DATE, "2012-01");
4141

4242
worker.cleanup(entry);
4343

4444
assertEquals(Optional.of("2011"), entry.getField(StandardField.YEAR));
4545
assertEquals(Optional.of("#jan#"), entry.getField(StandardField.MONTH));
46-
assertEquals(Optional.of("2012"), entry.getField(StandardField.DATE));
46+
assertEquals(Optional.of("2012-01"), entry.getField(StandardField.DATE));
47+
}
48+
49+
@Test
50+
public void cleanupWithDateAlreadyPresentAndDifferentFromMonthDoesNothing() {
51+
BibEntry entry = new BibEntry();
52+
entry.setField(StandardField.YEAR, "2011");
53+
entry.setField(StandardField.MONTH, "#jan#");
54+
entry.setField(StandardField.DATE, "2011-02");
55+
56+
worker.cleanup(entry);
57+
58+
assertEquals(Optional.of("2011"), entry.getField(StandardField.YEAR));
59+
assertEquals(Optional.of("#jan#"), entry.getField(StandardField.MONTH));
60+
assertEquals(Optional.of("2011-02"), entry.getField(StandardField.DATE));
61+
}
62+
63+
@Test
64+
public void cleanupWithEmptyDateDoesNothing() {
65+
BibEntry entry = new BibEntry();
66+
entry.setField(StandardField.YEAR, "");
67+
entry.setField(StandardField.MONTH, "");
68+
entry.setField(StandardField.DATE, "");
69+
70+
worker.cleanup(entry);
71+
72+
assertEquals(Optional.empty(), entry.getField(StandardField.YEAR));
73+
assertEquals(Optional.empty(), entry.getField(StandardField.MONTH));
74+
assertEquals(Optional.empty(), entry.getField(StandardField.DATE));
75+
}
76+
77+
@Test
78+
public void cleanupWithDateAlreadyPresentAndEqualsToYearAndMonth() {
79+
BibEntry entry = new BibEntry();
80+
entry.setField(StandardField.YEAR, "2011");
81+
entry.setField(StandardField.MONTH, "#jan#");
82+
entry.setField(StandardField.DATE, "2011-01");
83+
84+
worker.cleanup(entry);
85+
86+
assertEquals(Optional.empty(), entry.getField(StandardField.YEAR));
87+
assertEquals(Optional.empty(), entry.getField(StandardField.MONTH));
88+
assertEquals(Optional.of("2011-01"), entry.getField(StandardField.DATE));
4789
}
4890

4991
@Test
@@ -55,4 +97,5 @@ public void cleanupMovesJournalToJournaltitle() {
5597
assertEquals(Optional.empty(), entry.getField(StandardField.JOURNAL));
5698
assertEquals(Optional.of("Best of JabRef"), entry.getField(StandardField.JOURNALTITLE));
5799
}
100+
58101
}

0 commit comments

Comments
 (0)