Skip to content

Commit

Permalink
If no HU matches, then do *not* show every HU
Browse files Browse the repository at this point in the history
Also:
* CreateViewRequest: javadoc
* WEBUI_Picking_OpenHUsToPick: minor formatting

557: Picking Window: Open HU Selection window filtering 

Task-Url: #557
  • Loading branch information
metas-ts committed Aug 31, 2017
1 parent 16738e5 commit 2e01d97
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.util.Objects;
import java.util.Set;

import javax.annotation.Nullable;

import org.adempiere.ad.dao.IQueryFilter;
import org.adempiere.ad.dao.ISqlQueryFilter;
import org.adempiere.model.PlainContextAware;
Expand Down Expand Up @@ -44,6 +46,7 @@
import de.metas.ui.web.window.descriptor.factory.DocumentDescriptorFactory;
import de.metas.ui.web.window.descriptor.factory.standard.LayoutFactory;
import de.metas.ui.web.window.descriptor.sql.SqlDocumentEntityDataBindingDescriptor;
import lombok.NonNull;

/*
* #%L
Expand Down Expand Up @@ -106,8 +109,7 @@ private SqlViewBinding createSqlViewBinding()
.setDisplayFieldNames(displayFieldNames)
.setSqlWhereClause(I_M_HU.COLUMNNAME_M_HU_Item_Parent_ID + " is null" // top level
+ " AND " + I_M_HU.COLUMNNAME_IsActive + "=" + DB.TO_BOOLEAN(Boolean.TRUE)) // active
.setRowIdsConverter(HUSqlViewRowIdsConverter.instance)
;
.setRowIdsConverter(HUSqlViewRowIdsConverter.instance);

//
// View Fields
Expand All @@ -133,7 +135,7 @@ private SqlViewBinding createSqlViewBinding()
.addViewFilterConverter(HUBarcodeSqlDocumentFilterConverter.FILTER_ID, HUBarcodeSqlDocumentFilterConverter.instance)
.addViewFilterConverter(HUIdsFilterHelper.FILTER_ID, HUIdsFilterHelper.SQL_DOCUMENT_FILTER_CONVERTER);
}

//
return sqlViewBinding.build();
}
Expand Down Expand Up @@ -223,7 +225,9 @@ public HUEditorView createView(final CreateViewRequest request)
referencingTableName = null;
}

@SuppressWarnings("deprecation") // as long as the deprecated getFilterOnlyIds() is around we can't ignore it
final List<DocumentFilter> stickyFilters = extractStickyFilters(request.getStickyFilters(), request.getFilterOnlyIds());

final List<DocumentFilter> filters = request.getOrUnwrapFilters(getSqlViewBinding().getViewFilterDescriptors());

final HUEditorView.Builder huViewBuilder = HUEditorView.builder(getSqlViewBinding())
Expand All @@ -248,13 +252,22 @@ public HUEditorView createView(final CreateViewRequest request)
return huViewBuilder.build();
}

private static List<DocumentFilter> extractStickyFilters(final List<DocumentFilter> requestStickyFilters, final Set<Integer> huIds)
/**
*
* @param requestStickyFilters
* @param huIds {@code null} means "no restriction". Empty means "select none"
* @return
*/
private static List<DocumentFilter> extractStickyFilters(
@NonNull final List<DocumentFilter> requestStickyFilters,
@Nullable final Set<Integer> huIds)
{
List<DocumentFilter> stickyFilters = new ArrayList<>(requestStickyFilters);
final List<DocumentFilter> stickyFilters = new ArrayList<>(requestStickyFilters);

final DocumentFilter stickyFilter_HUIds_Existing = HUIdsFilterHelper.findExistingOrNull(stickyFilters);

// Create the sticky filter by HUIds from builder's huIds (if any huIds)
if (stickyFilter_HUIds_Existing == null && !huIds.isEmpty())
if (stickyFilter_HUIds_Existing == null && huIds != null)
{
final DocumentFilter stickyFilter_HUIds_New = HUIdsFilterHelper.createFilter(huIds);
stickyFilters.add(0, stickyFilter_HUIds_New);
Expand Down
37 changes: 27 additions & 10 deletions src/main/java/de/metas/ui/web/handlingunits/HUIdsFilterHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.adempiere.model.PlainContextAware;
import org.adempiere.util.Services;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
Expand Down Expand Up @@ -57,13 +58,18 @@ public final class HUIdsFilterHelper
@ToString
public static final class HUIdsFilterData
{
public static HUIdsFilterData ofHUIds(final Collection<Integer> huIds)
/**
*
* @param huIds may be empty, but not null. Empty means that <b>no</b> HU will be matched.
* @return
*/
public static HUIdsFilterData ofHUIds(@NonNull final Collection<Integer> huIds)
{
final IHUQueryBuilder initialFilter = null;
return new HUIdsFilterData(huIds, initialFilter);
}

public static HUIdsFilterData ofHUQuery(final IHUQueryBuilder initialHUQuery)
public static HUIdsFilterData ofHUQuery(@NonNull final IHUQueryBuilder initialHUQuery)
{
final Collection<Integer> huIds = null;
return new HUIdsFilterData(huIds, initialHUQuery);
Expand All @@ -76,17 +82,21 @@ public static HUIdsFilterData newEmpty()
return new HUIdsFilterData(huIds, initialHUQuery);
}

/**
* Important: {@code null} means "no restriction" (i.e. we can select allHUs) whereas empty means that no HU matches the filter.
*/
private final ImmutableSet<Integer> initialHUIds;

private final IHUQueryBuilder initialHUQuery;

private final Set<Integer> mustHUIds;
private final Set<Integer> shallNotHUIds;

private HUIdsFilterData(
@Nullable final Collection<Integer> initialHUIds,
@Nullable final Collection<Integer> initialHUIds,
@Nullable final IHUQueryBuilder initialHUQuery)
{
this.initialHUIds = initialHUIds != null ? ImmutableSet.copyOf(initialHUIds) : ImmutableSet.of();
this.initialHUIds = initialHUIds == null ? null : ImmutableSet.copyOf(initialHUIds);
this.initialHUQuery = initialHUQuery;
mustHUIds = new HashSet<>();
shallNotHUIds = new HashSet<>();
Expand Down Expand Up @@ -165,9 +175,8 @@ public static final class MutableHUIdsList

public static final String FILTER_ID = "huIds";
private static final String FILTER_PARAM_Data = "$data";

public static final transient HUIdsSqlDocumentFilterConverter SQL_DOCUMENT_FILTER_CONVERTER = new HUIdsSqlDocumentFilterConverter();

public static final transient HUIdsSqlDocumentFilterConverter SQL_DOCUMENT_FILTER_CONVERTER = new HUIdsSqlDocumentFilterConverter();

public static final DocumentFilter findExistingOrNull(final Collection<DocumentFilter> filters)
{
Expand All @@ -181,7 +190,12 @@ public static final DocumentFilter findExistingOrNull(final Collection<DocumentF
.findFirst().orElse(null);
}

public static final DocumentFilter createFilter(final Collection<Integer> huIds)
/**
* @param huIds huIds may be empty, but not null. Empty means that <b>no</b> HU will be matched.
*
* @return
*/
public static final DocumentFilter createFilter(@NonNull final Collection<Integer> huIds)
{
final HUIdsFilterData filterData = HUIdsFilterData.ofHUIds(huIds);
return DocumentFilter.singleParameterFilter(FILTER_ID, FILTER_PARAM_Data, Operator.EQUAL, filterData);
Expand Down Expand Up @@ -233,6 +247,9 @@ public static boolean isHighVolume(final List<DocumentFilter> stickyFilters)

public static final class HUIdsSqlDocumentFilterConverter implements SqlDocumentFilterConverter
{
@VisibleForTesting
static final String SQL_TRUE = "1=1";

private HUIdsSqlDocumentFilterConverter()
{
}
Expand All @@ -243,9 +260,9 @@ public String getSql(final SqlParamsCollector sqlParamsOut, final DocumentFilter
final HUIdsFilterData huIdsFilter = extractFilterData(filter);
final ImmutableList<Integer> onlyHUIds = ImmutableList.copyOf(Iterables.concat(huIdsFilter.getInitialHUIds(), huIdsFilter.getMustHUIds()));

if (onlyHUIds.isEmpty() && !huIdsFilter.hasInitialHUQuery())
if (onlyHUIds == null && !huIdsFilter.hasInitialHUQuery())
{
return "1=1"; // pass through
return SQL_TRUE; // no restrictions were specified; pass through
}

//
Expand All @@ -258,7 +275,7 @@ public String getSql(final SqlParamsCollector sqlParamsOut, final DocumentFilter
huQuery.setContext(PlainContextAware.newOutOfTrx());

// Only HUs
if (!onlyHUIds.isEmpty())
if (onlyHUIds != null)
{
huQuery.addOnlyHUIds(onlyHUIds);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected String doIt()
.build();

final List<I_M_HU> availableHUsToPick = huPickingSlotBL.retrieveAvailableHUsToPick(request);
final List<Integer> availableHUsToPickIDs = availableHUsToPick.stream().map(hu->hu.getM_HU_ID()).collect(Collectors.toList());
final List<Integer> availableHUsToPickIDs = availableHUsToPick.stream().map(hu -> hu.getM_HU_ID()).collect(Collectors.toList());

final RelatedProcessDescriptor processToPickSelectedHU = RelatedProcessDescriptor.builder()
.processId(adProcessDAO.retriveProcessIdByClassIfUnique(Env.getCtx(), WEBUI_Picking_PickSelectedHU.class))
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/de/metas/ui/web/view/CreateViewRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public static final Builder deleteStickyFilterBuilder(
private final DocumentFiltersList filters;

/**
* This one is becoming king of legacy.... it's a particular filter which filters by given IDs.
* This one is becoming king of legacy.... it's a particular kind of sticky filter which filters by given IDs.
*
* @deprecated please rather use {@link #getFilters()} {@link #getStickyFilters()}.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package de.metas.ui.web.handlingunits;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Test;

import com.google.common.collect.ImmutableList;

import de.metas.ui.web.document.filter.DocumentFilter;
import de.metas.ui.web.document.filter.sql.SqlParamsCollector;
import de.metas.ui.web.handlingunits.HUIdsFilterHelper.HUIdsSqlDocumentFilterConverter;

/*
* #%L
* metasfresh-webui-api
* %%
* Copyright (C) 2017 metas GmbH
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/

public class HUIdsFilterHelperTest
{
/**
* Verifies that if {@link HUIdsFilterHelper#createFilter(java.util.Collection)} is called with an empty list,
* then the filter's SQL does <b>not</b> select every single f**king HU on this planet.
*/
@Test
public void testEmptyHUIdsCollection()
{
final DocumentFilter noHusFilter = HUIdsFilterHelper.createFilter(ImmutableList.of());
final String sql = HUIdsFilterHelper.SQL_DOCUMENT_FILTER_CONVERTER.getSql(SqlParamsCollector.newInstance(), noHusFilter);

assertThat(sql).doesNotContain(HUIdsSqlDocumentFilterConverter.SQL_TRUE);
}

}

0 comments on commit 2e01d97

Please sign in to comment.