From 8cf73c04ac6018bcb2b48a3190d298df6a84a234 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Mon, 11 Apr 2016 16:59:17 +0200 Subject: [PATCH] Continued a bit: - implemented business method addCountry() - updated jar(s) MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- lib/jcountry-lib.jar | Bin 0 -> 1446 bytes .../data/JobsCountrySingletonBean.java | 82 ++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 lib/jcountry-lib.jar diff --git a/lib/jcountry-lib.jar b/lib/jcountry-lib.jar new file mode 100644 index 0000000000000000000000000000000000000000..a6ce084a8bc9febc8e0a4d5a09f42b7d5302b73e GIT binary patch literal 1446 zcma)5%Wl&^6ur}=bz^D>Rm1`oWJdx@96*Ia0LhIKQRUHyRNbP9J){%4c4RxKVOI&k zf`4GuEfQ>5@Bx*OSRt`1AAnf0tGHtu=Rsn)*4WqMbM9k&PrIH-$^feX-(S9Y3=@`u zBs8nLT4B3YQ_oHS+7a{=q1xMbuLcMgD}yV7HnrAvt=icwHfv34@n_8S3VY0PxozFg z-Yk|BS+gc~v;o!kSr+&3Fmzs#6?t6Yx0z;=SfTCo)WrxTL>wII`_$^O80e*0XkWL7mgkIOI9C>O z4C>Jt;;*kCu74o_VwK~Gl^}D6TRoF`wzb8m^@JVR9xLi5b={M*kDoBYZCID!yaX2{ zNE48$VT42i*K!SUGEv`AGk#$qteoFNTJ=z81994}1Q!XcQPT{*bLJ9Q&*d9^`kbmm zkDF?PyIz?6F0^@a{3u=y9@!T+Rwgkj{4J#=(d<0i(r8WuILX;A=yjemDEhfE;t z*KgwgS9iL7rh5c3{uZ~@4s$p)`3u?=H9VZ3fkh{zhDB;7jO2Jt zg0SPG8+ebX6MCLZK2Kpj-B11*-9T^SoU;s}xWGiYKhd2!)3{_pfq@C?Wt7PjH5%c# t7($Wb7_EgdLVZs6F{{u&p^lGU#|MfTU0<*5`m&e;knlRiQ18Wrz;D?GXoUa( literal 0 HcmV?d00001 diff --git a/src/java/org/mxchange/jcountry/data/JobsCountrySingletonBean.java b/src/java/org/mxchange/jcountry/data/JobsCountrySingletonBean.java index 2e9c313..0dfc857 100644 --- a/src/java/org/mxchange/jcountry/data/JobsCountrySingletonBean.java +++ b/src/java/org/mxchange/jcountry/data/JobsCountrySingletonBean.java @@ -16,11 +16,15 @@ */ package org.mxchange.jcountry.data; +import java.text.MessageFormat; +import java.util.GregorianCalendar; import java.util.List; import javax.ejb.Singleton; import javax.ejb.Startup; +import javax.persistence.NoResultException; import javax.persistence.Query; import org.mxchange.jcoreee.database.BaseDatabaseBean; +import org.mxchange.jcountry.exceptions.CountryAlreadyAddedException; /** * A singleton bean for country informations @@ -36,6 +40,36 @@ public class JobsCountrySingletonBean extends BaseDatabaseBean implements Countr */ private static final long serialVersionUID = 15_846_983_298_691_207L; + @Override + public void addCountry (final Country country) throws CountryAlreadyAddedException { + // Is it already there? + if (null == country) { + // Throw NPE + throw new NullPointerException("country is null"); //NOI18N + } else if (country.getCountryCode().isEmpty()) { + // Code is not set + throw new IllegalArgumentException("country.countryCode is empty"); //NOI18N + } else if (country.getCountryI18nkey().isEmpty()) { + // I18n key is not set + throw new IllegalArgumentException("country.countryI18nKey is empty"); //NOI18N + } else if (country.getCountryId() != null) { + // Should be null + throw new IllegalArgumentException(MessageFormat.format("country.countryId is not null ({0})", country.getCountryId())); //NOI18N + } else if (this.isCountryAdded(country)) { + // Yes, then abort here + throw new CountryAlreadyAddedException(country); + } + + // Add timestamp + country.setCountryEntryCreated(new GregorianCalendar()); + + // It is not added, so persist it + this.getEntityManager().persist(country); + + // Flush it to get id number back, maybe it is directly needed? + this.getEntityManager().flush(); + } + @Override @SuppressWarnings ("unchecked") public List allCountries () { @@ -46,4 +80,52 @@ public class JobsCountrySingletonBean extends BaseDatabaseBean implements Countr return query.getResultList(); } + /** + * Checks whether given country is already added by i18n key or country + * code, what comes first. + *

+ * @param country Country instance to check + *

+ * @return Whether the country was found + */ + private boolean isCountryAdded (final Country country) { + if (null == country) { + // Throw NPE + throw new NullPointerException("country is null"); //NOI18N + } else if (country.getCountryCode().isEmpty()) { + // Code is not set + throw new IllegalArgumentException("country.countryCode is empty"); //NOI18N + } else if (country.getCountryI18nkey().isEmpty()) { + // I18n key is not set + throw new IllegalArgumentException("country.countryI18nKey is empty"); //NOI18N + } else if (country.getCountryId() != null) { + // Should be null + throw new IllegalArgumentException(MessageFormat.format("country.countryId is not null ({0})", country.getCountryId())); //NOI18N + } + + // Default is not found + boolean isAdded = false; + + // Get query instance + 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 + Country foundCountry = (Country) query.getSingleResult(); + + // Found it? + isAdded = (foundCountry instanceof Country); + } catch (final NoResultException ex) { + // Not found, don't log this + } + + // Return result + return isAdded; + } + } -- 2.39.5