Skip to content

Commit

Permalink
partial solution to HU-Transform needs to filter by HU status #606
Browse files Browse the repository at this point in the history
* HUEditorRowQuery: can now exclude rows by status
* WebuiHUTransformParametersFiller: uses this to filter out destroyed
HUs' when looking for target rows
  • Loading branch information
metas-ts committed Sep 21, 2017
1 parent 1c5418a commit 7d50582
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 67 deletions.
19 changes: 15 additions & 4 deletions src/main/java/de/metas/ui/web/handlingunits/HUEditorRowQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
public class HUEditorRowQuery
{
/** User string filter (e.g. what user typed in the lookup field) */
private final String stringFilter;
private final String userInputFilter;

/** If specified only the rows of given type are considered */
private final HUEditorRowType rowType;
Expand All @@ -46,15 +46,24 @@ public class HUEditorRowQuery
* M_HU_IDs to exclude.
* The HUs are excluded AFTER they are exploded,
* so if you exclude an M_HU_ID then you will not get it in the result but it's children will be part of the result.
*
* Note: this list is never {@code null}, empty means "no restriction".
*/
private final ImmutableSet<Integer> excludeHUIds;

/**
* Note: this list is never {@code null}, empty means "no restriction".
*/
private final ImmutableSet<String> excludeHUStatuses;

@Builder
private HUEditorRowQuery(final String stringFilter,
private HUEditorRowQuery(
final String userInputFilter,
final HUEditorRowType rowType,
@Singular final ImmutableSet<Integer> excludeHUIds)
@Singular final ImmutableSet<Integer> excludeHUIds,
@Singular final ImmutableSet<String> excludeHUStatuses)
{
this.stringFilter = stringFilter;
this.userInputFilter = userInputFilter;
this.rowType = rowType;

if (excludeHUIds == null || excludeHUIds.isEmpty())
Expand All @@ -65,5 +74,7 @@ private HUEditorRowQuery(final String stringFilter,
{
this.excludeHUIds = excludeHUIds.stream().filter(huId -> huId > 0).collect(ImmutableSet.toImmutableSet());
}

this.excludeHUStatuses = excludeHUStatuses;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@
interface HUEditorViewBuffer
{
ViewId getViewId();

List<DocumentFilter> getStickyFilters();

long size();

void invalidateAll();
Expand All @@ -64,7 +64,7 @@ interface HUEditorViewBuffer

/** @return top level rows and included rows recursive stream */
Stream<HUEditorRow> streamAllRecursive() throws UnsupportedOperationException;

/** @return top level rows and included rows recursive stream which are matching the given query */
default Stream<HUEditorRow> streamAllRecursive(@NonNull final HUEditorRowQuery query)
{
Expand All @@ -78,29 +78,33 @@ default Stream<HUEditorRow> streamAllRecursive(@NonNull final HUEditorRowQuery q
}

// Filter by string filter
final String stringFilter = query.getStringFilter();
final String stringFilter = query.getUserInputFilter();
if (!Check.isEmpty(stringFilter, true))
{
result = result.filter(row -> row.matchesStringFilter(stringFilter));
}

// Exclude M_HU_IDs
final ImmutableSet<Integer> excludeHUIds = query.getExcludeHUIds();
if(!excludeHUIds.isEmpty())
if (!excludeHUIds.isEmpty())
{
result = result.filter(row -> !excludeHUIds.contains(row.getM_HU_ID()));
}

if (!query.getExcludeHUStatuses().isEmpty())
{
result = result.filter(row -> !query.getExcludeHUStatuses().contains(row.getHUStatusKey()));
}

return result;
}

/** @return true if there is any top level or included row which is matching given query */
default boolean matchesAnyRowRecursive(final HUEditorRowQuery query)
{
return streamAllRecursive(query).findAny().isPresent();
}


/**
* Stream all rows (including children) which match any of given <code>rowIds</code>.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import de.metas.handlingunits.IHandlingUnitsDAO;
import de.metas.handlingunits.allocation.transfer.HUTransformService;
import de.metas.handlingunits.model.I_M_HU;
import de.metas.handlingunits.model.I_M_HU_PI;
import de.metas.handlingunits.model.I_M_HU_PI_Item;
import de.metas.handlingunits.model.I_M_HU_PI_Version;
import de.metas.handlingunits.model.X_M_HU;
import de.metas.printing.esb.base.util.Check;
import de.metas.process.IADProcessDAO;
import de.metas.process.IProcessDefaultParametersProvider;
import de.metas.ui.web.handlingunits.HUEditorRow;
Expand Down Expand Up @@ -148,76 +150,95 @@ else if (WEBUI_M_HU_Transform.PARAM_HUPlanningReceiptOwnerPM_LU.equals(parameter
*/
public final LookupValuesList getActions(final int processId)
{
final IADProcessDAO adProcessDAO = Services.get(IADProcessDAO.class);
final I_AD_Process_Para processParameter = adProcessDAO.retriveProcessParameter(Env.getCtx(), processId, WEBUI_M_HU_Transform.PARAM_Action);
final int actionsReferenceId = processParameter.getAD_Reference_Value_ID();
final Collection<ADRefListItem> allActiveActionItems = adReferenceDAO.retrieveListItems(actionsReferenceId);

final Set<String> selectableTypes = new HashSet<>();
final Set<String> allowedActions = new HashSet<>();

final HUEditorRow huRow = getSelectedRow();
if (huRow.isCU())
{
selectableTypes.add(ActionType.CU_To_NewCU.toString());
selectableTypes.add(ActionType.CU_To_NewTUs.toString());

final boolean existsTUs;
if (isCheckExistingHUsInsideView())
{
existsTUs = getView().matchesAnyRowRecursive(HUEditorRowQuery.builder()
.rowType(HUEditorRowType.TU)
.excludeHUId(getParentHUIdOfSelectedRow())
.build());
}
else
{
existsTUs = true;
}

if (existsTUs)
{
selectableTypes.add(ActionType.CU_To_ExistingTU.toString());
}
allowedActions.addAll(getActionTypesForCUs());
}

else if (huRow.isTU())
{
selectableTypes.add(ActionType.TU_To_NewTUs.toString());
selectableTypes.add(ActionType.TU_To_NewLUs.toString());
selectableTypes.add(ActionType.TU_Set_Ownership.toString());

final boolean existsLUs;
if (isCheckExistingHUsInsideView())
{
existsLUs = getView().matchesAnyRowRecursive(HUEditorRowQuery.builder()
.rowType(HUEditorRowType.LU)
.excludeHUId(getParentHUIdOfSelectedRow())
.build());
}
else
{
existsLUs = true;
}
if (existsLUs)
{
selectableTypes.add(ActionType.TU_To_ExistingLU.toString());
}
allowedActions.addAll(getActionTypesForTUs());
}

else if (huRow.isLU())
{
selectableTypes.add(ActionType.LU_Set_Ownership.toString());
allowedActions.add(ActionType.LU_Set_Ownership.toString());
}

final IADProcessDAO adProcessDAO = Services.get(IADProcessDAO.class);
final I_AD_Process_Para processParameter = adProcessDAO.retriveProcessParameter(Env.getCtx(), processId, WEBUI_M_HU_Transform.PARAM_Action);
final int actionsReferenceId = processParameter.getAD_Reference_Value_ID();
final Collection<ADRefListItem> allActiveActionItems = adReferenceDAO.retrieveListItems(actionsReferenceId);

final String adLanguage = Env.getAD_Language();

return allActiveActionItems.stream()
.filter(item -> selectableTypes.contains(item.getValueName()))
.filter(item -> allowedActions.contains(item.getValueName()))
.map(item -> StringLookupValue.of(item.getValueName(), item.getName()))
.sorted(Comparator.comparing(lookupValue -> lookupValue.getDisplayName(adLanguage)))
.collect(LookupValuesList.collect());
}

private Set<String> getActionTypesForCUs()
{
final Set<String> selectableTypes = new HashSet<>();

selectableTypes.add(ActionType.CU_To_NewCU.toString());
selectableTypes.add(ActionType.CU_To_NewTUs.toString());

final boolean existsTUs;
if (isCheckExistingHUsInsideView())
{
existsTUs = getView().matchesAnyRowRecursive(HUEditorRowQuery.builder()
.rowType(HUEditorRowType.TU)
.excludeHUId(getParentHUIdOfSelectedRow())
.excludeHUStatus(X_M_HU.HUSTATUS_Destroyed)
.build());
}
else
{
existsTUs = true;
}

if (existsTUs)
{
selectableTypes.add(ActionType.CU_To_ExistingTU.toString());
}

return selectableTypes;
}

private Set<String> getActionTypesForTUs()
{
final Set<String> selectableTypes = new HashSet<>();

selectableTypes.add(ActionType.TU_To_NewTUs.toString());
selectableTypes.add(ActionType.TU_To_NewLUs.toString());
selectableTypes.add(ActionType.TU_Set_Ownership.toString());

final boolean existsLUs;
if (isCheckExistingHUsInsideView())
{
existsLUs = getView().matchesAnyRowRecursive(HUEditorRowQuery.builder()
.rowType(HUEditorRowType.LU)
.excludeHUId(getParentHUIdOfSelectedRow())
.excludeHUStatus(X_M_HU.HUSTATUS_Destroyed)
.build());
}
else
{
existsLUs = true;
}
if (existsLUs)
{
selectableTypes.add(ActionType.TU_To_ExistingLU.toString());
}
return selectableTypes;
}

/**
* @return a list of PI item products that match the selected CU's product and partner, sorted by name.
*/
Expand All @@ -239,10 +260,10 @@ private static LookupValuesList retrieveHUPItemProductsForNewTU(final HUEditorRo
final I_C_BPartner bPartner = cuRow.getM_HU().getC_BPartner();

return WEBUI_ProcessHelper.retrieveHUPIItemProducts(
Env.getCtx(),
product,
Env.getCtx(),
product,
bPartner,
false); // includeVirtualItem = false..moving a cu onto a "virtual" TU makes no sense. Instead, the user can just leave the CU as it is, or take it out of a physical TU
false); // includeVirtualItem = false..moving a cu onto a "virtual" TU makes no sense. Instead, the user can just leave the CU as it is, or take it out of a physical TU
}

/**
Expand All @@ -269,9 +290,10 @@ private LookupValuesList getTUsLookupValues_InsideView(final LookupDataSourceCon
{
return getView()
.streamAllRecursive(HUEditorRowQuery.builder()
.stringFilter(context.getFilterOrIfAny(null))
.userInputFilter(context.getFilterOrIfAny(null))
.rowType(HUEditorRowType.TU) // ..needs to be a TU
.excludeHUId(getParentHUIdOfSelectedRow()) // ..may not be the one TU that 'cu' is already attached to
.excludeHUStatus(X_M_HU.HUSTATUS_Destroyed)
.build())
.sorted(Comparator.comparing(HUEditorRow::getM_HU_ID))
.map(row -> row.toLookupValue())
Expand Down Expand Up @@ -323,9 +345,10 @@ private LookupValuesList getLUsLookupValues_InsideView(final LookupDataSourceCon
{
return getView()
.streamAllRecursive(HUEditorRowQuery.builder()
.stringFilter(context.getFilterOrIfAny(null))
.userInputFilter(context.getFilterOrIfAny(null))
.rowType(HUEditorRowType.LU) // ..needs to be a LU
.excludeHUId(getParentHUIdOfSelectedRow()) // ..may not be the one LU that 'tu' is already attached to
.excludeHUStatus(X_M_HU.HUSTATUS_Destroyed)
.build())
.sorted(Comparator.comparing(HUEditorRow::getM_HU_ID))
.map(row -> row.toLookupValue())
Expand Down Expand Up @@ -375,9 +398,10 @@ public LookupValuesList getM_HU_PI_Item_IDs()

final HUEditorRow tuRow = getSelectedRow();
final I_M_HU tuHU = tuRow.getM_HU();
final I_M_HU_PI tuPI = handlingUnitsBL.getEffectivePIVersion(tuHU).getM_HU_PI();
final I_M_HU_PI_Version effectivePIVersion = handlingUnitsBL.getEffectivePIVersion(tuHU);
Check.errorIf(effectivePIVersion == null, "tuHU is inconsistent; hu={}", tuHU);

final List<I_M_HU_PI_Item> luPIItems = handlingUnitsDAO.retrieveParentPIItemsForParentPI(tuPI, null, tuHU.getC_BPartner());
final List<I_M_HU_PI_Item> luPIItems = handlingUnitsDAO.retrieveParentPIItemsForParentPI(effectivePIVersion.getM_HU_PI(), null, tuHU.getC_BPartner());

return luPIItems.stream()
.filter(luPIItem -> luPIItem.getM_HU_PI_Version().isCurrent() && luPIItem.getM_HU_PI_Version().isActive() && luPIItem.getM_HU_PI_Version().getM_HU_PI().isActive())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package de.metas.ui.web.handlingunits;

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

import org.junit.Test;

/*
* #%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 HUEditorRowQueryTest
{
@Test
public void testListsNotNullInEmptyQuery()
{
final HUEditorRowQuery emptyQuery = HUEditorRowQuery.builder().build();

assertThat(emptyQuery.getExcludeHUIds()).isNotNull();
assertThat(emptyQuery.getExcludeHUIds()).isEmpty();

assertThat(emptyQuery.getExcludeHUStatuses()).isNotNull();
assertThat(emptyQuery.getExcludeHUStatuses()).isEmpty();
}
}

0 comments on commit 7d50582

Please sign in to comment.