]> git.mxchange.org Git - jfinancials-mailer-ejb.git/blob - src/java/org/mxchange/jphone/phonenumbers/phone/AddressbookPhoneSessionBean.java
Continued a bit:
[jfinancials-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.jcoreee.database.BaseDatabaseBean;
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 BaseDatabaseBean 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         }
51
52         @SuppressWarnings ("unchecked")
53         @Override
54         public List<DialableFaxNumber> allFaxNumbers () {
55                 // Trace message
56                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allFaxNumbers: CALLED!", this.getClass().getSimpleName())); //NOI18N
57
58                 // Get query
59                 Query query = this.getEntityManager().createNamedQuery("AllFaxNumbers", FaxNumber.class); //NOI18N
60
61                 // Get list from it
62                 List<DialableFaxNumber> list = query.getResultList();
63
64                 // Trace message
65                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allFaxNumbers: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N
66
67                 // Return it
68                 return list;
69         }
70
71         @SuppressWarnings ("unchecked")
72         @Override
73         public List<DialableLandLineNumber> allLandLineNumbers () {
74                 // Trace message
75                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allLandLineNumbers: CALLED!", this.getClass().getSimpleName())); //NOI18N
76
77                 // Get query
78                 Query query = this.getEntityManager().createNamedQuery("AllLandLineNumbers", LandLineNumber.class); //NOI18N
79
80                 // Get list from it
81                 List<DialableLandLineNumber> list = query.getResultList();
82
83                 // Trace message
84                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allLandLineNumbers: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N
85
86                 // Return it
87                 return list;
88         }
89
90         @SuppressWarnings ("unchecked")
91         @Override
92         public List<DialableMobileNumber> allMobileNumbers () {
93                 // Trace message
94                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allMobileNumbers: CALLED!", this.getClass().getSimpleName())); //NOI18N
95
96                 // Get query
97                 Query query = this.getEntityManager().createNamedQuery("AllMobileNumbers", MobileNumber.class); //NOI18N
98
99                 // Get list from it
100                 List<DialableMobileNumber> list = query.getResultList();
101
102                 // Trace message
103                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allMobileNumbers: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N
104
105                 // Return it
106                 return list;
107         }
108
109         @Override
110         public DialableFaxNumber findFaxNumberById (final Long faxNumberId) throws PhoneEntityNotFoundException {
111                 // Trace message
112                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findFaxNumberById: mobileNumberId={1} - CALLED!", this.getClass().getSimpleName(), faxNumberId)); //NOI18N
113
114                 // The id number should be valid
115                 if (null == faxNumberId) {
116                         // Throw NPE
117                         throw new NullPointerException("faxNumberId is null"); //NOI18N
118                 } else if (faxNumberId < 1) {
119                         // Not valid
120                         throw new IllegalArgumentException(MessageFormat.format("faxNumberId={0} is not valid.", faxNumberId)); //NOI18N
121                 }
122
123                 // Now find it
124                 Query query = this.getEntityManager().createNamedQuery("SearchFaxNumberId", FaxNumber.class); //NOI18N
125
126                 // Set parameter
127                 query.setParameter("faxNumberId", faxNumberId); //NOI18N
128
129                 // Init instance
130                 DialableFaxNumber faxNumber = null;
131
132                 // Try to get a result
133                 try {
134                         // Get a single result
135                         faxNumber = (DialableFaxNumber) query.getSingleResult();
136                 } catch (NoResultException ex) {
137                         // The entry was not found, so throw it again
138                         throw new PhoneEntityNotFoundException(faxNumberId, ex);
139                 }
140
141                 // Trace message
142                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findFaxNumberById: cellphone={1} - EXIT!", this.getClass().getSimpleName(), faxNumber)); //NOI18N
143
144                 // Return found instance
145                 return faxNumber;
146         }
147
148         @Override
149         public DialableLandLineNumber findLandLineNumberById (final Long landLineNumberId) throws PhoneEntityNotFoundException {
150                 // Trace message
151                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findLandLineNumberById: mobileNumberId={1} - CALLED!", this.getClass().getSimpleName(), landLineNumberId)); //NOI18N
152
153                 // The id number should be valid
154                 if (null == landLineNumberId) {
155                         // Throw NPE
156                         throw new NullPointerException("landLineNumberId is null"); //NOI18N
157                 } else if (landLineNumberId < 1) {
158                         // Not valid
159                         throw new IllegalArgumentException(MessageFormat.format("landLineNumberId={0} is not valid.", landLineNumberId)); //NOI18N
160                 }
161
162                 // Now find it
163                 Query query = this.getEntityManager().createNamedQuery("SearchLandLineNumberId", LandLineNumber.class); //NOI18N
164
165                 // Set parameter
166                 query.setParameter("landLineNumberId", landLineNumberId); //NOI18N
167
168                 // Init instance
169                 DialableLandLineNumber landLineNumber = null;
170
171                 // Try to get a result
172                 try {
173                         // Get a single result
174                         landLineNumber = (DialableLandLineNumber) query.getSingleResult();
175                 } catch (NoResultException ex) {
176                         // The entry was not found, so throw it again
177                         throw new PhoneEntityNotFoundException(landLineNumberId, ex);
178                 }
179
180                 // Trace message
181                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findLandLineNumberById: cellphone={1} - EXIT!", this.getClass().getSimpleName(), landLineNumber)); //NOI18N
182
183                 // Return found instance
184                 return landLineNumber;
185         }
186
187         @Override
188         public DialableMobileNumber findMobileNumberById (final Long mobileNumberId) throws PhoneEntityNotFoundException {
189                 // Trace message
190                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findMobileNumberById: mobileNumberId={1} - CALLED!", this.getClass().getSimpleName(), mobileNumberId)); //NOI18N
191
192                 // The id number should be valid
193                 if (null == mobileNumberId) {
194                         // Throw NPE
195                         throw new NullPointerException("mobileNumberId is null"); //NOI18N
196                 } else if (mobileNumberId < 1) {
197                         // Not valid
198                         throw new IllegalArgumentException(MessageFormat.format("mobileNumberId={0} is not valid.", mobileNumberId)); //NOI18N
199                 }
200
201                 // Now find it
202                 Query query = this.getEntityManager().createNamedQuery("SearchMobileNumberId", MobileNumber.class); //NOI18N
203
204                 // Set parameter
205                 query.setParameter("mobileNumberId", mobileNumberId); //NOI18N
206
207                 // Init instance
208                 DialableMobileNumber cellphone = null;
209
210                 // Try to get a result
211                 try {
212                         // Get a single result
213                         cellphone = (DialableMobileNumber) query.getSingleResult();
214                 } catch (NoResultException ex) {
215                         // The entry was not found, so throw it again
216                         throw new PhoneEntityNotFoundException(mobileNumberId, ex);
217                 }
218
219                 // Trace message
220                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findMobileNumberById: cellphone={1} - EXIT!", this.getClass().getSimpleName(), cellphone)); //NOI18N
221
222                 // Return found instance
223                 return cellphone;
224         }
225
226 }