]> git.mxchange.org Git - addressbook-mailer-ejb.git/blob - src/java/org/mxchange/jcountry/data/AddressbookCountrySingletonBean.java
updated own name and resources
[addressbook-mailer-ejb.git] / src / java / org / mxchange / jcountry / data / AddressbookCountrySingletonBean.java
1 /*
2  * Copyright (C) 2016 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.jcoreee.database.BaseDatabaseBean;
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 bean for country informations")
36 public class AddressbookCountrySingletonBean extends BaseDatabaseBean implements CountrySingletonBeanRemote {
37
38         /**
39          * Serial number
40          */
41         private static final long serialVersionUID = 15_846_983_298_691_207L;
42
43         @Override
44         public Country addCountry (final Country country) throws CountryAlreadyAddedException {
45                 // Trace message
46                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addCountry: country={1} - CALLED!", this.getClass().getSimpleName(), country)); //NOI18N
47
48                 // Is it already there?
49                 if (null == country) {
50                         // Throw NPE
51                         throw new NullPointerException("country is null"); //NOI18N
52                 } else if (country.getCountryCode().isEmpty()) {
53                         // Code is not set
54                         throw new IllegalArgumentException("country.countryCode is empty"); //NOI18N
55                 } else if (country.getCountryI18nKey().isEmpty()) {
56                         // I18n key is not set
57                         throw new IllegalArgumentException("country.countryI18nKey is empty"); //NOI18N
58                 } else if (country.getCountryId() != null) {
59                         // Should be null
60                         throw new IllegalArgumentException(MessageFormat.format("country.countryId is not null ({0})", country.getCountryId())); //NOI18N
61                 } else if (this.isCountryAdded(country)) {
62                         // Yes, then abort here
63                         throw new CountryAlreadyAddedException(country);
64                 }
65
66                 // Add timestamp
67                 country.setCountryEntryCreated(new GregorianCalendar());
68
69                 // It is not added, so persist it
70                 this.getEntityManager().persist(country);
71
72                 // Flush it to get id number back, maybe it is directly needed?
73                 this.getEntityManager().flush();
74
75                 // Trace message
76                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addCountry: country={1} - EXIT!", this.getClass().getSimpleName(), country)); //NOI18N
77
78                 // Return updated instance
79                 return country;
80         }
81
82         @Override
83         @SuppressWarnings ("unchecked")
84         public List<Country> allCountries () {
85                 // Trace message
86                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allCountries: CALLED!", this.getClass().getSimpleName())); //NOI18N
87
88                 // Init query
89                 Query query = this.getEntityManager().createNamedQuery("AllCountries", CountryData.class); //NOI18N
90
91                 // Get list
92                 List<Country> countries = query.getResultList();
93
94                 // Trace message
95                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allCountries: countries.size()={1} - EXIT!", this.getClass().getSimpleName(), countries.size())); //NOI18N
96
97                 // Return it
98                 return countries;
99         }
100
101         /**
102          * Checks whether given country is already added by i18n key or country
103          * code, what comes first.
104          * <p>
105          * @param country Country instance to check
106          * <p>
107          * @return Whether the country was found
108          */
109         private boolean isCountryAdded (final Country country) {
110                 if (null == country) {
111                         // Throw NPE
112                         throw new NullPointerException("country is null"); //NOI18N
113                 } else if (country.getCountryCode().isEmpty()) {
114                         // Code is not set
115                         throw new IllegalArgumentException("country.countryCode is empty"); //NOI18N
116                 } else if (country.getCountryI18nKey().isEmpty()) {
117                         // I18n key is not set
118                         throw new IllegalArgumentException("country.countryI18nKey is empty"); //NOI18N
119                 } else if (country.getCountryId() != null) {
120                         // Should be null
121                         throw new IllegalArgumentException(MessageFormat.format("country.countryId is not null ({0})", country.getCountryId())); //NOI18N
122                 }
123
124                 // Default is not found
125                 boolean isAdded = false;
126
127                 // Get query instance
128                 Query query = this.getEntityManager().createNamedQuery("SearchCountryByCodeI18nKey", CountryData.class); //NOI18N
129
130                 // Assign all parameters
131                 query.setParameter("code", country.getCountryCode()); //NOI18N
132                 query.setParameter("key", country.getCountryI18nKey()); //NOI18N
133
134                 // Try to get a single result
135                 try {
136                         // Get single result
137                         Country foundCountry = (Country) query.getSingleResult();
138
139                         // Found it?
140                         isAdded = (foundCountry instanceof Country);
141                 } catch (final NoResultException ex) {
142                         // Not found, don't log this
143                 }
144
145                 // Return result
146                 return isAdded;
147         }
148
149 }