From 7d50582463b054d82698c6910d88b3d434688165 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Sch=C3=B6neberg?= Date: Thu, 21 Sep 2017 07:12:01 +0200 Subject: [PATCH] partial solution to HU-Transform needs to filter by HU status #606 * HUEditorRowQuery: can now exclude rows by status * WebuiHUTransformParametersFiller: uses this to filter out destroyed HUs' when looking for target rows --- .../web/handlingunits/HUEditorRowQuery.java | 19 ++- .../web/handlingunits/HUEditorViewBuffer.java | 18 ++- .../WebuiHUTransformParametersFiller.java | 136 ++++++++++-------- .../handlingunits/HUEditorRowQueryTest.java | 42 ++++++ 4 files changed, 148 insertions(+), 67 deletions(-) create mode 100644 src/test/java/de/metas/ui/web/handlingunits/HUEditorRowQueryTest.java diff --git a/src/main/java/de/metas/ui/web/handlingunits/HUEditorRowQuery.java b/src/main/java/de/metas/ui/web/handlingunits/HUEditorRowQuery.java index 4b881d050..f2ff2d351 100644 --- a/src/main/java/de/metas/ui/web/handlingunits/HUEditorRowQuery.java +++ b/src/main/java/de/metas/ui/web/handlingunits/HUEditorRowQuery.java @@ -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; @@ -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 excludeHUIds; + /** + * Note: this list is never {@code null}, empty means "no restriction". + */ + private final ImmutableSet excludeHUStatuses; + @Builder - private HUEditorRowQuery(final String stringFilter, + private HUEditorRowQuery( + final String userInputFilter, final HUEditorRowType rowType, - @Singular final ImmutableSet excludeHUIds) + @Singular final ImmutableSet excludeHUIds, + @Singular final ImmutableSet excludeHUStatuses) { - this.stringFilter = stringFilter; + this.userInputFilter = userInputFilter; this.rowType = rowType; if (excludeHUIds == null || excludeHUIds.isEmpty()) @@ -65,5 +74,7 @@ private HUEditorRowQuery(final String stringFilter, { this.excludeHUIds = excludeHUIds.stream().filter(huId -> huId > 0).collect(ImmutableSet.toImmutableSet()); } + + this.excludeHUStatuses = excludeHUStatuses; } } diff --git a/src/main/java/de/metas/ui/web/handlingunits/HUEditorViewBuffer.java b/src/main/java/de/metas/ui/web/handlingunits/HUEditorViewBuffer.java index efe921c34..ad0c82e26 100644 --- a/src/main/java/de/metas/ui/web/handlingunits/HUEditorViewBuffer.java +++ b/src/main/java/de/metas/ui/web/handlingunits/HUEditorViewBuffer.java @@ -49,9 +49,9 @@ interface HUEditorViewBuffer { ViewId getViewId(); - + List getStickyFilters(); - + long size(); void invalidateAll(); @@ -64,7 +64,7 @@ interface HUEditorViewBuffer /** @return top level rows and included rows recursive stream */ Stream streamAllRecursive() throws UnsupportedOperationException; - + /** @return top level rows and included rows recursive stream which are matching the given query */ default Stream streamAllRecursive(@NonNull final HUEditorRowQuery query) { @@ -78,7 +78,7 @@ default Stream 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)); @@ -86,21 +86,25 @@ default Stream streamAllRecursive(@NonNull final HUEditorRowQuery q // Exclude M_HU_IDs final ImmutableSet 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 rowIds. * diff --git a/src/main/java/de/metas/ui/web/handlingunits/process/WebuiHUTransformParametersFiller.java b/src/main/java/de/metas/ui/web/handlingunits/process/WebuiHUTransformParametersFiller.java index dfce5ef4c..95508a054 100644 --- a/src/main/java/de/metas/ui/web/handlingunits/process/WebuiHUTransformParametersFiller.java +++ b/src/main/java/de/metas/ui/web/handlingunits/process/WebuiHUTransformParametersFiller.java @@ -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; @@ -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 allActiveActionItems = adReferenceDAO.retrieveListItems(actionsReferenceId); - - final Set selectableTypes = new HashSet<>(); + final Set 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 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 getActionTypesForCUs() + { + final Set 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 getActionTypesForTUs() + { + final Set 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. */ @@ -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 } /** @@ -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()) @@ -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()) @@ -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 luPIItems = handlingUnitsDAO.retrieveParentPIItemsForParentPI(tuPI, null, tuHU.getC_BPartner()); + final List 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()) diff --git a/src/test/java/de/metas/ui/web/handlingunits/HUEditorRowQueryTest.java b/src/test/java/de/metas/ui/web/handlingunits/HUEditorRowQueryTest.java new file mode 100644 index 000000000..1aa29f97b --- /dev/null +++ b/src/test/java/de/metas/ui/web/handlingunits/HUEditorRowQueryTest.java @@ -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 + * . + * #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(); + } +}