2 * Copyright (C) 2016, 2017 Roland Häder
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.model.data;
19 import java.text.MessageFormat;
20 import java.util.Date;
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.jcountry.exceptions.CountryAlreadyAddedException;
27 import org.mxchange.jjobs.database.BaseJobsDatabaseBean;
30 * A singleton EJB for country informations
32 * @author Roland Häder<roland@mxchange.org>
35 @Singleton (name = "country", description = "A singleton session-scoped bean for country informations")
36 public class JobsCountrySingletonBean extends BaseJobsDatabaseBean implements CountrySingletonBeanRemote {
41 private static final long serialVersionUID = 15_846_983_298_691_207L;
46 public JobsCountrySingletonBean () {
47 // Call super constructor
52 public Country addCountry (final Country country) throws CountryAlreadyAddedException {
54 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addCountry: country={1} - CALLED!", this.getClass().getSimpleName(), country)); //NOI18N
56 // Is it already there?
57 if (null == country) {
59 throw new NullPointerException("country is null"); //NOI18N
60 } else if (country.getCountryCode().isEmpty()) {
62 throw new IllegalArgumentException("country.countryCode is empty"); //NOI18N
63 } else if (country.getCountryI18nKey().isEmpty()) {
64 // I18n key is not set
65 throw new IllegalArgumentException("country.countryI18nKey is empty"); //NOI18N
66 } else if (country.getCountryId() != null) {
68 throw new IllegalArgumentException(MessageFormat.format("country.countryId is not null ({0})", country.getCountryId())); //NOI18N
69 } else if (this.isCountryAdded(country)) {
70 // Yes, then abort here
71 throw new CountryAlreadyAddedException(country);
75 country.setCountryEntryCreated(new Date());
77 // It is not added, so persist it
78 this.getEntityManager().persist(country);
81 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addCountry: country={1} - EXIT!", this.getClass().getSimpleName(), country)); //NOI18N
83 // Return updated instance
88 @SuppressWarnings ("unchecked")
89 public List<Country> allCountries () {
91 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allCountries: CALLED!", this.getClass().getSimpleName())); //NOI18N
94 final Query query = this.getEntityManager().createNamedQuery("AllCountries", CountryData.class); //NOI18N
97 final List<Country> countries = query.getResultList();
100 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allCountries: countries.size()={1} - EXIT!", this.getClass().getSimpleName(), countries.size())); //NOI18N
107 * Checks whether given country is already added by i18n key or country
108 * code, what comes first.
110 * @param country Country instance to check
112 * @return Whether the country was found
114 private boolean isCountryAdded (final Country country) {
115 if (null == country) {
117 throw new NullPointerException("country is null"); //NOI18N
118 } else if (country.getCountryCode().isEmpty()) {
120 throw new IllegalArgumentException("country.countryCode is empty"); //NOI18N
121 } else if (country.getCountryI18nKey().isEmpty()) {
122 // I18n key is not set
123 throw new IllegalArgumentException("country.countryI18nKey is empty"); //NOI18N
124 } else if (country.getCountryId() != null) {
126 throw new IllegalArgumentException(MessageFormat.format("country.countryId is not null ({0})", country.getCountryId())); //NOI18N
129 // Default is not found
130 boolean isAdded = false;
132 // Get query instance
133 final Query query = this.getEntityManager().createNamedQuery("SearchCountryByCodeI18nKey", CountryData.class); //NOI18N
135 // Assign all parameters
136 query.setParameter("code", country.getCountryCode()); //NOI18N
137 query.setParameter("key", country.getCountryI18nKey()); //NOI18N
139 // Try to get a single result
142 final Country foundCountry = (Country) query.getSingleResult();
145 isAdded = (foundCountry instanceof Country);
146 } catch (final NoResultException ex) {
147 // Not found, don't log this