]> 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 managedContact = this.getEntityManager().find(detachedContact.getClass(), detachedContact.getContactId());
364
365                 // Should be found
366                 assert (managedContact 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: managedContact.contactId={1}", this.getClass().getSimpleName(), managedContact.getContactId())); //NOI18N
370
371                 // Is a fax number set?
372                 if (detachedContact.getContactFaxNumber() instanceof DialableFaxNumber) {
373                         // Make fax numbers managed
374                         managedContact.setContactFaxNumber(this.getManaged(detachedContact.getContactFaxNumber(), detachedContact.getContactFaxNumber()));
375                 }
376
377                 // Is a land-line number set?
378                 if (detachedContact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
379                         // Make land-line numbers managed
380                         managedContact.setContactLandLineNumber(this.getManaged(detachedContact.getContactLandLineNumber(), detachedContact.getContactLandLineNumber()));
381                 }
382
383                 // Is a mobile number set?
384                 if (detachedContact.getContactMobileNumber() instanceof DialableMobileNumber) {
385                         // Make mobile numbers managed
386                         managedContact.setContactMobileNumber(this.getManaged(detachedContact.getContactMobileNumber(), detachedContact.getContactMobileNumber()));
387                 }
388
389                 // Set updated timestamp
390                 managedContact.setContactUpdated(new GregorianCalendar());
391
392                 // Copy all
393                 managedContact.copyAll(detachedContact);
394
395                 // Trace message
396                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.mergeContactData: managedContact={1} - EXIT!", this.getClass().getSimpleName(), managedContact)); //NOI18N
397
398                 // Return detached contact
399                 return managedContact;
400         }
401
402         /**
403          * Merges given (detached) contact's cellphone, land-line and fax numbers
404          * <p>
405          * @param detachedContact Detached contact instance
406          */
407         protected void mergeContactsMobileLandLineFaxNumbers (final Contact detachedContact) {
408                 // Trace message
409                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.mergeContactsMobileLandLineFaxNumbers: detachedContact={1} - CALLED!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
410
411                 // The contact instance must be valid
412                 if (null == detachedContact) {
413                         // Throw NPE again
414                         throw new NullPointerException("detachedContact is null"); //NOI18N
415                 } else if (detachedContact.getContactId() == null) {
416                         // Throw NPE again
417                         throw new NullPointerException("detachedContact.contactId is null"); //NOI18N //NOI18N
418                 } else if (detachedContact.getContactId() < 1) {
419                         // Not valid
420                         throw new IllegalStateException(MessageFormat.format("{0}.detachedContact.contactId={1} is not valid.", this.getClass().getSimpleName(), detachedContact.getContactId())); //NOI18N
421                 }
422
423                 // Get all instances
424                 DialableMobileNumber cellphone = detachedContact.getContactMobileNumber();
425                 DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
426                 DialableFaxNumber fax = detachedContact.getContactFaxNumber();
427
428                 // Is there a  cellphone instance set?
429                 if (cellphone instanceof DialableMobileNumber) {
430                         // Debug message
431                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId())); //NOI18N
432
433                         // Then find it, too
434                         DialableMobileNumber foundMobile = this.getEntityManager().find(cellphone.getClass(), cellphone.getPhoneId());
435
436                         // Should be there
437                         assert (foundMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", foundMobile.getPhoneId()); //NOI18N
438
439                         // Copy all
440                         foundMobile.copyAll(detachedContact.getContactMobileNumber());
441
442                         // Set it back
443                         detachedContact.setContactMobileNumber(foundMobile);
444                 }
445
446                 // Is there a  fax instance set?
447                 if (fax instanceof DialableFaxNumber) {
448                         // Debug message
449                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); //NOI18N
450
451                         // Then find it, too
452                         DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId());
453
454                         // Copy all
455                         foundFax.copyAll(detachedContact.getContactFaxNumber());
456
457                         // Set it back
458                         detachedContact.setContactFaxNumber(foundFax);
459                 }
460
461                 // Is there a  fax instance set?
462                 if (landLine instanceof DialableLandLineNumber) {
463                         // Debug message
464                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); //NOI18N
465
466                         // Then find it, too
467                         DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId());
468
469                         // Should be there
470                         assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
471
472                         // Copy all
473                         foundLandLine.copyAll(detachedContact.getContactLandLineNumber());
474
475                         // Set it back
476                         detachedContact.setContactLandLineNumber(foundLandLine);
477                 }
478
479                 // Trace message
480                 this.getLoggerBeanLocal().logTrace("mergeContactsMobileLandLineFaxNumbers: EXIT!"); //NOI18N
481         }
482
483         /**
484          * Sends an email with given subject line, template name to given recipient
485          * and user data
486          * <p>
487          * @param subjectLine Subject line
488          * @param templateName Template name
489          * @param emailAddress Recipient's email address
490          * @param user User instance
491          * @param baseUrl Base URL
492          */
493         protected void sendEmail (final String subjectLine, final String templateName, final Address emailAddress, final User user, final String baseUrl) {
494                 // Trace message
495                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("sendEmail: subjectLine={0},templateName={1},emailAddress={2},user={3},baseUrl={4} - CALLED!", subjectLine, templateName, emailAddress, user, baseUrl)); //NOI18N
496
497                 // All should be set
498                 if (null == subjectLine) {
499                         // Throw NPE
500                         throw new NullPointerException("subjectLine is null"); //NOI18N
501                 } else if (subjectLine.isEmpty()) {
502                         // No subject line
503                         throw new IllegalArgumentException("subjectLine is empty"); //NOI18N
504                 } else if (null == templateName) {
505                         // Throw NPE
506                         throw new NullPointerException("templateName is null"); //NOI18N
507                 } else if (templateName.isEmpty()) {
508                         // No template name
509                         throw new IllegalArgumentException("templateName is empty"); //NOI18N
510                 } else if (null == emailAddress) {
511                         // Throw NPE
512                         throw new NullPointerException("emailAddress is null"); //NOI18N
513                 }
514
515                 // Prepare mail wrapper
516                 WrapableEmailDelivery emailWrapper = new EmailDeliveryWrapper();
517
518                 // Set all values
519                 Properties variables = UserUtils.getAllUserFields(user);
520
521                 // Set base URL
522                 variables.put("baseUrl", baseUrl); //NOI18N
523
524                 // Set all
525                 // @TODO Language from message bundle
526                 emailWrapper.setRecipient(emailAddress);
527                 emailWrapper.setLocale(user.getUserLocale());
528                 emailWrapper.setSubjectLine(subjectLine);
529                 emailWrapper.setTemplateName(templateName);
530                 emailWrapper.setTemplateVariables(variables);
531
532                 try {
533                         // Send out email change
534                         ObjectMessage message = this.session.createObjectMessage();
535                         message.setObject(emailWrapper);
536
537                         // Send message
538                         this.sendMessage(message, this.messageProducer);
539                 } catch (final JMSException ex) {
540                         // Throw again
541                         throw new EJBException(ex);
542                 }
543
544                 // Trace message
545                 this.getLoggerBeanLocal().logTrace("sendEmail: EXIT!"); //NOI18N
546         }
547
548         /**
549          * Updates all contact's phone instances from other contact, both contacts
550          * should be the same.
551          * <p>
552          * @param contact Contact to set instances
553          * @param other Other contact to get instances from
554          */
555         protected void setAllContactPhoneEntries (final Contact contact, final Contact other) {
556                 // Trace message
557                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntries: contact={1},other={2} - CALLED!", this.getClass().getSimpleName(), contact, other)); //NOI18N
558
559                 // Both must be the same and not null
560                 if (null == contact) {
561                         // Throw NPE
562                         throw new NullPointerException("contact is null"); //NOI18N
563                 } else if (null == other) {
564                         // Throw NPE
565                         throw new NullPointerException("other is null"); //NOI18N
566                 } else if (!Objects.equals(contact, other)) {
567                         // Not same instances
568                         throw new IllegalArgumentException(MessageFormat.format("contact={0} and other={1} are not equal!", contact, other)); //NOI18N
569                 }
570
571                 // Debug message
572                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: other.contactMobileNumber={0}", other.getContactMobileNumber())); //NOI18N
573
574                 // Is other cellphone not set?
575                 if ((other.getContactMobileNumber() == null) || (PhoneUtils.isSameMobileNumber(contact.getContactMobileNumber(), other.getContactMobileNumber()))) {
576                         // Debug message
577                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: Copying cellphone entry ...", this.getClass().getSimpleName())); //NOI18N
578
579                         // Is the fax number set?
580                         if (other.getContactMobileNumber() instanceof DialableMobileNumber) {
581                                 // Copy cellphone number
582                                 contact.setContactMobileNumber(this.getManaged(other.getContactMobileNumber(), contact.getContactMobileNumber()));
583                         } else {
584                                 // Null it
585                                 contact.setContactMobileNumber(null);
586                         }
587                 }
588
589                 // Debug message
590                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: other.contactLandLineNumber={0}", other.getContactLandLineNumber())); //NOI18N
591
592                 // Is other cellphone not set?
593                 if ((other.getContactLandLineNumber() == null) || (PhoneUtils.isSameLandLineNumber(contact.getContactLandLineNumber(), other.getContactLandLineNumber()))) {
594                         // Debug message
595                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: Copying land-line entry ...", this.getClass().getSimpleName())); //NOI18N
596
597                         // Is the land-line number set?
598                         if (other.getContactLandLineNumber() instanceof DialableLandLineNumber) {
599                                 // Copy land-line number
600                                 contact.setContactLandLineNumber(this.getManaged(other.getContactLandLineNumber(), contact.getContactLandLineNumber()));
601                         } else {
602                                 // Null it
603                                 contact.setContactLandLineNumber(null);
604                         }
605                 }
606
607                 // Debug message
608                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: other.contactFaxNumber={1}", this.getClass().getSimpleName(), other.getContactFaxNumber())); //NOI18N
609
610                 // Is other cellphone not set?
611                 if ((other.getContactFaxNumber() == null) || (PhoneUtils.isSameFaxNumber(contact.getContactFaxNumber(), other.getContactFaxNumber()))) {
612                         // Debug message
613                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: Copying fax entry ...", this.getClass().getSimpleName())); //NOI18N
614
615                         // Is the fax number set?
616                         if (other.getContactFaxNumber() instanceof DialableFaxNumber) {
617                                 // Copy fax number
618                                 contact.setContactFaxNumber(this.getManaged(other.getContactFaxNumber(), contact.getContactFaxNumber()));
619                         } else {
620                                 // Null it
621                                 contact.setContactFaxNumber(null);
622                         }
623                 }
624
625                 // Trace message
626                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntries: EXIT!", this.getClass().getSimpleName())); //NOI18N
627         }
628
629         /**
630          * Updates all contacts's phone entry's updated timestamps
631          * <p>
632          * @param contact Contact instance to update
633          * @param isMobileUnlinked Whether a cellphone entry has been unlinked in
634          * contact instance
635          * @param isLandlineUnlinked Whether a land-line entry has been unlinked in
636          * contact instance
637          * @param isFaxUnlinked Whether a fax entry has been unlinked in contact
638          * instance
639          */
640         protected void setAllContactPhoneEntriesUpdated (final Contact contact, final boolean isMobileUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
641                 // Trace message
642                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: contact={1},isMobileUnlinked={2},isLandlineUnlinked={3},isFaxUnlinked={4} - CALLED", this.getClass().getSimpleName(), contact, isMobileUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N
643
644                 // The contact instance must be valid
645                 if (null == contact) {
646                         // Throw NPE again
647                         throw new NullPointerException("contact is null"); //NOI18N
648                 } else if (contact.getContactId() == null) {
649                         // Throw NPE again
650                         throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
651                 } else if (contact.getContactId() < 1) {
652                         // Not valid
653                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
654                 }
655
656                 // Get all phone instances
657                 DialableLandLineNumber landLineNumber = contact.getContactLandLineNumber();
658                 DialableFaxNumber faxNumber = contact.getContactFaxNumber();
659                 DialableMobileNumber mobileNumber = contact.getContactMobileNumber();
660
661                 // Flags and instances must be constistent
662                 if (isMobileUnlinked && mobileNumber instanceof DialableMobileNumber) {
663                         // Bad state
664                         throw new IllegalStateException("isCellPhoneUnlinked is TRUE, but mobileNumber is set."); //NOI18N
665                 } else if (isLandlineUnlinked && landLineNumber instanceof DialableLandLineNumber) {
666                         // Bad state
667                         throw new IllegalStateException("isLandlineUnlinked is TRUE, but landLineNumber is set."); //NOI18N
668                 } else if (isFaxUnlinked && faxNumber instanceof DialableFaxNumber) {
669                         // Bad state
670                         throw new IllegalStateException("isFaxUnlinked is TRUE, but faxNumber is set."); //NOI18N
671                 }
672
673                 // Is a phone number instance set?
674                 if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() instanceof Long) && (landLineNumber.getPhoneId() > 0)) {
675                         // Debug message
676                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: Setting updated timestamp for land-line number ...", this.getClass().getSimpleName())); //NOI18N
677
678                         // Set updated timestamp
679                         landLineNumber.setPhoneEntryUpdated(new GregorianCalendar());
680                 }
681
682                 // Is a fax number instance set?
683                 if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneId() instanceof Long) && (faxNumber.getPhoneId() > 0)) {
684                         // Debug message
685                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: Setting updated timestamp for fax number ...", this.getClass().getSimpleName())); //NOI18N
686
687                         // Set updated timestamp
688                         faxNumber.setPhoneEntryUpdated(new GregorianCalendar());
689                 }
690
691                 // Is a mobile number instance set?
692                 if ((mobileNumber instanceof DialableMobileNumber) && (mobileNumber.getPhoneId() instanceof Long) && (mobileNumber.getPhoneId() > 0)) {
693                         // Debug message
694                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: Setting updated timestamp for cellphone number ...", this.getClass().getSimpleName())); //NOI18N
695
696                         // Set updated timestamp
697                         mobileNumber.setPhoneEntryUpdated(new GregorianCalendar());
698                 }
699
700                 // Trace message
701                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: EXIT!", this.getClass().getSimpleName())); //NOI18N
702         }
703
704 }