From 1babeb0d2c4d988937a8ba3a6acd43fdc661a36e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Sch=C3=B6neberg?= Date: Fri, 8 Sep 2017 15:05:38 +0200 Subject: [PATCH] HUEditorProcessTemplate - methods require decision about top level also * use the new method to simplify code here and there * fix the actual problem in WEBUI_M_HU_MoveToGarbage (<= this issue) * also potentially fix it in * WEBUI_M_HU_MoveToAnotherWarehouse * WEBUI_M_HU_ReturnFromCustomer * WEBUI_M_HU_ReturnToVendor * WEBUI_M_HU_Messages: add a new top-level related rejection message so the hopefully the dev takes the hint and makes up their minds on whether the need top level or all storage error on disposal https://github.com/metasfresh/metasfresh-webui-api/issues/578 --- .../process/HUEditorProcessTemplate.java | 65 ++++++++++++++----- .../process/WEBUI_M_HU_Messages.java | 3 + .../WEBUI_M_HU_MoveToAnotherWarehouse.java | 8 +-- .../WEBUI_M_HU_MoveToDirectWarehouse.java | 23 ++----- .../process/WEBUI_M_HU_MoveToGarbage.java | 14 ++-- .../WEBUI_M_HU_MoveToQualityWarehouse.java | 16 +++-- .../WEBUI_M_HU_ReturnFromCustomer.java | 29 ++++----- .../process/WEBUI_M_HU_ReturnToVendor.java | 23 +++---- 8 files changed, 98 insertions(+), 83 deletions(-) diff --git a/src/main/java/de/metas/ui/web/handlingunits/process/HUEditorProcessTemplate.java b/src/main/java/de/metas/ui/web/handlingunits/process/HUEditorProcessTemplate.java index 27081cc48..93bd6ae03 100644 --- a/src/main/java/de/metas/ui/web/handlingunits/process/HUEditorProcessTemplate.java +++ b/src/main/java/de/metas/ui/web/handlingunits/process/HUEditorProcessTemplate.java @@ -3,17 +3,18 @@ import java.util.List; import java.util.Set; -import org.adempiere.ad.dao.IQueryBL; import org.adempiere.util.Services; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; +import de.metas.handlingunits.IHandlingUnitsDAO; import de.metas.handlingunits.model.I_M_HU; import de.metas.ui.web.handlingunits.HUEditorRow; import de.metas.ui.web.handlingunits.HUEditorView; import de.metas.ui.web.process.adprocess.ViewBasedProcessTemplate; import de.metas.ui.web.window.datatypes.DocumentIdsSelection; +import lombok.NonNull; /* * #%L @@ -56,35 +57,63 @@ protected final HUEditorRow getSingleSelectedRow() { return HUEditorRow.cast(super.getSingleSelectedRow()); } - - protected final List getSelectedRows() + + enum Select + { + ONLY_TOPLEVEL, ALL + } + + /** + * + * @param select if {@link Select#ONLY_TOPLEVEL} then the method only returns row with {@link HUEditorRow#isTopLevel()} {@code == true}; + * @return + */ + protected final List getSelectedRows(@NonNull final Select select) { final DocumentIdsSelection selectedDocumentIds = getSelectedDocumentIds(); - return getView().getByIds(selectedDocumentIds); + final List allRows = getView().getByIds(selectedDocumentIds); + + if (select == Select.ALL) + { + return allRows; + } + return allRows.stream() + .filter(HUEditorRow::isTopLevel) + .collect(ImmutableList.toImmutableList()); + } - - protected final Set getSelectedHUIds() + + /** + * Calls {@link #getSelectedRows(Select)} and returns the {@link HUEditorRow}s' {@code M_HU_ID}s. + * + * @param select + * @return + */ + protected final Set getSelectedHUIds(@NonNull final Select select) { - return getSelectedRows() + return getSelectedRows(select) .stream() .map(HUEditorRow::getM_HU_ID) .filter(huId -> huId > 0) .collect(ImmutableSet.toImmutableSet()); } - protected final List getSelectedHUs() + /** + * Gets all selected {@link HUEditorRow}s and loads the top level-HUs from those. + * I.e. this method does not rely on {@link HUEditorRow#isTopLevel()}, but checks the underlying HU. + * + * @param select + * @return + */ + protected final List getSelectedHUs(@NonNull final Select select) { - final Set huIds = getSelectedHUIds(); - if (huIds.isEmpty()) - { - return ImmutableList.of(); - } + // get all IDs, we we will check for ourselves if they are toplevel or not + final Set huIds = getSelectedHUIds(Select.ALL); - return Services.get(IQueryBL.class) - .createQueryBuilder(I_M_HU.class) - .addInArrayFilter(I_M_HU.COLUMN_M_HU_ID, huIds) - .create() + return Services.get(IHandlingUnitsDAO.class) + .createHUQueryBuilder().addOnlyHUIds(huIds) + .setOnlyTopLevelHUs(select == Select.ONLY_TOPLEVEL) + .createQuery() .list(I_M_HU.class); } - } diff --git a/src/main/java/de/metas/ui/web/handlingunits/process/WEBUI_M_HU_Messages.java b/src/main/java/de/metas/ui/web/handlingunits/process/WEBUI_M_HU_Messages.java index 886aaf8c5..3f950309d 100644 --- a/src/main/java/de/metas/ui/web/handlingunits/process/WEBUI_M_HU_Messages.java +++ b/src/main/java/de/metas/ui/web/handlingunits/process/WEBUI_M_HU_Messages.java @@ -34,4 +34,7 @@ * */ public static final String MSG_NotEnoughTUsFound = "WEBUI_M_HU_MoveTUsToDirectWarehouse.NotEnoughTUsFound"; + + public static final String MSG_WEBUI_ONLY_TOP_LEVEL_HU = "WEBUI_Only_TopLevelHU"; + } diff --git a/src/main/java/de/metas/ui/web/handlingunits/process/WEBUI_M_HU_MoveToAnotherWarehouse.java b/src/main/java/de/metas/ui/web/handlingunits/process/WEBUI_M_HU_MoveToAnotherWarehouse.java index 5ae2756cd..34fb09404 100644 --- a/src/main/java/de/metas/ui/web/handlingunits/process/WEBUI_M_HU_MoveToAnotherWarehouse.java +++ b/src/main/java/de/metas/ui/web/handlingunits/process/WEBUI_M_HU_MoveToAnotherWarehouse.java @@ -43,8 +43,6 @@ */ public class WEBUI_M_HU_MoveToAnotherWarehouse extends HUEditorProcessTemplate implements IProcessPrecondition { - private static final String MSG_NoSelectedHU = "NoHUSelected"; - private final transient IHUMovementBL huMovementBL = Services.get(IHUMovementBL.class); @Param(parameterName = I_M_Warehouse.COLUMNNAME_M_Warehouse_ID, mandatory = true) @@ -55,10 +53,10 @@ public class WEBUI_M_HU_MoveToAnotherWarehouse extends HUEditorProcessTemplate i @Override protected ProcessPreconditionsResolution checkPreconditionsApplicable() { - final Set huIds = getSelectedHUIds(); + final Set huIds = getSelectedHUIds(Select.ONLY_TOPLEVEL); if (huIds.isEmpty()) { - return ProcessPreconditionsResolution.reject(msgBL.getTranslatableMsgText(MSG_NoSelectedHU)); + return ProcessPreconditionsResolution.reject(msgBL.getTranslatableMsgText(WEBUI_M_HU_Messages.MSG_WEBUI_ONLY_TOP_LEVEL_HU)); } return ProcessPreconditionsResolution.accept(); @@ -68,7 +66,7 @@ protected ProcessPreconditionsResolution checkPreconditionsApplicable() protected String doIt() throws Exception { - final List hus = getSelectedHUs(); + final List hus = getSelectedHUs(Select.ONLY_TOPLEVEL); movementResult = huMovementBL.moveHUsToWarehouse(hus, warehouse); return MSG_OK; diff --git a/src/main/java/de/metas/ui/web/handlingunits/process/WEBUI_M_HU_MoveToDirectWarehouse.java b/src/main/java/de/metas/ui/web/handlingunits/process/WEBUI_M_HU_MoveToDirectWarehouse.java index fe7da3abc..7f3d03974 100644 --- a/src/main/java/de/metas/ui/web/handlingunits/process/WEBUI_M_HU_MoveToDirectWarehouse.java +++ b/src/main/java/de/metas/ui/web/handlingunits/process/WEBUI_M_HU_MoveToDirectWarehouse.java @@ -1,19 +1,15 @@ package de.metas.ui.web.handlingunits.process; import java.util.List; +import java.util.Set; import org.adempiere.exceptions.AdempiereException; -import org.adempiere.util.Services; import org.springframework.beans.factory.annotation.Autowired; -import com.google.common.collect.ImmutableList; - -import de.metas.handlingunits.IHandlingUnitsBL; import de.metas.handlingunits.model.I_M_HU; import de.metas.process.IProcessPrecondition; import de.metas.process.ProcessPreconditionsResolution; import de.metas.process.RunOutOfTrx; -import de.metas.ui.web.handlingunits.HUEditorRow; import de.metas.ui.web.window.datatypes.DocumentIdsSelection; import de.metas.ui.web.window.model.DocumentCollection; @@ -47,8 +43,6 @@ */ public class WEBUI_M_HU_MoveToDirectWarehouse extends HUEditorProcessTemplate implements IProcessPrecondition { - private final transient IHandlingUnitsBL handlingUnitsBL = Services.get(IHandlingUnitsBL.class); - @Autowired private DocumentCollection documentsCollection; @@ -61,14 +55,10 @@ protected ProcessPreconditionsResolution checkPreconditionsApplicable() return ProcessPreconditionsResolution.rejectBecauseNoSelection(); } - final boolean hasTopLevelHUsSelected = getView() - .streamByIds(selectedRowIds) - .filter(HUEditorRow::isTopLevel) - .anyMatch(HUEditorRow::isTopLevel); - - if (!hasTopLevelHUsSelected) + final Set huIds = getSelectedHUIds(Select.ONLY_TOPLEVEL); + if (huIds.isEmpty()) { - return ProcessPreconditionsResolution.rejectWithInternalReason("not a top level HU"); + return ProcessPreconditionsResolution.reject(msgBL.getTranslatableMsgText(WEBUI_M_HU_Messages.MSG_WEBUI_ONLY_TOP_LEVEL_HU)); } return ProcessPreconditionsResolution.accept(); @@ -78,10 +68,7 @@ protected ProcessPreconditionsResolution checkPreconditionsApplicable() @RunOutOfTrx protected String doIt() { - final List selectedTopLevelHUs = getSelectedHUs() - .stream() - .filter(handlingUnitsBL::isTopLevel) - .collect(ImmutableList.toImmutableList()); + final List selectedTopLevelHUs = getSelectedHUs(Select.ONLY_TOPLEVEL); if (selectedTopLevelHUs.isEmpty()) { throw new AdempiereException("@NoSelection@"); diff --git a/src/main/java/de/metas/ui/web/handlingunits/process/WEBUI_M_HU_MoveToGarbage.java b/src/main/java/de/metas/ui/web/handlingunits/process/WEBUI_M_HU_MoveToGarbage.java index 946f4522c..dd15a3108 100644 --- a/src/main/java/de/metas/ui/web/handlingunits/process/WEBUI_M_HU_MoveToGarbage.java +++ b/src/main/java/de/metas/ui/web/handlingunits/process/WEBUI_M_HU_MoveToGarbage.java @@ -4,6 +4,7 @@ import java.util.List; import java.util.Set; +import org.adempiere.exceptions.AdempiereException; import org.adempiere.util.Services; import org.compiere.util.Env; @@ -44,8 +45,6 @@ */ public class WEBUI_M_HU_MoveToGarbage extends HUEditorProcessTemplate implements IProcessPrecondition { - private static final String MSG_NoSelectedHU = "NoHUSelected"; - private final transient IHUInventoryBL huInventoryBL = Services.get(IHUInventoryBL.class); private Set huIdsDestroyed; @@ -53,10 +52,10 @@ public class WEBUI_M_HU_MoveToGarbage extends HUEditorProcessTemplate implements @Override protected ProcessPreconditionsResolution checkPreconditionsApplicable() { - final Set huIds = getSelectedHUIds(); + final Set huIds = getSelectedHUIds(Select.ONLY_TOPLEVEL); if (huIds.isEmpty()) { - return ProcessPreconditionsResolution.reject(msgBL.getTranslatableMsgText(MSG_NoSelectedHU)); + return ProcessPreconditionsResolution.reject(msgBL.getTranslatableMsgText(WEBUI_M_HU_Messages.MSG_WEBUI_ONLY_TOP_LEVEL_HU)); } return ProcessPreconditionsResolution.accept(); @@ -65,7 +64,12 @@ protected ProcessPreconditionsResolution checkPreconditionsApplicable() @Override protected String doIt() throws Exception { - final List husToDestroy = getSelectedHUs(); + final List husToDestroy = getSelectedHUs(Select.ONLY_TOPLEVEL); + if (husToDestroy.isEmpty()) + { + throw new AdempiereException("@NoSelection@"); + } + final Timestamp movementDate = Env.getDate(getCtx()); huInventoryBL.moveToGarbage(husToDestroy, movementDate); diff --git a/src/main/java/de/metas/ui/web/handlingunits/process/WEBUI_M_HU_MoveToQualityWarehouse.java b/src/main/java/de/metas/ui/web/handlingunits/process/WEBUI_M_HU_MoveToQualityWarehouse.java index 7dbfcb58a..70e802dfd 100644 --- a/src/main/java/de/metas/ui/web/handlingunits/process/WEBUI_M_HU_MoveToQualityWarehouse.java +++ b/src/main/java/de/metas/ui/web/handlingunits/process/WEBUI_M_HU_MoveToQualityWarehouse.java @@ -3,6 +3,7 @@ import java.util.List; import java.util.Set; +import org.adempiere.exceptions.AdempiereException; import org.adempiere.util.Services; import de.metas.handlingunits.model.I_M_HU; @@ -44,8 +45,6 @@ */ public class WEBUI_M_HU_MoveToQualityWarehouse extends HUEditorProcessTemplate implements IProcessPrecondition { - private static final String MSG_NoSelectedHU = "NoHUSelected"; - private final transient IHUMovementBL huMovementBL = Services.get(IHUMovementBL.class); @Param(parameterName = I_M_Warehouse.COLUMNNAME_M_Warehouse_ID, mandatory = true) @@ -56,10 +55,10 @@ public class WEBUI_M_HU_MoveToQualityWarehouse extends HUEditorProcessTemplate i @Override protected ProcessPreconditionsResolution checkPreconditionsApplicable() { - final Set huIds = getSelectedHUIds(); + final Set huIds = getSelectedHUIds(Select.ONLY_TOPLEVEL); if (huIds.isEmpty()) { - return ProcessPreconditionsResolution.reject(msgBL.getTranslatableMsgText(MSG_NoSelectedHU)); + return ProcessPreconditionsResolution.reject(msgBL.getTranslatableMsgText(WEBUI_M_HU_Messages.MSG_WEBUI_ONLY_TOP_LEVEL_HU)); } return ProcessPreconditionsResolution.accept(); @@ -70,8 +69,13 @@ protected String doIt() throws Exception { Check.assume(warehouse.isQualityReturnWarehouse(), "not a quality returns warehouse"); - final List hus = getSelectedHUs(); - movementResult = huMovementBL.moveHUsToWarehouse(hus, warehouse); + final List selectedTopLevelHUs = getSelectedHUs(Select.ONLY_TOPLEVEL); + if (selectedTopLevelHUs.isEmpty()) + { + throw new AdempiereException("@NoSelection@"); + } + + movementResult = huMovementBL.moveHUsToWarehouse(selectedTopLevelHUs, warehouse); return MSG_OK; } diff --git a/src/main/java/de/metas/ui/web/handlingunits/process/WEBUI_M_HU_ReturnFromCustomer.java b/src/main/java/de/metas/ui/web/handlingunits/process/WEBUI_M_HU_ReturnFromCustomer.java index aeb131f03..f3c2ecce4 100644 --- a/src/main/java/de/metas/ui/web/handlingunits/process/WEBUI_M_HU_ReturnFromCustomer.java +++ b/src/main/java/de/metas/ui/web/handlingunits/process/WEBUI_M_HU_ReturnFromCustomer.java @@ -3,11 +3,9 @@ import java.util.List; import java.util.Set; +import org.adempiere.exceptions.AdempiereException; import org.adempiere.util.Services; -import com.google.common.collect.ImmutableList; - -import de.metas.handlingunits.IHandlingUnitsBL; import de.metas.handlingunits.inout.IHUInOutBL; import de.metas.handlingunits.model.I_M_HU; import de.metas.process.IProcessPrecondition; @@ -26,11 +24,11 @@ * * 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 + * 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 + * License along with this program. If not, see * . * #L% */ @@ -43,19 +41,15 @@ */ public class WEBUI_M_HU_ReturnFromCustomer extends HUEditorProcessTemplate implements IProcessPrecondition { - private static final String MSG_NoSelectedHU = "NoHUSelected"; - - private final transient IHandlingUnitsBL handlingUnitsBL = Services.get(IHandlingUnitsBL.class); - private List husToReturn = null; @Override protected ProcessPreconditionsResolution checkPreconditionsApplicable() { - final Set huIds = getSelectedHUIds(); + final Set huIds = getSelectedHUIds(Select.ONLY_TOPLEVEL); if (huIds.isEmpty()) { - return ProcessPreconditionsResolution.reject(msgBL.getTranslatableMsgText(MSG_NoSelectedHU)); + return ProcessPreconditionsResolution.reject(msgBL.getTranslatableMsgText(WEBUI_M_HU_Messages.MSG_WEBUI_ONLY_TOP_LEVEL_HU)); } return ProcessPreconditionsResolution.accept(); @@ -64,11 +58,12 @@ protected ProcessPreconditionsResolution checkPreconditionsApplicable() @Override protected String doIt() { - husToReturn = getSelectedHUs() - .stream() - .filter(handlingUnitsBL::isTopLevel) // only top level HUs - .collect(ImmutableList.toImmutableList()); - + husToReturn = getSelectedHUs(Select.ONLY_TOPLEVEL); + if (husToReturn.isEmpty()) + { + throw new AdempiereException("@NoSelection@"); + } + Services.get(IHUInOutBL.class).createCustomerReturnInOutForHUs(husToReturn); return MSG_OK; } @@ -78,7 +73,7 @@ protected void postProcess(final boolean success) { if (husToReturn != null && !husToReturn.isEmpty()) { - getView().removeHUsAndInvalidate(getSelectedHUs()); + getView().removeHUsAndInvalidate(getSelectedHUs(Select.ONLY_TOPLEVEL)); } } diff --git a/src/main/java/de/metas/ui/web/handlingunits/process/WEBUI_M_HU_ReturnToVendor.java b/src/main/java/de/metas/ui/web/handlingunits/process/WEBUI_M_HU_ReturnToVendor.java index 01a630f18..e52aa3686 100644 --- a/src/main/java/de/metas/ui/web/handlingunits/process/WEBUI_M_HU_ReturnToVendor.java +++ b/src/main/java/de/metas/ui/web/handlingunits/process/WEBUI_M_HU_ReturnToVendor.java @@ -4,12 +4,10 @@ import java.util.List; import java.util.Set; +import org.adempiere.exceptions.AdempiereException; import org.adempiere.util.Services; import org.compiere.util.Env; -import com.google.common.collect.ImmutableList; - -import de.metas.handlingunits.IHandlingUnitsBL; import de.metas.handlingunits.inout.IHUInOutBL; import de.metas.handlingunits.model.I_M_HU; import de.metas.process.IProcessPrecondition; @@ -45,19 +43,15 @@ */ public class WEBUI_M_HU_ReturnToVendor extends HUEditorProcessTemplate implements IProcessPrecondition { - private static final String MSG_NoSelectedHU = "NoHUSelected"; - - private final transient IHandlingUnitsBL handlingUnitsBL = Services.get(IHandlingUnitsBL.class); - private List husToReturn = null; @Override protected ProcessPreconditionsResolution checkPreconditionsApplicable() { - final Set huIds = getSelectedHUIds(); + final Set huIds = getSelectedHUIds(Select.ONLY_TOPLEVEL); if (huIds.isEmpty()) { - return ProcessPreconditionsResolution.reject(msgBL.getTranslatableMsgText(MSG_NoSelectedHU)); + return ProcessPreconditionsResolution.reject(msgBL.getTranslatableMsgText(WEBUI_M_HU_Messages.MSG_WEBUI_ONLY_TOP_LEVEL_HU)); } return ProcessPreconditionsResolution.accept(); @@ -66,11 +60,12 @@ protected ProcessPreconditionsResolution checkPreconditionsApplicable() @Override protected String doIt() throws Exception { - husToReturn = getSelectedHUs() - .stream() - .filter(handlingUnitsBL::isTopLevel) // only top level HUs - .collect(ImmutableList.toImmutableList()); - + husToReturn = getSelectedHUs(Select.ONLY_TOPLEVEL); + if (husToReturn.isEmpty()) + { + throw new AdempiereException("@NoSelection@"); + } + final Timestamp movementDate = Env.getDate(getCtx()); Services.get(IHUInOutBL.class).createVendorReturnInOutForHUs(husToReturn, movementDate); return MSG_OK;