Skip to content

Commit

Permalink
address multiple codacy and codebeat issues
Browse files Browse the repository at this point in the history
528: Exception when opening PP Order issue / receipt 

Task-Url: #528
  • Loading branch information
metas-ts committed Aug 30, 2017
1 parent a74775c commit eedbe12
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.adempiere.exceptions.AdempiereException;
import org.adempiere.util.GuavaCollectors;
import org.adempiere.util.Services;
import org.adempiere.util.lang.MutableInt;
import org.adempiere.util.lang.impl.TableRecordReference;
import org.compiere.Adempiere;
import org.compiere.model.IQuery;
Expand All @@ -21,15 +20,12 @@
import de.metas.handlingunits.model.I_M_ReceiptSchedule;
import de.metas.handlingunits.receiptschedule.IHUReceiptScheduleBL;
import de.metas.process.IProcessPrecondition;
import de.metas.process.IProcessPreconditionsContext;
import de.metas.process.JavaProcess;
import de.metas.process.Param;
import de.metas.process.ProcessPreconditionsResolution;
import de.metas.process.RunOutOfTrx;
import de.metas.ui.web.WebRestApiApplication;
import de.metas.ui.web.handlingunits.HUEditorRow;
import de.metas.ui.web.handlingunits.HUEditorView;
import de.metas.ui.web.process.ViewAsPreconditionsContext;
import de.metas.ui.web.process.adprocess.ViewBasedProcessTemplate;
import de.metas.ui.web.view.IViewsRepository;
import de.metas.ui.web.view.ViewId;
Expand Down Expand Up @@ -57,56 +53,8 @@
* #L%
*/
@Profile(WebRestApiApplication.PROFILE_Webui)
public class WEBUI_M_HU_CreateMaterialReceipt extends JavaProcess implements IProcessPrecondition
public class WEBUI_M_HU_CreateMaterialReceipt extends WEBUI_M_HU_Receipt_Base implements IProcessPrecondition
{
@Override
public ProcessPreconditionsResolution checkPreconditionsApplicable(final IProcessPreconditionsContext context)
{
final ViewAsPreconditionsContext viewContext = ViewAsPreconditionsContext.castOrNull(context);
if (viewContext == null)
{
return ProcessPreconditionsResolution.rejectWithInternalReason("webui view not available");
}

if (viewContext.isNoSelection())
{
return ProcessPreconditionsResolution.rejectBecauseNoSelection();
}

final MutableInt checkedDocumentsCount = new MutableInt(0);
final ProcessPreconditionsResolution firstRejection = viewContext.getView(HUEditorView.class)
.streamByIds(viewContext.getSelectedDocumentIds())
.filter(document -> document.isPureHU())
//
.peek(document -> checkedDocumentsCount.incrementAndGet()) // count checked documents
.map(document -> rejectResolutionOrNull(document)) // create reject resolution if any
.filter(resolution -> resolution != null) // filter out those which are not errors
.findFirst()
.orElse(null);
if (firstRejection != null)
{
// found a record which is not eligible => don't run the process
return firstRejection;
}
if (checkedDocumentsCount.getValue() <= 0)
{
return ProcessPreconditionsResolution.rejectWithInternalReason("no eligible rows");
}

// Safe to run the process
return ProcessPreconditionsResolution.accept();
}

private static final ProcessPreconditionsResolution rejectResolutionOrNull(final HUEditorRow document)
{
if (!document.isHUStatusPlanning())
{
return ProcessPreconditionsResolution.reject("Only planning HUs can be received"); // TODO: trl
}

return null;
}

@Autowired
private IViewsRepository viewsRepo;
@Autowired
Expand All @@ -120,10 +68,22 @@ private static final ProcessPreconditionsResolution rejectResolutionOrNull(final

public WEBUI_M_HU_CreateMaterialReceipt()
{
super();
Adempiere.autowire(this);
}

/**
* Only allows rows whose HU is in the "planning" status.
*/
@Override
final ProcessPreconditionsResolution rejectResolutionOrNull(final HUEditorRow document)
{
if (!document.isHUStatusPlanning())
{
return ProcessPreconditionsResolution.reject("Only planning HUs can be received"); // TODO: trl
}
return null;
}

@Override
@RunOutOfTrx
protected String doIt() throws Exception
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package de.metas.ui.web.handlingunits.process;

import org.adempiere.util.lang.MutableInt;

/*
* #%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%
*/

import de.metas.process.IProcessPrecondition;
import de.metas.process.IProcessPreconditionsContext;
import de.metas.process.JavaProcess;
import de.metas.process.ProcessPreconditionsResolution;
import de.metas.ui.web.handlingunits.HUEditorRow;
import de.metas.ui.web.handlingunits.HUEditorView;
import de.metas.ui.web.process.ViewAsPreconditionsContext;

/**
* Common base class to dedupliate code.
*
* @author metas-dev <dev@metasfresh.com>
*
*/
public abstract class WEBUI_M_HU_Receipt_Base extends JavaProcess implements IProcessPrecondition
{
@Override
public ProcessPreconditionsResolution checkPreconditionsApplicable(final IProcessPreconditionsContext context)
{
final ViewAsPreconditionsContext viewContext = ViewAsPreconditionsContext.castOrNull(context);
if (viewContext == null)
{
return ProcessPreconditionsResolution.rejectWithInternalReason("webui view not available");
}

final boolean isHUView = viewContext.getView() instanceof HUEditorView;
if (!isHUView)
{
return ProcessPreconditionsResolution.rejectWithInternalReason("The current view is not an HUEditorView; view=" + viewContext.getView() + ";");
}

if (viewContext.isNoSelection())
{
return ProcessPreconditionsResolution.rejectBecauseNoSelection();
}

final MutableInt checkedDocumentsCount = new MutableInt(0);
final ProcessPreconditionsResolution firstRejection = viewContext.getView(HUEditorView.class)
.streamByIds(viewContext.getSelectedDocumentIds())
.filter(document -> document.isPureHU())
//
.peek(document -> checkedDocumentsCount.incrementAndGet()) // count checked documents
.map(document -> rejectResolutionOrNull(document)) // create reject resolution if any
.filter(resolution -> resolution != null) // filter out those which are not errors
.findFirst()
.orElse(null);
if (firstRejection != null)
{
// found a record which is not eligible => don't run the process
return firstRejection;
}
if (checkedDocumentsCount.getValue() <= 0)
{
return ProcessPreconditionsResolution.rejectWithInternalReason("no eligible rows");
}

return ProcessPreconditionsResolution.accept();
}

/**
* Check the individual given row, to find out if this process can be applied to it or not.
*
* @param document
* @return
*/
abstract ProcessPreconditionsResolution rejectResolutionOrNull(HUEditorRow document);

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.adempiere.exceptions.AdempiereException;
import org.adempiere.util.GuavaCollectors;
import org.adempiere.util.Services;
import org.adempiere.util.lang.MutableInt;
import org.adempiere.util.lang.impl.TableRecordReference;
import org.compiere.Adempiere;
import org.compiere.model.I_M_InOut;
Expand All @@ -19,15 +18,12 @@
import de.metas.handlingunits.model.I_M_HU;
import de.metas.handlingunits.model.I_M_ReceiptSchedule;
import de.metas.process.IProcessPrecondition;
import de.metas.process.IProcessPreconditionsContext;
import de.metas.process.JavaProcess;
import de.metas.process.Param;
import de.metas.process.ProcessPreconditionsResolution;
import de.metas.process.RunOutOfTrx;
import de.metas.ui.web.WebRestApiApplication;
import de.metas.ui.web.handlingunits.HUEditorRow;
import de.metas.ui.web.handlingunits.HUEditorView;
import de.metas.ui.web.process.ViewAsPreconditionsContext;
import de.metas.ui.web.process.adprocess.ViewBasedProcessTemplate;
import de.metas.ui.web.view.IViewsRepository;
import de.metas.ui.web.view.ViewId;
Expand Down Expand Up @@ -62,61 +58,8 @@
*
*/
@Profile(value = WebRestApiApplication.PROFILE_Webui)
public class WEBUI_M_HU_ReverseReceipt extends JavaProcess implements IProcessPrecondition
public class WEBUI_M_HU_ReverseReceipt extends WEBUI_M_HU_Receipt_Base implements IProcessPrecondition
{
@Override
public ProcessPreconditionsResolution checkPreconditionsApplicable(final IProcessPreconditionsContext context)
{
final ViewAsPreconditionsContext viewContext = ViewAsPreconditionsContext.castOrNull(context);
if (viewContext == null)
{
return ProcessPreconditionsResolution.rejectWithInternalReason("webui view not available");
}

final boolean isHUView = viewContext.getView() instanceof HUEditorView;
if (!isHUView)
{
return ProcessPreconditionsResolution.rejectWithInternalReason("The current view is not an HUEditorView; view=" + viewContext.getView() + ";");
}

if (viewContext.isNoSelection())
{
return ProcessPreconditionsResolution.rejectBecauseNoSelection();
}

final MutableInt checkedDocumentsCount = new MutableInt(0);
final ProcessPreconditionsResolution firstRejection = viewContext.getView(HUEditorView.class)
.streamByIds(viewContext.getSelectedDocumentIds())
.filter(document -> document.isPureHU())
//
.peek(document -> checkedDocumentsCount.incrementAndGet()) // count checked documents
.map(document -> rejectResolutionOrNull(document)) // create reject resolution if any
.filter(resolution -> resolution != null) // filter out those which are not errors
.findFirst()
.orElse(null);
if (firstRejection != null)
{
// found a record which is not eligible => don't run the process
return firstRejection;
}
if (checkedDocumentsCount.getValue() <= 0)
{
return ProcessPreconditionsResolution.rejectWithInternalReason("no eligible rows");
}

return ProcessPreconditionsResolution.accept();
}

private static final ProcessPreconditionsResolution rejectResolutionOrNull(final HUEditorRow document)
{
if (!document.isHUStatusActive())
{
return ProcessPreconditionsResolution.reject("Only active HUs can be reversed"); // TODO: trl
}

return null;
}

@Autowired
private IViewsRepository viewsRepo;
@Autowired
Expand All @@ -129,10 +72,21 @@ private static final ProcessPreconditionsResolution rejectResolutionOrNull(final

public WEBUI_M_HU_ReverseReceipt()
{
super();
Adempiere.autowire(this);
}

/**
* Only allows rows whose HUs are in the "active" status.
*/
final ProcessPreconditionsResolution rejectResolutionOrNull(final HUEditorRow document)
{
if (!document.isHUStatusActive())
{
return ProcessPreconditionsResolution.reject("Only active HUs can be reversed"); // TODO: trl
}
return null;
}

@Override
@RunOutOfTrx
protected String doIt() throws Exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public class PackingInfoProcessParams
@Param(parameterName = PARAM_M_HU_PI_Item_Product_ID)
private int tu_HU_PI_Item_Product_ID;
//
public static final String PARAM_PARAM_M_HU_PI_Item_ID = "M_HU_PI_Item_ID";
@Param(parameterName = PARAM_PARAM_M_HU_PI_Item_ID)
public static final String PARAM_M_HU_PI_Item_ID = "M_HU_PI_Item_ID";
@Param(parameterName = PARAM_M_HU_PI_Item_ID)
private int lu_PI_Item_ID;
//
public static final String PARAM_QtyCU = "QtyCU";
Expand Down Expand Up @@ -108,7 +108,7 @@ public Object getParameterDefaultValue(final String parameterName)
{
case PARAM_M_HU_PI_Item_Product_ID:
return defaultLUTUConfig.getM_HU_PI_Item_Product_ID();
case PARAM_PARAM_M_HU_PI_Item_ID:
case PARAM_M_HU_PI_Item_ID:
return defaultLUTUConfig.getM_LU_HU_PI_Item_ID();
case PARAM_QtyCU:
return defaultLUTUConfig.getQtyCU();
Expand Down Expand Up @@ -380,17 +380,34 @@ public I_M_HU_LUTU_Configuration createAndSaveNewLUTUConfig()
lutuConfigNew.setQtyCU(qtyCU);
lutuConfigNew.setIsInfiniteQtyCU(false);

//
// TU
configureLUTUConfigTU(lutuConfigNew, M_HU_PI_Item_Product_ID, qtyTU);

// LU
configureLUTUConfigLU(lutuConfigNew, lu_PI_Item_ID);

lutuConfigurationFactory.save(lutuConfigNew);
return lutuConfigNew;
}

private void configureLUTUConfigTU(
@NonNull final I_M_HU_LUTU_Configuration lutuConfigNew,
final int M_HU_PI_Item_Product_ID,
@NonNull final BigDecimal qtyTU)
{
final I_M_HU_PI_Item_Product tuPIItemProduct = InterfaceWrapperHelper.create(Env.getCtx(), M_HU_PI_Item_Product_ID, I_M_HU_PI_Item_Product.class, ITrx.TRXNAME_None);
final I_M_HU_PI tuPI = tuPIItemProduct.getM_HU_PI_Item().getM_HU_PI_Version().getM_HU_PI();

lutuConfigNew.setM_HU_PI_Item_Product(tuPIItemProduct);
lutuConfigNew.setM_TU_HU_PI(tuPI);
lutuConfigNew.setQtyTU(qtyTU);
lutuConfigNew.setIsInfiniteQtyTU(false);
}

//
// LU
private void configureLUTUConfigLU(
@NonNull final I_M_HU_LUTU_Configuration lutuConfigNew,
final int lu_PI_Item_ID)
{
if (lu_PI_Item_ID > 0)
{
final BigDecimal qtyLU = getQtyLU();
Expand All @@ -412,9 +429,6 @@ public I_M_HU_LUTU_Configuration createAndSaveNewLUTUConfig()
lutuConfigNew.setM_LU_HU_PI_Item(null);
lutuConfigNew.setQtyLU(BigDecimal.ZERO);
}

lutuConfigurationFactory.save(lutuConfigNew);
return lutuConfigNew;
}

public int getLuPiItemId()
Expand Down
Loading

0 comments on commit eedbe12

Please sign in to comment.