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