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