]> git.mxchange.org Git - addressbook-core.git/blob - src/org/mxchange/addressbook/database/BaseAddressbookDatabaseBean.java
Continued:
[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 contact'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("setAllContactPhoneEntriesCreated: contact={0} - CALLED!", 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("setAllContactPhoneEntriesCreated: landLineNumber={0},faxNumber={1},mobileNumber={2}", 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("setAllContactPhoneEntriesCreated: Setting created timestamp for land-line number ..."); //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("setAllContactPhoneEntriesCreated: Setting created timestamp for fax number ..."); //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("setAllContactPhoneEntriesCreated: Setting created timestamp for cellphone number ..."); //NOI18N
104
105                         // Set updated timestamp
106                         mobileNumber.setPhoneEntryCreated(new GregorianCalendar());
107                 }
108
109                 // Trace message
110                 this.getLoggerBeanLocal().logTrace("setAllContactPhoneEntriesCreated: EXIT!"); //NOI18N
111         }
112
113         /**
114          * Returns a detached instance from given cellphone instance
115          * <p>
116          * @param mobileNumber Mobile instance
117          * @param fetchedNumber Found cellphone number in database
118          * <p>
119          * @return Detached instance
120          */
121         protected DialableMobileNumber getDetached (final DialableMobileNumber mobileNumber, final DialableMobileNumber fetchedNumber) {
122                 // Trace message
123                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: mobileNumber={0},fetchedNumber={1} - CALLED!", 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 (fetchedNumber.getPhoneId() == null) {
130                         // ..and again
131                         throw new NullPointerException("fetchedNumber.phoneId is null"); //NOI18N
132                 }
133
134                 // Debug message
135                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); //NOI18N
136
137                 // Init query instance
138                 DialableMobileNumber foundNumber = this.getEntityManager().find(fetchedNumber.getClass(), fetchedNumber.getPhoneId());
139
140                 // Debug message
141                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); //NOI18N
142
143                 // Default is null
144                 DialableMobileNumber detachedNumber = null;
145
146                 // Is there a difference?
147                 if (!PhoneUtils.isSameMobileNumber(mobileNumber, fetchedNumber)) {
148                         // Merge this entry
149                         detachedNumber = this.getEntityManager().merge(foundNumber);
150
151                         // Copy all
152                 }
153
154                 // Trace message
155                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: detachedNumber={0} - EXIT!", detachedNumber)); //NOI18N
156
157                 // Return it
158                 return detachedNumber;
159         }
160
161         /**
162          * Returns a detached instance from given land-line instance
163          * <p>
164          * @param landLineNumber Land-line instance
165          * @param fetchedNumber Found land-line number in database
166          * <p>
167          * @return Detached instance
168          */
169         protected DialableLandLineNumber getDetached (final DialableLandLineNumber landLineNumber, final DialableLandLineNumber fetchedNumber) {
170                 // Trace message
171                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: landLineNumber={0},fetchedNumber={1} - CALLED!", landLineNumber, fetchedNumber)); //NOI18N
172
173                 // Should be valid
174                 if (null == landLineNumber) {
175                         // Throw NPE
176                         throw new NullPointerException("landLineNumber is null"); //NOI18N
177                 } else if (fetchedNumber.getPhoneId() == null) {
178                         // ..and again
179                         throw new NullPointerException("landLineNumber.phoneId is null"); //NOI18N
180                 }
181
182                 // Debug message
183                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); //NOI18N
184
185                 // Init query instance
186                 DialableLandLineNumber foundNumber = this.getEntityManager().find(fetchedNumber.getClass(), fetchedNumber.getPhoneId());
187
188                 // Debug message
189                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); //NOI18N
190
191                 // Default is null
192                 DialableLandLineNumber detachedNumber = null;
193
194                 // Is there a difference?
195                 if (!PhoneUtils.isSameLandLineNumber(landLineNumber, fetchedNumber)) {
196                         // Merge this entry
197                         detachedNumber = this.getEntityManager().merge(foundNumber);
198                 }
199
200                 // Trace message
201                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: detachedNumber={0} - EXIT!", detachedNumber)); //NOI18N
202
203                 // Return it
204                 return detachedNumber;
205         }
206
207         /**
208          * Returns a detached instance from given fax instance
209          * <p>
210          * @param faxNumber Fax instance
211          * @param fetchedNumber Found fax number in database
212          * <p>
213          * @return Detached instance
214          */
215         protected DialableFaxNumber getDetached (final DialableFaxNumber faxNumber, final DialableFaxNumber fetchedNumber) {
216                 // Trace message
217                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: faxNumber={0},fetchedNumber={1} - CALLED!", faxNumber, fetchedNumber)); //NOI18N
218
219                 // Should be valid
220                 if (null == faxNumber) {
221                         // Throw NPE
222                         throw new NullPointerException("faxNumber is null"); //NOI18N
223                 } else if (fetchedNumber.getPhoneId() == null) {
224                         // ..and again
225                         throw new NullPointerException("fetchedNumber.phoneId is null"); //NOI18N
226                 }
227
228                 // Debug message
229                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); //NOI18N
230
231                 // Init query instance
232                 DialableFaxNumber foundNumber = this.getEntityManager().find(fetchedNumber.getClass(), fetchedNumber.getPhoneId());
233
234                 // Debug message
235                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); //NOI18N
236
237                 // Default is null
238                 DialableFaxNumber detachedNumber = null;
239
240                 // Is there a difference?
241                 if (!PhoneUtils.isSameFaxNumber(faxNumber, fetchedNumber)) {
242                         // Merge this entry
243                         detachedNumber = this.getEntityManager().merge(foundNumber);
244                 }
245
246                 // Trace message
247                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: detachedNumber={0} - EXIT!", detachedNumber)); //NOI18N
248
249                 // Return it
250                 return detachedNumber;
251         }
252
253         /**
254          * Get back a managed instance from given user
255          * <p>
256          * @param user Unmanaged/detached user instance
257          * <p>
258          * @return Managed user instance
259          */
260         protected User getManagedUser (final User user) {
261                 // Trace message
262                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getManaged: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
263
264                 // user should not be null
265                 if (null == user) {
266                         // Abort here
267                         throw new NullPointerException("user is null"); //NOI18N
268                 } else if (user.getUserId() == null) {
269                         // Id is set
270                         throw new NullPointerException("user.userId is null"); //NOI18N
271                 } else if (user.getUserId() < 1) {
272                         // Id is set
273                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is null", user.getUserId())); //NOI18N
274                 } else if (user.getUserContact() == null) {
275                         // Throw NPE again
276                         throw new NullPointerException("user.userContact is null"); //NOI18N
277                 } else if (user.getUserContact().getContactId() == null) {
278                         // Throw NPE again
279                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
280                 } else if (user.getUserContact().getContactId() < 1) {
281                         // Not valid id number
282                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N#
283                 }
284
285                 // Try to find it (should be there)
286                 User managedUser = this.getEntityManager().find(LoginUser.class, user.getUserId());
287
288                 // Should be there
289                 assert (managedUser instanceof User) : "managedUser is null"; //NOI18N
290
291                 // Trace message
292                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getManaged: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
293
294                 // Return it
295                 return managedUser;
296         }
297
298         /**
299          * Merges given contact's data
300          * <p>
301          * @param contact Contact instance to merge
302          * <p>
303          * @return Detached contact instance
304          */
305         protected Contact mergeContactData (final Contact contact) {
306                 // Trace message
307                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeContactData: contact={0} - CALLED!", contact)); //NOI18N
308
309                 // The contact instance must be valid
310                 if (null == contact) {
311                         // Throw NPE again
312                         throw new NullPointerException("contact is null"); //NOI18N
313                 } else if (contact.getContactId() == null) {
314                         // Throw NPE again
315                         throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
316                 } else if (contact.getContactId() < 1) {
317                         // Not valid
318                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
319                 }
320
321                 // Set updated timestamp
322                 contact.setContactUpdated(new GregorianCalendar());
323
324                 // Get contact from it and find it
325                 Contact foundContact = this.getEntityManager().find(contact.getClass(), contact.getContactId());
326
327                 // Should be found
328                 assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", contact.getContactId()); //NOI18N
329
330                 // Debug message
331                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: foundContact.contactId={0}", foundContact.getContactId())); //NOI18N
332
333                 // Merge contact instance
334                 Contact detachedContact = this.getEntityManager().merge(foundContact);
335
336                 // Copy all
337                 detachedContact.copyAll(contact);
338
339                 // Trace message
340                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N
341
342                 // Return detached contact
343                 return detachedContact;
344         }
345
346         /**
347          * Merges given (detached) contact's cellphone, land-line and fax numbers
348          * <p>
349          * @param detachedContact Detached contact instance
350          */
351         protected void mergeContactsMobileLandLineFaxNumbers (final Contact detachedContact) {
352                 // Trace message
353                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeContactsMobileLandLineFaxNumbers: detachedContact={0} - CALLED!", detachedContact)); //NOI18N
354
355                 // The contact instance must be valid
356                 if (null == detachedContact) {
357                         // Throw NPE again
358                         throw new NullPointerException("detachedContact is null"); //NOI18N
359                 } else if (detachedContact.getContactId() == null) {
360                         // Throw NPE again
361                         throw new NullPointerException("detachedContact.contactId is null"); //NOI18N //NOI18N
362                 } else if (detachedContact.getContactId() < 1) {
363                         // Not valid
364                         throw new IllegalStateException(MessageFormat.format("detachedContact.contactId={0} is not valid.", detachedContact.getContactId())); //NOI18N
365                 }
366
367                 // Get all instances
368                 DialableMobileNumber cellphone = detachedContact.getContactMobileNumber();
369                 DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
370                 DialableFaxNumber fax = detachedContact.getContactFaxNumber();
371
372                 // Is there a  cellphone instance set?
373                 if (cellphone instanceof DialableMobileNumber) {
374                         // Debug message
375                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId())); //NOI18N
376
377                         // Then find it, too
378                         DialableMobileNumber foundMobile = this.getEntityManager().find(cellphone.getClass(), cellphone.getPhoneId());
379
380                         // Should be there
381                         assert (foundMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", foundMobile.getPhoneId()); //NOI18N
382
383                         // Then merge it, too
384                         DialableMobileNumber detachedMobile = this.getEntityManager().merge(foundMobile);
385
386                         // Should be there
387                         assert (detachedMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", detachedMobile.getPhoneId()); //NOI18N
388
389                         // Copy all
390                         detachedMobile.copyAll(detachedContact.getContactMobileNumber());
391
392                         // Set it back
393                         detachedContact.setContactMobileNumber(detachedMobile);
394                 }
395
396                 // Is there a  fax instance set?
397                 if (fax instanceof DialableFaxNumber) {
398                         // Debug message
399                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); //NOI18N
400
401                         // Then find it, too
402                         DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId());
403
404                         // Should be there
405                         assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N
406
407                         // Then merge it, too
408                         DialableFaxNumber detachedFax = this.getEntityManager().merge(foundFax);
409
410                         // Should be there
411                         assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId()); //NOI18N
412
413                         // Copy all
414                         detachedFax.copyAll(detachedContact.getContactFaxNumber());
415
416                         // Set it back
417                         detachedContact.setContactFaxNumber(detachedFax);
418                 }
419
420                 // Is there a  fax instance set?
421                 if (landLine instanceof DialableLandLineNumber) {
422                         // Debug message
423                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); //NOI18N
424
425                         // Then find it, too
426                         DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId());
427
428                         // Should be there
429                         assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
430
431                         // Then merge it, too
432                         DialableLandLineNumber detachedLandLine = this.getEntityManager().merge(foundLandLine);
433
434                         // Should be there
435                         assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId()); //NOI18N
436
437                         // Copy all
438                         detachedLandLine.copyAll(detachedContact.getContactLandLineNumber());
439
440                         // Set it back
441                         detachedContact.setContactLandLineNumber(detachedLandLine);
442                 }
443
444                 // Trace message
445                 this.getLoggerBeanLocal().logTrace("mergeContactsMobileLandLineFaxNumbers: EXIT!"); //NOI18N
446         }
447
448         /**
449          * Sends an email with given subject line, template name to given recipient
450          * and user data
451          * <p>
452          * @param subjectLine Subject line
453          * @param templateName Template name
454          * @param emailAddress Recipient's email address
455          * @param user User instance
456          * @param baseUrl Base URL
457          */
458         protected void sendEmail (final String subjectLine, final String templateName, final Address emailAddress, final User user, final String baseUrl) {
459                 // Trace message
460                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("sendEmail: subjectLine={0},templateName={1},emailAddress={2},user={3},baseUrl={4} - CALLED!", subjectLine, templateName, emailAddress, user, baseUrl)); //NOI18N
461
462                 // All should be set
463                 if (null == subjectLine) {
464                         // Throw NPE
465                         throw new NullPointerException("subjectLine is null"); //NOI18N
466                 } else if (subjectLine.isEmpty()) {
467                         // No subject line
468                         throw new IllegalArgumentException("subjectLine is empty"); //NOI18N
469                 } else if (null == templateName) {
470                         // Throw NPE
471                         throw new NullPointerException("templateName is null"); //NOI18N
472                 } else if (templateName.isEmpty()) {
473                         // No template name
474                         throw new IllegalArgumentException("templateName is empty"); //NOI18N
475                 } else if (null == emailAddress) {
476                         // Throw NPE
477                         throw new NullPointerException("emailAddress is null"); //NOI18N
478                 }
479
480                 // Prepare mail wrapper
481                 WrapableEmailDelivery emailWrapper = new EmailDeliveryWrapper();
482
483                 // Set all values
484                 Properties variables = UserUtils.getAllUserFields(user);
485
486                 // Set base URL
487                 variables.put("baseUrl", baseUrl); //NOI18N
488
489                 // Set all
490                 // @TODO Language from message bundle
491                 emailWrapper.setRecipient(emailAddress);
492                 emailWrapper.setLocale(user.getUserLocale());
493                 emailWrapper.setSubjectLine(subjectLine);
494                 emailWrapper.setTemplateName(templateName);
495                 emailWrapper.setTemplateVariables(variables);
496
497                 try {
498                         // Send out email change
499                         ObjectMessage message = this.getSession().createObjectMessage();
500                         message.setObject(emailWrapper);
501
502                         // Send message
503                         this.sendMessage(message);
504                 } catch (final JMSException ex) {
505                         // Throw again
506                         throw new EJBException(ex);
507                 }
508
509                 // Trace message
510                 this.getLoggerBeanLocal().logTrace("sendEmail: EXIT!"); //NOI18N
511         }
512
513         /**
514          * Updates all contact's phone instances from other contact, both contacts
515          * should be the same.
516          * <p>
517          * @param contact Contact to set instances
518          * @param other Other contact to get instances from
519          */
520         protected void setAllContactPhoneEntries (final Contact contact, final Contact other) {
521                 // Trace message
522                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("setAllContactPhoneEntries: contact={0},other={1} - CALLED!", contact, other)); //NOI18N
523
524                 // Both must be the same and not null
525                 if (null == contact) {
526                         // Throw NPE
527                         throw new NullPointerException("contact is null"); //NOI18N
528                 } else if (null == other) {
529                         // Throw NPE
530                         throw new NullPointerException("other is null"); //NOI18N
531                 } else if (!Objects.equals(contact, other)) {
532                         // Not same instances
533                         throw new IllegalArgumentException(MessageFormat.format("contact={0} and other={1} are not equal!", contact, other)); //NOI18N
534                 }
535
536                 // Debug message
537                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllContactPhoneEntries: other.contactMobileNumber={0}", other.getContactMobileNumber())); //NOI18N
538
539                 // Is other cellphone not set?
540                 if ((other.getContactMobileNumber() == null) || (PhoneUtils.isSameMobileNumber(contact.getContactMobileNumber(), other.getContactMobileNumber()))) {
541                         // Debug message
542                         this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntries: Copying cellphone entry ..."); //NOI18N
543
544                         // Is the fax number set?
545                         if (other.getContactMobileNumber() instanceof DialableMobileNumber) {
546                                 // Copy cellphone number
547                                 contact.setContactMobileNumber(this.getDetached(other.getContactMobileNumber(), contact.getContactMobileNumber()));
548                         } else {
549                                 // Null it
550                                 contact.setContactMobileNumber(null);
551                         }
552                 }
553
554                 // Debug message
555                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllContactPhoneEntries: other.contactLandLineNumber={0}", other.getContactLandLineNumber())); //NOI18N
556
557                 // Is other cellphone not set?
558                 if ((other.getContactLandLineNumber() == null) || (PhoneUtils.isSameLandLineNumber(contact.getContactLandLineNumber(), other.getContactLandLineNumber()))) {
559                         // Debug message
560                         this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntries: Copying land-line entry ..."); //NOI18N
561
562                         // Is the land-line number set?
563                         if (other.getContactLandLineNumber() instanceof DialableLandLineNumber) {
564                                 // Copy land-line number
565                                 contact.setContactLandLineNumber(this.getDetached(other.getContactLandLineNumber(), contact.getContactLandLineNumber()));
566                         } else {
567                                 // Null it
568                                 contact.setContactLandLineNumber(null);
569                         }
570                 }
571
572                 // Debug message
573                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllContactPhoneEntries: other.contactFaxNumber={0}", other.getContactFaxNumber())); //NOI18N
574
575                 // Is other cellphone not set?
576                 if ((other.getContactFaxNumber() == null) || (PhoneUtils.isSameFaxNumber(contact.getContactFaxNumber(), other.getContactFaxNumber()))) {
577                         // Debug message
578                         this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntries: Copying fax entry ..."); //NOI18N
579
580                         // Is the fax number set?
581                         if (other.getContactFaxNumber() instanceof DialableFaxNumber) {
582                                 // Copy fax number
583                                 contact.setContactFaxNumber(this.getDetached(other.getContactFaxNumber(), contact.getContactFaxNumber()));
584                         } else {
585                                 // Null it
586                                 contact.setContactFaxNumber(null);
587                         }
588                 }
589
590                 // Trace message
591                 this.getLoggerBeanLocal().logTrace("setAllContactPhoneEntries: EXIT!"); //NOI18N
592         }
593
594         /**
595          * Updates all contact's phone entry's updated timestamps
596          * <p>
597          * @param contact Contact instance to update
598          * @param isMobileUnlinked Whether a cellphone entry has been unlinked in
599          * contact instance
600          * @param isLandlineUnlinked Whether a land-line entry has been unlinked in
601          * contact instance
602          * @param isFaxUnlinked Whether a fax entry has been unlinked in contact
603          * instance
604          */
605         protected void setAllContactPhoneEntriesUpdated (final Contact contact, final boolean isMobileUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
606                 // Trace message
607                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("setAllContactPhoneEntriesUpdated: contact={0},isMobileUnlinked={1},isLandlineUnlinked={2},isFaxUnlinked={3} - CALLED", contact, isMobileUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N
608
609                 // The contact instance must be valid
610                 if (null == contact) {
611                         // Throw NPE again
612                         throw new NullPointerException("contact is null"); //NOI18N
613                 } else if (contact.getContactId() == null) {
614                         // Throw NPE again
615                         throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
616                 } else if (contact.getContactId() < 1) {
617                         // Not valid
618                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
619                 }
620
621                 // Get all phone instances
622                 DialableLandLineNumber landLineNumber = contact.getContactLandLineNumber();
623                 DialableFaxNumber faxNumber = contact.getContactFaxNumber();
624                 DialableMobileNumber mobileNumber = contact.getContactMobileNumber();
625
626                 // Flags and instances must be constistent
627                 if (isMobileUnlinked && mobileNumber instanceof DialableMobileNumber) {
628                         // Bad state
629                         throw new IllegalStateException("isCellPhoneUnlinked is TRUE, but mobileNumber is set."); //NOI18N
630                 } else if (isLandlineUnlinked && landLineNumber instanceof DialableLandLineNumber) {
631                         // Bad state
632                         throw new IllegalStateException("isLandlineUnlinked is TRUE, but landLineNumber is set."); //NOI18N
633                 } else if (isFaxUnlinked && faxNumber instanceof DialableFaxNumber) {
634                         // Bad state
635                         throw new IllegalStateException("isFaxUnlinked is TRUE, but faxNumber is set."); //NOI18N
636                 }
637
638                 // Is a phone number instance set?
639                 if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() instanceof Long) && (landLineNumber.getPhoneId() > 0)) {
640                         // Debug message
641                         this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesUpdated: Setting updated timestamp for land-line number ..."); //NOI18N
642
643                         // Set updated timestamp
644                         landLineNumber.setPhoneEntryUpdated(new GregorianCalendar());
645                 }
646
647                 // Is a fax number instance set?
648                 if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneId() instanceof Long) && (faxNumber.getPhoneId() > 0)) {
649                         // Debug message
650                         this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesUpdated: Setting updated timestamp for fax number ..."); //NOI18N
651
652                         // Set updated timestamp
653                         faxNumber.setPhoneEntryUpdated(new GregorianCalendar());
654                 }
655
656                 // Is a mobile number instance set?
657                 if ((mobileNumber instanceof DialableMobileNumber) && (mobileNumber.getPhoneId() instanceof Long) && (mobileNumber.getPhoneId() > 0)) {
658                         // Debug message
659                         this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesUpdated: Setting updated timestamp for cellphone number ..."); //NOI18N
660
661                         // Set updated timestamp
662                         mobileNumber.setPhoneEntryUpdated(new GregorianCalendar());
663                 }
664
665                 // Trace message
666                 this.getLoggerBeanLocal().logTrace("setAllContactPhoneEntriesUpdated: EXIT!"); //NOI18N
667         }
668
669 }