]> git.mxchange.org Git - pizzaservice-ejb.git/blob - src/java/org/mxchange/jcountry/data/PizzaCountrySingletonBean.java
5600e4ea8765455e3eaa0837ba1c4ec78d17d9a2
[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 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/pizzaservice-singleton-country", description = "A singleton session bean for country informations")
36 public class PizzaCountrySingletonBean 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 void 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
73         @Override
74         @SuppressWarnings ("unchecked")
75         public List<Country> allCountries () {
76                 // Init query
77                 Query query = this.getEntityManager().createNamedQuery("AllCountries", List.class); //NOI18N
78
79                 // Return it
80                 return query.getResultList();
81         }
82
83         /**
84          * Checks whether given country is already added by i18n key or country
85          * code, what comes first.
86          * <p>
87          * @param country Country instance to check
88          * <p>
89          * @return Whether the country was found
90          */
91         private boolean isCountryAdded (final Country country) {
92                 if (null == country) {
93                         // Throw NPE
94                         throw new NullPointerException("country is null"); //NOI18N
95                 } else if (country.getCountryCode().isEmpty()) {
96                         // Code is not set
97                         throw new IllegalArgumentException("country.countryCode is empty"); //NOI18N
98                 } else if (country.getCountryI18nkey().isEmpty()) {
99                         // I18n key is not set
100                         throw new IllegalArgumentException("country.countryI18nKey is empty"); //NOI18N
101                 } else if (country.getCountryId() != null) {
102                         // Should be null
103                         throw new IllegalArgumentException(MessageFormat.format("country.countryId is not null ({0})", country.getCountryId())); //NOI18N
104                 }
105
106                 // Default is not found
107                 boolean isAdded = false;
108
109                 // Get query instance
110                 Query query = this.getEntityManager().createNamedQuery("SearchCountryByCodeI18nKey", CountryData.class); //NOI18N
111
112                 // Assign all parameters
113                 query.setParameter("code", country.getCountryCode()); //NOI18N
114                 query.setParameter("key", country.getCountryI18nkey()); //NOI18N
115
116                 // Try to get a single result
117                 try {
118                         // Get single result
119                         Country foundCountry = (Country) query.getSingleResult();
120
121                         // Found it?
122                         isAdded = (foundCountry instanceof Country);
123                 } catch (final NoResultException ex) {
124                         // Not found, don't log this
125                 }
126
127                 // Return result
128                 return isAdded;
129         }
130
131 }