2 * Copyright (C) 2016 Roland Haeder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation, either version 3 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
14 * You should have received a copy of the GNU Affero General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.jcountry.data;
19 import java.text.MessageFormat;
20 import java.util.GregorianCalendar;
21 import java.util.List;
22 import javax.ejb.Singleton;
23 import javax.ejb.Startup;
24 import javax.persistence.NoResultException;
25 import javax.persistence.Query;
26 import org.mxchange.jcoreee.database.BaseDatabaseBean;
27 import org.mxchange.jcountry.exceptions.CountryAlreadyAddedException;
30 * A singleton EJB for country informations
32 * @author Roland Haeder<roland@mxchange.org>
35 @Singleton (name = "country", description = "A singleton session bean for country informations")
36 public class AddressbookCountrySingletonBean extends BaseDatabaseBean implements CountrySingletonBeanRemote {
41 private static final long serialVersionUID = 15_846_983_298_691_207L;
44 public Country addCountry (final Country country) throws CountryAlreadyAddedException {
46 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addCountry: country={1} - CALLED!", this.getClass().getSimpleName(), country)); //NOI18N
48 // Is it already there?
49 if (null == country) {
51 throw new NullPointerException("country is null"); //NOI18N
52 } else if (country.getCountryCode().isEmpty()) {
54 throw new IllegalArgumentException("country.countryCode is empty"); //NOI18N
55 } else if (country.getCountryI18nkey().isEmpty()) {
56 // I18n key is not set
57 throw new IllegalArgumentException("country.countryI18nKey is empty"); //NOI18N
58 } else if (country.getCountryId() != null) {
60 throw new IllegalArgumentException(MessageFormat.format("country.countryId is not null ({0})", country.getCountryId())); //NOI18N
61 } else if (this.isCountryAdded(country)) {
62 // Yes, then abort here
63 throw new CountryAlreadyAddedException(country);
67 country.setCountryEntryCreated(new GregorianCalendar());
69 // It is not added, so persist it
70 this.getEntityManager().persist(country);
72 // Flush it to get id number back, maybe it is directly needed?
73 this.getEntityManager().flush();
76 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addCountry: country={1} - EXIT!", this.getClass().getSimpleName(), country)); //NOI18N
78 // Return updated instance
83 @SuppressWarnings ("unchecked")
84 public List<Country> allCountries () {
86 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allCountries: CALLED!", this.getClass().getSimpleName())); //NOI18N
89 Query query = this.getEntityManager().createNamedQuery("AllCountries", CountryData.class); //NOI18N
92 List<Country> countries = query.getResultList();
95 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allCountries: countries.size()={1} - EXIT!", this.getClass().getSimpleName(), countries.size())); //NOI18N
102 * Checks whether given country is already added by i18n key or country
103 * code, what comes first.
105 * @param country Country instance to check
107 * @return Whether the country was found
109 private boolean isCountryAdded (final Country country) {
110 if (null == country) {
112 throw new NullPointerException("country is null"); //NOI18N
113 } else if (country.getCountryCode().isEmpty()) {
115 throw new IllegalArgumentException("country.countryCode is empty"); //NOI18N
116 } else if (country.getCountryI18nkey().isEmpty()) {
117 // I18n key is not set
118 throw new IllegalArgumentException("country.countryI18nKey is empty"); //NOI18N
119 } else if (country.getCountryId() != null) {
121 throw new IllegalArgumentException(MessageFormat.format("country.countryId is not null ({0})", country.getCountryId())); //NOI18N
124 // Default is not found
125 boolean isAdded = false;
127 // Get query instance
128 Query query = this.getEntityManager().createNamedQuery("SearchCountryByCodeI18nKey", CountryData.class); //NOI18N
130 // Assign all parameters
131 query.setParameter("code", country.getCountryCode()); //NOI18N
132 query.setParameter("key", country.getCountryI18nkey()); //NOI18N
134 // Try to get a single result
137 Country foundCountry = (Country) query.getSingleResult();
140 isAdded = (foundCountry instanceof Country);
141 } catch (final NoResultException ex) {
142 // Not found, don't log this