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