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