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 bean for country informations
32 * @author Roland Haeder<roland@mxchange.org>
35 @Singleton (name = "country", mappedName = "ejb/addressbook-singleton-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 {
45 // Is it already there?
46 if (null == country) {
48 throw new NullPointerException("country is null"); //NOI18N
49 } else if (country.getCountryCode().isEmpty()) {
51 throw new IllegalArgumentException("country.countryCode is empty"); //NOI18N
52 } else if (country.getCountryI18nkey().isEmpty()) {
53 // I18n key is not set
54 throw new IllegalArgumentException("country.countryI18nKey is empty"); //NOI18N
55 } else if (country.getCountryId() != null) {
57 throw new IllegalArgumentException(MessageFormat.format("country.countryId is not null ({0})", country.getCountryId())); //NOI18N
58 } else if (this.isCountryAdded(country)) {
59 // Yes, then abort here
60 throw new CountryAlreadyAddedException(country);
64 country.setCountryEntryCreated(new GregorianCalendar());
66 // It is not added, so persist it
67 this.getEntityManager().persist(country);
69 // Flush it to get id number back, maybe it is directly needed?
70 this.getEntityManager().flush();
72 // Return updated instance
77 @SuppressWarnings ("unchecked")
78 public List<Country> allCountries () {
80 Query query = this.getEntityManager().createNamedQuery("AllCountries", List.class); //NOI18N
83 return query.getResultList();
87 * Checks whether given country is already added by i18n key or country
88 * code, what comes first.
90 * @param country Country instance to check
92 * @return Whether the country was found
94 private boolean isCountryAdded (final Country country) {
95 if (null == country) {
97 throw new NullPointerException("country is null"); //NOI18N
98 } else if (country.getCountryCode().isEmpty()) {
100 throw new IllegalArgumentException("country.countryCode is empty"); //NOI18N
101 } else if (country.getCountryI18nkey().isEmpty()) {
102 // I18n key is not set
103 throw new IllegalArgumentException("country.countryI18nKey is empty"); //NOI18N
104 } else if (country.getCountryId() != null) {
106 throw new IllegalArgumentException(MessageFormat.format("country.countryId is not null ({0})", country.getCountryId())); //NOI18N
109 // Default is not found
110 boolean isAdded = false;
112 // Get query instance
113 Query query = this.getEntityManager().createNamedQuery("SearchCountryByCodeI18nKey", CountryData.class); //NOI18N
115 // Assign all parameters
116 query.setParameter("code", country.getCountryCode()); //NOI18N
117 query.setParameter("key", country.getCountryI18nkey()); //NOI18N
119 // Try to get a single result
122 Country foundCountry = (Country) query.getSingleResult();
125 isAdded = (foundCountry instanceof Country);
126 } catch (final NoResultException ex) {
127 // Not found, don't log this