]> git.mxchange.org Git - addressbook-ejb.git/blob - src/java/org/mxchange/jcountry/model/data/AddressbookAdminCountrySingletonBean.java
Please cherry-pick:
[addressbook-ejb.git] / src / java / org / mxchange / jcountry / model / data / AddressbookAdminCountrySingletonBean.java
1 /*
2  * Copyright (C) 2016 - 2018 Free Software Foundation
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jcountry.model.data;
18
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import java.util.Objects;
22 import javax.ejb.EJB;
23 import javax.ejb.Stateless;
24 import org.mxchange.jcountry.exceptions.CountryAlreadyAddedException;
25 import org.mxchange.addressbook.enterprise.BaseAddressbookEnterpriseBean;
26
27 /**
28  * A stateless EJB for administrative country purposes
29  * <p>
30  * @author Roland Häder<roland@mxchange.org>
31  */
32 @Stateless (name = "adminCountry", description = "A stateless session-scoped bean for administrative country purposes")
33 public class AddressbookAdminCountrySingletonBean extends BaseAddressbookEnterpriseBean implements AdminCountrySessionBeanRemote {
34
35         /**
36          * Serial number
37          */
38         private static final long serialVersionUID = 15_846_983_298_691_208L;
39
40         /**
41          * Remote country EJB
42          */
43         @EJB (lookup = "java:global/addressbook-ejb/country!org.mxchange.jcountry.model.data.CountrySingletonBeanRemote")
44         private CountrySingletonBeanRemote countryBean;
45
46         /**
47          * Default constructor
48          */
49         public AddressbookAdminCountrySingletonBean () {
50                 // Call super constructor
51                 super();
52         }
53
54         @Override
55         public Country addCountry (final Country country) throws CountryAlreadyAddedException {
56                 // Trace message
57                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addCountry: country={1} - CALLED!", this.getClass().getSimpleName(), country)); //NOI18N
58
59                 // Is it already there?
60                 if (null == country) {
61                         // Throw NPE
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()) {
67                         // Code is not set
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) {
76                         // Should be 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);
81                 }
82
83                 // Add timestamp
84                 country.setCountryEntryCreated(new Date());
85
86                 // It is not added, so persist it
87                 this.getEntityManager().persist(country);
88
89                 // Trace message
90                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addCountry: country={1} - EXIT!", this.getClass().getSimpleName(), country)); //NOI18N
91
92                 // Return updated instance
93                 return country;
94         }
95
96         /**
97          * Checks whether given country is already added by i18n key or country
98          * code, what comes first.
99          * <p>
100          * @param country Country instance to check
101          * <p>
102          * @return Whether the country was found
103          */
104         private boolean isCountryAdded (final Country country) {
105                 if (null == country) {
106                         // Throw NPE
107                         throw new NullPointerException("country is null"); //NOI18N
108                 } else if (country.getCountryCode().isEmpty()) {
109                         // Code is not set
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) {
115                         // Should be null
116                         throw new IllegalArgumentException(MessageFormat.format("country.countryId is not null ({0})", country.getCountryId())); //NOI18N
117                 }
118
119                 // Default is not found
120                 boolean isAdded = false;
121
122                 // Try to match code/i18n key (should be both unique!)
123                 for (final Country currentCountry : this.countryBean.fetchAllCountries()) {
124                         // Is it matching
125                         if (Objects.equals(country, currentCountry)) {
126                                 // Yes, then set flag and abort loop
127                                 isAdded = true;
128                                 break;
129                         }
130                 }
131
132                 // Return result
133                 return isAdded;
134         }
135
136 }