]> git.mxchange.org Git - addressbook-mailer-ejb.git/blob - src/java/org/mxchange/jphone/phonenumbers/phone/AddressbookPhoneSessionBean.java
247d4200bf1e23eb85ded4d0827071be6ebff72d
[addressbook-mailer-ejb.git] / src / java / org / mxchange / jphone / phonenumbers / phone / AddressbookPhoneSessionBean.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.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 Haeder<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<DialableMobileNumber> allMobileNumbers () {
55                 // Trace message
56                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allMobileNumbers: CALLED!", this.getClass().getSimpleName())); //NOI18N
57
58                 // Get query
59                 Query query = this.getEntityManager().createNamedQuery("AllCellphoneNumbers", MobileNumber.class); //NOI18N
60
61                 // Get list from it
62                 List<DialableMobileNumber> list = query.getResultList();
63
64                 // Trace message
65                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allMobileNumbers: 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<DialableFaxNumber> allFaxNumbers () {
74                 // Trace message
75                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allFaxNumbers: CALLED!", this.getClass().getSimpleName())); //NOI18N
76
77                 // Get query
78                 Query query = this.getEntityManager().createNamedQuery("AllFaxNumbers", FaxNumber.class); //NOI18N
79
80                 // Get list from it
81                 List<DialableFaxNumber> list = query.getResultList();
82
83                 // Trace message
84                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allFaxNumbers: 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<DialableLandLineNumber> allLandLineNumbers () {
93                 // Trace message
94                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allLandLineNumbers: CALLED!", this.getClass().getSimpleName())); //NOI18N
95
96                 // Get query
97                 Query query = this.getEntityManager().createNamedQuery("AllLandLineNumbers", LandLineNumber.class); //NOI18N
98
99                 // Get list from it
100                 List<DialableLandLineNumber> list = query.getResultList();
101
102                 // Trace message
103                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allLandLineNumbers: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N
104
105                 // Return it
106                 return list;
107         }
108
109         @Override
110         public DialableMobileNumber findMobileNumberById (final Long cellphoneId) throws PhoneEntityNotFoundException {
111                 // Trace message
112                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findCellphoneById: cellphoneId={1} - CALLED!", this.getClass().getSimpleName(), cellphoneId)); //NOI18N
113
114                 // The id number should be valid
115                 if (null == cellphoneId) {
116                         // Throw NPE
117                         throw new NullPointerException("cellphoneId is null"); //NOI18N
118                 } else if (cellphoneId < 1) {
119                         // Not valid
120                         throw new IllegalArgumentException(MessageFormat.format("cellphoneId={0} is not valid.", cellphoneId)); //NOI18N
121                 }
122
123                 // Now find it
124                 Query query = this.getEntityManager().createNamedQuery("SearchCellphoneId", MobileNumber.class); //NOI18N
125
126                 // Set parameter
127                 query.setParameter("cellphoneId", cellphoneId); //NOI18N
128
129                 // Init instance
130                 DialableMobileNumber cellphone = null;
131
132                 // Try to get a result
133                 try {
134                         // Get a single result
135                         cellphone = (DialableMobileNumber) query.getSingleResult();
136                 } catch (NoResultException ex) {
137                         // The entry was not found, so throw it again
138                         throw new PhoneEntityNotFoundException(cellphoneId, ex);
139                 }
140
141                 // Trace message
142                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findCellphoneById: cellphone={1} - EXIT!", this.getClass().getSimpleName(), cellphone)); //NOI18N
143
144                 // Return found instance
145                 return cellphone;
146         }
147
148 }