]> git.mxchange.org Git - pizzaservice-ejb.git/blob - src/java/org/mxchange/jcontacts/contact/PizzaContactSessionBean.java
44edd2cb7f5169f0f63fb57b7f1366c63ea48b9d
[pizzaservice-ejb.git] / src / java / org / mxchange / jcontacts / contact / PizzaContactSessionBean.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.jcontacts.contact;
18
19 import java.text.MessageFormat;
20 import java.util.Iterator;
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.exceptions.ContactNotFoundException;
26 import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean;;
27 import org.mxchange.jcontacts.contact.utils.ContactUtils;
28
29 /**
30  * A contact EJB
31  * <p>
32  * @author Roland Haeder<roland@mxchange.org>
33  */
34 @Stateless (name = "contact", mappedName = "ejb/stateless-pizza-contact", description = "A bean handling contact data")
35 public class PizzaContactSessionBean extends BasePizzaDatabaseBean implements ContactSessionBeanRemote {
36
37         /**
38          * Serial number
39          */
40         private static final long serialVersionUID = 542_145_347_916L;
41
42         /**
43          * Default constructor
44          */
45         public PizzaContactSessionBean () {
46         }
47
48         @Override
49         public Contact findContactById (final Long contactId) throws ContactNotFoundException {
50                 // Log trace message
51                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: contactId={0} - CALLED!", contactId)); //NOI18N
52
53                 // The parameter must be valid
54                 if (null == contactId) {
55                         // Throw NPE
56                         throw new NullPointerException("contactId is null"); //NOI18N
57                 } else if (contactId < 1) {
58                         // Not valid
59                         throw new IllegalArgumentException(MessageFormat.format("contactId={0} is not valid", contactId)); //NOI18N
60                 }
61
62                 // Get query instance
63                 Query query = this.getEntityManager().createNamedQuery("SearchContactById", Contact.class); //NOI18N
64
65                 // Set parameter
66                 query.setParameter("contactId", contactId); //NOI18N
67
68                 // Init contact instance
69                 Contact contact;
70
71                 // Try to find a result
72                 try {
73                         // Find a single result
74                         contact = (Contact) query.getSingleResult();
75
76                         // Log trace message
77                         this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: Found contact={0}", contact)); //NOI18N
78                 } catch (final NoResultException ex) {
79                         // No result found
80                         throw new ContactNotFoundException(contactId, ex);
81                 }
82
83                 // Log trace message
84                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: contact={0} - EXIT!", contact)); //NOI18N
85
86                 // Return found instance
87                 return contact;
88         }
89
90         @Override
91         @SuppressWarnings ("unchecked")
92         public List<Contact> getAllContacts () {
93                 // Log trace message
94                 this.getLoggerBeanLocal().logTrace("getAllContacts - CALLED!"); //NOI18N
95
96                 // Create query instance
97                 Query query = this.getEntityManager().createNamedQuery("AllContacts", List.class); //NOI18N
98
99                 // Get list
100                 List<Contact> contacts = query.getResultList();
101
102                 // Log trace message
103                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getAllContacts: contacts.size()={0} - EXIT!", contacts.size())); //NOI18N
104
105                 // Return it
106                 return contacts;
107         }
108
109         @Override
110         @SuppressWarnings ("unchecked")
111         public List<String> getEmailAddressList () {
112                 // Log trace message
113                 this.getLoggerBeanLocal().logTrace("getEmailAddressList - CALLED!"); //NOI18N
114
115                 // Create query instance
116                 Query query = this.getEntityManager().createNamedQuery("AllContactEmailAddresses", List.class); //NOI18N
117
118                 // Get list
119                 List<String> emailAddresses = query.getResultList();
120
121                 // Log trace message
122                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getEmailAddressList: emailAddresses.size()={0} - EXIT!", emailAddresses.size())); //NOI18N
123
124                 // Return it
125                 return emailAddresses;
126         }
127
128         @Override
129         public boolean isContactFound (final Contact contact) {
130                 // Log trace message
131                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isContactFound: contact={0} - CALLED!", contact)); //NOI18N
132
133                 // Parameter should be valid
134                 if (null == contact) {
135                         // Throw NPE
136                         throw new NullPointerException("contact is null"); //NOI18N
137                 } else if ((contact.getContactId() instanceof Long) && (contact.getContactId() > 0)) {
138                         try {
139                                 // Id set, ask other method
140                                 return (this.findContactById(contact.getContactId()) instanceof Contact);
141                         } catch (final ContactNotFoundException ex) {
142                                 // Not found, should not happen
143                                 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is set, but not found.", contact.getContactId()), ex); //NOI18N
144                         }
145                 }
146
147                 // Default is not found
148                 boolean isFound = false;
149
150                 // Get whole list
151                 List<Contact> contacts = this.getAllContacts();
152
153                 // Is the list empty?
154                 if (contacts.isEmpty()) {
155                         // Then abort here
156                         this.getLoggerBeanLocal().logTrace("isContactFound: No contacts registered, returning 'false' ..."); //NOI18N
157                         return false;
158                 }
159
160                 // Get iterator
161                 Iterator<Contact> iterator = contacts.iterator();
162
163                 // Loop through all
164                 while (iterator.hasNext()) {
165                         // Get contact
166                         Contact next = iterator.next();
167
168                         // Debug message
169                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isContactFound: next={0}", next)); //NOI18N
170
171                         // Is same contact?
172                         if (ContactUtils.isSameContact(contact, next)) {
173                                 // Debug message
174                                 this.getLoggerBeanLocal().logDebug("isContactFound: Found same contact."); //NOI18N
175
176                                 // Found it
177                                 isFound = true;
178                                 break;
179                         }
180                 }
181
182                 // Return status
183                 return isFound;
184         }
185
186         @Override
187         public Contact updateContactData (final Contact contact, final boolean isCellphoneUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
188                 // Log trace message
189                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: contact={0},isCellphoneUnlinked={1},isLandlineUnlinked={2},isFaxUnlinked={3} - CALLED!", contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N
190
191                 // The contact instance must be valid
192                 if (null == contact) {
193                         // Throw NPE again
194                         throw new NullPointerException("contact is null"); //NOI18N
195                 } else if (contact.getContactId() == null) {
196                         // Throw NPE again
197                         throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
198                 } else if (contact.getContactId() < 1) {
199                         // Not valid
200                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
201                 }
202
203                 // Set updated timestamp
204                 this.setAllContactPhoneEntriesUpdated(contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked);
205
206                 // Merge cellphone, land-line and fix
207                 Contact detachedContact = this.mergeContactData(contact);
208
209                 // Trace message
210                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N
211
212                 // Return it
213                 return detachedContact;
214         }
215
216         @Override
217         public Contact updateContactData (final Contact contact) {
218                 // Log trace message
219                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: contact={0} - CALLED!", contact)); //NOI18N
220
221                 // The contact instance must be valid
222                 if (null == contact) {
223                         // Throw NPE again
224                         throw new NullPointerException("contact is null"); //NOI18N
225                 } else if (contact.getContactId() == null) {
226                         // Throw NPE again
227                         throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
228                 } else if (contact.getContactId() < 1) {
229                         // Not valid
230                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
231                 }
232
233                 // Is cell phone/land-line/fax number unlinked?
234                 boolean isCellphoneUnlinked = (contact.getContactCellphoneNumber() == null);
235                 boolean isLandLineUnlinked = (contact.getContactLandLineNumber() == null);
236                 boolean isFaxUnlinked = (contact.getContactFaxNumber() == null);
237
238                 // Call other Method
239                 Contact detachedContact = this.updateContactData(contact, isCellphoneUnlinked, isLandLineUnlinked, isFaxUnlinked);
240
241                 // Trace message
242                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N
243
244                 // Return it
245                 return detachedContact;
246         }
247
248 }