2 * Copyright (C) 2016 - 2020 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 java.util.Objects;
23 import javax.ejb.Stateless;
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 @EJB (lookup = "java:global/jfinancials-ejb/country!org.mxchange.jcountry.model.data.CountrySingletonBeanRemote")
44 private CountrySingletonBeanRemote countryBean;
49 public JobsAdminCountrySingletonBean () {
50 // Call super constructor
55 public Country addCountry (final Country country) throws CountryAlreadyAddedException {
57 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addCountry: country={1} - CALLED!", this.getClass().getSimpleName(), country)); //NOI18N
59 // Is it already there?
60 if (null == country) {
62 throw new NullPointerException("country is null"); //NOI18N
63 } else if (country.getCountryCode().isEmpty()) {
65 throw new IllegalArgumentException("country.countryCode is empty"); //NOI18N
66 } else if (country.getCountryI18nKey().isEmpty()) {
67 // I18n key is not set
68 throw new IllegalArgumentException("country.countryI18nKey is empty"); //NOI18N
69 } else if (country.getCountryId() != null) {
71 throw new IllegalArgumentException(MessageFormat.format("country.countryId is not null ({0})", country.getCountryId())); //NOI18N
72 } else if (this.isCountryAdded(country)) {
73 // Yes, then abort here
74 throw new CountryAlreadyAddedException(country);
78 country.setCountryEntryCreated(new Date());
80 // It is not added, so persist it
81 this.getEntityManager().persist(country);
84 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addCountry: country={1} - EXIT!", this.getClass().getSimpleName(), country)); //NOI18N
86 // Return updated instance
91 * Checks whether given country is already added by i18n key or country
92 * code, what comes first.
94 * @param country Country instance to check
96 * @return Whether the country was found
98 private boolean isCountryAdded (final Country country) {
99 if (null == country) {
101 throw new NullPointerException("country is null"); //NOI18N
102 } else if (country.getCountryCode().isEmpty()) {
104 throw new IllegalArgumentException("country.countryCode is empty"); //NOI18N
105 } else if (country.getCountryI18nKey().isEmpty()) {
106 // I18n key is not set
107 throw new IllegalArgumentException("country.countryI18nKey is empty"); //NOI18N
108 } else if (country.getCountryId() != null) {
110 throw new IllegalArgumentException(MessageFormat.format("country.countryId is not null ({0})", country.getCountryId())); //NOI18N
113 // Default is not found
114 boolean isAdded = false;
116 // Try to match code/i18n key (should be both unique!)
117 for (final Country currentCountry : this.countryBean.allCountries()) {
119 if (Objects.equals(country, currentCountry)) {
120 // Yes, then set flag and abort loop