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 de.chotime.jratecalc.database.BasePizzaDatabaseBean;
20 import java.text.MessageFormat;
21 import java.util.GregorianCalendar;
22 import java.util.List;
23 import javax.ejb.Singleton;
24 import javax.ejb.Startup;
25 import javax.persistence.NoResultException;
26 import javax.persistence.Query;
27 import org.mxchange.jcountry.exceptions.CountryAlreadyAddedException;
28 import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean;
31 * A singleton EJB for country informations
33 * @author Roland Haeder<roland@mxchange.org>
36 @Singleton (name = "country", mappedName = "ejb/pizzaservice-singleton-country", description = "A singleton session bean for country informations")
37 public class PizzaCountrySingletonBean extends BasePizzaDatabaseBean implements CountrySingletonBeanRemote {
42 private static final long serialVersionUID = 15_846_983_298_691_207L;
45 public Country addCountry (final Country country) throws CountryAlreadyAddedException {
46 // Is it already there?
47 if (null == country) {
49 throw new NullPointerException("country is null"); //NOI18N
50 } else if (country.getCountryCode().isEmpty()) {
52 throw new IllegalArgumentException("country.countryCode is empty"); //NOI18N
53 } else if (country.getCountryI18nkey().isEmpty()) {
54 // I18n key is not set
55 throw new IllegalArgumentException("country.countryI18nKey is empty"); //NOI18N
56 } else if (country.getCountryId() != null) {
58 throw new IllegalArgumentException(MessageFormat.format("country.countryId is not null ({0})", country.getCountryId())); //NOI18N
59 } else if (this.isCountryAdded(country)) {
60 // Yes, then abort here
61 throw new CountryAlreadyAddedException(country);
65 country.setCountryEntryCreated(new GregorianCalendar());
67 // It is not added, so persist it
68 this.getEntityManager().persist(country);
70 // Flush it to get id number back, maybe it is directly needed?
71 this.getEntityManager().flush();
73 // Return updated instance
78 @SuppressWarnings ("unchecked")
79 public List<Country> allCountries () {
81 Query query = this.getEntityManager().createNamedQuery("AllCountries", List.class); //NOI18N
84 return query.getResultList();
88 * Checks whether given country is already added by i18n key or country
89 * code, what comes first.
91 * @param country Country instance to check
93 * @return Whether the country was found
95 private boolean isCountryAdded (final Country country) {
96 if (null == country) {
98 throw new NullPointerException("country is null"); //NOI18N
99 } else if (country.getCountryCode().isEmpty()) {
101 throw new IllegalArgumentException("country.countryCode is empty"); //NOI18N
102 } else if (country.getCountryI18nkey().isEmpty()) {
103 // I18n key is not set
104 throw new IllegalArgumentException("country.countryI18nKey is empty"); //NOI18N
105 } else if (country.getCountryId() != null) {
107 throw new IllegalArgumentException(MessageFormat.format("country.countryId is not null ({0})", country.getCountryId())); //NOI18N
110 // Default is not found
111 boolean isAdded = false;
113 // Get query instance
114 Query query = this.getEntityManager().createNamedQuery("SearchCountryByCodeI18nKey", Country.class); //NOI18N
116 // Assign all parameters
117 query.setParameter("code", country.getCountryCode()); //NOI18N
118 query.setParameter("key", country.getCountryI18nkey()); //NOI18N
120 // Try to get a single result
123 Country foundCountry = (Country) query.getSingleResult();
126 isAdded = (foundCountry instanceof Country);
127 } catch (final NoResultException ex) {
128 // Not found, don't log this