]> git.mxchange.org Git - pizzaservice-mailer-ejb.git/blob - src/java/org/mxchange/jphone/phonenumbers/phone/AddressbookPhoneSessionBean.java
63084f5470f1610f851c8181c2b6ea1d16c782dd
[pizzaservice-mailer-ejb.git] / src / java / org / mxchange / jphone / phonenumbers / phone / AddressbookPhoneSessionBean.java
1 /*
2  * Copyright (C) 2016, 2017 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.jphone.phonenumbers.phone;
18
19 import java.text.MessageFormat;
20 import java.util.List;
21 import javax.ejb.Stateless;
22 import javax.persistence.NoResultException;
23 import javax.persistence.Query;
24 import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
25 import org.mxchange.jphone.exceptions.PhoneEntityNotFoundException;
26 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
27 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
28 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
29 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
30 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
31 import org.mxchange.jphone.phonenumbers.mobile.MobileNumber;
32
33 /**
34  * A general phone EJB
35  * <p>
36  * @author Roland Häder<roland@mxchange.org>
37  */
38 @Stateless (name = "phone", description = "A bean handling phone data")
39 public class AddressbookPhoneSessionBean extends BaseAddressbookDatabaseBean implements PhoneSessionBeanRemote {
40
41         /**
42          * Serial number
43          */
44         private static final long serialVersionUID = 134_945_698_127_601L;
45
46         /**
47          * Default constructor
48          */
49         public AddressbookPhoneSessionBean () {
50                 // Call super constructor
51                 super();
52         }
53
54         @SuppressWarnings ("unchecked")
55         @Override
56         public List<DialableFaxNumber> allFaxNumbers () {
57                 // Trace message
58                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allFaxNumbers: CALLED!", this.getClass().getSimpleName())); //NOI18N
59
60                 // Get query
61                 Query query = this.getEntityManager().createNamedQuery("AllFaxNumbers", FaxNumber.class); //NOI18N
62
63                 // Get list from it
64                 List<DialableFaxNumber> list = query.getResultList();
65
66                 // Trace message
67                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allFaxNumbers: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N
68
69                 // Return it
70                 return list;
71         }
72
73         @SuppressWarnings ("unchecked")
74         @Override
75         public List<DialableLandLineNumber> allLandLineNumbers () {
76                 // Trace message
77                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allLandLineNumbers: CALLED!", this.getClass().getSimpleName())); //NOI18N
78
79                 // Get query
80                 Query query = this.getEntityManager().createNamedQuery("AllLandLineNumbers", LandLineNumber.class); //NOI18N
81
82                 // Get list from it
83                 List<DialableLandLineNumber> list = query.getResultList();
84
85                 // Trace message
86                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allLandLineNumbers: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N
87
88                 // Return it
89                 return list;
90         }
91
92         @SuppressWarnings ("unchecked")
93         @Override
94         public List<DialableMobileNumber> allMobileNumbers () {
95                 // Trace message
96                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allMobileNumbers: CALLED!", this.getClass().getSimpleName())); //NOI18N
97
98                 // Get query
99                 Query query = this.getEntityManager().createNamedQuery("AllMobileNumbers", MobileNumber.class); //NOI18N
100
101                 // Get list from it
102                 List<DialableMobileNumber> list = query.getResultList();
103
104                 // Trace message
105                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allMobileNumbers: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N
106
107                 // Return it
108                 return list;
109         }
110
111         @Override
112         public DialableFaxNumber findFaxNumberById (final Long faxNumberId) throws PhoneEntityNotFoundException {
113                 // Trace message
114                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findFaxNumberById: mobileNumberId={1} - CALLED!", this.getClass().getSimpleName(), faxNumberId)); //NOI18N
115
116                 // The id number should be valid
117                 if (null == faxNumberId) {
118                         // Throw NPE
119                         throw new NullPointerException("faxNumberId is null"); //NOI18N
120                 } else if (faxNumberId < 1) {
121                         // Not valid
122                         throw new IllegalArgumentException(MessageFormat.format("faxNumberId={0} is not valid.", faxNumberId)); //NOI18N
123                 }
124
125                 // Now find it
126                 Query query = this.getEntityManager().createNamedQuery("SearchFaxNumberId", FaxNumber.class); //NOI18N
127
128                 // Set parameter
129                 query.setParameter("faxNumberId", faxNumberId); //NOI18N
130
131                 // Init instance
132                 DialableFaxNumber faxNumber = null;
133
134                 // Try to get a result
135                 try {
136                         // Get a single result
137                         faxNumber = (DialableFaxNumber) query.getSingleResult();
138                 } catch (NoResultException ex) {
139                         // The entry was not found, so throw it again
140                         throw new PhoneEntityNotFoundException(faxNumberId, ex);
141                 }
142
143                 // Trace message
144                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findFaxNumberById: cellphone={1} - EXIT!", this.getClass().getSimpleName(), faxNumber)); //NOI18N
145
146                 // Return found instance
147                 return faxNumber;
148         }
149
150         @Override
151         public DialableLandLineNumber findLandLineNumberById (final Long landLineNumberId) throws PhoneEntityNotFoundException {
152                 // Trace message
153                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findLandLineNumberById: mobileNumberId={1} - CALLED!", this.getClass().getSimpleName(), landLineNumberId)); //NOI18N
154
155                 // The id number should be valid
156                 if (null == landLineNumberId) {
157                         // Throw NPE
158                         throw new NullPointerException("landLineNumberId is null"); //NOI18N
159                 } else if (landLineNumberId < 1) {
160                         // Not valid
161                         throw new IllegalArgumentException(MessageFormat.format("landLineNumberId={0} is not valid.", landLineNumberId)); //NOI18N
162                 }
163
164                 // Now find it
165                 Query query = this.getEntityManager().createNamedQuery("SearchLandLineNumberId", LandLineNumber.class); //NOI18N
166
167                 // Set parameter
168                 query.setParameter("landLineNumberId", landLineNumberId); //NOI18N
169
170                 // Init instance
171                 DialableLandLineNumber landLineNumber = null;
172
173                 // Try to get a result
174                 try {
175                         // Get a single result
176                         landLineNumber = (DialableLandLineNumber) query.getSingleResult();
177                 } catch (NoResultException ex) {
178                         // The entry was not found, so throw it again
179                         throw new PhoneEntityNotFoundException(landLineNumberId, ex);
180                 }
181
182                 // Trace message
183                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findLandLineNumberById: cellphone={1} - EXIT!", this.getClass().getSimpleName(), landLineNumber)); //NOI18N
184
185                 // Return found instance
186                 return landLineNumber;
187         }
188
189         @Override
190         public DialableMobileNumber findMobileNumberById (final Long mobileNumberId) throws PhoneEntityNotFoundException {
191                 // Trace message
192                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findMobileNumberById: mobileNumberId={1} - CALLED!", this.getClass().getSimpleName(), mobileNumberId)); //NOI18N
193
194                 // The id number should be valid
195                 if (null == mobileNumberId) {
196                         // Throw NPE
197                         throw new NullPointerException("mobileNumberId is null"); //NOI18N
198                 } else if (mobileNumberId < 1) {
199                         // Not valid
200                         throw new IllegalArgumentException(MessageFormat.format("mobileNumberId={0} is not valid.", mobileNumberId)); //NOI18N
201                 }
202
203                 // Now find it
204                 Query query = this.getEntityManager().createNamedQuery("SearchMobileNumberId", MobileNumber.class); //NOI18N
205
206                 // Set parameter
207                 query.setParameter("mobileNumberId", mobileNumberId); //NOI18N
208
209                 // Init instance
210                 DialableMobileNumber cellphone = null;
211
212                 // Try to get a result
213                 try {
214                         // Get a single result
215                         cellphone = (DialableMobileNumber) query.getSingleResult();
216                 } catch (NoResultException ex) {
217                         // The entry was not found, so throw it again
218                         throw new PhoneEntityNotFoundException(mobileNumberId, ex);
219                 }
220
221                 // Trace message
222                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findMobileNumberById: cellphone={1} - EXIT!", this.getClass().getSimpleName(), cellphone)); //NOI18N
223
224                 // Return found instance
225                 return cellphone;
226         }
227
228 }