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/jjobs-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() == null) {
64 // Required field not set
65 throw new NullPointerException("country.countryCode is null"); //NOI18N
66 } else if (country.getCountryCode().isEmpty()) {
68 throw new IllegalArgumentException("country.countryCode is empty"); //NOI18N
69 } else if (country.getCountryI18nKey() == null) {
70 // Required field not set
71 throw new NullPointerException("country.countryI18nKey is null"); //NOI18N
72 } else if (country.getCountryI18nKey().isEmpty()) {
73 // I18n key is not set
74 throw new IllegalArgumentException("country.countryI18nKey is empty"); //NOI18N
75 } else if (country.getCountryId() != null) {
77 throw new IllegalArgumentException(MessageFormat.format("country.countryId is not null ({0})", country.getCountryId())); //NOI18N
78 } else if (this.isCountryAdded(country)) {
79 // Yes, then abort here
80 throw new CountryAlreadyAddedException(country);
84 country.setCountryEntryCreated(new Date());
86 // It is not added, so persist it
87 this.getEntityManager().persist(country);
90 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addCountry: country={1} - EXIT!", this.getClass().getSimpleName(), country)); //NOI18N
92 // Return updated instance
97 * Checks whether given country is already added by i18n key or country
98 * code, what comes first.
100 * @param country Country instance to check
102 * @return Whether the country was found
104 private boolean isCountryAdded (final Country country) {
105 if (null == country) {
107 throw new NullPointerException("country is null"); //NOI18N
108 } else if (country.getCountryCode().isEmpty()) {
110 throw new IllegalArgumentException("country.countryCode is empty"); //NOI18N
111 } else if (country.getCountryI18nKey().isEmpty()) {
112 // I18n key is not set
113 throw new IllegalArgumentException("country.countryI18nKey is empty"); //NOI18N
114 } else if (country.getCountryId() != null) {
116 throw new IllegalArgumentException(MessageFormat.format("country.countryId is not null ({0})", country.getCountryId())); //NOI18N
119 // Default is not found
120 boolean isAdded = false;
122 // Try to match code/i18n key (should be both unique!)
123 for (final Country currentCountry : this.countryBean.fetchAllCountries()) {
125 if (Objects.equals(country, currentCountry)) {
126 // Yes, then set flag and abort loop