Skip to content

Commit

Permalink
Add methods to check if a BPartner has contact and the contact has em…
Browse files Browse the repository at this point in the history
…ail address

#6579
  • Loading branch information
TheBestPessimist committed May 6, 2020
1 parent ebba029 commit fce34d0
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 112 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package de.metas.bpartner.service;

import java.util.Collection;

/*
* #%L
* de.metas.adempiere.adempiere.base
* %%
* Copyright (C) 2015 metas GmbH
* Copyright (C) 2020 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
Expand All @@ -24,6 +20,10 @@
* #L%
*/

package de.metas.bpartner.service;

import java.util.Collection;

import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -148,7 +148,7 @@ public interface IBPartnerDAO extends ISingletonService

<T extends I_AD_User> T getContactById(BPartnerContactId contactId, Class<T> modelClass);

EMailAddress getContactEMail(BPartnerContactId contactId);
@NonNull EMailAddress getContactEMail(BPartnerContactId contactId);

PricingSystemId retrievePricingSystemIdOrNullInTrx(BPartnerId bPartnerId, SOTrx soTrx);

Expand Down Expand Up @@ -194,6 +194,8 @@ public interface IBPartnerDAO extends ISingletonService
*/
I_C_BPartner retrieveBPartnerByValueOrSuffix(Properties ctx, String bpValue, String bpValueSuffixToFallback);

boolean hasEmailAddress(@NonNull BPartnerContactId contactId);

<T extends org.compiere.model.I_AD_User> T retrieveDefaultContactOrNull(I_C_BPartner bPartner, Class<T> clazz);

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
package de.metas.bpartner.service.impl;

import static de.metas.util.Check.assumeNotNull;
import static de.metas.util.Check.isEmpty;
import static org.adempiere.model.InterfaceWrapperHelper.load;
import static org.adempiere.model.InterfaceWrapperHelper.loadByRepoIdAwaresOutOfTrx;
import static org.adempiere.model.InterfaceWrapperHelper.loadOutOfTrx;

import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/*
* #%L
* de.metas.adempiere.adempiere.base
* %%
* Copyright (C) 2015 metas GmbH
* Copyright (C) 2020 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
Expand All @@ -33,51 +20,11 @@
* #L%
*/

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.StringJoiner;
import java.util.stream.Stream;

import javax.annotation.Nullable;

import org.adempiere.ad.dao.ICompositeQueryFilter;
import org.adempiere.ad.dao.IQueryBL;
import org.adempiere.ad.dao.IQueryBuilder;
import org.adempiere.ad.dao.IQueryOrderBy;
import org.adempiere.ad.dao.IQueryOrderBy.Direction;
import org.adempiere.ad.dao.IQueryOrderBy.Nulls;
import org.adempiere.ad.trx.api.ITrx;
import org.adempiere.exceptions.AdempiereException;
import org.adempiere.exceptions.DBException;
import org.adempiere.model.InterfaceWrapperHelper;
import org.adempiere.service.ClientId;
import org.adempiere.util.proxy.Cached;
import org.compiere.model.IQuery;
import org.compiere.model.I_AD_Org;
import org.compiere.model.I_AD_User;
import org.compiere.model.I_C_BP_Group;
import org.compiere.model.I_C_BP_Relation;
import org.compiere.model.I_C_BPartner;
import org.compiere.model.I_C_BPartner_Location;
import org.compiere.model.I_C_Location;
import org.compiere.model.X_C_Location;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.slf4j.Logger;
package de.metas.bpartner.service.impl;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

import de.metas.bpartner.BPGroupId;
import de.metas.bpartner.BPartnerContactId;
import de.metas.bpartner.BPartnerId;
Expand Down Expand Up @@ -115,6 +62,55 @@
import de.metas.util.StringUtils;
import de.metas.util.lang.ExternalId;
import lombok.NonNull;
import org.adempiere.ad.dao.ICompositeQueryFilter;
import org.adempiere.ad.dao.IQueryBL;
import org.adempiere.ad.dao.IQueryBuilder;
import org.adempiere.ad.dao.IQueryOrderBy;
import org.adempiere.ad.dao.IQueryOrderBy.Direction;
import org.adempiere.ad.dao.IQueryOrderBy.Nulls;
import org.adempiere.ad.trx.api.ITrx;
import org.adempiere.exceptions.AdempiereException;
import org.adempiere.exceptions.DBException;
import org.adempiere.model.InterfaceWrapperHelper;
import org.adempiere.service.ClientId;
import org.adempiere.util.proxy.Cached;
import org.compiere.model.IQuery;
import org.compiere.model.I_AD_Org;
import org.compiere.model.I_AD_User;
import org.compiere.model.I_C_BP_Group;
import org.compiere.model.I_C_BP_Relation;
import org.compiere.model.I_C_BPartner;
import org.compiere.model.I_C_BPartner_Location;
import org.compiere.model.I_C_Location;
import org.compiere.model.X_C_Location;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.slf4j.Logger;

import javax.annotation.Nullable;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.StringJoiner;
import java.util.stream.Stream;

import static de.metas.util.Check.assumeNotNull;
import static de.metas.util.Check.isEmpty;
import static org.adempiere.model.InterfaceWrapperHelper.load;
import static org.adempiere.model.InterfaceWrapperHelper.loadByRepoIdAwaresOutOfTrx;
import static org.adempiere.model.InterfaceWrapperHelper.loadOutOfTrx;

public class BPartnerDAO implements IBPartnerDAO
{
Expand Down Expand Up @@ -272,6 +268,7 @@ public <T extends I_AD_User> T getContactById(BPartnerContactId contactId, Class
}

@Override
@NonNull
public EMailAddress getContactEMail(@NonNull final BPartnerContactId contactId)
{
final I_AD_User contact = getContactById(contactId);
Expand All @@ -288,6 +285,19 @@ public EMailAddress getContactEMail(@NonNull final BPartnerContactId contactId)
return contactEmail;
}

@Override
public boolean hasEmailAddress(@NonNull final BPartnerContactId contactId)
{
final I_AD_User contact = getContactById(contactId);
if (contact == null)
{
throw new AdempiereException("@NotFound@ " + contactId);
}

final EMailAddress contactEmail = EMailAddress.ofNullableString(contact.getEMail());
return null != contactEmail;
}

@Override
public <T extends org.compiere.model.I_AD_User> T retrieveDefaultContactOrNull(final I_C_BPartner bPartner, final Class<T> clazz)
{
Expand Down Expand Up @@ -619,14 +629,13 @@ public PricingSystemId retrievePricingSystemIdOrNullInTrx(@NonNull final BPartne
/**
* Returns the <code>M_PricingSystem_ID</code> to use for a given bPartner.
*
* @param bPartnerId the ID of the BPartner for which we need the pricing system id
* @param soTrx
* <ul>
* <li>if <code>true</code>, then the method first checks <code>C_BPartner.M_PricingSystem_ID</code> , then (if the BPartner has a C_BP_Group_ID) in
* <code>C_BP_Group.M_PricingSystem_ID</code> and finally (if the C_BPArtner has a AD_Org_ID>0) in <code>AD_OrgInfo.M_PricingSystem_ID</code></li>
* <li>if <code>false</code></li>, then the method first checks <code>C_BPartner.PO_PricingSystem_ID</code>, then (if the BPartner has a C_BP_Group_ID!) in
* <code>C_BP_Group.PO_PricingSystem_ID</code>. Note that <code>AD_OrgInfo</code> has currently no <code>PO_PricingSystem_ID</code> column.
* </ul>
* @param bpartnerId the ID of the BPartner for which we need the pricing system id
* @param soTrx <ul>
* <li>if <code>true</code>, then the method first checks <code>C_BPartner.M_PricingSystem_ID</code> , then (if the BPartner has a C_BP_Group_ID) in
* <code>C_BP_Group.M_PricingSystem_ID</code> and finally (if the C_BPArtner has a AD_Org_ID>0) in <code>AD_OrgInfo.M_PricingSystem_ID</code></li>
* <li>if <code>false</code></li>, then the method first checks <code>C_BPartner.PO_PricingSystem_ID</code>, then (if the BPartner has a C_BP_Group_ID!) in
* <code>C_BP_Group.PO_PricingSystem_ID</code>. Note that <code>AD_OrgInfo</code> has currently no <code>PO_PricingSystem_ID</code> column.
* </ul>
*/
private PricingSystemId retrievePricingSystemIdOrNull(
@NonNull final BPartnerId bpartnerId,
Expand Down Expand Up @@ -1225,8 +1234,8 @@ public Optional<BPartnerId> retrieveBPartnerIdBy(@NonNull final BPartnerQuery qu
if (existingBPartnerId == null && query.isFailIfNotExists())
{
final String msg = StringUtils.formatMessage("Found no existing BPartner;"
+ " Searched via the following properties one-after-one (list may be empty): {};"
+ " The search was restricted to the following orgIds (empty means no restriction): {}",
+ " Searched via the following properties one-after-one (list may be empty): {};"
+ " The search was restricted to the following orgIds (empty means no restriction): {}",
searchedByInfo.toString(),
query.getOnlyOrgIds().stream().map(OrgId::getRepoId).collect(ImmutableList.toImmutableList()).toString());
throw new BPartnerIdNotFoundException(msg);
Expand All @@ -1239,9 +1248,9 @@ public Optional<BPartnerId> retrieveBPartnerIdBy(@NonNull final BPartnerQuery qu
public ImmutableSet<BPartnerId> retrieveBPartnerIdsBy(@NonNull final BPartnerQuery query)
{
final IQueryBuilder<I_C_BPartner> queryBuilder = createQueryBuilder(I_C_BPartner.class)
// .addOnlyContextClient()
// .addOnlyActiveRecordsFilter() also load inactive records!
;
// .addOnlyContextClient()
// .addOnlyActiveRecordsFilter() also load inactive records!
;

if (!query.getOnlyOrgIds().isEmpty())
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
/*
* #%L
* de.metas.business
* %%
* Copyright (C) 2020 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%
*/

package de.metas.order;

import java.time.ZoneId;
import java.util.Properties;

import lombok.NonNull;
import org.compiere.model.I_AD_User;
import org.compiere.model.I_C_BPartner;
import org.compiere.model.I_C_DocType;
Expand Down Expand Up @@ -63,7 +86,7 @@ public interface IOrderBL extends ISingletonService

BPartnerLocationId getBillToLocationIdOrNull(I_C_Order order);

BPartnerContactId getBillToContactId(I_C_Order order);
@NonNull BPartnerContactId getBillToContactId(I_C_Order order);

/**
* Check if there is a price list for the given location and pricing system.
Expand Down Expand Up @@ -200,6 +223,8 @@ default void setDocTypeTargetIdAndUpdateDescription(I_C_Order order, int docType
*/
void reopenLine(I_C_OrderLine orderLine);

boolean hasBillToContactId(@NonNull I_C_Order order);

/**
* Update and save the <code>C_Order</code> fields
* <ul>
Expand Down

0 comments on commit fce34d0

Please sign in to comment.