Skip to content

Commit

Permalink
CAUSEWAY-2297: convert table models to java records (part 2)
Browse files Browse the repository at this point in the history
  • Loading branch information
andi-huber committed Nov 21, 2024
1 parent 74bd180 commit 4e32cf5
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@
import org.apache.causeway.core.metamodel.tabular.internal.DataTableInternal;
import org.apache.causeway.core.metamodel.tabular.simple.DataTable;

import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;

public interface DataTableInteractive extends MultiselectChoices {

// -- FACTORIES
Expand All @@ -66,12 +63,10 @@ public static DataTableInteractive forAction(
/**
* Sorting helper class, that has the column index to be sorted by and the sort direction.
*/
@RequiredArgsConstructor
@EqualsAndHashCode
public static class ColumnSort implements Serializable {
private static final long serialVersionUID = 1L;
final int columnIndex;
final MmSortUtils.SortDirection sortDirection;
public record ColumnSort(
int columnIndex,
MmSortUtils.SortDirection sortDirection) implements Serializable {

public Optional<Comparator<ManagedObject>> asComparator(final Can<? extends DataColumn> columns) {
var columnToSort = columns.get(columnIndex).orElse(null);
var sortProperty = columnToSort.associationMetaModel().getSpecialization().leftIfAny();
Expand All @@ -82,8 +77,8 @@ public Optional<Comparator<ManagedObject>> asComparator(final Can<? extends Data

// -- TITLE, ROWS AND COLUMNS

Observable<String> getTitle();
Observable<Can<DataColumn>> getDataColumns();
Observable<String> titleObservable();
Observable<Can<DataColumn>> dataColumnsObservable();
Observable<Can<ManagedObject>> getDataElements();
Observable<Can<DataRow>> getDataRowsFilteredAndSorted();

Expand Down Expand Up @@ -116,7 +111,7 @@ public Optional<Comparator<ManagedObject>> asComparator(final Can<? extends Data

// -- SORTING

Bindable<ColumnSort> getColumnSort();
Bindable<ColumnSort> columnSortBinable();

// -- SELECTION

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ record DataRowInternal(

@Override
public Optional<DataColumn> lookupColumnById(final @NonNull String columnId) {
return parentTable.getDataColumns().getValue().stream()
return parentTable.dataColumnsObservable().getValue().stream()
.filter(dataColumn->dataColumn.columnId().equals(columnId))
.findFirst();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NonNull;
import lombok.experimental.Accessors;

public class DataTableInternal
implements DataTableInteractive {
Expand Down Expand Up @@ -109,10 +110,15 @@ public static DataTableInternal forAction(
@Getter private final @NonNull LazyObservable<Can<DataRow>> dataRows;
@Getter private final @NonNull LazyObservable<Can<DataRow>> dataRowsFilteredAndSorted;
@Getter private final @NonNull LazyObservable<Can<DataRow>> dataRowsSelected;
@Getter private final _BindableAbstract<ColumnSort> columnSort;

@Getter private final @NonNull LazyObservable<Can<DataColumn>> dataColumns;
@Getter private final @NonNull LazyObservable<String> title;
@Accessors(fluent=true)
@Getter private final _BindableAbstract<ColumnSort> columnSortBinable;

@Accessors(fluent=true)
@Getter private final @NonNull LazyObservable<Can<DataColumn>> dataColumnsObservable;

@Accessors(fluent=true)
@Getter private final @NonNull LazyObservable<String> titleObservable;

private final Optional<FilterHandler> filterHandler;

Expand All @@ -137,7 +143,7 @@ private DataTableInternal(
this.filterHandler = _FilterUtils.createFilterHandler(elementType);

this.searchArgument = _Bindables.forValue("");
this.columnSort = _Bindables.forValue(null);
this.columnSortBinable = _Bindables.forValue(null);

this.dataElements = _Observables.lazy(()->elements
//.map(mmc::injectServicesInto) // I believe is redundant, has major performance impact
Expand Down Expand Up @@ -166,19 +172,19 @@ private DataTableInternal(
dataRowsFilteredAndSorted.invalidate();
});

this.columnSort.addListener((e,o,n)->{
this.columnSortBinable.addListener((e,o,n)->{
dataRowsFilteredAndSorted.invalidate();
});

this.dataColumns = _Observables.lazy(()->
this.dataColumnsObservable = _Observables.lazy(()->
managedMember.getElementType()
.streamAssociationsForColumnRendering(managedMember.getIdentifier(), managedMember.getOwner())
.map(assoc->new DataColumnInternal(this, assoc))
.collect(Can.toCan()));

//XXX future extension: the title could dynamically reflect the number of elements selected
//eg... 5 Orders selected
this.title = _Observables.lazy(()->
this.titleObservable = _Observables.lazy(()->
managedMember
.getFriendlyName());
}
Expand Down Expand Up @@ -253,8 +259,8 @@ private CollectionFilterService.Tokens tokens(final ManagedObject element){
// -- SORTING

private Optional<Comparator<DataRow>> sortingComparator() {
return Optional.ofNullable(columnSort.getValue())
.flatMap(sort->sort.asComparator(dataColumns.getValue()))
return Optional.ofNullable(columnSortBinable.getValue())
.flatMap(sort->sort.asComparator(dataColumnsObservable.getValue()))
.or(()->managedMember.getMetaModel().getElementComparator())
.map(elementComparator->(rowA, rowB)->elementComparator.compare(rowA.rowElement(), rowB.rowElement()));
}
Expand Down Expand Up @@ -368,8 +374,8 @@ public ActionInteraction startAssociatedActionInteraction(final String actionId,
public DataTable export() {
return new DataTable(
getElementType(),
getTitle().getValue(),
getDataColumns().getValue()
titleObservable().getValue(),
dataColumnsObservable().getValue()
.map(DataColumn::associationMetaModel),
getDataRowsFilteredAndSorted().getValue()
.stream()
Expand All @@ -381,8 +387,8 @@ public DataTable export() {
private DataTable exportAll() {
return new DataTable(
getElementType(),
getTitle().getValue(),
getDataColumns().getValue()
titleObservable().getValue(),
dataColumnsObservable().getValue()
.map(DataColumn::associationMetaModel),
getDataElements().getValue());
}
Expand Down Expand Up @@ -416,7 +422,7 @@ static Memento create(
tableInteractive.exportAll(),
tableInteractive.searchArgument.getValue(),
tableInteractive.getSelectedRowIndexes(),
tableInteractive.getColumnSort().getValue());
tableInteractive.columnSortBinable().getValue());
}

private final @NonNull Identifier featureId;
Expand Down Expand Up @@ -454,7 +460,7 @@ public DataTableInternal getDataTableModel(final ManagedObject owner) {
.collect(Can.toCan()));

if(columnSort!=null) {
dataTableInteractive.columnSort.setValue(columnSort);
dataTableInteractive.columnSortBinable.setValue(columnSort);
}
dataTableInteractive.searchArgument.setValue(searchArgument);
dataTableInteractive.doProgrammaticToggle(()->{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,15 @@
* @since 2.0 {@index}
*/
public record DataColumn(
@NonNull DataTable parentTable,
@NonNull ObjectAssociation metamodel,
@NonNull String columnId,
@NonNull String columnFriendlyName,
@NonNull Optional<String> columnDescription,
boolean isMultivalued
) {

public DataColumn(final DataTable parentTable, final ObjectAssociation metamodel) {
this(parentTable, metamodel, metamodel.getId(),
public DataColumn(final ObjectAssociation metamodel) {
this(metamodel, metamodel.getId(),
metamodel.getCanonicalFriendlyName(),
metamodel.getCanonicalDescription(),
metamodel.isCollection());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
* @since 2.0 {@index}
*/
public record DataRow(
@NonNull DataTable parentTable,
@NonNull ManagedObject rowElement) {

/**
Expand Down
Loading

0 comments on commit 4e32cf5

Please sign in to comment.