]> git.mxchange.org Git - pizzaservice-ejb.git/blob - src/java/org/mxchange/jphone/phonenumbers/phone/PizzaAdminContactPhoneSessionBean.java
updated jar(s)
[pizzaservice-ejb.git] / src / java / org / mxchange / jphone / phonenumbers / phone / PizzaAdminContactPhoneSessionBean.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.LinkedList;
21 import java.util.List;
22 import javax.ejb.Stateless;
23 import javax.persistence.NoResultException;
24 import javax.persistence.Query;
25 import org.mxchange.jcontacts.contact.Contact;
26 import org.mxchange.jcontacts.contact.UserContact;
27 import org.mxchange.jcontacts.phone.AdminContactsPhoneSessionBeanRemote;
28 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
29 import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean;
30
31 /**
32  * A session EJB for administrative contact's phone number purposes
33  * <p>
34  * @author Roland Haeder<roland@mxchange.org>
35  */
36 @Stateless (name = "admincontactphone", description = "Administrative bean handling contact's phone data")
37 public class PizzaAdminContactPhoneSessionBean extends BasePizzaDatabaseBean implements AdminContactsPhoneSessionBeanRemote {
38
39         /**
40          * Serial number
41          */
42         private static final long serialVersionUID = 189_217_561_460_237_108L;
43
44         @Override
45         @SuppressWarnings ("unchecked")
46         public List<Contact> allContacts (final DialableCellphoneNumber cellPhone) {
47                 // Trace message
48                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allContacts: cellPhone={1} - CALLED!", this.getClass().getSimpleName(), cellPhone)); //NOI18N
49
50                 // The parameter should be valid
51                 if (null == cellPhone) {
52                         // Throw NPE
53                         throw new NullPointerException("cellPhone is null"); //NOI18N
54                 } else if (cellPhone.getPhoneId() == null) {
55                         // Phone id is null, means not persisted or correclty updated instance
56                         throw new NullPointerException("cellPhone.phoneId is null"); //NOI18N
57                 } else if (cellPhone.getPhoneId() < 1) {
58                         // Not valid id number
59                         throw new IllegalArgumentException(MessageFormat.format("cellPhone.phoneId={0} is not valid", cellPhone.getPhoneId())); //NOI18N
60                 }
61
62                 // Init list instance
63                 List<Contact> contacts = new LinkedList<>();
64
65                 // Get query object from all found contact-cellphone links
66                 Query query = this.getEntityManager().createNamedQuery("AllContactsByCellphone", UserContact.class); //NOI18N
67
68                 // Set parameter
69                 query.setParameter("cellPhone", cellPhone); //NOI18N
70
71                 // Try to find all
72                 try {
73                         contacts = query.getResultList();
74                 } catch (final NoResultException ex) {
75                         // No results found
76                 }
77
78                 // Trace message
79                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allContacts: contacts.size()={1} - EXIT!", this.getClass().getSimpleName(), contacts.size())); //NOI18N
80
81                 // Return list
82                 return contacts;
83         }
84
85 }