From d2901c420650f526d8759636947dd238d41ac529 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) --- lib/jcountry-core.jar | Bin 6272 -> 9970 bytes lib/jcountry-lib.jar | Bin 1293 -> 1542 bytes .../data/PizzaCountrySingletonBean.java | 82 ++++++++++++++++++ 3 files changed, 82 insertions(+) diff --git a/lib/jcountry-core.jar b/lib/jcountry-core.jar index 4ecdf7ee64679ee99388e46e4ebc51c0694d3180..3b26377fa896aaca3df2df69ef7b74c4533e677a 100644 GIT binary patch literal 9970 zcmc&)Yj7LY6+SD=T3cB=ek2ZYLb7>4{D>`wkdQY~90x>V$Hd0qq+r(4##WFeBdvo? zN=taRlvhh>r)4_*L75Iir<9?D!tm&8+F^#i{Cv7xc?{^4W8RyKXsE;xM?cD|6#W%rpo`Uje#@vQGoeBMgV z+9vMd;okn;P0_);Z8>(T@5mC%@97_yy6&33bk?!+Su4{wJ+RB{I+?eU8QUDp<>zyG z%Yo_crf5^N9HFl*XbMp-KNG7g(Ih427L&79cE+wE+7T2zm&`3>o%~W2oplv;QkG*? zQM|IEqJ7@ZI)y5#n<}d=Chd84TTuP`|M+d=FYAc-*HuwnKpu44+@H)?g~IzEzxfWt zchWYUw(4}1PFHKxK4UvlYIHW2wFh%48+W_9Pn@&PTd@q>92=X><(>VwTTsca>RKk+ zJ)x1|8BU}$YAdsNBA1!=oV_RAHbED6iTC>CNlcFL!rNgUqh^L91;CN81 zYr#opVuM!3&Zew9D(mnnE7w>f>nK;?N+M{1l__uod}HS1^f^1}?C(CUQNyfNn00%v zuB$ttQFJV~kWbpj(ix0Oqd1&-Xlk^X$)ZFBrTOh2_!_l%!`Q7yqgCQoIu{$W^XT{V zz1DQbj%cJ&2tQ0JY71*%qk1BoBczLS12u|slvaqdL0}_1Z>E*v+#<`ZvfL)i?XtW| zmRHMi2dxo$SI}B` zjn+uMUOtc7d}F6H``4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index c0d42e1..5600e4e 100644 --- a/src/java/org/mxchange/jcountry/data/PizzaCountrySingletonBean.java +++ b/src/java/org/mxchange/jcountry/data/PizzaCountrySingletonBean.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 PizzaCountrySingletonBean extends BaseDatabaseBean implements Count */ 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 PizzaCountrySingletonBean extends BaseDatabaseBean implements Count 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