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