]> git.mxchange.org Git - addressbook-mailer-ejb.git/blob - src/java/org/mxchange/jcountry/data/AddressbookCountrySingletonBean.java
addCountry() now must return the updated instance (with timestamp and valid countryId...
[addressbook-mailer-ejb.git] / src / java / org / mxchange / jcountry / data / AddressbookCountrySingletonBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
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 bean for country informations
31  * <p>
32  * @author Roland Haeder<roland@mxchange.org>
33  */
34 @Startup
35 @Singleton (name = "country", mappedName = "ejb/addressbook-singleton-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                 // Is it already there?
46                 if (null == country) {
47                         // Throw NPE
48                         throw new NullPointerException("country is null"); //NOI18N
49                 } else if (country.getCountryCode().isEmpty()) {
50                         // Code is not set
51                         throw new IllegalArgumentException("country.countryCode is empty"); //NOI18N
52                 } else if (country.getCountryI18nkey().isEmpty()) {
53                         // I18n key is not set
54                         throw new IllegalArgumentException("country.countryI18nKey is empty"); //NOI18N
55                 } else if (country.getCountryId() != null) {
56                         // Should be null
57                         throw new IllegalArgumentException(MessageFormat.format("country.countryId is not null ({0})", country.getCountryId())); //NOI18N
58                 } else if (this.isCountryAdded(country)) {
59                         // Yes, then abort here
60                         throw new CountryAlreadyAddedException(country);
61                 }
62
63                 // Add timestamp
64                 country.setCountryEntryCreated(new GregorianCalendar());
65
66                 // It is not added, so persist it
67                 this.getEntityManager().persist(country);
68
69                 // Flush it to get id number back, maybe it is directly needed?
70                 this.getEntityManager().flush();
71
72                 // Return updated instance
73                 return country;
74         }
75
76         @Override
77         @SuppressWarnings ("unchecked")
78         public List<Country> allCountries () {
79                 // Init query
80                 Query query = this.getEntityManager().createNamedQuery("AllCountries", List.class); //NOI18N
81
82                 // Return it
83                 return query.getResultList();
84         }
85
86         /**
87          * Checks whether given country is already added by i18n key or country
88          * code, what comes first.
89          * <p>
90          * @param country Country instance to check
91          * <p>
92          * @return Whether the country was found
93          */
94         private boolean isCountryAdded (final Country country) {
95                 if (null == country) {
96                         // Throw NPE
97                         throw new NullPointerException("country is null"); //NOI18N
98                 } else if (country.getCountryCode().isEmpty()) {
99                         // Code is not set
100                         throw new IllegalArgumentException("country.countryCode is empty"); //NOI18N
101                 } else if (country.getCountryI18nkey().isEmpty()) {
102                         // I18n key is not set
103                         throw new IllegalArgumentException("country.countryI18nKey is empty"); //NOI18N
104                 } else if (country.getCountryId() != null) {
105                         // Should be null
106                         throw new IllegalArgumentException(MessageFormat.format("country.countryId is not null ({0})", country.getCountryId())); //NOI18N
107                 }
108
109                 // Default is not found
110                 boolean isAdded = false;
111
112                 // Get query instance
113                 Query query = this.getEntityManager().createNamedQuery("SearchCountryByCodeI18nKey", CountryData.class); //NOI18N
114
115                 // Assign all parameters
116                 query.setParameter("code", country.getCountryCode()); //NOI18N
117                 query.setParameter("key", country.getCountryI18nkey()); //NOI18N
118
119                 // Try to get a single result
120                 try {
121                         // Get single result
122                         Country foundCountry = (Country) query.getSingleResult();
123
124                         // Found it?
125                         isAdded = (foundCountry instanceof Country);
126                 } catch (final NoResultException ex) {
127                         // Not found, don't log this
128                 }
129
130                 // Return result
131                 return isAdded;
132         }
133
134 }