]> git.mxchange.org Git - addressbook-core.git/blob - src/org/mxchange/addressbook/database/BaseAddressbookDatabaseBean.java
tpzo fixed ...
[addressbook-core.git] / src / org / mxchange / addressbook / database / BaseAddressbookDatabaseBean.java
1 /*
2  * Copyright (C) 2016 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.faces.FacesException;
25 import javax.jms.Connection;
26 import javax.jms.JMSException;
27 import javax.jms.MessageProducer;
28 import javax.jms.ObjectMessage;
29 import javax.jms.Queue;
30 import javax.jms.QueueConnectionFactory;
31 import javax.jms.Session;
32 import javax.mail.Address;
33 import javax.naming.Context;
34 import javax.naming.InitialContext;
35 import javax.naming.NamingException;
36 import org.mxchange.jcontacts.contact.Contact;
37 import org.mxchange.jcoreee.database.BaseDatabaseBean;
38 import org.mxchange.jmailee.model.delivery.wrapper.EmailDeliveryWrapper;
39 import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery;
40 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
41 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
42 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
43 import org.mxchange.jphone.utils.PhoneUtils;
44 import org.mxchange.jusercore.model.user.LoginUser;
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 Häder<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/addressbook-queue-factory"); //NOI18N
93
94                         // Lookup queue
95                         this.queue = (Queue) context.lookup("jms/addressbook-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 contact'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                 DialableMobileNumber mobileNumber = contact.getContactMobileNumber();
130
131                 // Debug message
132                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllContactPhoneEntriesCreated: landLineNumber={0},faxNumber={1},cellphoneNumber={2}", landLineNumber, faxNumber, mobileNumber)); //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 ((mobileNumber instanceof DialableMobileNumber) && (mobileNumber.getPhoneId() == null)) {
154                         // Debug message
155                         this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesCreated: Setting created timestamp for cellphone number ..."); //NOI18N
156
157                         // Set updated timestamp
158                         mobileNumber.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 mobileNumber Mobile instance
169          * @param fetchedNumber Found cellphone number in database
170          * <p>
171          * @return Detached instance
172          */
173         protected DialableMobileNumber getDetached (final DialableMobileNumber mobileNumber, final DialableMobileNumber fetchedNumber) {
174                 // Trace message
175                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: cellphoneNumber={0},fetchedNumber={1} - CALLED!", mobileNumber, fetchedNumber)); //NOI18N
176
177                 // Should be valid
178                 if (null == mobileNumber) {
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                 DialableMobileNumber 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                 DialableMobileNumber detachedNumber = null;
197
198                 // Is there a difference?
199                 if (!PhoneUtils.isSameMobileNumber(mobileNumber, 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          * Get back a managed instance from given user
307          * <p>
308          * @param user Unmanaged/detached user instance
309          * <p>
310          * @return Managed user instance
311          */
312         protected User getManagedUser (final User user) {
313                 // Trace message
314                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getManaged: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
315
316                 // user should not be null
317                 if (null == user) {
318                         // Abort here
319                         throw new NullPointerException("user is null"); //NOI18N
320                 } else if (user.getUserId() == null) {
321                         // Id is set
322                         throw new NullPointerException("user.userId is null"); //NOI18N
323                 } else if (user.getUserId() < 1) {
324                         // Id is set
325                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is null", user.getUserId())); //NOI18N
326                 } else if (user.getUserContact() == null) {
327                         // Throw NPE again
328                         throw new NullPointerException("user.userContact is null"); //NOI18N
329                 } else if (user.getUserContact().getContactId() == null) {
330                         // Throw NPE again
331                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
332                 } else if (user.getUserContact().getContactId() < 1) {
333                         // Not valid id number
334                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N#
335                 }
336
337                 // Try to find it (should be there)
338                 User managedUser = this.getEntityManager().find(LoginUser.class, user.getUserId());
339
340                 // Should be there
341                 assert (managedUser instanceof User) : "managedUser is null"; //NOI18N
342
343                 // Trace message
344                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getManaged: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
345
346                 // Return it
347                 return managedUser;
348         }
349
350         /**
351          * Merges given contact's data
352          * <p>
353          * @param contact Contact instance to merge
354          * <p>
355          * @return Detached contact instance
356          */
357         protected Contact mergeContactData (final Contact contact) {
358                 // Trace message
359                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeContactData: contact={0} - CALLED!", contact)); //NOI18N
360
361                 // The contact instance must be valid
362                 if (null == contact) {
363                         // Throw NPE again
364                         throw new NullPointerException("contact is null"); //NOI18N
365                 } else if (contact.getContactId() == null) {
366                         // Throw NPE again
367                         throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
368                 } else if (contact.getContactId() < 1) {
369                         // Not valid
370                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
371                 }
372
373                 // Set updated timestamp
374                 contact.setContactUpdated(new GregorianCalendar());
375
376                 // Get contact from it and find it
377                 Contact foundContact = this.getEntityManager().find(contact.getClass(), contact.getContactId());
378
379                 // Should be found
380                 assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", contact.getContactId()); //NOI18N
381
382                 // Debug message
383                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: foundContact.contactId={0}", foundContact.getContactId())); //NOI18N
384
385                 // Merge contact instance
386                 Contact detachedContact = this.getEntityManager().merge(foundContact);
387
388                 // Copy all
389                 detachedContact.copyAll(contact);
390
391                 // Trace message
392                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N
393
394                 // Return detached contact
395                 return detachedContact;
396         }
397
398         /**
399          * Merges given (detached) contact's cellphone, land-line and fax numbers
400          * <p>
401          * @param detachedContact Detached contact instance
402          */
403         protected void mergeContactsMobileLandLineFaxNumbers (final Contact detachedContact) {
404                 // Trace message
405                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeContactsMobileLandLineFaxNumbers: detachedContact={0} - CALLED!", detachedContact)); //NOI18N
406
407                 // The contact instance must be valid
408                 if (null == detachedContact) {
409                         // Throw NPE again
410                         throw new NullPointerException("detachedContact is null"); //NOI18N
411                 } else if (detachedContact.getContactId() == null) {
412                         // Throw NPE again
413                         throw new NullPointerException("detachedContact.contactId is null"); //NOI18N //NOI18N
414                 } else if (detachedContact.getContactId() < 1) {
415                         // Not valid
416                         throw new IllegalStateException(MessageFormat.format("detachedContact.contactId={0} is not valid.", detachedContact.getContactId())); //NOI18N
417                 }
418
419                 // Get all instances
420                 DialableMobileNumber cellphone = detachedContact.getContactMobileNumber();
421                 DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
422                 DialableFaxNumber fax = detachedContact.getContactFaxNumber();
423
424                 // Is there a  cellphone instance set?
425                 if (cellphone instanceof DialableMobileNumber) {
426                         // Debug message
427                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId())); //NOI18N
428
429                         // Then find it, too
430                         DialableMobileNumber foundMobile = this.getEntityManager().find(cellphone.getClass(), cellphone.getPhoneId());
431
432                         // Should be there
433                         assert (foundMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", foundMobile.getPhoneId()); //NOI18N
434
435                         // Then merge it, too
436                         DialableMobileNumber detachedMobile = this.getEntityManager().merge(foundMobile);
437
438                         // Should be there
439                         assert (detachedMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", detachedMobile.getPhoneId()); //NOI18N
440
441                         // Copy all
442                         detachedMobile.copyAll(detachedContact.getContactMobileNumber());
443
444                         // Set it back
445                         detachedContact.setContactMobileNumber(detachedMobile);
446                 }
447
448                 // Is there a  fax instance set?
449                 if (fax instanceof DialableFaxNumber) {
450                         // Debug message
451                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); //NOI18N
452
453                         // Then find it, too
454                         DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId());
455
456                         // Should be there
457                         assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N
458
459                         // Then merge it, too
460                         DialableFaxNumber detachedFax = this.getEntityManager().merge(foundFax);
461
462                         // Should be there
463                         assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId()); //NOI18N
464
465                         // Copy all
466                         detachedFax.copyAll(detachedContact.getContactFaxNumber());
467
468                         // Set it back
469                         detachedContact.setContactFaxNumber(detachedFax);
470                 }
471
472                 // Is there a  fax instance set?
473                 if (landLine instanceof DialableLandLineNumber) {
474                         // Debug message
475                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); //NOI18N
476
477                         // Then find it, too
478                         DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId());
479
480                         // Should be there
481                         assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
482
483                         // Then merge it, too
484                         DialableLandLineNumber detachedLandLine = this.getEntityManager().merge(foundLandLine);
485
486                         // Should be there
487                         assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId()); //NOI18N
488
489                         // Copy all
490                         detachedLandLine.copyAll(detachedContact.getContactLandLineNumber());
491
492                         // Set it back
493                         detachedContact.setContactLandLineNumber(detachedLandLine);
494                 }
495
496                 // Trace message
497                 this.getLoggerBeanLocal().logTrace("mergeContactsMobileLandLineFaxNumbers: EXIT!"); //NOI18N
498         }
499
500         /**
501          * Sends an email with given subject line, template name to given recipient
502          * and user data
503          * <p>
504          * @param subjectLine Subject line
505          * @param templateName Template name
506          * @param emailAddress Recipient's email address
507          * @param user User instance
508          * @param baseUrl Base URL
509          */
510         protected void sendEmail (final String subjectLine, final String templateName, final Address emailAddress, final User user, final String baseUrl) {
511                 // Trace message
512                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("sendEmail: subjectLine={0},templateName={1},emailAddress={2},user={3},baseUrl={4} - CALLED!", subjectLine, templateName, emailAddress, user, baseUrl)); //NOI18N
513
514                 // All should be set
515                 if (null == subjectLine) {
516                         // Throw NPE
517                         throw new NullPointerException("subjectLine is null"); //NOI18N
518                 } else if (subjectLine.isEmpty()) {
519                         // No subject line
520                         throw new IllegalArgumentException("subjectLine is empty"); //NOI18N
521                 } else if (null == templateName) {
522                         // Throw NPE
523                         throw new NullPointerException("templateName is null"); //NOI18N
524                 } else if (templateName.isEmpty()) {
525                         // No template name
526                         throw new IllegalArgumentException("templateName is empty"); //NOI18N
527                 } else if (null == emailAddress) {
528                         // Throw NPE
529                         throw new NullPointerException("emailAddress is null"); //NOI18N
530                 }
531
532                 // Prepare mail wrapper
533                 WrapableEmailDelivery emailWrapper = new EmailDeliveryWrapper();
534
535                 // Set all values
536                 Properties variables = UserUtils.getAllUserFields(user);
537
538                 // Set base URL
539                 variables.put("baseUrl", baseUrl); //NOI18N
540
541                 // Set all
542                 // @TODO Language from message bundle
543                 emailWrapper.setRecipient(emailAddress);
544                 emailWrapper.setLocale(user.getUserLocale());
545                 emailWrapper.setSubjectLine(subjectLine);
546                 emailWrapper.setTemplateName(templateName);
547                 emailWrapper.setTemplateVariables(variables);
548
549                 try {
550                         // Send out email change
551                         ObjectMessage message = this.session.createObjectMessage();
552                         message.setObject(emailWrapper);
553
554                         // Send message
555                         this.sendMessage(message, this.messageProducer);
556                 } catch (final JMSException ex) {
557                         // Throw again
558                         throw new EJBException(ex);
559                 }
560
561                 // Trace message
562                 this.getLoggerBeanLocal().logTrace("sendEmail: EXIT!"); //NOI18N
563         }
564
565         /**
566          * Updates all contact's phone instances from other contact, both contacts
567          * should be the same.
568          * <p>
569          * @param contact Contact to set instances
570          * @param other Other contact to get instances from
571          */
572         protected void setAllContactPhoneEntries (final Contact contact, final Contact other) {
573                 // Trace message
574                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("setAllContactPhoneEntries: contact={0},other={1} - CALLED!", contact, other)); //NOI18N
575
576                 // Both must be the same and not null
577                 if (null == contact) {
578                         // Throw NPE
579                         throw new NullPointerException("contact is null"); //NOI18N
580                 } else if (null == other) {
581                         // Throw NPE
582                         throw new NullPointerException("other is null"); //NOI18N
583                 } else if (!Objects.equals(contact, other)) {
584                         // Not same instances
585                         throw new IllegalArgumentException(MessageFormat.format("contact={0} and other={1} are not equal!", contact, other)); //NOI18N
586                 }
587
588                 // Debug message
589                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllContactPhoneEntries: other.contactMobileNumber={0}", other.getContactMobileNumber())); //NOI18N
590
591                 // Is other cellphone not set?
592                 if ((other.getContactMobileNumber() == null) || (PhoneUtils.isSameMobileNumber(contact.getContactMobileNumber(), other.getContactMobileNumber()))) {
593                         // Debug message
594                         this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntries: Copying cellphone entry ..."); //NOI18N
595
596                         // Is the fax number set?
597                         if (other.getContactMobileNumber() instanceof DialableMobileNumber) {
598                                 // Copy cellphone number
599                                 contact.setContactMobileNumber(this.getDetached(other.getContactMobileNumber(), contact.getContactMobileNumber()));
600                         } else {
601                                 // Null it
602                                 contact.setContactMobileNumber(null);
603                         }
604                 }
605
606                 // Debug message
607                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllContactPhoneEntries: other.contactLandLineNumber={0}", other.getContactLandLineNumber())); //NOI18N
608
609                 // Is other cellphone not set?
610                 if ((other.getContactLandLineNumber() == null) || (PhoneUtils.isSameLandLineNumber(contact.getContactLandLineNumber(), other.getContactLandLineNumber()))) {
611                         // Debug message
612                         this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntries: Copying land-line entry ..."); //NOI18N
613
614                         // Is the land-line number set?
615                         if (other.getContactLandLineNumber() instanceof DialableLandLineNumber) {
616                                 // Copy land-line number
617                                 contact.setContactLandLineNumber(this.getDetached(other.getContactLandLineNumber(), contact.getContactLandLineNumber()));
618                         } else {
619                                 // Null it
620                                 contact.setContactLandLineNumber(null);
621                         }
622                 }
623
624                 // Debug message
625                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllContactPhoneEntries: other.contactFaxNumber={0}", other.getContactFaxNumber())); //NOI18N
626
627                 // Is other cellphone not set?
628                 if ((other.getContactFaxNumber() == null) || (PhoneUtils.isSameFaxNumber(contact.getContactFaxNumber(), other.getContactFaxNumber()))) {
629                         // Debug message
630                         this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntries: Copying fax entry ..."); //NOI18N
631
632                         // Is the fax number set?
633                         if (other.getContactFaxNumber() instanceof DialableFaxNumber) {
634                                 // Copy fax number
635                                 contact.setContactFaxNumber(this.getDetached(other.getContactFaxNumber(), contact.getContactFaxNumber()));
636                         } else {
637                                 // Null it
638                                 contact.setContactFaxNumber(null);
639                         }
640                 }
641
642                 // Trace message
643                 this.getLoggerBeanLocal().logTrace("setAllContactPhoneEntries: EXIT!"); //NOI18N
644         }
645
646         /**
647          * Updates all contact's phone entry's updated timestamps
648          * <p>
649          * @param contact Contact instance to update
650          * @param isMobileUnlinked Whether a cellphone entry has been unlinked in
651          * contact instance
652          * @param isLandlineUnlinked Whether a land-line entry has been unlinked in
653          * contact instance
654          * @param isFaxUnlinked Whether a fax entry has been unlinked in contact
655          * instance
656          */
657         protected void setAllContactPhoneEntriesUpdated (final Contact contact, final boolean isMobileUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
658                 // Trace message
659                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("setAllContactPhoneEntriesUpdated: contact={0},isMobileUnlinked={1},isLandlineUnlinked={2},isFaxUnlinked={3} - CALLED", contact, isMobileUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N
660
661                 // The contact instance must be valid
662                 if (null == contact) {
663                         // Throw NPE again
664                         throw new NullPointerException("contact is null"); //NOI18N
665                 } else if (contact.getContactId() == null) {
666                         // Throw NPE again
667                         throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
668                 } else if (contact.getContactId() < 1) {
669                         // Not valid
670                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
671                 }
672
673                 // Get all phone instances
674                 DialableLandLineNumber landLineNumber = contact.getContactLandLineNumber();
675                 DialableFaxNumber faxNumber = contact.getContactFaxNumber();
676                 DialableMobileNumber cellphoneNumber = contact.getContactMobileNumber();
677
678                 // Flags and instances must be constistent
679                 if (isMobileUnlinked && cellphoneNumber instanceof DialableMobileNumber) {
680                         // Bad state
681                         throw new IllegalStateException("isCellPhoneUnlinked is TRUE, but cellphoneNumber is set."); //NOI18N
682                 } else if (isLandlineUnlinked && landLineNumber instanceof DialableLandLineNumber) {
683                         // Bad state
684                         throw new IllegalStateException("isLandlineUnlinked is TRUE, but landLineNumber is set."); //NOI18N
685                 } else if (isFaxUnlinked && faxNumber instanceof DialableFaxNumber) {
686                         // Bad state
687                         throw new IllegalStateException("isFaxUnlinked is TRUE, but faxNumber is set."); //NOI18N
688                 }
689
690                 // Is a phone number instance set?
691                 if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() instanceof Long) && (landLineNumber.getPhoneId() > 0)) {
692                         // Debug message
693                         this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesUpdated: Setting updated timestamp for land-line number ..."); //NOI18N
694
695                         // Set updated timestamp
696                         landLineNumber.setPhoneEntryUpdated(new GregorianCalendar());
697                 }
698
699                 // Is a fax number instance set?
700                 if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneId() instanceof Long) && (faxNumber.getPhoneId() > 0)) {
701                         // Debug message
702                         this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesUpdated: Setting updated timestamp for fax number ..."); //NOI18N
703
704                         // Set updated timestamp
705                         faxNumber.setPhoneEntryUpdated(new GregorianCalendar());
706                 }
707
708                 // Is a mobile number instance set?
709                 if ((cellphoneNumber instanceof DialableMobileNumber) && (cellphoneNumber.getPhoneId() instanceof Long) && (cellphoneNumber.getPhoneId() > 0)) {
710                         // Debug message
711                         this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesUpdated: Setting updated timestamp for cellphone number ..."); //NOI18N
712
713                         // Set updated timestamp
714                         cellphoneNumber.setPhoneEntryUpdated(new GregorianCalendar());
715                 }
716
717                 // Trace message
718                 this.getLoggerBeanLocal().logTrace("setAllContactPhoneEntriesUpdated: EXIT!"); //NOI18N
719         }
720
721 }