]> git.mxchange.org Git - addressbook-mailer-ejb.git/blob - src/java/org/mxchange/jcountry/data/AddressbookCountrySingletonBean.java
8d1f5a47b409a28875b65d9aa92987d7ed9bc723
[addressbook-mailer-ejb.git] / src / java / org / mxchange / jcountry / data / AddressbookCountrySingletonBean.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
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.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                 // Flush it to get id number back, maybe it is directly needed?
81                 this.getEntityManager().flush();
82
83                 // Trace message
84                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addCountry: country={1} - EXIT!", this.getClass().getSimpleName(), country)); //NOI18N
85
86                 // Return updated instance
87                 return country;
88         }
89
90         @Override
91         @SuppressWarnings ("unchecked")
92         public List<Country> allCountries () {
93                 // Trace message
94                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allCountries: CALLED!", this.getClass().getSimpleName())); //NOI18N
95
96                 // Init query
97                 Query query = this.getEntityManager().createNamedQuery("AllCountries", CountryData.class); //NOI18N
98
99                 // Get list
100                 List<Country> countries = query.getResultList();
101
102                 // Trace message
103                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allCountries: countries.size()={1} - EXIT!", this.getClass().getSimpleName(), countries.size())); //NOI18N
104
105                 // Return it
106                 return countries;
107         }
108
109         /**
110          * Checks whether given country is already added by i18n key or country
111          * code, what comes first.
112          * <p>
113          * @param country Country instance to check
114          * <p>
115          * @return Whether the country was found
116          */
117         private boolean isCountryAdded (final Country country) {
118                 if (null == country) {
119                         // Throw NPE
120                         throw new NullPointerException("country is null"); //NOI18N
121                 } else if (country.getCountryCode().isEmpty()) {
122                         // Code is not set
123                         throw new IllegalArgumentException("country.countryCode is empty"); //NOI18N
124                 } else if (country.getCountryI18nKey().isEmpty()) {
125                         // I18n key is not set
126                         throw new IllegalArgumentException("country.countryI18nKey is empty"); //NOI18N
127                 } else if (country.getCountryId() != null) {
128                         // Should be null
129                         throw new IllegalArgumentException(MessageFormat.format("country.countryId is not null ({0})", country.getCountryId())); //NOI18N
130                 }
131
132                 // Default is not found
133                 boolean isAdded = false;
134
135                 // Get query instance
136                 Query query = this.getEntityManager().createNamedQuery("SearchCountryByCodeI18nKey", CountryData.class); //NOI18N
137
138                 // Assign all parameters
139                 query.setParameter("code", country.getCountryCode()); //NOI18N
140                 query.setParameter("key", country.getCountryI18nKey()); //NOI18N
141
142                 // Try to get a single result
143                 try {
144                         // Get single result
145                         Country foundCountry = (Country) query.getSingleResult();
146
147                         // Found it?
148                         isAdded = (foundCountry instanceof Country);
149                 } catch (final NoResultException ex) {
150                         // Not found, don't log this
151                 }
152
153                 // Return result
154                 return isAdded;
155         }
156
157 }