Skip to content

Commit

Permalink
view: kick out a row if is no longer matching the filters
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed Mar 26, 2019
1 parent ddb5603 commit a4fb07b
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import javax.annotation.Nullable;

import org.adempiere.ad.element.api.AdTabId;
import org.compiere.Adempiere;
import org.elasticsearch.client.Client;

Expand Down Expand Up @@ -74,7 +75,10 @@ private DocumentFilterDescriptorsProviderFactory()
super();
}

public DocumentFilterDescriptorsProvider createFiltersProvider(final int adTabId, @Nullable final String tableName, final Collection<DocumentFieldDescriptor> fields)
public DocumentFilterDescriptorsProvider createFiltersProvider(
final AdTabId adTabId,
@Nullable final String tableName,
final Collection<DocumentFieldDescriptor> fields)
{
return CompositeDocumentFilterDescriptorsProvider.compose(
createUserQueryDocumentFilterDescriptorsProvider(adTabId, tableName, fields),
Expand Down Expand Up @@ -170,11 +174,11 @@ private DocumentFieldWidgetType extractFilterWidgetType(final DocumentFieldDescr
}

private static DocumentFilterDescriptorsProvider createUserQueryDocumentFilterDescriptorsProvider(
final int adTabId,
final AdTabId adTabId,
@Nullable final String tableName,
final Collection<DocumentFieldDescriptor> fields)
{
if (tableName != null && adTabId > 0)
if (tableName != null && adTabId != null)
{
return new UserQueryDocumentFilterDescriptorsProvider(adTabId, tableName, fields);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Optional;
import java.util.function.Supplier;

import org.adempiere.ad.element.api.AdTabId;
import org.adempiere.ad.table.api.IADTableDAO;
import org.compiere.apps.search.IUserQuery;
import org.compiere.apps.search.IUserQueryField;
Expand Down Expand Up @@ -61,10 +62,12 @@ final class UserQueryDocumentFilterDescriptorsProvider implements DocumentFilter
private final UserQueryRepository repository;
private final Supplier<Map<String, DocumentFilterDescriptor>> filtersSupplier = CachedSuppliers.renewOnCacheReset(this::retrieveAllByFilterId);

public UserQueryDocumentFilterDescriptorsProvider(final int adTabId, final String tableName, final Collection<DocumentFieldDescriptor> fields)
public UserQueryDocumentFilterDescriptorsProvider(
@NonNull final AdTabId adTabId,
final String tableName,
final Collection<DocumentFieldDescriptor> fields)
{
Check.assumeNotEmpty(tableName, "tableName is not empty");
Check.assume(adTabId > 0, "adTabId > 0");

final int adTableId = Services.get(IADTableDAO.class).retrieveTableId(tableName);

Expand All @@ -74,7 +77,7 @@ public UserQueryDocumentFilterDescriptorsProvider(final int adTabId, final Strin
.collect(GuavaCollectors.toImmutableList());

repository = UserQueryRepository.builder()
.setAD_Tab_ID(adTabId)
.setAD_Tab_ID(adTabId.getRepoId())
.setAD_Table_ID(adTableId)
.setAD_User_ID(100) // FIXME: hardcoded, see https://github.com/metasfresh/metasfresh-webui/issues/162
.setSearchFields(searchFields)
Expand Down
28 changes: 26 additions & 2 deletions src/main/java/de/metas/ui/web/view/DefaultView.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ public static final Builder builder(final IViewDataRepository viewDataRepository

private final IViewInvalidationAdvisor viewInvalidationAdvisor;

//
// View refreshing on change events
private final boolean refreshViewOnChangeEvents;
private final ChangedRowIdsCollector changedRowIdsToCheck = new ChangedRowIdsCollector();

private DefaultView(final Builder builder)
Expand All @@ -146,6 +149,7 @@ private DefaultView(final Builder builder)
viewFilterDescriptors = builder.getViewFilterDescriptors();
stickyFilters = builder.getStickyFilters();
filters = builder.getFilters();
refreshViewOnChangeEvents = builder.isRefreshViewOnChangeEvents();

//
// Selection
Expand Down Expand Up @@ -552,8 +556,11 @@ public void notifyRecordsChanged(final TableRecordReferenceSet recordRefs)
}

//
// TODO: Schedule rows to be checked and added or removed from current view
changedRowIdsToCheck.addChangedRows(rowIds);
// Schedule rows to be checked and added or removed from current view
if (refreshViewOnChangeEvents)
{
changedRowIdsToCheck.addChangedRows(rowIds);
}

// Invalidate local rowsById cache
cache_rowsById.removeAll(rowIds);
Expand All @@ -565,6 +572,11 @@ public void notifyRecordsChanged(final TableRecordReferenceSet recordRefs)

private void checkChangedRows()
{
if (!refreshViewOnChangeEvents)
{
return;
}

changedRowIdsToCheck.process(rowIds -> selectionsRef
.get()
.computeDefaultSelection(defaultSelection -> viewDataRepository.removeRowIdsNotMatchingFilters(defaultSelection, getAllFilters(), rowIds)));
Expand Down Expand Up @@ -748,6 +760,7 @@ public static final class Builder

private LinkedHashMap<String, DocumentFilter> _stickyFiltersById;
private LinkedHashMap<String, DocumentFilter> _filtersById = new LinkedHashMap<>();
private boolean refreshViewOnChangeEvents = false;

private IViewInvalidationAdvisor viewInvalidationAdvisor = DefaultViewInvalidationAdvisor.instance;

Expand Down Expand Up @@ -902,6 +915,17 @@ public Builder addFiltersIfAbsent(final Collection<DocumentFilter> filters)
return this;
}

public Builder refreshViewOnChangeEvents(boolean refreshViewOnChangeEvents)
{
this.refreshViewOnChangeEvents = refreshViewOnChangeEvents;
return this;
}

public boolean isRefreshViewOnChangeEvents()
{
return refreshViewOnChangeEvents;
}

public Builder viewInvalidationAdvisor(@NonNull final IViewInvalidationAdvisor viewInvalidationAdvisor)
{
this.viewInvalidationAdvisor = viewInvalidationAdvisor;
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/de/metas/ui/web/view/SqlViewFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ private SqlViewBinding createViewBinding(@NonNull final SqlViewBindingKey key)

final SqlViewBinding.Builder builder = createBuilderForEntityBindingAndFieldNames(entityBinding, displayFieldNames)
.filterDescriptors(filterDescriptors)
.refreshViewOnChangeEvents(entityDescriptor.isRefreshViewOnChangeEvents())
.viewInvalidationAdvisor(getViewInvalidationAdvisor(windowId));

if (windowId2SqlDocumentFilterConverterDecorator.containsKey(windowId))
Expand All @@ -429,7 +430,7 @@ private SqlViewBinding.Builder createBuilderForEntityBindingAndFieldNames(
@NonNull final SqlDocumentEntityDataBindingDescriptor entityBinding,
@NonNull final Set<String> displayFieldNames)
{
final SqlViewBinding.Builder builder = createBuilderForEntityBinding(entityBinding);
final SqlViewBinding.Builder builder = prepareSqlViewBinding(entityBinding);

entityBinding.getFields()
.stream()
Expand All @@ -439,14 +440,13 @@ private SqlViewBinding.Builder createBuilderForEntityBindingAndFieldNames(
return builder;
}

private SqlViewBinding.Builder createBuilderForEntityBinding(@NonNull final SqlDocumentEntityDataBindingDescriptor entityBinding)
private static SqlViewBinding.Builder prepareSqlViewBinding(@NonNull final SqlDocumentEntityDataBindingDescriptor entityBinding)
{
final SqlViewBinding.Builder builder = SqlViewBinding.builder()
return SqlViewBinding.builder()
.tableName(entityBinding.getTableName())
.tableAlias(entityBinding.getTableAlias())
.sqlWhereClause(entityBinding.getSqlWhereClause())
.defaultOrderBys(entityBinding.getDefaultOrderBys());
return builder;
}

private static final SqlViewRowFieldBinding createViewFieldBinding(final SqlDocumentFieldDataBindingDescriptor documentField, final Collection<String> availableDisplayColumnNames)
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/de/metas/ui/web/view/descriptor/SqlViewBinding.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import de.metas.ui.web.window.model.DocumentQueryOrderBy;
import de.metas.util.Check;
import de.metas.util.GuavaCollectors;
import lombok.Getter;
import lombok.NonNull;
import lombok.Singular;

Expand Down Expand Up @@ -82,6 +83,8 @@ public class SqlViewBinding implements SqlEntityBinding

private final DocumentFilterDescriptorsProvider filterDescriptors;
private final SqlDocumentFilterConvertersList filterConverters;
@Getter
private final boolean refreshViewOnChangeEvents;

private final SqlViewRowIdsConverter rowIdsConverter;

Expand Down Expand Up @@ -146,6 +149,8 @@ private SqlViewBinding(final Builder builder)
filterConverters = builder.buildViewFilterConverters();

filterConverterDecorator = builder.sqlDocumentFilterConverterDecorator;

refreshViewOnChangeEvents = builder.refreshViewOnChangeEvents;

rowIdsConverter = builder.getRowIdsConverter();

Expand Down Expand Up @@ -364,6 +369,7 @@ public static final class Builder
private OrderByFieldNameAliasMap.OrderByFieldNameAliasMapBuilder orderByFieldNameAliasMap = OrderByFieldNameAliasMap.builder();
private DocumentFilterDescriptorsProvider filterDescriptors = NullDocumentFilterDescriptorsProvider.instance;
private SqlDocumentFilterConvertersList.Builder filterConverters = null;
private boolean refreshViewOnChangeEvents;

private SqlViewRowIdsConverter rowIdsConverter = null;

Expand Down Expand Up @@ -608,5 +614,11 @@ private IViewInvalidationAdvisor getViewInvalidationAdvisor()
{
return viewInvalidationAdvisor;
}

public Builder refreshViewOnChangeEvents(boolean refreshViewOnChangeEvents)
{
this.refreshViewOnChangeEvents = refreshViewOnChangeEvents;
return this;
}
}
}
Loading

1 comment on commit a4fb07b

@teosarca
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

task #1178

Please sign in to comment.