]> git.mxchange.org Git - addressbook-ejb.git/commitdiff
Continued a bit:
authorRoland Haeder <roland@mxchange.org>
Mon, 11 Apr 2016 14:59:17 +0000 (16:59 +0200)
committerRoland Haeder <roland@mxchange.org>
Mon, 11 Apr 2016 17:52:45 +0000 (19:52 +0200)
- implemented business method addCountry()
- updated jar(s)

lib/jcountry-core.jar
lib/jcountry-lib.jar
src/java/org/mxchange/jcountry/data/AddressbookCountrySingletonBean.java

index 38df2036331e7e4b7a90686ddb8d425f98de1623..3b26377fa896aaca3df2df69ef7b74c4533e677a 100644 (file)
Binary files a/lib/jcountry-core.jar and b/lib/jcountry-core.jar differ
index a6ce084a8bc9febc8e0a4d5a09f42b7d5302b73e..6db466e10ab2585c09d819ebe2e10bcbc7039f99 100644 (file)
Binary files a/lib/jcountry-lib.jar and b/lib/jcountry-lib.jar differ
index 3646dce02394b4ac6a9cbcea8f963c696304fa13..fbcdb44c42b7895b7bbd6550dd439f0faef3dae2 100644 (file)
 */
 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 AddressbookCountrySingletonBean extends BaseDatabaseBean implements
         */
        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<Country> allCountries () {
@@ -46,4 +80,52 @@ public class AddressbookCountrySingletonBean extends BaseDatabaseBean implements
                return query.getResultList();
        }
 
+       /**
+        * Checks whether given country is already added by i18n key or country
+        * code, what comes first.
+        * <p>
+        * @param country Country instance to check
+        * <p>
+        * @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;
+       }
+
 }