]> git.mxchange.org Git - addressbook-ejb.git/blob - src/java/org/mxchange/jcountry/model/data/AddressbookCountrySingletonBean.java
2d6761aee4e7a32f21aa83981eaacdf1fd25c0de
[addressbook-ejb.git] / src / java / org / mxchange / jcountry / model / data / AddressbookCountrySingletonBean.java
1 /*
2  * Copyright (C) 2016, 2020 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.GregorianCalendar;
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.addressbook.database.BaseAddressbookDatabaseBean;
27 import org.mxchange.jcountry.exceptions.CountryAlreadyAddedException;
28
29 /**
30  * A singleton EJB for country informations
31  * <p>
32  * @author Roland Häder<roland@mxchange.org>
33  */
34 @Startup
35 @Singleton (name = "country", description = "A singleton session-scoped bean for country informations")
36 public class AddressbookCountrySingletonBean extends BaseAddressbookDatabaseBean implements CountrySingletonBeanRemote {
37
38         /**
39          * Serial number
40          */
41         private static final long serialVersionUID = 15_846_983_298_691_207L;
42
43         /**
44          * Default constructor
45          */
46         public AddressbookCountrySingletonBean () {
47                 // Call super constructor
48                 super();
49         }
50
51         @Override
52         public Country addCountry (final Country country) throws CountryAlreadyAddedException {
53                 // Trace message
54                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addCountry: country={1} - CALLED!", this.getClass().getSimpleName(), country)); //NOI18N
55
56                 // Is it already there?
57                 if (null == country) {
58                         // Throw NPE
59                         throw new NullPointerException("country is null"); //NOI18N
60                 } else if (country.getCountryCode().isEmpty()) {
61                         // Code is not set
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) {
67                         // Should be 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);
72                 }
73
74                 // Add timestamp
75                 country.setCountryEntryCreated(new GregorianCalendar());
76
77                 // It is not added, so persist it
78                 this.getEntityManager().persist(country);
79
80                 // Trace message
81                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addCountry: country={1} - EXIT!", this.getClass().getSimpleName(), country)); //NOI18N
82
83                 // Return updated instance
84                 return country;
85         }
86
87         @Override
88         @SuppressWarnings ("unchecked")
89         public List<Country> allCountries () {
90                 // Trace message
91                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allCountries: CALLED!", this.getClass().getSimpleName())); //NOI18N
92
93                 // Init query
94                 final Query query = this.getEntityManager().createNamedQuery("AllCountries", CountryData.class); //NOI18N
95
96                 // Get list
97                 final List<Country> countries = query.getResultList();
98
99                 // Trace message
100                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allCountries: countries.size()={1} - EXIT!", this.getClass().getSimpleName(), countries.size())); //NOI18N
101
102                 // Return it
103                 return countries;
104         }
105
106         /**
107          * Checks whether given country is already added by i18n key or country
108          * code, what comes first.
109          * <p>
110          * @param country Country instance to check
111          * <p>
112          * @return Whether the country was found
113          */
114         private boolean isCountryAdded (final Country country) {
115                 if (null == country) {
116                         // Throw NPE
117                         throw new NullPointerException("country is null"); //NOI18N
118                 } else if (country.getCountryCode().isEmpty()) {
119                         // Code is not set
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) {
125                         // Should be null
126                         throw new IllegalArgumentException(MessageFormat.format("country.countryId is not null ({0})", country.getCountryId())); //NOI18N
127                 }
128
129                 // Default is not found
130                 boolean isAdded = false;
131
132                 // Get query instance
133                 final Query query = this.getEntityManager().createNamedQuery("SearchCountryByCodeI18nKey", CountryData.class); //NOI18N
134
135                 // Assign all parameters
136                 query.setParameter("code", country.getCountryCode()); //NOI18N
137                 query.setParameter("key", country.getCountryI18nKey()); //NOI18N
138
139                 // Try to get a single result
140                 try {
141                         // Get single result
142                         final Country foundCountry = (Country) query.getSingleResult();
143
144                         // Found it?
145                         isAdded = (foundCountry instanceof Country);
146                 } catch (final NoResultException ex) {
147                         // Not found, don't log this
148                 }
149
150                 // Return result
151                 return isAdded;
152         }
153
154 }