Skip to content

Commit e6ceac8

Browse files
authored
Follow up fixes for Date Groups (#14241)
* Follow up fixes for Date Groups Fix exception for right clck menu include year simply date extracting * var name
1 parent 02f80e2 commit e6ceac8

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

jabgui/src/main/java/org/jabref/gui/groups/GroupNodeViewModel.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.jabref.model.groups.AutomaticGroup;
4141
import org.jabref.model.groups.AutomaticKeywordGroup;
4242
import org.jabref.model.groups.AutomaticPersonsGroup;
43+
import org.jabref.model.groups.DateGroup;
4344
import org.jabref.model.groups.ExplicitGroup;
4445
import org.jabref.model.groups.GroupEntryChanger;
4546
import org.jabref.model.groups.GroupTreeNode;
@@ -464,6 +465,8 @@ public boolean canAddEntriesIn() {
464465
return false;
465466
} else if (group instanceof AutomaticDateGroup) {
466467
return false;
468+
} else if (group instanceof DateGroup) {
469+
return false;
467470
} else {
468471
throw new UnsupportedOperationException("canAddEntriesIn method not yet implemented in group: " + group.getClass().getName());
469472
}
@@ -508,6 +511,7 @@ public boolean canAddGroupsIn() {
508511
case AutomaticKeywordGroup _,
509512
AutomaticPersonsGroup _,
510513
AutomaticDateGroup _,
514+
DateGroup _,
511515
SmartGroup _ ->
512516
false;
513517
case KeywordGroup _ ->
@@ -534,6 +538,7 @@ public boolean canRemove() {
534538
AutomaticKeywordGroup _,
535539
AutomaticPersonsGroup _,
536540
AutomaticDateGroup _,
541+
DateGroup _,
537542
TexGroup _ ->
538543
true;
539544
case KeywordGroup _ ->
@@ -553,6 +558,7 @@ public boolean isEditable() {
553558
AbstractGroup group = groupNode.getGroup();
554559
return switch (group) {
555560
case AllEntriesGroup _,
561+
DateGroup _,
556562
SmartGroup _ ->
557563
false;
558564
case ExplicitGroup _,

jablib/src/main/java/org/jabref/model/entry/field/FieldFactory.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,12 @@ public static Set<Field> getPersonNameFields() {
193193
return getFieldsFiltered(field -> field.getProperties().contains(FieldProperty.PERSON_NAMES));
194194
}
195195

196+
/// Gets all fields with [FieldProperty#DATE].
197+
/// Also includes [StandardField#YEAR].
198+
///
199+
/// @return Set of fields
196200
public static Set<Field> getDateFields() {
197-
return getFieldsFiltered(field -> field.getProperties().contains(FieldProperty.DATE));
201+
return getFieldsFiltered(field -> field.getProperties().contains(FieldProperty.DATE) || field == StandardField.YEAR);
198202
}
199203

200204
private static Set<Field> getFieldsFiltered(Predicate<Field> selector) {

jablib/src/main/java/org/jabref/model/groups/AutomaticDateGroup.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,20 @@ public DateGranularity getGranularity() {
4949
public Set<GroupTreeNode> createSubgroups(BibEntry entry) {
5050
var out = new LinkedHashSet<GroupTreeNode>();
5151

52-
DateGroup.extractDate(field, entry).ifPresent(d -> {
52+
DateGroup.extractDate(field, entry).ifPresent(date -> {
5353
switch (granularity) {
5454
case YEAR -> {
55-
DateGroup.extractYear(field, entry).ifPresent(y -> {
56-
String key = "%04d".formatted(y);
55+
date.getYear().ifPresent(year -> {
56+
String key = "%04d".formatted(year);
5757
out.add(new GroupTreeNode(new DateGroup(key, GroupHierarchyType.INDEPENDENT, field, key)));
5858
});
5959
}
6060
case MONTH ->
61-
DateGroup.getDateKey(d, "YYYY-MM").ifPresent(key -> {
61+
DateGroup.getDateKey(date, "YYYY-MM").ifPresent(key -> {
6262
out.add(new GroupTreeNode(new DateGroup(key, GroupHierarchyType.INDEPENDENT, field, key)));
6363
});
6464
case FULL_DATE ->
65-
DateGroup.getDateKey(d, "YYYY-MM-DD").ifPresent(key -> {
65+
DateGroup.getDateKey(date, "YYYY-MM-DD").ifPresent(key -> {
6666
out.add(new GroupTreeNode(new DateGroup(key, GroupHierarchyType.INDEPENDENT, field, key)));
6767
});
6868
}

jablib/src/main/java/org/jabref/model/groups/DateGroup.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@ public DateGroup(String groupName, GroupHierarchyType context, Field searchField
2222
this.date = date;
2323
}
2424

25-
static Optional<Integer> extractYear(Field field, BibEntry bibEntry) {
26-
return bibEntry.getField(field)
27-
.flatMap(Date::parse)
28-
.flatMap(Date::getYear);
29-
}
30-
3125
static Optional<Date> extractDate(Field field, BibEntry entry) {
3226
boolean isCore =
3327
(field == StandardField.DATE)
@@ -58,7 +52,7 @@ static Optional<String> getDateKey(Date d, String dateKeyFormat) {
5852
Optional<Integer> y = d.getYear();
5953
return switch (numOfdashes) {
6054
case 0 ->
61-
y.map(val -> "%04d".formatted(val)); // "YYYY"
55+
y.map("%04d"::formatted); // "YYYY"
6256
case 1 -> { // "YYYY-MM"
6357
if (d.getYear().isPresent() && d.getMonth().isPresent()) {
6458
String out = "%04d-%02d".formatted(d.getYear().get(), d.getMonth().get().getNumber());

0 commit comments

Comments
 (0)