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