Skip to content

Commit

Permalink
#5231 Refactor
Browse files Browse the repository at this point in the history
#5231 Import Replenishments
  • Loading branch information
cristinamghita committed May 24, 2019
1 parent dea6696 commit f0316d6
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 76 deletions.
@@ -0,0 +1,116 @@
/**
*
*/
package de.metas.replenishment.impexp;

import org.adempiere.model.InterfaceWrapperHelper;
import org.compiere.model.I_I_Replenish;
import org.compiere.model.I_M_Replenish;

import de.metas.util.Check;
import lombok.NonNull;
import lombok.experimental.UtilityClass;

/*
* #%L
* de.metas.business
* %%
* Copyright (C) 2019 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%
*/

/**
* @author metas-dev <dev@metasfresh.com>
*
*/
@UtilityClass
/* package */ class ReplenishImportHelper
{
final public boolean isValidRecordForImport(@NonNull final I_I_Replenish importRecord)
{
if (importRecord.getM_Product_ID() <= 0)
{
return false;
}

if (importRecord.getM_Warehouse_ID() <= 0)
{
return false;
}

if (importRecord.getLevel_Max() == null)
{
return false;
}

if (importRecord.getLevel_Min() == null)
{
return false;
}

if (Check.isEmpty(importRecord.getReplenishType(), true))
{
return false;
}

return true;
}

final public I_M_Replenish createNewReplenish(@NonNull final I_I_Replenish importRecord)
{
final I_M_Replenish replenish = InterfaceWrapperHelper.newInstance(I_M_Replenish.class, importRecord);
setReplenishmenttValueFields(importRecord, replenish);
return replenish;
}

final public I_M_Replenish uppdateReplenish(@NonNull final I_I_Replenish importRecord)
{
final I_M_Replenish replenish = InterfaceWrapperHelper.newInstance(I_M_Replenish.class, importRecord);
setReplenishmenttValueFields(importRecord, replenish);
return replenish;
}

private void setReplenishmenttValueFields(@NonNull final I_I_Replenish importRecord, @NonNull final I_M_Replenish replenish)
{
// mandatory fields
replenish.setM_Product_ID(importRecord.getM_Product_ID());
replenish.setM_Warehouse_ID(importRecord.getM_Warehouse_ID());
replenish.setLevel_Max(importRecord.getLevel_Max());
replenish.setLevel_Min(importRecord.getLevel_Min());
replenish.setReplenishType(importRecord.getReplenishType());
replenish.setTimeToMarket(importRecord.getTimeToMarket());

// optional fields
if (importRecord.getM_WarehouseSource_ID() > 0)
{
replenish.setM_WarehouseSource_ID(importRecord.getM_WarehouseSource_ID());
}
if (importRecord.getM_Locator_ID() > 0)
{
replenish.setM_Locator_ID(importRecord.getM_Locator_ID());
}
if (importRecord.getC_Calendar_ID() > 0)
{
replenish.setC_Calendar_ID(importRecord.getC_Calendar_ID());
}
if (importRecord.getC_Period_ID() > 0)
{
replenish.setC_Period_ID(importRecord.getC_Period_ID());
}
}

}
Expand Up @@ -5,6 +5,7 @@
import java.util.Properties;

import org.adempiere.ad.trx.api.ITrx;
import org.adempiere.exceptions.AdempiereException;
import org.adempiere.impexp.AbstractImportProcess;
import org.adempiere.impexp.IImportInterceptor;
import org.adempiere.model.InterfaceWrapperHelper;
Expand All @@ -15,7 +16,6 @@
import org.compiere.model.ModelValidationEngine;
import org.compiere.model.X_I_Replenish;

import de.metas.util.Check;
import lombok.NonNull;

/*
Expand Down Expand Up @@ -87,43 +87,16 @@ protected ImportRecordResult importRecord(@NonNull IMutable<Object> state,
@NonNull final I_I_Replenish importRecord,
final boolean isInsertOnly)
{
if (isValidRecordForImport(importRecord))
if (ReplenishImportHelper.isValidRecordForImport(importRecord))
{

return importReplenish(importRecord);
}
return null;

}

private boolean isValidRecordForImport(@NonNull final I_I_Replenish importRecord)
{
if (importRecord.getM_Product_ID() <= 0)
{
return false;
}

if (importRecord.getM_Warehouse_ID() <= 0)
{
return false;
}

if (importRecord.getLevel_Max() == null)
{
return false;
}

if (importRecord.getLevel_Min() == null)
{
return false;
}

if (Check.isEmpty(importRecord.getReplenishType(), true))
else
{
return false;
throw new AdempiereException("ProductPriceImporter.InvalidProductPriceList");
}

return true;
}

private ImportRecordResult importReplenish(@NonNull final I_I_Replenish importRecord)
Expand All @@ -133,12 +106,12 @@ private ImportRecordResult importReplenish(@NonNull final I_I_Replenish importRe
final I_M_Replenish replenish;
if (importRecord.getM_Replenish_ID() <= 0)
{
replenish = createNewReplenish(importRecord);
replenish = ReplenishImportHelper.createNewReplenish(importRecord);
replenishImportResult = ImportRecordResult.Inserted;
}
else
{
replenish = uppdateReplenish(importRecord);
replenish = ReplenishImportHelper.uppdateReplenish(importRecord);
replenishImportResult = ImportRecordResult.Updated;
}

Expand All @@ -150,47 +123,4 @@ private ImportRecordResult importReplenish(@NonNull final I_I_Replenish importRe

return replenishImportResult;
}

private I_M_Replenish createNewReplenish(@NonNull final I_I_Replenish importRecord)
{
final I_M_Replenish replenish = InterfaceWrapperHelper.newInstance(I_M_Replenish.class, importRecord);
setReplenishmenttValueFields(importRecord, replenish);
return replenish;
}

private I_M_Replenish uppdateReplenish(@NonNull final I_I_Replenish importRecord)
{
final I_M_Replenish replenish = InterfaceWrapperHelper.newInstance(I_M_Replenish.class, importRecord);
setReplenishmenttValueFields(importRecord, replenish);
return replenish;
}

private void setReplenishmenttValueFields(@NonNull final I_I_Replenish importRecord, @NonNull final I_M_Replenish replenish)
{
// mandatory fields
replenish.setM_Product_ID(importRecord.getM_Product_ID());
replenish.setM_Warehouse_ID(importRecord.getM_Warehouse_ID());
replenish.setLevel_Max(importRecord.getLevel_Max());
replenish.setLevel_Min(importRecord.getLevel_Min());
replenish.setReplenishType(importRecord.getReplenishType());
replenish.setTimeToMarket(importRecord.getTimeToMarket());

// optional fields
if (importRecord.getM_WarehouseSource_ID() > 0)
{
replenish.setM_WarehouseSource_ID(importRecord.getM_WarehouseSource_ID());
}
if (importRecord.getM_Locator_ID() > 0)
{
replenish.setM_Locator_ID(importRecord.getM_Locator_ID());
}
if (importRecord.getC_Calendar_ID() > 0)
{
replenish.setC_Calendar_ID(importRecord.getC_Calendar_ID());
}
if (importRecord.getC_Period_ID() > 0)
{
replenish.setC_Period_ID(importRecord.getC_Period_ID());
}
}
}

0 comments on commit f0316d6

Please sign in to comment.