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