]> git.mxchange.org Git - jjobs-ejb.git/blob - src/java/org/mxchange/jcountry/model/data/JobsAdminCountrySingletonBean.java
Don't cherry-pick:
[jjobs-ejb.git] / src / java / org / mxchange / jcountry / model / data / JobsAdminCountrySingletonBean.java
1 /*
2  * Copyright (C) 2016 - 2018 Free Software Foundation
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.model.data;
18
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import javax.ejb.Stateless;
22 import javax.persistence.NoResultException;
23 import javax.persistence.Query;
24 import org.mxchange.jcountry.exceptions.CountryAlreadyAddedException;
25 import org.mxchange.jjobs.beans.ejb.BaseJobsEnterpriseBean;
26
27 /**
28  * A stateless EJB for administrative country purposes
29  * <p>
30  * @author Roland Häder<roland@mxchange.org>
31  */
32 @Stateless (name = "adminCountry", description = "A stateless session-scoped bean for administrative country purposes")
33 public class JobsAdminCountrySingletonBean extends BaseJobsEnterpriseBean implements AdminCountrySessionBeanRemote {
34
35         /**
36          * Serial number
37          */
38         private static final long serialVersionUID = 15_846_983_298_691_208L;
39
40         /**
41          * Default constructor
42          */
43         public JobsAdminCountrySingletonBean () {
44                 // Call super constructor
45                 super();
46         }
47
48         @Override
49         public Country addCountry (final Country country) throws CountryAlreadyAddedException {
50                 // Trace message
51                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addCountry: country={1} - CALLED!", this.getClass().getSimpleName(), country)); //NOI18N
52
53                 // Is it already there?
54                 if (null == country) {
55                         // Throw NPE
56                         throw new NullPointerException("country is null"); //NOI18N
57                 } else if (country.getCountryCode().isEmpty()) {
58                         // Code is not set
59                         throw new IllegalArgumentException("country.countryCode is empty"); //NOI18N
60                 } else if (country.getCountryI18nKey().isEmpty()) {
61                         // I18n key is not set
62                         throw new IllegalArgumentException("country.countryI18nKey is empty"); //NOI18N
63                 } else if (country.getCountryId() != null) {
64                         // Should be null
65                         throw new IllegalArgumentException(MessageFormat.format("country.countryId is not null ({0})", country.getCountryId())); //NOI18N
66                 } else if (this.isCountryAdded(country)) {
67                         // Yes, then abort here
68                         throw new CountryAlreadyAddedException(country);
69                 }
70
71                 // Add timestamp
72                 country.setCountryEntryCreated(new Date());
73
74                 // It is not added, so persist it
75                 this.getEntityManager().persist(country);
76
77                 // Trace message
78                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addCountry: country={1} - EXIT!", this.getClass().getSimpleName(), country)); //NOI18N
79
80                 // Return updated instance
81                 return country;
82         }
83
84         /**
85          * Checks whether given country is already added by i18n key or country
86          * code, what comes first.
87          * <p>
88          * @param country Country instance to check
89          * <p>
90          * @return Whether the country was found
91          */
92         private boolean isCountryAdded (final Country country) {
93                 if (null == country) {
94                         // Throw NPE
95                         throw new NullPointerException("country is null"); //NOI18N
96                 } else if (country.getCountryCode().isEmpty()) {
97                         // Code is not set
98                         throw new IllegalArgumentException("country.countryCode is empty"); //NOI18N
99                 } else if (country.getCountryI18nKey().isEmpty()) {
100                         // I18n key is not set
101                         throw new IllegalArgumentException("country.countryI18nKey is empty"); //NOI18N
102                 } else if (country.getCountryId() != null) {
103                         // Should be null
104                         throw new IllegalArgumentException(MessageFormat.format("country.countryId is not null ({0})", country.getCountryId())); //NOI18N
105                 }
106
107                 // Default is not found
108                 boolean isAdded = false;
109
110                 // Get query instance
111                 final Query query = this.getEntityManager().createNamedQuery("SearchCountryByCodeI18nKey", CountryData.class); //NOI18N
112
113                 // Assign all parameters
114                 query.setParameter("code", country.getCountryCode()); //NOI18N
115                 query.setParameter("key", country.getCountryI18nKey()); //NOI18N
116
117                 // Try to get a single result
118                 try {
119                         // Get single result
120                         final Country foundCountry = (Country) query.getSingleResult();
121
122                         // Found it?
123                         isAdded = (foundCountry instanceof Country);
124                 } catch (final NoResultException ex) {
125                         // Not found, don't log this
126                 }
127
128                 // Return result
129                 return isAdded;
130         }
131
132 }