]> git.mxchange.org Git - jfinancials-ejb.git/blob - src/java/org/mxchange/jcontacts/contact/AddressbookContactSessionBean.java
Continued with logging: (please cherry-pick)
[jfinancials-ejb.git] / src / java / org / mxchange / jcontacts / contact / AddressbookContactSessionBean.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.GregorianCalendar;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Objects;
24 import javax.ejb.Stateless;
25 import javax.persistence.NoResultException;
26 import javax.persistence.Query;
27 import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
28 import org.mxchange.jcontacts.contact.utils.ContactUtils;
29 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
30 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
31
32 /**
33  * A contact EJB
34  * <p>
35  * @author Roland Haeder<roland@mxchange.org>
36  */
37 @Stateless (name = "contact", description = "A bean handling contact data")
38 public class AddressbookContactSessionBean extends BaseAddressbookDatabaseBean implements ContactSessionBeanRemote {
39
40         /**
41          * Serial number
42          */
43         private static final long serialVersionUID = 542_145_347_916L;
44
45         /**
46          * Default constructor
47          */
48         public AddressbookContactSessionBean () {
49         }
50
51         @Override
52         public Contact addContact (final Contact contact) throws ContactAlreadyAddedException {
53                 // Trace message
54                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addContact: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
55
56                 // Is the instance set?
57                 if (null == contact) {
58                         // Throw NPE
59                         throw new NullPointerException("contact is null"); //NOI18N
60                 } else if (contact.getContactId() != null) {
61                         // Should be null
62                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} - is not null", contact.getContactId())); //NOI18N
63                 }
64
65                 // Set created timestamp
66                 contact.setContactCreated(new GregorianCalendar());
67
68                 // Set all created timestamps, if instance is there
69                 this.setAllContactPhoneEntriesCreated(contact);
70
71                 // Persist it
72                 this.getEntityManager().persist(contact);
73
74                 // Flush it to get contactId set
75                 this.getEntityManager().flush();
76
77                 // Trace message
78                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addContact: contact.contactId={1} after persisting - EXIT!", this.getClass().getSimpleName(), contact.getContactId())); //NOI18N
79
80                 // Return it
81                 return contact;
82         }
83
84         @Override
85         public Contact findContactByEmailAddress (final String emailAddress) throws ContactNotFoundException {
86                 // Log trace message
87                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N
88
89                 // The parameter must be valid
90                 if (null == emailAddress) {
91                         // Throw NPE
92                         throw new NullPointerException("emailAddress is null"); //NOI18N
93                 } else if (emailAddress.isEmpty()) {
94                         // Not valid
95                         throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
96                 }
97
98                 // Get query instance
99                 Query query = this.getEntityManager().createNamedQuery("SearchContactByEmailAddress", UserContact.class); //NOI18N
100
101                 // Set parameter
102                 query.setParameter("emailAddress", emailAddress); //NOI18N
103
104                 // Init contact instance
105                 Contact contact;
106
107                 // Try to find a result
108                 try {
109                         // Find a single result
110                         contact = (Contact) query.getSingleResult();
111
112                         // Log trace message
113                         this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: Found contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N
114                 } catch (final NoResultException ex) {
115                         // No result found
116                         throw new ContactNotFoundException(emailAddress, ex);
117                 }
118
119                 // Log trace message
120                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); //NOI18N
121
122                 // Return found instance
123                 return contact;
124         }
125
126         @Override
127         public Contact findContactById (final Long contactId) throws ContactNotFoundException {
128                 // Log trace message
129                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contactId={1} - CALLED!", this.getClass().getSimpleName(), contactId)); //NOI18N
130
131                 // The parameter must be valid
132                 if (null == contactId) {
133                         // Throw NPE
134                         throw new NullPointerException("contactId is null"); //NOI18N
135                 } else if (contactId < 1) {
136                         // Not valid
137                         throw new IllegalArgumentException(MessageFormat.format("contactId={0} is not valid", contactId)); //NOI18N
138                 }
139
140                 // Get query instance
141                 Query query = this.getEntityManager().createNamedQuery("SearchContactById", UserContact.class); //NOI18N
142
143                 // Set parameter
144                 query.setParameter("contactId", contactId); //NOI18N
145
146                 // Init contact instance
147                 Contact contact;
148
149                 // Try to find a result
150                 try {
151                         // Find a single result
152                         contact = (Contact) query.getSingleResult();
153
154                         // Log trace message
155                         this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: Found contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N
156                 } catch (final NoResultException ex) {
157                         // No result found
158                         throw new ContactNotFoundException(contactId, ex);
159                 }
160
161                 // Log trace message
162                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); //NOI18N
163
164                 // Return found instance
165                 return contact;
166         }
167
168         @Override
169         @SuppressWarnings ("unchecked")
170         public List<Contact> getAllContacts () {
171                 // Log trace message
172                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts - CALLED!", this.getClass().getSimpleName())); //NOI18N
173
174                 // Create query instance
175                 Query query = this.getEntityManager().createNamedQuery("AllContacts", UserContact.class); //NOI18N
176
177                 // Get list
178                 List<Contact> contacts = query.getResultList();
179
180                 // Log trace message
181                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts: contacts.size()={1} - EXIT!", this.getClass().getSimpleName(), contacts.size())); //NOI18N
182
183                 // Return it
184                 return contacts;
185         }
186
187         @Override
188         @SuppressWarnings ("unchecked")
189         public List<String> getEmailAddressList () {
190                 // Log trace message
191                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList - CALLED!", this.getClass().getSimpleName())); //NOI18N
192
193                 // Create query instance
194                 Query query = this.getEntityManager().createNamedQuery("AllContactEmailAddresses", String.class); //NOI18N
195
196                 // Get list
197                 List<String> emailAddresses = query.getResultList();
198
199                 // Log trace message
200                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList: emailAddresses.size()={1} - EXIT!", this.getClass().getSimpleName(), emailAddresses.size())); //NOI18N
201
202                 // Return it
203                 return emailAddresses;
204         }
205
206         @Override
207         public boolean isEmailAddressRegistered (final String emailAddress) {
208                 // Log trace message
209                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N
210
211                 // The email address should be valid
212                 if (null == emailAddress) {
213                         // Is null
214                         throw new NullPointerException("emailAddress is null"); //NOI18N
215                 } else if (emailAddress.isEmpty()) {
216                         // Is empty
217                         throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
218                 }
219
220                 // Default is not found
221                 boolean isFound = false;
222
223                 try {
224                         // Ask other method for contact instance
225                         Contact contact = this.findContactByEmailAddress(emailAddress);
226
227                         // Log debug message
228                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: Found contact={1} for emailAddress={2}", this.getClass().getSimpleName(), contact, emailAddress)); //NOI18N
229
230                         // It is found ...
231                         isFound = true;
232                 } catch (final ContactNotFoundException ex) {
233                         // @TODO Was not found, log exception for spam check?
234                 }
235
236                 // Log trace message
237                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N
238
239                 // Return status
240                 return isFound;
241         }
242
243         @Override
244         public Contact lookupContact (final Contact contact) {
245                 // Log trace message
246                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
247
248                 // Parameter should be valid
249                 if (null == contact) {
250                         // Throw NPE
251                         throw new NullPointerException("contact is null"); //NOI18N
252                 } else if (contact.getContactId() > 0) {
253                         try {
254                                 // Id set, ask other method
255                                 return this.findContactById(contact.getContactId());
256                         } catch (final ContactNotFoundException ex) {
257                                 // Not found, should not happen
258                                 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is set, but not found.", contact.getContactId()), ex); //NOI18N
259                         }
260                 }
261
262                 // Default is not found
263                 Contact foundContact = null;
264
265                 // Get whole list
266                 List<Contact> contacts = this.getAllContacts();
267
268                 // Is the list empty?
269                 if (contacts.isEmpty()) {
270                         // Then abort here
271                         this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: No contacts registered, returning NULL ...", this.getClass().getSimpleName())); //NOI18N
272                         return null;
273                 }
274
275                 // Get iterator
276                 Iterator<Contact> iterator = contacts.iterator();
277
278                 // Loop through all
279                 while (iterator.hasNext()) {
280                         // Get contact
281                         Contact next = iterator.next();
282
283                         // Is same contact?
284                         if ((Objects.equals(contact, next)) || (ContactUtils.isSameContact(contact, next))) {
285                                 // Debug message
286                                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isContactFound: Found same contact: contactId={1}", this.getClass().getSimpleName(), next.getContactId())); //NOI18N
287
288                                 // Found it
289                                 foundContact = next;
290                                 break;
291                         }
292                 }
293
294                 // Trace message
295                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: foundContact={1} - EXIT!", this.getClass().getSimpleName(), foundContact)); //NOI18N
296
297                 // Return found contact
298                 return foundContact;
299         }
300
301         @Override
302         public Contact updateContactData (final Contact contact, final boolean isCellphoneUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
303                 // Log trace message
304                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1},isCellphoneUnlinked={2},isLandlineUnlinked={3},isFaxUnlinked={4} - CALLED!", this.getClass().getSimpleName(), contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N
305
306                 // The contact instance must be valid
307                 if (null == contact) {
308                         // Throw NPE again
309                         throw new NullPointerException("contact is null"); //NOI18N
310                 } else if (contact.getContactId() == null) {
311                         // Throw NPE again
312                         throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
313                 } else if (contact.getContactId() < 1) {
314                         // Not valid
315                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
316                 }
317
318                 // Set updated timestamp
319                 this.setAllContactPhoneEntriesUpdated(contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked);
320
321                 // Merge cellphone, land-line and fix
322                 Contact detachedContact = this.mergeContactData(contact);
323
324                 // Trace message
325                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
326
327                 // Return it
328                 return detachedContact;
329         }
330
331         @Override
332         public Contact updateContactData (final Contact contact) {
333                 // Log trace message
334                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
335
336                 // The contact instance must be valid
337                 if (null == contact) {
338                         // Throw NPE again
339                         throw new NullPointerException("contact is null"); //NOI18N
340                 } else if (contact.getContactId() == null) {
341                         // Throw NPE again
342                         throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
343                 } else if (contact.getContactId() < 1) {
344                         // Not valid
345                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
346                 }
347
348                 // Is cell phone/land-line/fax number unlinked?
349                 boolean isCellphoneUnlinked = (contact.getContactCellphoneNumber() == null);
350                 boolean isLandLineUnlinked = (contact.getContactLandLineNumber() == null);
351                 boolean isFaxUnlinked = (contact.getContactFaxNumber() == null);
352
353                 // Call other Method
354                 Contact detachedContact = this.updateContactData(contact, isCellphoneUnlinked, isLandLineUnlinked, isFaxUnlinked);
355
356                 // Trace message
357                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
358
359                 // Return it
360                 return detachedContact;
361         }
362
363 }