/*
- * Copyright (C) 2015 Roland Haeder
+ * Copyright (C) 2016 Roland Haeder
*
* 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 3 of the License, or
- * (at your option) any later version.
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 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.
+ * GNU Affero General Public License for more details.
*
- * You should have received a copy of the GNU General Public License
+ * You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
+*/
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
+ * A singleton EJB for country informations
* <p>
- * @author Roland Haeder
+ * @author Roland Haeder<roland@mxchange.org>
*/
@Startup
-@Singleton (name = "country", mappedName = "ejb/addressbook-singleton-country", description = "A singleton session bean for country informations")
-public class AddressbookCountrySingletonBean extends BaseDatabaseBean implements AddressbookCountrySingletonBeanRemote {
+@Singleton (name = "country", description = "A singleton session bean for country informations")
+public class AddressbookCountrySingletonBean extends BaseDatabaseBean implements CountrySingletonBeanRemote {
/**
* Serial number
*/
private static final long serialVersionUID = 15_846_983_298_691_207L;
+ @Override
+ public Country addCountry (final Country country) throws CountryAlreadyAddedException {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addCountry: country={1} - CALLED!", this.getClass().getSimpleName(), country)); //NOI18N
+
+ // 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();
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addCountry: country={1} - EXIT!", this.getClass().getSimpleName(), country)); //NOI18N
+
+ // Return updated instance
+ return country;
+ }
+
@Override
@SuppressWarnings ("unchecked")
public List<Country> allCountries () {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allCountries: CALLED!", this.getClass().getSimpleName())); //NOI18N
+
// Init query
- Query query = this.getEntityManager().createNamedQuery("AllCountries", List.class); //NOI18N
+ Query query = this.getEntityManager().createNamedQuery("AllCountries", CountryData.class); //NOI18N
+
+ // Get list
+ List<Country> countries = query.getResultList();
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allCountries: countries.size()={1} - EXIT!", this.getClass().getSimpleName(), countries.size())); //NOI18N
// Return it
- return query.getResultList();
+ return countries;
+ }
+
+ /**
+ * 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;
}
}