2 * Copyright (C) 2016 - 2018 Free Software Foundation
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 javax.ejb.Stateless;
22 import javax.persistence.NoResultException;
23 import javax.persistence.Query;
24 import org.mxchange.jcountry.exceptions.CountryAlreadyAddedException;
25 import org.mxchange.jjobs.enterprise.BaseJobsEnterpriseBean;
28 * A stateless EJB for administrative country purposes
30 * @author Roland Häder<roland@mxchange.org>
32 @Stateless (name = "adminCountry", description = "A stateless session-scoped bean for administrative country purposes")
33 public class JobsAdminCountrySingletonBean extends BaseJobsEnterpriseBean implements AdminCountrySessionBeanRemote {
38 private static final long serialVersionUID = 15_846_983_298_691_208L;
43 public JobsAdminCountrySingletonBean () {
44 // Call super constructor
49 public Country addCountry (final Country country) throws CountryAlreadyAddedException {
51 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addCountry: country={1} - CALLED!", this.getClass().getSimpleName(), country)); //NOI18N
53 // Is it already there?
54 if (null == country) {
56 throw new NullPointerException("country is null"); //NOI18N
57 } else if (country.getCountryCode().isEmpty()) {
59 throw new IllegalArgumentException("country.countryCode is empty"); //NOI18N
60 } else if (country.getCountryI18nKey().isEmpty()) {
61 // I18n key is not set
62 throw new IllegalArgumentException("country.countryI18nKey is empty"); //NOI18N
63 } else if (country.getCountryId() != null) {
65 throw new IllegalArgumentException(MessageFormat.format("country.countryId is not null ({0})", country.getCountryId())); //NOI18N
66 } else if (this.isCountryAdded(country)) {
67 // Yes, then abort here
68 throw new CountryAlreadyAddedException(country);
72 country.setCountryEntryCreated(new Date());
74 // It is not added, so persist it
75 this.getEntityManager().persist(country);
78 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addCountry: country={1} - EXIT!", this.getClass().getSimpleName(), country)); //NOI18N
80 // Return updated instance
85 * Checks whether given country is already added by i18n key or country
86 * code, what comes first.
88 * @param country Country instance to check
90 * @return Whether the country was found
92 private boolean isCountryAdded (final Country country) {
93 if (null == country) {
95 throw new NullPointerException("country is null"); //NOI18N
96 } else if (country.getCountryCode().isEmpty()) {
98 throw new IllegalArgumentException("country.countryCode is empty"); //NOI18N
99 } else if (country.getCountryI18nKey().isEmpty()) {
100 // I18n key is not set
101 throw new IllegalArgumentException("country.countryI18nKey is empty"); //NOI18N
102 } else if (country.getCountryId() != null) {
104 throw new IllegalArgumentException(MessageFormat.format("country.countryId is not null ({0})", country.getCountryId())); //NOI18N
107 // Default is not found
108 boolean isAdded = false;
110 // Get query instance
111 final Query query = this.getEntityManager().createNamedQuery("SearchCountryByCodeI18nKey", CountryData.class); //NOI18N
113 // Assign all parameters
114 query.setParameter("code", country.getCountryCode()); //NOI18N
115 query.setParameter("key", country.getCountryI18nKey()); //NOI18N
117 // Try to get a single result
120 final Country foundCountry = (Country) query.getSingleResult();
123 isAdded = (foundCountry instanceof Country);
124 } catch (final NoResultException ex) {
125 // Not found, don't log this