From afb3d823db72ab0f2ade9119494739b5c21fc394 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Wed, 1 Apr 2020 19:29:24 +0200 Subject: [PATCH] Please cherry-pick: - no need for SearchCountryByCodeI18nKey anymore when you can use AllCountries plus Objects.equals() MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../AddressbookAdminCountrySingletonBean.java | 36 +++++++++---------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/src/java/org/mxchange/jcountry/model/data/AddressbookAdminCountrySingletonBean.java b/src/java/org/mxchange/jcountry/model/data/AddressbookAdminCountrySingletonBean.java index 484605e..aba5a02 100644 --- a/src/java/org/mxchange/jcountry/model/data/AddressbookAdminCountrySingletonBean.java +++ b/src/java/org/mxchange/jcountry/model/data/AddressbookAdminCountrySingletonBean.java @@ -13,14 +13,14 @@ * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . -*/ + */ package org.mxchange.jcountry.model.data; import java.text.MessageFormat; import java.util.Date; +import java.util.Objects; +import javax.ejb.EJB; import javax.ejb.Stateless; -import javax.persistence.NoResultException; -import javax.persistence.Query; import org.mxchange.jcountry.exceptions.CountryAlreadyAddedException; import org.mxchange.addressbook.enterprise.BaseAddressbookEnterpriseBean; @@ -37,6 +37,12 @@ public class AddressbookAdminCountrySingletonBean extends BaseAddressbookEnterpr */ private static final long serialVersionUID = 15_846_983_298_691_208L; + /** + * Remote country EJB + */ + @EJB (lookup = "java:global/jfinancials-ejb/country!org.mxchange.jcountry.model.data.CountrySingletonBeanRemote") + private CountrySingletonBeanRemote countryBean; + /** * Default constructor */ @@ -107,22 +113,14 @@ public class AddressbookAdminCountrySingletonBean extends BaseAddressbookEnterpr // Default is not found boolean isAdded = false; - // Get query instance - final Query query = this.getEntityManager().createNamedQuery("SearchCountryByCodeI18nKey", CountryData.class); //NOI18N - - // Assign all parameters - query.setParameter("code", country.getCountryCode()); //NOI18N - query.setParameter("key", country.getCountryI18nKey()); //NOI18N - - // Try to get a single result - try { - // Get single result - final Country foundCountry = (Country) query.getSingleResult(); - - // Found it? - isAdded = (foundCountry instanceof Country); - } catch (final NoResultException ex) { - // Not found, don't log this + // Try to match code/i18n key (should be both unique!) + for (final Country currentCountry : this.countryBean.allCountries()) { + // Is it matching + if (Objects.equals(country, currentCountry)) { + // Yes, then set flag and abort loop + isAdded = true; + break; + } } // Return result -- 2.39.5