Skip to content

Commit

Permalink
use ID objects
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed Jul 24, 2018
1 parent 38c6ba1 commit b5c1588
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 46 deletions.
Expand Up @@ -4,9 +4,11 @@
import java.math.RoundingMode;

import org.adempiere.exceptions.AdempiereException;
import org.adempiere.uom.UomId;
import org.adempiere.util.Check;

import de.metas.lang.Percent;
import de.metas.product.ProductId;
import lombok.Builder;
import lombok.Getter;
import lombok.NonNull;
Expand Down Expand Up @@ -47,9 +49,9 @@ public final class GroupCompensationLine
private final int seqNo;

@Getter
private final int productId;
private final ProductId productId;
@Getter
private final int uomId;
private final UomId uomId;

@Getter
private final GroupCompensationType type;
Expand All @@ -69,28 +71,25 @@ public final class GroupCompensationLine
private BigDecimal lineNetAmt;

@Getter
private int groupTemplateLineId;
private GroupTemplateLineId groupTemplateLineId;

@Builder
public GroupCompensationLine(
final int repoId,
final int seqNo,
final int productId,
final int uomId,
@NonNull final ProductId productId,
@NonNull final UomId uomId,
@NonNull final GroupCompensationType type,
@NonNull final GroupCompensationAmtType amtType,
final Percent percentage,
final BigDecimal baseAmt,
final BigDecimal qty,
final BigDecimal price,
final BigDecimal lineNetAmt,
final int groupTemplateLineId)
final GroupTemplateLineId groupTemplateLineId)
{
Check.assume(productId > 0, "productId > 0");
Check.assume(uomId > 0, "uomId > 0");

this.repoId = repoId > 0 ? repoId : -1;
this.groupTemplateLineId = groupTemplateLineId > 0 ? groupTemplateLineId : -1;
this.groupTemplateLineId = groupTemplateLineId;

this.seqNo = seqNo;

Expand Down
Expand Up @@ -2,7 +2,10 @@

import java.math.BigDecimal;

import org.adempiere.uom.UomId;

import de.metas.lang.Percent;
import de.metas.product.ProductId;
import lombok.Builder;
import lombok.NonNull;
import lombok.Value;
Expand Down Expand Up @@ -33,8 +36,8 @@
@Builder
public final class GroupCompensationLineCreateRequest
{
private final int productId;
private final int uomId;
private final ProductId productId;
private final UomId uomId;

@NonNull
private final GroupCompensationType type;
Expand All @@ -45,5 +48,5 @@ public final class GroupCompensationLineCreateRequest
private final BigDecimal qty;
private final BigDecimal price;

private final int groupTemplateLineId;
private final GroupTemplateLineId groupTemplateLineId;
}
@@ -1,9 +1,8 @@
package de.metas.order.compensationGroup;

import static org.adempiere.model.InterfaceWrapperHelper.load;

import java.math.BigDecimal;

import org.adempiere.uom.UomId;
import org.adempiere.util.Services;
import org.compiere.model.I_C_UOM;
import org.compiere.model.I_M_Product;
Expand All @@ -17,6 +16,8 @@
import de.metas.pricing.rules.Discount;
import de.metas.pricing.service.IPricingBL;
import de.metas.product.IProductBL;
import de.metas.product.IProductDAO;
import de.metas.product.ProductId;
import lombok.NonNull;

/*
Expand Down Expand Up @@ -48,8 +49,10 @@ public class GroupCompensationLineCreateRequestFactory
public GroupCompensationLineCreateRequest createGroupCompensationLineCreateRequest(@NonNull final GroupTemplateLine templateLine, @NonNull final Group group)
{
final IProductBL productBL = Services.get(IProductBL.class);
final IProductDAO productsRepo = Services.get(IProductDAO.class);

final I_M_Product product = load(templateLine.getProductId(), I_M_Product.class);
final ProductId productId = templateLine.getProductId();
final I_M_Product product = productsRepo.getById(productId);
final I_C_UOM uom = productBL.getStockingUOM(product);

final GroupCompensationType type = extractGroupCompensationType(product);
Expand All @@ -62,8 +65,8 @@ public GroupCompensationLineCreateRequest createGroupCompensationLineCreateReque
}

return GroupCompensationLineCreateRequest.builder()
.productId(product.getM_Product_ID())
.uomId(uom.getC_UOM_ID())
.productId(productId)
.uomId(UomId.ofRepoId(uom.getC_UOM_ID()))
.type(type)
.amtType(amtType)
.percentage(percentage)
Expand Down
Expand Up @@ -6,6 +6,7 @@

import com.google.common.collect.ImmutableList;

import de.metas.product.ProductCategoryId;
import lombok.Builder;
import lombok.NonNull;
import lombok.Singular;
Expand Down Expand Up @@ -44,23 +45,23 @@
@Value
public class GroupTemplate
{
int id;
GroupTemplateId id;
String name;
int productCategoryId;
ProductCategoryId productCategoryId;
List<GroupTemplateLine> lines;

@Builder
private GroupTemplate(
final int id,
final GroupTemplateId id,
@NonNull final String name,
final int productCategoryId,
final ProductCategoryId productCategoryId,
@Singular List<GroupTemplateLine> lines)
{
Check.assumeNotEmpty(lines, "lines is not empty");

this.id = id > 0 ? id : 0;
this.id = id;
this.name = name;
this.productCategoryId = productCategoryId > 0 ? productCategoryId : 0;
this.productCategoryId = productCategoryId;
this.lines = ImmutableList.copyOf(lines);
}
}
@@ -0,0 +1,54 @@
package de.metas.order.compensationGroup;

import org.adempiere.util.Check;

import lombok.Value;

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

@Value
public class GroupTemplateId
{
int repoId;

public static GroupTemplateId ofRepoId(final int repoId)
{
return new GroupTemplateId(repoId);
}

public static GroupTemplateId ofRepoIdOrNull(final int repoId)
{
return repoId > 0 ? new GroupTemplateId(repoId) : null;
}

public static int toRepoId(final GroupTemplateId groupTemplateId)
{
return groupTemplateId != null ? groupTemplateId.getRepoId() : -1;
}

private GroupTemplateId(final int repoId)
{
this.repoId = Check.assumeGreaterThanZero(repoId, "repoId");
}

}
Expand Up @@ -45,21 +45,19 @@ public static GroupTemplateLine ofProductId(final ProductId productId)
.build();
}

int id;
GroupTemplateLineId id;
ProductId productId;
private Percent percentage;
private Predicate<Group> groupMatcher;

@Builder
private GroupTemplateLine(
final int id,
@Nullable final GroupTemplateLineId id,
@NonNull final ProductId productId,
@Nullable final BigDecimal percentage,
@Nullable final Predicate<Group> groupMatcher)
{
// id is OK to be <= 0

this.id = id > 0 ? id : 0;
this.id = id;
this.productId = productId;
this.percentage = Percent.ofNullable(percentage);
this.groupMatcher = groupMatcher != null ? groupMatcher : Predicates.alwaysTrue();
Expand Down
@@ -0,0 +1,53 @@
package de.metas.order.compensationGroup;

import org.adempiere.util.Check;

import lombok.Value;

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

@Value
public class GroupTemplateLineId
{
int repoId;

public static GroupTemplateLineId ofRepoId(final int repoId)
{
return new GroupTemplateLineId(repoId);
}

public static GroupTemplateLineId ofRepoIdOrNull(final int repoId)
{
return repoId > 0 ? ofRepoId(repoId) : null;
}

public static int toRepoId(final GroupTemplateLineId groupSchemaLineId)
{
return groupSchemaLineId != null ? groupSchemaLineId.getRepoId() : -1;
}

private GroupTemplateLineId(final int repoId)
{
this.repoId = Check.assumeGreaterThanZero(repoId, "repoId");
}
}
Expand Up @@ -77,7 +77,7 @@ private GroupTemplate retrieveById(final int groupTemplateId)
.collect(ImmutableList.toImmutableList());

return GroupTemplate.builder()
.id(schemaPO.getC_CompensationGroup_Schema_ID())
.id(GroupTemplateId.ofRepoId(schemaPO.getC_CompensationGroup_Schema_ID()))
.name(schemaPO.getName())
.lines(lines)
.build();
Expand All @@ -99,7 +99,7 @@ private GroupTemplateLine toGroupTemplateLine(final I_C_CompensationGroup_Schema
{
final BigDecimal percentage = schemaLinePO.getCompleteOrderDiscount();
return GroupTemplateLine.builder()
.id(schemaLinePO.getC_CompensationGroup_SchemaLine_ID())
.id(GroupTemplateLineId.ofRepoIdOrNull(schemaLinePO.getC_CompensationGroup_SchemaLine_ID()))
.groupMatcher(createGroupMatcher(schemaLinePO, allSchemaLinePOs))
.productId(ProductId.ofRepoId(schemaLinePO.getM_Product_ID()))
.percentage(percentage != null && percentage.signum() != 0 ? percentage : null)
Expand Down
Expand Up @@ -96,6 +96,11 @@ public static boolean isGeneratedCompensationLine(final I_C_OrderLine orderLine)
return isGeneratedCompensationLine(orderLine.getC_CompensationGroup_SchemaLine_ID());
}

public static boolean isGeneratedCompensationLine(final GroupTemplateLineId groupSchemaLineId)
{
return isGeneratedCompensationLine(GroupTemplateLineId.toRepoId(groupSchemaLineId));
}

public static boolean isGeneratedCompensationLine(final int groupSchemaLineId)
{
return groupSchemaLineId > 0;
Expand Down
Expand Up @@ -20,6 +20,7 @@
import org.adempiere.ad.dao.IQueryBuilder;
import org.adempiere.exceptions.AdempiereException;
import org.adempiere.model.InterfaceWrapperHelper;
import org.adempiere.uom.UomId;
import org.adempiere.util.Check;
import org.adempiere.util.GuavaCollectors;
import org.adempiere.util.NumberUtils;
Expand All @@ -45,6 +46,7 @@
import de.metas.order.IOrderDAO;
import de.metas.order.IOrderLineBL;
import de.metas.order.compensationGroup.Group.GroupBuilder;
import de.metas.product.ProductId;
import lombok.Builder;
import lombok.Getter;
import lombok.NonNull;
Expand Down Expand Up @@ -308,10 +310,10 @@ private GroupCompensationLine createCompensationLine(final I_C_OrderLine groupOr
{
return GroupCompensationLine.builder()
.repoId(groupOrderLine.getC_OrderLine_ID())
.groupTemplateLineId(groupOrderLine.getC_CompensationGroup_SchemaLine_ID())
.groupTemplateLineId(GroupTemplateLineId.ofRepoIdOrNull(groupOrderLine.getC_CompensationGroup_SchemaLine_ID()))
.seqNo(groupOrderLine.getLine())
.productId(groupOrderLine.getM_Product_ID())
.uomId(groupOrderLine.getC_UOM_ID())
.productId(ProductId.ofRepoId(groupOrderLine.getM_Product_ID()))
.uomId(UomId.ofRepoId(groupOrderLine.getC_UOM_ID()))
.type(GroupCompensationType.ofAD_Ref_List_Value(groupOrderLine.getGroupCompensationType()))
.amtType(GroupCompensationAmtType.ofAD_Ref_List_Value(groupOrderLine.getGroupCompensationAmtType()))
.percentage(Percent.of(groupOrderLine.getGroupCompensationPercentage()))
Expand Down Expand Up @@ -379,16 +381,16 @@ private void updateOrderLineFromCompensationLine(final I_C_OrderLine compensatio
compensationLinePO.setGroupCompensationPercentage(compensationLine.getPercentage() != null ? compensationLine.getPercentage().getValueAsBigDecimal() : null);
compensationLinePO.setGroupCompensationBaseAmt(compensationLine.getBaseAmt());

compensationLinePO.setM_Product_ID(compensationLine.getProductId());
compensationLinePO.setC_UOM_ID(compensationLine.getUomId());
compensationLinePO.setM_Product_ID(compensationLine.getProductId().getRepoId());
compensationLinePO.setC_UOM_ID(compensationLine.getUomId().getRepoId());

compensationLinePO.setQtyEntered(compensationLine.getQty());
compensationLinePO.setQtyOrdered(compensationLine.getQty());
compensationLinePO.setIsManualPrice(true);
compensationLinePO.setPriceEntered(compensationLine.getPrice());
compensationLinePO.setPriceActual(compensationLine.getPrice());

compensationLinePO.setC_CompensationGroup_SchemaLine_ID(compensationLine.getGroupTemplateLineId());
compensationLinePO.setC_CompensationGroup_SchemaLine_ID(GroupTemplateLineId.toRepoId(compensationLine.getGroupTemplateLineId()));
}

@Override
Expand Down Expand Up @@ -478,13 +480,13 @@ private static final GroupId createNewGroupId(final int orderId, final GroupTemp
final I_C_Order_CompensationGroup groupPO = newInstance(I_C_Order_CompensationGroup.class);
groupPO.setC_Order_ID(orderId);
groupPO.setName(template.getName());
if (template.getProductCategoryId() > 0)
if (template.getProductCategoryId() != null)
{
groupPO.setM_Product_Category_ID(template.getProductCategoryId());
groupPO.setM_Product_Category_ID(template.getProductCategoryId().getRepoId());
}
if (template.getId() > 0)
if (template.getId() != null)
{
groupPO.setC_CompensationGroup_Schema_ID(template.getId());
groupPO.setC_CompensationGroup_Schema_ID(template.getId().getRepoId());
}
InterfaceWrapperHelper.save(groupPO);

Expand Down

0 comments on commit b5c1588

Please sign in to comment.