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