]> git.mxchange.org Git - addressbook-core.git/blob - src/org/mxchange/addressbook/database/BaseAddressbookDatabaseBean.java
Please cherry-pick:
[addressbook-core.git] / src / org / mxchange / addressbook / database / BaseAddressbookDatabaseBean.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (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 General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.addressbook.database;
18
19 import java.text.MessageFormat;
20 import java.util.GregorianCalendar;
21 import java.util.Objects;
22 import java.util.Properties;
23 import javax.ejb.EJBException;
24 import javax.jms.JMSException;
25 import javax.jms.ObjectMessage;
26 import javax.mail.Address;
27 import org.mxchange.jcontacts.contact.Contact;
28 import org.mxchange.jcoreee.database.BaseDatabaseBean;
29 import org.mxchange.jmailee.model.delivery.wrapper.EmailDeliveryWrapper;
30 import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery;
31 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
32 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
33 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
34 import org.mxchange.jphone.utils.PhoneUtils;
35 import org.mxchange.jusercore.model.user.LoginUser;
36 import org.mxchange.jusercore.model.user.User;
37 import org.mxchange.jusercore.model.user.UserUtils;
38
39 /**
40  * A helper class for beans that access the database.
41  * <p>
42  * @author Roland Häder<roland@mxchange.org>
43  */
44 public abstract class BaseAddressbookDatabaseBean extends BaseDatabaseBean {
45
46         /**
47          * Serial number
48          */
49         private static final long serialVersionUID = 12_895_410_275_811_963L;
50
51         /**
52          * Protected constructor
53          */
54         protected BaseAddressbookDatabaseBean () {
55                 // Call super constructor
56                 super("jms/addressbook-queue-factory", "jms/addressbook-email-queue"); //NOI18N
57         }
58
59         /**
60          * Updates all contacts's phone entry's created timestamps
61          * <p>
62          * @param contact Contact instance to update
63          */
64         protected void setAllContactPhoneEntriesCreated (final Contact contact) {
65                 // Trace message
66                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
67
68                 // The contact instance must be valid
69                 if (null == contact) {
70                         // Throw NPE again
71                         throw new NullPointerException("contact is null"); //NOI18N
72                 }
73
74                 // Get all phone instances
75                 DialableLandLineNumber landLineNumber = contact.getContactLandLineNumber();
76                 DialableFaxNumber faxNumber = contact.getContactFaxNumber();
77                 DialableMobileNumber mobileNumber = contact.getContactMobileNumber();
78
79                 // Debug message
80                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: landLineNumber={1},faxNumber={2},mobileNumber={3}", this.getClass().getSimpleName(), landLineNumber, faxNumber, mobileNumber)); //NOI18N
81
82                 // Is a phone number instance set?
83                 if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() == null)) {
84                         // Debug message
85                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: Setting created timestamp for land-line number ...", this.getClass().getSimpleName())); //NOI18N
86
87                         // Set updated timestamp
88                         landLineNumber.setPhoneEntryCreated(new GregorianCalendar());
89                 }
90
91                 // Is a fax number instance set?
92                 if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneId() == null)) {
93                         // Debug message
94                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: Setting created timestamp for fax number ...", this.getClass().getSimpleName())); //NOI18N
95
96                         // Set updated timestamp
97                         faxNumber.setPhoneEntryCreated(new GregorianCalendar());
98                 }
99
100                 // Is a mobile number instance set?
101                 if ((mobileNumber instanceof DialableMobileNumber) && (mobileNumber.getPhoneId() == null)) {
102                         // Debug message
103                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: Setting created timestamp for cellphone number ...", this.getClass().getSimpleName())); //NOI18N
104
105                         // Set updated timestamp
106                         mobileNumber.setPhoneEntryCreated(new GregorianCalendar());
107                 }
108
109                 // Trace message
110                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: EXIT!", this.getClass().getSimpleName())); //NOI18N
111         }
112
113         /**
114          * Returns a managed instance from given mobile number
115          * <p>
116          * @param mobileNumber Mobile number
117          * @param fetchedNumber Found mobile number in database
118          * <p>
119          * @return Managed instance
120          */
121         protected DialableMobileNumber getManaged (final DialableMobileNumber mobileNumber, final DialableMobileNumber fetchedNumber) {
122                 // Trace message
123                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: mobileNumber={1},fetchedNumber={2} - CALLED!", this.getClass().getSimpleName(), mobileNumber, fetchedNumber)); //NOI18N
124
125                 // Should be valid
126                 if (null == mobileNumber) {
127                         // Throw NPE
128                         throw new NullPointerException("mobileNumber is null"); //NOI18N
129                 } else if (null == fetchedNumber) {
130                         // Throw NPE again
131                         throw new NullPointerException("fetchedNumber is null"); //NOI18N
132                 } else if (fetchedNumber.getPhoneId() == null) {
133                         // ..and again
134                         throw new NullPointerException("fetchedNumber.phoneId is null"); //NOI18N
135                 }
136
137                 // Debug message
138                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.getDetached: fetchedNumber.phoneId={1}", this.getClass().getSimpleName(), fetchedNumber.getPhoneId())); //NOI18N
139
140                 // Default is null
141                 DialableMobileNumber managedNumber = null;
142
143                 // Is there a difference?
144                 if (!PhoneUtils.isSameMobileNumber(mobileNumber, fetchedNumber)) {
145                         // Merge this entry
146                         managedNumber = this.getEntityManager().merge(fetchedNumber);
147                 }
148
149                 // Trace message
150                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: managedNumber={1} - EXIT!", this.getClass().getSimpleName(), managedNumber)); //NOI18N
151
152                 // Return it
153                 return managedNumber;
154         }
155
156         /**
157          * Returns a managed instance from given land-line number
158          * <p>
159          * @param landLineNumber Land-line number
160          * @param fetchedNumber Found land-line number in database
161          * <p>
162          * @return Managed instance
163          */
164         protected DialableLandLineNumber getManaged (final DialableLandLineNumber landLineNumber, final DialableLandLineNumber fetchedNumber) {
165                 // Trace message
166                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: landLineNumber={1},fetchedNumber={2} - CALLED!", this.getClass().getSimpleName(), landLineNumber, fetchedNumber)); //NOI18N
167
168                 // Should be valid
169                 if (null == landLineNumber) {
170                         // Throw NPE
171                         throw new NullPointerException("landLineNumber is null"); //NOI18N
172                 } else if (null == fetchedNumber) {
173                         // Throw NPE again
174                         throw new NullPointerException("fetchedNumber is null"); //NOI18N
175                 } else if (fetchedNumber.getPhoneId() == null) {
176                         // ..and again
177                         throw new NullPointerException("landLineNumber.phoneId is null"); //NOI18N
178                 }
179
180                 // Debug message
181                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.getDetached: fetchedNumber.phoneId={1}", this.getClass().getSimpleName(), fetchedNumber.getPhoneId())); //NOI18N
182
183                 // Default is null
184                 DialableLandLineNumber managedNumber = null;
185
186                 // Is there a difference?
187                 if (!PhoneUtils.isSameLandLineNumber(landLineNumber, fetchedNumber)) {
188                         // Merge this entry
189                         managedNumber = this.getEntityManager().merge(fetchedNumber);
190                 }
191
192                 // Trace message
193                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: managedNumber={1} - EXIT!", this.getClass().getSimpleName(), managedNumber)); //NOI18N
194
195                 // Return it
196                 return managedNumber;
197         }
198
199         /**
200          * Returns a managed instance from given fax number
201          * <p>
202          * @param faxNumber Fax number
203          * @param fetchedNumber Found fax number in database
204          * <p>
205          * @return Managed instance
206          */
207         protected DialableFaxNumber getManaged (final DialableFaxNumber faxNumber, final DialableFaxNumber fetchedNumber) {
208                 // Trace message
209                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: faxNumber={1},fetchedNumber={2} - CALLED!", this.getClass().getSimpleName(), faxNumber, fetchedNumber)); //NOI18N
210
211                 // Should be valid
212                 if (null == faxNumber) {
213                         // Throw NPE
214                         throw new NullPointerException("faxNumber is null"); //NOI18N
215                 } else if (null == fetchedNumber) {
216                         // Throw NPE again
217                         throw new NullPointerException("fetchedNumber is null"); //NOI18N
218                 } else if (fetchedNumber.getPhoneId() == null) {
219                         // ..and again
220                         throw new NullPointerException("fetchedNumber.phoneId is null"); //NOI18N
221                 }
222
223                 // Debug message
224                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.getDetached: fetchedNumber.phoneId={1}", this.getClass().getSimpleName(), fetchedNumber.getPhoneId())); //NOI18N
225
226                 // Default is null
227                 DialableFaxNumber managedNumber = null;
228
229                 // Is there a difference?
230                 if (!PhoneUtils.isSameFaxNumber(faxNumber, fetchedNumber)) {
231                         // Merge this entry
232                         managedNumber = this.getEntityManager().merge(fetchedNumber);
233                 }
234
235                 // Trace message
236                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: managedNumber={1} - EXIT!", this.getClass().getSimpleName(), managedNumber)); //NOI18N
237
238                 // Return it
239                 return managedNumber;
240         }
241
242         /**
243          * Get back a managed instance from given user
244          * <p>
245          * @param user Unmanaged/detached user instance
246          * <p>
247          * @return Managed user instance
248          */
249         protected User getManagedUser (final User user) {
250                 // Trace message
251                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getManaged: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
252
253                 // user should not be null
254                 if (null == user) {
255                         // Abort here
256                         throw new NullPointerException("user is null"); //NOI18N
257                 } else if (user.getUserId() == null) {
258                         // Id is set
259                         throw new NullPointerException("user.userId is null"); //NOI18N
260                 } else if (user.getUserId() < 1) {
261                         // Id is set
262                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is null", user.getUserId())); //NOI18N
263                 } else if (user.getUserContact() == null) {
264                         // Throw NPE again
265                         throw new NullPointerException("user.userContact is null"); //NOI18N
266                 } else if (user.getUserContact().getContactId() == null) {
267                         // Throw NPE again
268                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
269                 } else if (user.getUserContact().getContactId() < 1) {
270                         // Not valid id number
271                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N#
272                 }
273
274                 // Try to find it (should be there)
275                 User managedUser = this.getEntityManager().find(LoginUser.class, user.getUserId());
276
277                 // Should be there
278                 assert (managedUser instanceof User) : "managedUser is null"; //NOI18N
279
280                 // Trace message
281                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getManaged: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
282
283                 // Return it
284                 return managedUser;
285         }
286
287         /**
288          * Merges given (detached) contact's data
289          * <p>
290          * @param detachedContact Contact instance to merge
291          * <p>
292          * @return Detached contact instance
293          */
294         protected Contact mergeContactData (final Contact detachedContact) {
295                 // Trace message
296                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.mergeContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
297
298                 // The contact instance must be valid
299                 if (null == detachedContact) {
300                         // Throw NPE again
301                         throw new NullPointerException("detachedContact is null"); //NOI18N
302                 } else if (detachedContact.getContactId() == null) {
303                         // Throw NPE again
304                         throw new NullPointerException("detachedContact.contactId is null"); //NOI18N //NOI18N
305                 } else if (detachedContact.getContactId() < 1) {
306                         // Not valid
307                         throw new IllegalStateException(MessageFormat.format("{0}.detachedContact.contactId={1} is not valid.", this.getClass().getSimpleName(), detachedContact.getContactId())); //NOI18N
308                 }
309
310                 // Get contact from it and find it
311                 Contact managedContact = this.getEntityManager().find(detachedContact.getClass(), detachedContact.getContactId());
312
313                 // Should be found
314                 assert (managedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", detachedContact.getContactId()); //NOI18N
315
316                 // Debug message
317                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.updateUserPersonalData: managedContact.contactId={1}", this.getClass().getSimpleName(), managedContact.getContactId())); //NOI18N
318
319                 // Is a fax number set?
320                 if (detachedContact.getContactFaxNumber() instanceof DialableFaxNumber) {
321                         // Make fax numbers managed
322                         managedContact.setContactFaxNumber(this.getManaged(detachedContact.getContactFaxNumber(), detachedContact.getContactFaxNumber()));
323                 }
324
325                 // Is a land-line number set?
326                 if (detachedContact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
327                         // Make land-line numbers managed
328                         managedContact.setContactLandLineNumber(this.getManaged(detachedContact.getContactLandLineNumber(), detachedContact.getContactLandLineNumber()));
329                 }
330
331                 // Is a mobile number set?
332                 if (detachedContact.getContactMobileNumber() instanceof DialableMobileNumber) {
333                         // Make mobile numbers managed
334                         managedContact.setContactMobileNumber(this.getManaged(detachedContact.getContactMobileNumber(), detachedContact.getContactMobileNumber()));
335                 }
336
337                 // Set updated timestamp
338                 managedContact.setContactUpdated(new GregorianCalendar());
339
340                 // Copy all
341                 managedContact.copyAll(detachedContact);
342
343                 // Trace message
344                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.mergeContactData: managedContact={1} - EXIT!", this.getClass().getSimpleName(), managedContact)); //NOI18N
345
346                 // Return detached contact
347                 return managedContact;
348         }
349
350         /**
351          * Merges given (detached) contact's cellphone, land-line and fax numbers
352          * <p>
353          * @param detachedContact Detached contact instance
354          */
355         protected void mergeContactsMobileLandLineFaxNumbers (final Contact detachedContact) {
356                 // Trace message
357                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.mergeContactsMobileLandLineFaxNumbers: detachedContact={1} - CALLED!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
358
359                 // The contact instance must be valid
360                 if (null == detachedContact) {
361                         // Throw NPE again
362                         throw new NullPointerException("detachedContact is null"); //NOI18N
363                 } else if (detachedContact.getContactId() == null) {
364                         // Throw NPE again
365                         throw new NullPointerException("detachedContact.contactId is null"); //NOI18N //NOI18N
366                 } else if (detachedContact.getContactId() < 1) {
367                         // Not valid
368                         throw new IllegalStateException(MessageFormat.format("{0}.detachedContact.contactId={1} is not valid.", this.getClass().getSimpleName(), detachedContact.getContactId())); //NOI18N
369                 }
370
371                 // Get all instances
372                 DialableMobileNumber cellphone = detachedContact.getContactMobileNumber();
373                 DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
374                 DialableFaxNumber fax = detachedContact.getContactFaxNumber();
375
376                 // Is there a  cellphone instance set?
377                 if (cellphone instanceof DialableMobileNumber) {
378                         // Debug message
379                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId())); //NOI18N
380
381                         // Then find it, too
382                         DialableMobileNumber foundMobile = this.getEntityManager().find(cellphone.getClass(), cellphone.getPhoneId());
383
384                         // Should be there
385                         assert (foundMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", foundMobile.getPhoneId()); //NOI18N
386
387                         // Copy all
388                         foundMobile.copyAll(detachedContact.getContactMobileNumber());
389
390                         // Set it back
391                         detachedContact.setContactMobileNumber(foundMobile);
392                 }
393
394                 // Is there a  fax instance set?
395                 if (fax instanceof DialableFaxNumber) {
396                         // Debug message
397                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); //NOI18N
398
399                         // Then find it, too
400                         DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId());
401
402                         // Copy all
403                         foundFax.copyAll(detachedContact.getContactFaxNumber());
404
405                         // Set it back
406                         detachedContact.setContactFaxNumber(foundFax);
407                 }
408
409                 // Is there a  fax instance set?
410                 if (landLine instanceof DialableLandLineNumber) {
411                         // Debug message
412                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); //NOI18N
413
414                         // Then find it, too
415                         DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId());
416
417                         // Should be there
418                         assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
419
420                         // Copy all
421                         foundLandLine.copyAll(detachedContact.getContactLandLineNumber());
422
423                         // Set it back
424                         detachedContact.setContactLandLineNumber(foundLandLine);
425                 }
426
427                 // Trace message
428                 this.getLoggerBeanLocal().logTrace("mergeContactsMobileLandLineFaxNumbers: EXIT!"); //NOI18N
429         }
430
431         /**
432          * Sends an email with given subject line, template name to given recipient
433          * and user data
434          * <p>
435          * @param subjectLine Subject line
436          * @param templateName Template name
437          * @param emailAddress Recipient's email address
438          * @param user User instance
439          * @param baseUrl Base URL
440          */
441         protected void sendEmail (final String subjectLine, final String templateName, final Address emailAddress, final User user, final String baseUrl) {
442                 // Trace message
443                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("sendEmail: subjectLine={0},templateName={1},emailAddress={2},user={3},baseUrl={4} - CALLED!", subjectLine, templateName, emailAddress, user, baseUrl)); //NOI18N
444
445                 // All should be set
446                 if (null == subjectLine) {
447                         // Throw NPE
448                         throw new NullPointerException("subjectLine is null"); //NOI18N
449                 } else if (subjectLine.isEmpty()) {
450                         // No subject line
451                         throw new IllegalArgumentException("subjectLine is empty"); //NOI18N
452                 } else if (null == templateName) {
453                         // Throw NPE
454                         throw new NullPointerException("templateName is null"); //NOI18N
455                 } else if (templateName.isEmpty()) {
456                         // No template name
457                         throw new IllegalArgumentException("templateName is empty"); //NOI18N
458                 } else if (null == emailAddress) {
459                         // Throw NPE
460                         throw new NullPointerException("emailAddress is null"); //NOI18N
461                 }
462
463                 // Prepare mail wrapper
464                 WrapableEmailDelivery emailWrapper = new EmailDeliveryWrapper();
465
466                 // Set all values
467                 Properties variables = UserUtils.getAllUserFields(user);
468
469                 // Set base URL
470                 variables.put("baseUrl", baseUrl); //NOI18N
471
472                 // Set all
473                 // @TODO Language from message bundle
474                 emailWrapper.setRecipient(emailAddress);
475                 emailWrapper.setLocale(user.getUserLocale());
476                 emailWrapper.setSubjectLine(subjectLine);
477                 emailWrapper.setTemplateName(templateName);
478                 emailWrapper.setTemplateVariables(variables);
479
480                 try {
481                         // Send out email change
482                         ObjectMessage message = this.getSession().createObjectMessage();
483                         message.setObject(emailWrapper);
484
485                         // Send message
486                         this.sendMessage(message);
487                 } catch (final JMSException ex) {
488                         // Throw again
489                         throw new EJBException(ex);
490                 }
491
492                 // Trace message
493                 this.getLoggerBeanLocal().logTrace("sendEmail: EXIT!"); //NOI18N
494         }
495
496         /**
497          * Updates all contact's phone instances from other contact, both contacts
498          * should be the same.
499          * <p>
500          * @param contact Contact to set instances
501          * @param other Other contact to get instances from
502          */
503         protected void setAllContactPhoneEntries (final Contact contact, final Contact other) {
504                 // Trace message
505                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntries: contact={1},other={2} - CALLED!", this.getClass().getSimpleName(), contact, other)); //NOI18N
506
507                 // Both must be the same and not null
508                 if (null == contact) {
509                         // Throw NPE
510                         throw new NullPointerException("contact is null"); //NOI18N
511                 } else if (null == other) {
512                         // Throw NPE
513                         throw new NullPointerException("other is null"); //NOI18N
514                 } else if (!Objects.equals(contact, other)) {
515                         // Not same instances
516                         throw new IllegalArgumentException(MessageFormat.format("contact={0} and other={1} are not equal!", contact, other)); //NOI18N
517                 }
518
519                 // Debug message
520                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: other.contactMobileNumber={0}", other.getContactMobileNumber())); //NOI18N
521
522                 // Is other cellphone not set?
523                 if ((other.getContactMobileNumber() == null) || (PhoneUtils.isSameMobileNumber(contact.getContactMobileNumber(), other.getContactMobileNumber()))) {
524                         // Debug message
525                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: Copying cellphone entry ...", this.getClass().getSimpleName())); //NOI18N
526
527                         // Is the fax number set?
528                         if (other.getContactMobileNumber() instanceof DialableMobileNumber) {
529                                 // Copy cellphone number
530                                 contact.setContactMobileNumber(this.getManaged(other.getContactMobileNumber(), contact.getContactMobileNumber()));
531                         } else {
532                                 // Null it
533                                 contact.setContactMobileNumber(null);
534                         }
535                 }
536
537                 // Debug message
538                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: other.contactLandLineNumber={0}", other.getContactLandLineNumber())); //NOI18N
539
540                 // Is other cellphone not set?
541                 if ((other.getContactLandLineNumber() == null) || (PhoneUtils.isSameLandLineNumber(contact.getContactLandLineNumber(), other.getContactLandLineNumber()))) {
542                         // Debug message
543                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: Copying land-line entry ...", this.getClass().getSimpleName())); //NOI18N
544
545                         // Is the land-line number set?
546                         if (other.getContactLandLineNumber() instanceof DialableLandLineNumber) {
547                                 // Copy land-line number
548                                 contact.setContactLandLineNumber(this.getManaged(other.getContactLandLineNumber(), contact.getContactLandLineNumber()));
549                         } else {
550                                 // Null it
551                                 contact.setContactLandLineNumber(null);
552                         }
553                 }
554
555                 // Debug message
556                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: other.contactFaxNumber={1}", this.getClass().getSimpleName(), other.getContactFaxNumber())); //NOI18N
557
558                 // Is other cellphone not set?
559                 if ((other.getContactFaxNumber() == null) || (PhoneUtils.isSameFaxNumber(contact.getContactFaxNumber(), other.getContactFaxNumber()))) {
560                         // Debug message
561                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: Copying fax entry ...", this.getClass().getSimpleName())); //NOI18N
562
563                         // Is the fax number set?
564                         if (other.getContactFaxNumber() instanceof DialableFaxNumber) {
565                                 // Copy fax number
566                                 contact.setContactFaxNumber(this.getManaged(other.getContactFaxNumber(), contact.getContactFaxNumber()));
567                         } else {
568                                 // Null it
569                                 contact.setContactFaxNumber(null);
570                         }
571                 }
572
573                 // Trace message
574                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntries: EXIT!", this.getClass().getSimpleName())); //NOI18N
575         }
576
577         /**
578          * Updates all contact's phone entry's updated timestamps
579          * <p>
580          * @param contact Contact instance to update
581          * @param isMobileUnlinked Whether a cellphone entry has been unlinked in
582          * contact instance
583          * @param isLandlineUnlinked Whether a land-line entry has been unlinked in
584          * contact instance
585          * @param isFaxUnlinked Whether a fax entry has been unlinked in contact
586          * instance
587          */
588         protected void setAllContactPhoneEntriesUpdated (final Contact contact, final boolean isMobileUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
589                 // Trace message
590                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: contact={1},isMobileUnlinked={2},isLandlineUnlinked={3},isFaxUnlinked={4} - CALLED", this.getClass().getSimpleName(), contact, isMobileUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N
591
592                 // The contact instance must be valid
593                 if (null == contact) {
594                         // Throw NPE again
595                         throw new NullPointerException("contact is null"); //NOI18N
596                 } else if (contact.getContactId() == null) {
597                         // Throw NPE again
598                         throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
599                 } else if (contact.getContactId() < 1) {
600                         // Not valid
601                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
602                 }
603
604                 // Get all phone instances
605                 DialableLandLineNumber landLineNumber = contact.getContactLandLineNumber();
606                 DialableFaxNumber faxNumber = contact.getContactFaxNumber();
607                 DialableMobileNumber mobileNumber = contact.getContactMobileNumber();
608
609                 // Flags and instances must be constistent
610                 if (isMobileUnlinked && mobileNumber instanceof DialableMobileNumber) {
611                         // Bad state
612                         throw new IllegalStateException("isCellPhoneUnlinked is TRUE, but mobileNumber is set."); //NOI18N
613                 } else if (isLandlineUnlinked && landLineNumber instanceof DialableLandLineNumber) {
614                         // Bad state
615                         throw new IllegalStateException("isLandlineUnlinked is TRUE, but landLineNumber is set."); //NOI18N
616                 } else if (isFaxUnlinked && faxNumber instanceof DialableFaxNumber) {
617                         // Bad state
618                         throw new IllegalStateException("isFaxUnlinked is TRUE, but faxNumber is set."); //NOI18N
619                 }
620
621                 // Is a phone number instance set?
622                 if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() instanceof Long) && (landLineNumber.getPhoneId() > 0)) {
623                         // Debug message
624                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: Setting updated timestamp for land-line number ...", this.getClass().getSimpleName())); //NOI18N
625
626                         // Set updated timestamp
627                         landLineNumber.setPhoneEntryUpdated(new GregorianCalendar());
628                 }
629
630                 // Is a fax number instance set?
631                 if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneId() instanceof Long) && (faxNumber.getPhoneId() > 0)) {
632                         // Debug message
633                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: Setting updated timestamp for fax number ...", this.getClass().getSimpleName())); //NOI18N
634
635                         // Set updated timestamp
636                         faxNumber.setPhoneEntryUpdated(new GregorianCalendar());
637                 }
638
639                 // Is a mobile number instance set?
640                 if ((mobileNumber instanceof DialableMobileNumber) && (mobileNumber.getPhoneId() instanceof Long) && (mobileNumber.getPhoneId() > 0)) {
641                         // Debug message
642                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: Setting updated timestamp for cellphone number ...", this.getClass().getSimpleName())); //NOI18N
643
644                         // Set updated timestamp
645                         mobileNumber.setPhoneEntryUpdated(new GregorianCalendar());
646                 }
647
648                 // Trace message
649                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: EXIT!", this.getClass().getSimpleName())); //NOI18N
650         }
651
652 }