]> git.mxchange.org Git - jjobs-ejb.git/blob - src/java/org/mxchange/jjobs/database/BaseJobsDatabaseBean.java
Please cherry-pick:
[jjobs-ejb.git] / src / java / org / mxchange / jjobs / database / BaseJobsDatabaseBean.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jjobs.database;
18
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import java.util.List;
22 import java.util.Objects;
23 import java.util.Properties;
24 import javax.ejb.EJBException;
25 import javax.jms.JMSException;
26 import javax.jms.ObjectMessage;
27 import javax.mail.Address;
28 import javax.mail.internet.AddressException;
29 import javax.mail.internet.InternetAddress;
30 import org.mxchange.jcontacts.model.contact.Contact;
31 import org.mxchange.jcontacts.model.contact.ContactUtils;
32 import org.mxchange.jcontacts.model.contact.UserContact;
33 import org.mxchange.jcontactsbusiness.model.basicdata.BusinessBasicData;
34 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
35 import org.mxchange.jcontactsbusiness.model.branchoffice.BusinessBranchOffice;
36 import org.mxchange.jcontactsbusiness.model.employee.BusinessEmployee;
37 import org.mxchange.jcontactsbusiness.model.headquarters.CompanyHeadquartersData;
38 import org.mxchange.jcontactsbusiness.model.headquarters.HeadquartersData;
39 import org.mxchange.jcontactsbusiness.model.opening_time.OpeningTime;
40 import org.mxchange.jcoreee.database.BaseDatabaseBean;
41 import org.mxchange.jcountry.model.data.Country;
42 import org.mxchange.jcountry.model.data.CountryData;
43 import org.mxchange.jmailee.model.delivery.wrapper.EmailDeliveryWrapper;
44 import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery;
45 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
46 import org.mxchange.jphone.model.phonenumbers.fax.FaxNumbers;
47 import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
48 import org.mxchange.jphone.model.phonenumbers.landline.LandLineNumbers;
49 import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
50 import org.mxchange.jphone.model.phonenumbers.mobile.MobileNumbers;
51 import org.mxchange.jphone.utils.PhoneUtils;
52 import org.mxchange.jusercore.model.user.LoginUser;
53 import org.mxchange.jusercore.model.user.User;
54 import org.mxchange.jusercore.model.user.UserUtils;
55 import org.mxchange.jcontactsbusiness.model.employee.Employable;
56 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
57
58 /**
59  * A helper class for beans that access the database.
60  * <p>
61  * @author Roland Häder<roland@mxchange.org>
62  */
63 public abstract class BaseJobsDatabaseBean extends BaseDatabaseBean {
64
65         /**
66          * Serial number
67          */
68         private static final long serialVersionUID = 12_895_410_275_811_963L;
69
70         /**
71          * Protected constructor
72          */
73         protected BaseJobsDatabaseBean () {
74                 // Call super constructor
75                 super();
76         }
77
78         /**
79          * Constructor with queue factory JNDI and queue JNDI names
80          * <p>
81          * @param factoryJndi JNDI name for queue factory
82          * @param queueJndi   JNDI name for email queue
83          */
84         protected BaseJobsDatabaseBean (final String factoryJndi, final String queueJndi) {
85                 // Call super constructor
86                 super(factoryJndi, queueJndi);
87         }
88
89         /**
90          * Updates all contact's phone entry's created timestamps
91          * <p>
92          * @param contact Contact instance to update
93          */
94         protected void setAllContactPhoneEntriesCreated (final Contact contact) {
95                 // Trace message
96                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
97
98                 // The contact instance must be valid
99                 if (null == contact) {
100                         // Throw NPE again
101                         throw new NullPointerException("contact is null"); //NOI18N
102                 }
103
104                 // Get all phone instances
105                 final DialableLandLineNumber landLineNumber = contact.getContactLandLineNumber();
106                 final DialableFaxNumber faxNumber = contact.getContactFaxNumber();
107                 final DialableMobileNumber mobileNumber = contact.getContactMobileNumber();
108
109                 // Debug message
110                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: landLineNumber={1},faxNumber={2},mobileNumber={3}", this.getClass().getSimpleName(), landLineNumber, faxNumber, mobileNumber)); //NOI18N
111
112                 // Is a phone number instance set?
113                 if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() == null)) {
114                         // Debug message
115                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: Setting created timestamp for land-line number ...", this.getClass().getSimpleName())); //NOI18N
116
117                         // Set updated timestamp
118                         landLineNumber.setPhoneEntryCreated(new Date());
119                 }
120
121                 // Is a fax number instance set?
122                 if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneId() == null)) {
123                         // Debug message
124                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: Setting created timestamp for fax number ...", this.getClass().getSimpleName())); //NOI18N
125
126                         // Set updated timestamp
127                         faxNumber.setPhoneEntryCreated(new Date());
128                 }
129
130                 // Is a mobile number instance set?
131                 if ((mobileNumber instanceof DialableMobileNumber) && (mobileNumber.getPhoneId() == null)) {
132                         // Debug message
133                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: Setting created timestamp for mobile number ...", this.getClass().getSimpleName())); //NOI18N
134
135                         // Set updated timestamp
136                         mobileNumber.setPhoneEntryCreated(new Date());
137                 }
138
139                 // Trace message
140                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: EXIT!", this.getClass().getSimpleName())); //NOI18N
141         }
142
143         /**
144          * Returns a managed instance from given mobile number
145          * <p>
146          * @param mobileNumber  Mobile instance
147          * @param fetchedNumber Found mobile number in database
148          * <p>
149          * @return Managed instance
150          */
151         protected DialableMobileNumber getManaged (final DialableMobileNumber mobileNumber, final DialableMobileNumber fetchedNumber) {
152                 // Trace message
153                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: mobileNumber={1},fetchedNumber={2} - CALLED!", this.getClass().getSimpleName(), mobileNumber, fetchedNumber)); //NOI18N
154
155                 // Should be valid
156                 if (null == mobileNumber) {
157                         // Throw NPE
158                         throw new NullPointerException("mobileNumber is null"); //NOI18N
159                 } else if (null == fetchedNumber) {
160                         // Throw NPE again
161                         throw new NullPointerException("fetchedNumber is null"); //NOI18N
162                 } else if (fetchedNumber.getPhoneId() == null) {
163                         // ..and again
164                         throw new NullPointerException("fetchedNumber.phoneId is null"); //NOI18N
165                 }
166
167                 // Debug message
168                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); //NOI18N
169
170                 // Init query instance
171                 final DialableMobileNumber foundNumber = this.getEntityManager().find(fetchedNumber.getClass(), fetchedNumber.getPhoneId());
172
173                 // Debug message
174                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); //NOI18N
175
176                 // Default is null
177                 DialableMobileNumber detachedNumber = null;
178
179                 // Is there a difference?
180                 if (!PhoneUtils.isSameMobileNumber(mobileNumber, fetchedNumber)) {
181                         // Merge this entry
182                         detachedNumber = this.getEntityManager().merge(foundNumber);
183
184                         // @TODO Copy all
185                 }
186
187                 // Trace message
188                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: detachedNumber={1} - EXIT!", this.getClass().getSimpleName(), detachedNumber)); //NOI18N
189
190                 // Return it
191                 return detachedNumber;
192         }
193
194         /**
195          * Returns a managed instance from given land-line number
196          * <p>
197          * @param landLineNumber Land-line instance
198          * @param fetchedNumber  Found land-line number in database
199          * <p>
200          * @return Managed instance
201          */
202         protected DialableLandLineNumber getManaged (final DialableLandLineNumber landLineNumber, final DialableLandLineNumber fetchedNumber) {
203                 // Trace message
204                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: landLineNumber={1},fetchedNumber={2} - CALLED!", this.getClass().getSimpleName(), landLineNumber, fetchedNumber)); //NOI18N
205
206                 // Should be valid
207                 if (null == landLineNumber) {
208                         // Throw NPE
209                         throw new NullPointerException("landLineNumber is null"); //NOI18N
210                 } else if (null == fetchedNumber) {
211                         // Throw NPE again
212                         throw new NullPointerException("fetchedNumber is null"); //NOI18N
213                 } else if (fetchedNumber.getPhoneId() == null) {
214                         // ..and again
215                         throw new NullPointerException("landLineNumber.phoneId is null"); //NOI18N
216                 }
217
218                 // Debug message
219                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); //NOI18N
220
221                 // Init query instance
222                 final DialableLandLineNumber foundNumber = this.getEntityManager().find(fetchedNumber.getClass(), fetchedNumber.getPhoneId());
223
224                 // Debug message
225                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); //NOI18N
226
227                 // Default is null
228                 DialableLandLineNumber detachedNumber = null;
229
230                 // Is there a difference?
231                 if (!PhoneUtils.isSameLandLineNumber(landLineNumber, fetchedNumber)) {
232                         // Merge this entry
233                         detachedNumber = this.getEntityManager().merge(foundNumber);
234
235                         // @TODO Copy all
236                 }
237
238                 // Trace message
239                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: managedNumber={1} - EXIT!", this.getClass().getSimpleName(), detachedNumber)); //NOI18N
240
241                 // Return it
242                 return detachedNumber;
243         }
244
245         /**
246          * Returns a managed instance from given fax number
247          * <p>
248          * @param faxNumber     Fax instance
249          * @param fetchedNumber Found fax number in database
250          * <p>
251          * @return Managed instance
252          */
253         protected DialableFaxNumber getManaged (final DialableFaxNumber faxNumber, final DialableFaxNumber fetchedNumber) {
254                 // Trace message
255                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: faxNumber={1},fetchedNumber={2} - CALLED!", this.getClass().getSimpleName(), faxNumber, fetchedNumber)); //NOI18N
256
257                 // Should be valid
258                 if (null == faxNumber) {
259                         // Throw NPE
260                         throw new NullPointerException("faxNumber is null"); //NOI18N
261                 } else if (null == fetchedNumber) {
262                         // Throw NPE again
263                         throw new NullPointerException("fetchedNumber is null"); //NOI18N
264                 } else if (fetchedNumber.getPhoneId() == null) {
265                         // ..and again
266                         throw new NullPointerException("fetchedNumber.phoneId is null"); //NOI18N
267                 }
268
269                 // Debug message
270                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); //NOI18N
271
272                 // Init query instance
273                 final DialableFaxNumber foundNumber = this.getEntityManager().find(fetchedNumber.getClass(), fetchedNumber.getPhoneId());
274
275                 // Debug message
276                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); //NOI18N
277
278                 // Default is null
279                 DialableFaxNumber detachedNumber = null;
280
281                 // Is there a difference?
282                 if (!PhoneUtils.isSameFaxNumber(faxNumber, fetchedNumber)) {
283                         // Merge this entry
284                         detachedNumber = this.getEntityManager().merge(foundNumber);
285
286                         // @TODO Copy all
287                 }
288
289                 // Trace message
290                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: managedNumber={1} - EXIT!", this.getClass().getSimpleName(), detachedNumber)); //NOI18N
291
292                 // Return it
293                 return detachedNumber;
294         }
295
296         /**
297          * Get back a managed instance from given contact
298          * <p>
299          * @param contact Unmanaged/detached contact instance
300          * <p>
301          * @return Managed contact instance
302          */
303         protected Contact createManaged (final Contact contact) {
304                 // Trace message
305                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
306
307                 // user should not be null
308                 if (null == contact) {
309                         // Abort here
310                         throw new NullPointerException("contact is null"); //NOI18N
311                 } else if (contact.getContactId() == null) {
312                         // Id is set
313                         throw new NullPointerException("contact.contactId is null"); //NOI18N
314                 } else if (contact.getContactId() < 1) {
315                         // Id is set
316                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
317                 }
318
319                 // Try to find it (should be there)
320                 final Contact managedContact = this.getEntityManager().find(UserContact.class, contact.getContactId());
321
322                 // Should be there
323                 assert (managedContact instanceof Contact) : "managedContact is null"; //NOI18N
324
325                 // Trace message
326                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: managedContact={1} - EXIT!", this.getClass().getSimpleName(), managedContact)); //NOI18N
327
328                 // Return it
329                 return managedContact;
330         }
331
332         /**
333          * Get back a managed instance from given country
334          * <p>
335          * @param country Unmanaged/detached country instance
336          * <p>
337          * @return Managed country instance
338          */
339         protected Country createManaged (final Country country) {
340                 // Trace message
341                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: country={1} - CALLED!", this.getClass().getSimpleName(), country)); //NOI18N
342
343                 // user should not be null
344                 if (null == country) {
345                         // Abort here
346                         throw new NullPointerException("country is null"); //NOI18N
347                 } else if (country.getCountryId() == null) {
348                         // Id is set
349                         throw new NullPointerException("country.countryId is null"); //NOI18N
350                 } else if (country.getCountryId() < 1) {
351                         // Id is set
352                         throw new IllegalArgumentException(MessageFormat.format("country.countryId={0} is invalid", country.getCountryId())); //NOI18N
353                 }
354
355                 // Try to find it (should be there)
356                 final Country managedCountry = this.getEntityManager().find(CountryData.class, country.getCountryId());
357
358                 // Should be there
359                 assert (managedCountry instanceof Country) : "managedCountry is null"; //NOI18N
360
361                 // Trace message
362                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: managedCountry={1} - EXIT!", this.getClass().getSimpleName(), managedCountry)); //NOI18N
363
364                 // Return it
365                 return managedCountry;
366         }
367
368         /**
369          * Get back a managed instance from given basic data
370          * <p>
371          * @param basicData Unmanaged/detached basic data instance
372          * <p>
373          * @return Managed basic data instance
374          */
375         protected BasicData createManaged (final BasicData basicData) {
376                 // Trace message
377                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: basicData={1} - CALLED!", this.getClass().getSimpleName(), basicData)); //NOI18N
378
379                 // user should not be null
380                 if (null == basicData) {
381                         // Abort here
382                         throw new NullPointerException("basicData is null"); //NOI18N
383                 } else if (basicData.getBasicDataId() == null) {
384                         // Id is set
385                         throw new NullPointerException("basicData.basicDataId is null"); //NOI18N
386                 } else if (basicData.getBasicDataId() < 1) {
387                         // Id is set
388                         throw new IllegalArgumentException(MessageFormat.format("basicData.basicDataId={0} is invalid", basicData.getBasicDataId())); //NOI18N
389                 }
390
391                 // Try to find it (should be there)
392                 final BasicData managedBasicData = this.getEntityManager().find(BusinessBasicData.class, basicData.getBasicDataId());
393
394                 // Should be there
395                 assert (managedBasicData instanceof BasicData) : "managedBasicData is null"; //NOI18N
396
397                 // Trace message
398                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: managedBasicData={1} - EXIT!", this.getClass().getSimpleName(), managedBasicData)); //NOI18N
399
400                 // Return it
401                 return managedBasicData;
402         }
403
404         /**
405          * Get back a managed instance from given branch office
406          * <p>
407          * @param branchOffice Unmanaged/detached branch office instance
408          * <p>
409          * @return Managed basic data instance
410          */
411         protected BranchOffice createManaged (final BranchOffice branchOffice) {
412                 // Trace message
413                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: branchOffice={1} - CALLED!", this.getClass().getSimpleName(), branchOffice)); //NOI18N
414
415                 // user should not be null
416                 if (null == branchOffice) {
417                         // Abort here
418                         throw new NullPointerException("branchOffice is null"); //NOI18N
419                 } else if (branchOffice.getBranchId() == null) {
420                         // Id is set
421                         throw new NullPointerException("branchOffice.branchOfficeId is null"); //NOI18N
422                 } else if (branchOffice.getBranchId() < 1) {
423                         // Id is set
424                         throw new IllegalArgumentException(MessageFormat.format("branchOffice.branchOfficeId={0} is invalid", branchOffice.getBranchId())); //NOI18N
425                 }
426
427                 // Try to find it (should be there)
428                 final BranchOffice managedBranchOffice = this.getEntityManager().find(BusinessBranchOffice.class, branchOffice.getBranchId());
429
430                 // Should be there
431                 assert (managedBranchOffice instanceof BasicData) : "managedBranchOffice is null"; //NOI18N
432
433                 // Trace message
434                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: managedBranchOffice={1} - EXIT!", this.getClass().getSimpleName(), managedBranchOffice)); //NOI18N
435
436                 // Return it
437                 return managedBranchOffice;
438         }
439
440         /**
441          * Get back a managed instance from given employee
442          * <p>
443          * @param employee Unmanaged/detached employee instance
444          * <p>
445          * @return Managed employee instance
446          */
447         protected Employable createManaged (final Employable employee) {
448                 // Trace message
449                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: employee={1} - CALLED!", this.getClass().getSimpleName(), employee)); //NOI18N
450
451                 // user should not be null
452                 if (null == employee) {
453                         // Abort here
454                         throw new NullPointerException("employee is null"); //NOI18N
455                 } else if (employee.getEmployeeId() == null) {
456                         // Id is set
457                         throw new NullPointerException("employee.employeeId is null"); //NOI18N
458                 } else if (employee.getEmployeeId() < 1) {
459                         // Id is set
460                         throw new IllegalArgumentException(MessageFormat.format("employee.employeeId={0} is invalid", employee.getEmployeeId())); //NOI18N
461                 }
462
463                 // Try to find it (should be there)
464                 final Employable managedEmployee = this.getEntityManager().find(BusinessEmployee.class, employee.getEmployeeId());
465
466                 // Should be there
467                 assert (managedEmployee instanceof Employable) : "managedEmployee is null"; //NOI18N
468
469                 // Trace message
470                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: managedEmployee={1} - EXIT!", this.getClass().getSimpleName(), managedEmployee)); //NOI18N
471
472                 // Return it
473                 return managedEmployee;
474         }
475
476         /**
477          * Get back a managed instance from given headquarters
478          * <p>
479          * @param headquarters Unmanaged/detached headquarters instance
480          * <p>
481          * @return Managed basic data instance
482          */
483         protected HeadquartersData createManaged (final HeadquartersData headquarters) {
484                 // Trace message
485                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: branchOffice={1} - CALLED!", this.getClass().getSimpleName(), headquarters)); //NOI18N
486
487                 // user should not be null
488                 if (null == headquarters) {
489                         // Abort here
490                         throw new NullPointerException("branchOffice is null"); //NOI18N
491                 } else if (headquarters.getHeadquartersId() == null) {
492                         // Id is set
493                         throw new NullPointerException("branchOffice.headquartersId is null"); //NOI18N
494                 } else if (headquarters.getHeadquartersId() < 1) {
495                         // Id is set
496                         throw new IllegalArgumentException(MessageFormat.format("branchOffice.headquartersId={0} is invalid", headquarters.getHeadquartersId())); //NOI18N
497                 }
498
499                 // Try to find it (should be there)
500                 final HeadquartersData managedHeadquarters = this.getEntityManager().find(CompanyHeadquartersData.class, headquarters.getHeadquartersId());
501
502                 // Should be there
503                 assert (managedHeadquarters instanceof BasicData) : "managedHeadquarters is null"; //NOI18N
504
505                 // Trace message
506                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: managedHeadquarters={1} - EXIT!", this.getClass().getSimpleName(), managedHeadquarters)); //NOI18N
507
508                 // Return it
509                 return managedHeadquarters;
510         }
511
512         /**
513          * Get back a managed instance from given user
514          * <p>
515          * @param user Unmanaged/detached user instance
516          * <p>
517          * @return Managed user instance
518          */
519         protected User createManaged (final User user) {
520                 // Trace message
521                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
522
523                 // user should not be null
524                 if (null == user) {
525                         // Abort here
526                         throw new NullPointerException("user is null"); //NOI18N
527                 } else if (user.getUserId() == null) {
528                         // Id is set
529                         throw new NullPointerException("user.userId is null"); //NOI18N
530                 } else if (user.getUserId() < 1) {
531                         // Id is set
532                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
533                 } else if (user.getUserContact() == null) {
534                         // Throw NPE again
535                         throw new NullPointerException("user.userContact is null"); //NOI18N
536                 } else if (user.getUserContact().getContactId() == null) {
537                         // Throw NPE again
538                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
539                 } else if (user.getUserContact().getContactId() < 1) {
540                         // Not valid id number
541                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N#
542                 }
543
544                 // Try to find it (should be there)
545                 final User managedUser = this.getEntityManager().find(LoginUser.class, user.getUserId());
546
547                 // Should be there
548                 assert (managedUser instanceof User) : "managedUser is null"; //NOI18N
549
550                 // Trace message
551                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
552
553                 // Return it
554                 return managedUser;
555         }
556
557         /**
558          * Sets created timestamp on all opening times.
559          * <p>
560          * @param openingTimes Opening times list
561          * <p>
562          * @throws NullPointerException If given list is null
563          * @throws IllegalArgumentException If given list is empty
564          * @throws IllegalStateException If openingId is not null
565          */
566         protected void setAllOpeningTimesCreated (final List<OpeningTime> openingTimes) {
567                 // Validate parameter
568                 if (null == openingTimes) {
569                         // Throw NPE
570                         throw new NullPointerException("openingTimes is null");
571                 } else if (openingTimes.isEmpty()) {
572                         // Throw IAE
573                         throw new IllegalArgumentException("openingTimes is empty");
574                 }
575
576                 // Walk through whole list
577                 for (final OpeningTime openingTime : openingTimes) {
578                         // Id should not be set
579                         if (openingTime.getOpeningId() != null) {
580                                 // Abort here
581                                 throw new IllegalStateException("openingTime.openingId is not null");
582                         }
583
584                         // Set created timestamp
585                         openingTime.setOpeningCreated(new Date());
586                 }
587         }
588
589         /**
590          * Updates all contact's phone entry's created timestamps
591          * <p>
592          * @param contact Contact instance to update
593          */
594         protected void setAllPhoneEntriesCreated (final Contact contact) {
595                 // Trace message
596                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("setAllPhoneEntriesCreated: contact={0} - CALLED!", contact)); //NOI18N
597
598                 // The contact instance must be valid
599                 if (null == contact) {
600                         // Throw NPE
601                         throw new NullPointerException("contact is null"); //NOI18N
602                 } else if (contact.getContactId() != null) {
603                         // Throw IAE
604                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is unexpected.", contact.getContactId()));
605                 }
606
607                 // Get all phone instances
608                 final DialableLandLineNumber landLineNumber = contact.getContactLandLineNumber();
609                 final DialableFaxNumber faxNumber = contact.getContactFaxNumber();
610                 final DialableMobileNumber mobileNumber = contact.getContactMobileNumber();
611
612                 // Debug message
613                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllPhoneEntriesCreated: landLineNumber={0},faxNumber={1},mobileNumber={2}", landLineNumber, faxNumber, mobileNumber)); //NOI18N
614
615                 // Is a phone number instance set?
616                 if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() == null)) {
617                         // Debug message
618                         this.getLoggerBeanLocal().logDebug("setAllPhoneEntriesCreated: Setting created timestamp for land-line number ..."); //NOI18N
619
620                         // Set updated timestamp
621                         landLineNumber.setPhoneEntryCreated(new Date());
622                 }
623
624                 // Is a fax number instance set?
625                 if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneId() == null)) {
626                         // Debug message
627                         this.getLoggerBeanLocal().logDebug("setAllPhoneEntriesCreated: Setting created timestamp for fax number ..."); //NOI18N
628
629                         // Set updated timestamp
630                         faxNumber.setPhoneEntryCreated(new Date());
631                 }
632
633                 // Is a mobile number instance set?
634                 if ((mobileNumber instanceof DialableMobileNumber) && (mobileNumber.getPhoneId() == null)) {
635                         // Debug message
636                         this.getLoggerBeanLocal().logDebug("setAllPhoneEntriesCreated: Setting created timestamp for mobile number ..."); //NOI18N
637
638                         // Set updated timestamp
639                         mobileNumber.setPhoneEntryCreated(new Date());
640                 }
641
642                 // Trace message
643                 this.getLoggerBeanLocal().logTrace("setAllPhoneEntriesCreated: EXIT!"); //NOI18N
644         }
645
646         /**
647          * Updates all branch office's phone entry's created timestamps
648          * <p>
649          * @param branchOffice Branch office instance to update
650          */
651         protected void setAllPhoneEntriesCreated (final BranchOffice branchOffice) {
652                 // Trace message
653                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("setAllPhoneEntriesCreated: branchOffice={0} - CALLED!", branchOffice)); //NOI18N
654
655                 // The contact instance must be valid
656                 if (null == branchOffice) {
657                         // Throw NPE again
658                         throw new NullPointerException("branchOffice is null"); //NOI18N
659                 } else if (branchOffice.getBranchId() != null) {
660                         // Throw IAE
661                         throw new IllegalArgumentException(MessageFormat.format("branchOffice.branchId={0} is unexpected.", branchOffice.getBranchId()));
662                 }
663
664                 // Get all phone instances
665                 final DialableLandLineNumber landLineNumber = branchOffice.getBranchLandLineNumber();
666                 final DialableFaxNumber faxNumber = branchOffice.getBranchFaxNumber();
667
668                 // Debug message
669                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllPhoneEntriesCreated: landLineNumber={0},faxNumber={1}", landLineNumber, faxNumber)); //NOI18N
670
671                 // Is a phone number instance set?
672                 if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() == null)) {
673                         // Debug message
674                         this.getLoggerBeanLocal().logDebug("setAllPhoneEntriesCreated: Setting created timestamp for land-line number ..."); //NOI18N
675
676                         // Set updated timestamp
677                         landLineNumber.setPhoneEntryCreated(new Date());
678                 }
679
680                 // Is a fax number instance set?
681                 if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneId() == null)) {
682                         // Debug message
683                         this.getLoggerBeanLocal().logDebug("setAllPhoneEntriesCreated: Setting created timestamp for fax number ..."); //NOI18N
684
685                         // Set updated timestamp
686                         faxNumber.setPhoneEntryCreated(new Date());
687                 }
688
689                 // Trace message
690                 this.getLoggerBeanLocal().logTrace("setAllPhoneEntriesCreated: EXIT!"); //NOI18N
691         }
692
693         /**
694          * Updates all company's phone entry's created timestamps
695          * <p>
696          * @param basicData Company basic data instance to update
697          */
698         protected void setAllPhoneEntriesCreated (final BasicData basicData) {
699                 // Trace message
700                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("setAllPhoneEntriesCreated: basicData={0} - CALLED!", basicData)); //NOI18N
701
702                 // The contact instance must be valid
703                 if (null == basicData) {
704                         // Throw NPE again
705                         throw new NullPointerException("basicData is null"); //NOI18N
706                 } else if (basicData.getBasicDataId() != null) {
707                         // Throw IAE
708                         throw new IllegalArgumentException(MessageFormat.format("basicData.basicDataId={0} is unexpected.", basicData.getBasicDataId()));
709                 }
710
711                 // Get all phone instances
712                 final DialableLandLineNumber landLineNumber = basicData.getCompanyLandLineNumber();
713                 final DialableFaxNumber faxNumber = basicData.getCompanyFaxNumber();
714
715                 // Debug message
716                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllPhoneEntriesCreated: landLineNumber={0},faxNumber={1}", landLineNumber, faxNumber)); //NOI18N
717
718                 // Is a phone number instance set?
719                 if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() == null)) {
720                         // Debug message
721                         this.getLoggerBeanLocal().logDebug("setAllPhoneEntriesCreated: Setting created timestamp for land-line number ..."); //NOI18N
722
723                         // Set updated timestamp
724                         landLineNumber.setPhoneEntryCreated(new Date());
725
726                         // Set it back in basic data
727                         basicData.setCompanyLandLineNumber(landLineNumber);
728                 }
729
730                 // Is a fax number instance set?
731                 if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneId() == null)) {
732                         // Debug message
733                         this.getLoggerBeanLocal().logDebug("setAllPhoneEntriesCreated: Setting created timestamp for fax number ..."); //NOI18N
734
735                         // Set updated timestamp
736                         faxNumber.setPhoneEntryCreated(new Date());
737
738                         // Set it back in basic data
739                         basicData.setCompanyFaxNumber(faxNumber);
740                 }
741
742                 // Trace message
743                 this.getLoggerBeanLocal().logTrace("setAllPhoneEntriesCreated: EXIT!"); //NOI18N
744         }
745
746         /**
747          * Returns a detached instance from given mobile instance
748          * <p>
749          * @param mobileNumber  Mobile instance
750          * @param fetchedNumber Found mobile number in database
751          * <p>
752          * @return Detached instance
753          */
754         protected DialableMobileNumber getDetached (final DialableMobileNumber mobileNumber, final DialableMobileNumber fetchedNumber) {
755                 // Trace message
756                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: mobileNumber={0},fetchedNumber={1} - CALLED!", mobileNumber, fetchedNumber)); //NOI18N
757
758                 // Should be valid
759                 if (null == mobileNumber) {
760                         // Throw NPE
761                         throw new NullPointerException("mobileNumber is null"); //NOI18N
762                 } else if (fetchedNumber.getPhoneId() == null) {
763                         // ..and again
764                         throw new NullPointerException("fetchedNumber.phoneId is null"); //NOI18N
765                 }
766
767                 // Debug message
768                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); //NOI18N
769
770                 // Init query instance
771                 final DialableMobileNumber foundNumber = this.getEntityManager().find(fetchedNumber.getClass(), fetchedNumber.getPhoneId());
772
773                 // Debug message
774                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); //NOI18N
775
776                 // Default is null
777                 DialableMobileNumber detachedNumber = null;
778
779                 // Is there a difference?
780                 if (!PhoneUtils.isSameMobileNumber(mobileNumber, fetchedNumber)) {
781                         // Merge this entry
782                         detachedNumber = this.getEntityManager().merge(foundNumber);
783
784                         // @TODO Copy all
785                 }
786
787                 // Trace message
788                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: detachedNumber={0} - EXIT!", detachedNumber)); //NOI18N
789
790                 // Return it
791                 return detachedNumber;
792         }
793
794         /**
795          * Returns a detached instance from given land-line instance
796          * <p>
797          * @param landLineNumber Land-line instance
798          * @param fetchedNumber  Found land-line number in database
799          * <p>
800          * @return Detached instance
801          */
802         protected DialableLandLineNumber getDetached (final DialableLandLineNumber landLineNumber, final DialableLandLineNumber fetchedNumber) {
803                 // Trace message
804                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: landLineNumber={0},fetchedNumber={1} - CALLED!", landLineNumber, fetchedNumber)); //NOI18N
805
806                 // Should be valid
807                 if (null == landLineNumber) {
808                         // Throw NPE
809                         throw new NullPointerException("landLineNumber is null"); //NOI18N
810                 } else if (fetchedNumber.getPhoneId() == null) {
811                         // ..and again
812                         throw new NullPointerException("landLineNumber.phoneId is null"); //NOI18N
813                 }
814
815                 // Debug message
816                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); //NOI18N
817
818                 // Init query instance
819                 final DialableLandLineNumber foundNumber = this.getEntityManager().find(fetchedNumber.getClass(), fetchedNumber.getPhoneId());
820
821                 // Debug message
822                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); //NOI18N
823
824                 // Default is null
825                 DialableLandLineNumber detachedNumber = null;
826
827                 // Is there a difference?
828                 if (!PhoneUtils.isSameLandLineNumber(landLineNumber, fetchedNumber)) {
829                         // Merge this entry
830                         detachedNumber = this.getEntityManager().merge(foundNumber);
831
832                         // @TODO Copy all
833                 }
834
835                 // Trace message
836                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: detachedNumber={0} - EXIT!", detachedNumber)); //NOI18N
837
838                 // Return it
839                 return detachedNumber;
840         }
841
842         /**
843          * Returns a detached instance from given fax instance
844          * <p>
845          * @param faxNumber     Fax instance
846          * @param fetchedNumber Found fax number in database
847          * <p>
848          * @return Detached instance
849          */
850         protected DialableFaxNumber getDetached (final DialableFaxNumber faxNumber, final DialableFaxNumber fetchedNumber) {
851                 // Trace message
852                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: faxNumber={0},fetchedNumber={1} - CALLED!", faxNumber, fetchedNumber)); //NOI18N
853
854                 // Should be valid
855                 if (null == faxNumber) {
856                         // Throw NPE
857                         throw new NullPointerException("faxNumber is null"); //NOI18N
858                 } else if (fetchedNumber.getPhoneId() == null) {
859                         // ..and again
860                         throw new NullPointerException("fetchedNumber.phoneId is null"); //NOI18N
861                 }
862
863                 // Debug message
864                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); //NOI18N
865
866                 // Init query instance
867                 final DialableFaxNumber foundNumber = this.getEntityManager().find(fetchedNumber.getClass(), fetchedNumber.getPhoneId());
868
869                 // Debug message
870                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); //NOI18N
871
872                 // Default is null
873                 DialableFaxNumber detachedNumber = null;
874
875                 // Is there a difference?
876                 if (!PhoneUtils.isSameFaxNumber(faxNumber, fetchedNumber)) {
877                         // Merge this entry
878                         detachedNumber = this.getEntityManager().merge(foundNumber);
879
880                         // @TODO Copy all
881                 }
882
883                 // Trace message
884                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: detachedNumber={0} - EXIT!", detachedNumber)); //NOI18N
885
886                 // Return it
887                 return detachedNumber;
888         }
889
890         /**
891          * Merges given contact's data
892          * <p>
893          * @param detachedContact Contact instance to merge
894          * <p>
895          * @return Detached contact instance
896          */
897         protected Contact mergeContactData (final Contact detachedContact) {
898                 // Trace message
899                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.mergeContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
900
901                 // The contact instance must be valid
902                 if (null == detachedContact) {
903                         // Throw NPE again
904                         throw new NullPointerException("detachedContact is null"); //NOI18N
905                 } else if (detachedContact.getContactId() == null) {
906                         // Throw NPE again
907                         throw new NullPointerException("detachedContact.contactId is null"); //NOI18N //NOI18N
908                 } else if (detachedContact.getContactId() < 1) {
909                         // Not valid
910                         throw new IllegalStateException(MessageFormat.format("{0}.detachedContact.contactId={1} is not valid.", this.getClass().getSimpleName(), detachedContact.getContactId())); //NOI18N
911                 }
912
913                 // Set updated timestamp
914                 detachedContact.setContactUpdated(new Date());
915
916                 // Get contact from it and find it
917                 final Contact foundContact = this.getEntityManager().find(detachedContact.getClass(), detachedContact.getContactId());
918
919                 // Should be found
920                 assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", detachedContact.getContactId()); //NOI18N
921
922                 // Debug message
923                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.updateUserPersonalData: foundContact.contactId={1}", this.getClass().getSimpleName(), foundContact.getContactId())); //NOI18N
924
925                 // Is a fax number set?
926                 if (detachedContact.getContactFaxNumber() instanceof DialableFaxNumber) {
927                         // Make fax numbers managed
928                         foundContact.setContactFaxNumber(this.getManaged(detachedContact.getContactFaxNumber(), detachedContact.getContactFaxNumber()));
929                 }
930
931                 // Is a land-line number set?
932                 if (detachedContact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
933                         // Make land-line numbers managed
934                         foundContact.setContactLandLineNumber(this.getManaged(detachedContact.getContactLandLineNumber(), detachedContact.getContactLandLineNumber()));
935                 }
936
937                 // Is a mobile number set?
938                 if (detachedContact.getContactMobileNumber() instanceof DialableMobileNumber) {
939                         // Make mobile numbers managed
940                         foundContact.setContactMobileNumber(this.getManaged(detachedContact.getContactMobileNumber(), detachedContact.getContactMobileNumber()));
941                 }
942
943                 // Merge contact instance
944                 final Contact managedContact = this.getEntityManager().merge(foundContact);
945
946                 // Copy all
947                 ContactUtils.copyAll(detachedContact, managedContact);
948
949                 // Trace message
950                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.mergeContactData: foundContact={1} - EXIT!", this.getClass().getSimpleName(), managedContact)); //NOI18N
951
952                 // Return detached contact
953                 return managedContact;
954         }
955
956         /**
957          * Merges given (detached) contact's mobile, land-line and fax numbers
958          * <p>
959          * @param detachedContact Detached contact instance
960          */
961         protected void mergeContactsMobileLandLineFaxNumbers (final Contact detachedContact) {
962                 // Trace message
963                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.mergeContactsMobileLandLineFaxNumbers: detachedContact={1} - CALLED!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
964
965                 // The contact instance must be valid
966                 if (null == detachedContact) {
967                         // Throw NPE again
968                         throw new NullPointerException("detachedContact is null"); //NOI18N
969                 } else if (detachedContact.getContactId() == null) {
970                         // Throw NPE again
971                         throw new NullPointerException("detachedContact.contactId is null"); //NOI18N //NOI18N
972                 } else if (detachedContact.getContactId() < 1) {
973                         // Not valid
974                         throw new IllegalStateException(MessageFormat.format("{0}.detachedContact.contactId={1} is not valid.", this.getClass().getSimpleName(), detachedContact.getContactId())); //NOI18N
975                 }
976
977                 // Get all instances
978                 final DialableMobileNumber mobile = detachedContact.getContactMobileNumber();
979                 final DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
980                 final DialableFaxNumber fax = detachedContact.getContactFaxNumber();
981
982                 // Is there a  mobile instance set?
983                 if (mobile instanceof DialableMobileNumber) {
984                         // Debug message
985                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: mobile.phoneId={0} is being updated ...", mobile.getPhoneId())); //NOI18N
986
987                         // Then find it, too
988                         final DialableMobileNumber foundMobile = this.getEntityManager().find(mobile.getClass(), mobile.getPhoneId());
989
990                         // Should be there
991                         assert (foundMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", foundMobile.getPhoneId()); //NOI18N
992
993                         // Then merge it, too
994                         final DialableMobileNumber managedMobile = this.getEntityManager().merge(foundMobile);
995
996                         // Should be there
997                         assert (managedMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", managedMobile.getPhoneId()); //NOI18N
998
999                         // Copy all
1000                         MobileNumbers.copyAll(detachedContact.getContactMobileNumber(), managedMobile);
1001
1002                         // Set it back
1003                         detachedContact.setContactMobileNumber(managedMobile);
1004                 }
1005
1006                 // Is there a  fax instance set?
1007                 if (fax instanceof DialableFaxNumber) {
1008                         // Debug message
1009                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); //NOI18N
1010
1011                         // Then find it, too
1012                         final DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId());
1013
1014                         // Should be there
1015                         assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N
1016
1017                         // Then merge it, too
1018                         final DialableFaxNumber managedFax = this.getEntityManager().merge(foundFax);
1019
1020                         // Should be there
1021                         assert (managedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", managedFax.getPhoneId()); //NOI18N
1022
1023                         // Copy all
1024                         FaxNumbers.copyAll(detachedContact.getContactFaxNumber(), managedFax);
1025
1026                         // Set it back
1027                         detachedContact.setContactFaxNumber(managedFax);
1028                 }
1029
1030                 // Is there a  fax instance set?
1031                 if (landLine instanceof DialableLandLineNumber) {
1032                         // Debug message
1033                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); //NOI18N
1034
1035                         // Then find it, too
1036                         final DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId());
1037
1038                         // Should be there
1039                         assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
1040
1041                         // Then merge it, too
1042                         final DialableLandLineNumber managedLandLine = this.getEntityManager().merge(foundLandLine);
1043
1044                         // Should be there
1045                         assert (managedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", managedLandLine.getPhoneId()); //NOI18N
1046
1047                         // Copy all
1048                         LandLineNumbers.copyAll(detachedContact.getContactLandLineNumber(), managedLandLine);
1049
1050                         // Set it back
1051                         detachedContact.setContactLandLineNumber(managedLandLine);
1052                 }
1053
1054                 // Trace message
1055                 this.getLoggerBeanLocal().logTrace("mergeContactsMobileLandLineFaxNumbers: EXIT!"); //NOI18N
1056         }
1057
1058         /**
1059          * Sends an email with given subject line, template name to given recipient
1060          * and user data
1061          * <p>
1062          * @param subjectLine    Subject line
1063          * @param templateName   Template name
1064          * @param user           User instance
1065          * @param baseUrl        Base URL
1066          * @param randomPassword A randomly-generated password or NULL if user had
1067          *                       to enter it.
1068          */
1069         protected void sendEmail (final String subjectLine, final String templateName, final User user, final String baseUrl, final String randomPassword) {
1070                 // Trace message
1071                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("sendEmail: subjectLine={0},templateName={1},user={2},baseUrl={3} - CALLED!", subjectLine, templateName, user, baseUrl)); //NOI18N
1072
1073                 // All should be set
1074                 if (null == subjectLine) {
1075                         // Throw NPE
1076                         throw new NullPointerException("subjectLine is null"); //NOI18N
1077                 } else if (subjectLine.isEmpty()) {
1078                         // No subject line
1079                         throw new IllegalArgumentException("subjectLine is empty"); //NOI18N
1080                 } else if (null == templateName) {
1081                         // Throw NPE
1082                         throw new NullPointerException("templateName is null"); //NOI18N
1083                 } else if (templateName.isEmpty()) {
1084                         // No template name
1085                         throw new IllegalArgumentException("templateName is empty"); //NOI18N
1086                 } else if (null == user) {
1087                         // Throw NPE
1088                         throw new NullPointerException("user is null"); //NOI18N
1089                 } else if (user.getUserId() == null) {
1090                         // Throw NPE again
1091                         throw new NullPointerException("user.userId is null"); //NOI18N
1092                 } else if (user.getUserId() < 1) {
1093                         // Not valid number
1094                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
1095                 } else if (user.getUserName() == null) {
1096                         // Throw NPE again
1097                         throw new NullPointerException("user.userName is null"); //NOI18N
1098                 } else if (user.getUserName().isEmpty()) {
1099                         // Empty string
1100                         throw new IllegalArgumentException("user.userName is empty"); //NOI18N
1101                 } else if (user.getUserAccountStatus() == null) {
1102                         // Throw NPE
1103                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
1104                 } else if (user.getUserContact() == null) {
1105                         // Throw it again
1106                         throw new NullPointerException("user.userContact is null"); //NOI18N
1107                 } else if (user.getUserContact().getContactId() == null) {
1108                         // .. and again
1109                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
1110                 } else if (user.getUserContact().getContactId() < 1) {
1111                         // Invalid id
1112                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is invalid", user.getUserContact().getContactId())); //NOI18N
1113                 } else if (user.getUserContact().getContactPersonalTitle() == null) {
1114                         // Throw NPE again
1115                         throw new NullPointerException("user.userContact.contactPersonalTitle is null"); //NOI18N
1116                 } else if (user.getUserLocale() == null) {
1117                         // Throw NPE again
1118                         throw new NullPointerException("user.userLocale is null"); //NOI18N
1119                 } else if (this.getSession() == null) {
1120                         // Throw NPE
1121                         throw new NullPointerException("this.session is not set. Have you forgotten to call super(String, String) and called only super() ?"); //NOI18N
1122                 }
1123
1124                 // Set all values
1125                 final Properties variables = UserUtils.getAllUserFields(user);
1126
1127                 // Set base URL and random password
1128                 variables.put("baseUrl", baseUrl); //NOI18N
1129                 variables.put("randomPassword", ""); //NOI18N
1130
1131                 // Is the random password set?
1132                 if ((randomPassword instanceof String) && (!randomPassword.isEmpty())) {
1133                         variables.put("randomPassword", randomPassword); //NOI18N
1134                 }
1135
1136                 // Init addresss
1137                 final Address recipientAddress;
1138
1139                 try {
1140                         // Create email address and set
1141                         recipientAddress = new InternetAddress(user.getUserContact().getContactEmailAddress());
1142                 } catch (final AddressException ex) {
1143                         // Throw again
1144                         throw new EJBException(ex);
1145                 }
1146
1147                 // Prepare mail wrapper
1148                 // @TODO Language from message bundle
1149                 final WrapableEmailDelivery emailWrapper = new EmailDeliveryWrapper(recipientAddress, subjectLine, templateName, variables, user.getUserLocale());
1150
1151                 try {
1152                         // Send out email change
1153                         final ObjectMessage message = this.getSession().createObjectMessage();
1154                         message.setObject(emailWrapper);
1155
1156                         // Send message
1157                         this.sendMessage(message);
1158                 } catch (final JMSException ex) {
1159                         // Throw again
1160                         throw new EJBException(ex);
1161                 }
1162
1163                 // Trace message
1164                 this.getLoggerBeanLocal().logTrace("sendEmail: EXIT!"); //NOI18N
1165         }
1166
1167         /**
1168          * Updates all contact's phone instances from other contact, both contacts
1169          * should be the same.
1170          * <p>
1171          * @param contact Contact to set instances
1172          * @param other   Other contact to get instances from
1173          */
1174         protected void setAllContactPhoneEntries (final Contact contact, final Contact other) {
1175                 // Trace message
1176                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntries: contact={1},other={2} - CALLED!", this.getClass().getSimpleName(), contact, other)); //NOI18N
1177
1178                 // Both must be the same and not null
1179                 if (null == contact) {
1180                         // Throw NPE
1181                         throw new NullPointerException("contact is null"); //NOI18N
1182                 } else if (null == other) {
1183                         // Throw NPE
1184                         throw new NullPointerException("other is null"); //NOI18N
1185                 } else if (!Objects.equals(contact, other)) {
1186                         // Not same instances
1187                         throw new IllegalArgumentException(MessageFormat.format("contact={0} and other={1} are not equal!", contact, other)); //NOI18N
1188                 }
1189
1190                 // Debug message
1191                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: other.contactMobileNumber={0}", other.getContactMobileNumber())); //NOI18N
1192
1193                 // Is other mobile not set?
1194                 if ((other.getContactMobileNumber() == null) || (PhoneUtils.isSameMobileNumber(contact.getContactMobileNumber(), other.getContactMobileNumber()))) {
1195                         // Debug message
1196                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: Copying mobile entry ...", this.getClass().getSimpleName())); //NOI18N
1197
1198                         // Is the fax number set?
1199                         if (other.getContactMobileNumber() instanceof DialableMobileNumber) {
1200                                 // Copy mobile number
1201                                 contact.setContactMobileNumber(this.getManaged(other.getContactMobileNumber(), contact.getContactMobileNumber()));
1202                         } else {
1203                                 // Null it
1204                                 contact.setContactMobileNumber(null);
1205                         }
1206                 }
1207
1208                 // Debug message
1209                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: other.contactLandLineNumber={0}", other.getContactLandLineNumber())); //NOI18N
1210
1211                 // Is other mobile not set?
1212                 if ((other.getContactLandLineNumber() == null) || (PhoneUtils.isSameLandLineNumber(contact.getContactLandLineNumber(), other.getContactLandLineNumber()))) {
1213                         // Debug message
1214                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: Copying land-line entry ...", this.getClass().getSimpleName())); //NOI18N
1215
1216                         // Is the land-line number set?
1217                         if (other.getContactLandLineNumber() instanceof DialableLandLineNumber) {
1218                                 // Copy land-line number
1219                                 contact.setContactLandLineNumber(this.getManaged(other.getContactLandLineNumber(), contact.getContactLandLineNumber()));
1220                         } else {
1221                                 // Null it
1222                                 contact.setContactLandLineNumber(null);
1223                         }
1224                 }
1225
1226                 // Debug message
1227                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: other.contactFaxNumber={1}", this.getClass().getSimpleName(), other.getContactFaxNumber())); //NOI18N
1228
1229                 // Is other mobile not set?
1230                 if ((other.getContactFaxNumber() == null) || (PhoneUtils.isSameFaxNumber(contact.getContactFaxNumber(), other.getContactFaxNumber()))) {
1231                         // Debug message
1232                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: Copying fax entry ...", this.getClass().getSimpleName())); //NOI18N
1233
1234                         // Is the fax number set?
1235                         if (other.getContactFaxNumber() instanceof DialableFaxNumber) {
1236                                 // Copy fax number
1237                                 contact.setContactFaxNumber(this.getManaged(other.getContactFaxNumber(), contact.getContactFaxNumber()));
1238                         } else {
1239                                 // Null it
1240                                 contact.setContactFaxNumber(null);
1241                         }
1242                 }
1243
1244                 // Trace message
1245                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntries: EXIT!", this.getClass().getSimpleName())); //NOI18N
1246         }
1247
1248         /**
1249          * Updates all contact's phone entry's updated timestamps
1250          * <p>
1251          * @param contact            Contact instance to update
1252          * @param isMobileUnlinked   Whether a mobile entry has been unlinked in
1253          *                           contact instance
1254          * @param isLandlineUnlinked Whether a land-line entry has been unlinked in
1255          *                           contact instance
1256          * @param isFaxUnlinked      Whether a fax entry has been unlinked in
1257          *                           contact instance
1258          */
1259         protected void setAllContactPhoneEntriesUpdated (final Contact contact, final boolean isMobileUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
1260                 // Trace message
1261                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: contact={1},isMobileUnlinked={2},isLandlineUnlinked={3},isFaxUnlinked={4} - CALLED", this.getClass().getSimpleName(), contact, isMobileUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N
1262
1263                 // The contact instance must be valid
1264                 if (null == contact) {
1265                         // Throw NPE again
1266                         throw new NullPointerException("contact is null"); //NOI18N
1267                 } else if (contact.getContactId() == null) {
1268                         // Throw NPE again
1269                         throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
1270                 } else if (contact.getContactId() < 1) {
1271                         // Not valid
1272                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1273                 }
1274
1275                 // Get all phone instances
1276                 final DialableLandLineNumber landLineNumber = contact.getContactLandLineNumber();
1277                 final DialableFaxNumber faxNumber = contact.getContactFaxNumber();
1278                 final DialableMobileNumber mobileNumber = contact.getContactMobileNumber();
1279
1280                 // Flags and instances must be constistent
1281                 if (isMobileUnlinked && mobileNumber instanceof DialableMobileNumber) {
1282                         // Bad state
1283                         throw new IllegalStateException("isCellPhoneUnlinked is TRUE, but mobileNumber is set."); //NOI18N
1284                 } else if (isLandlineUnlinked && landLineNumber instanceof DialableLandLineNumber) {
1285                         // Bad state
1286                         throw new IllegalStateException("isLandlineUnlinked is TRUE, but landLineNumber is set."); //NOI18N
1287                 } else if (isFaxUnlinked && faxNumber instanceof DialableFaxNumber) {
1288                         // Bad state
1289                         throw new IllegalStateException("isFaxUnlinked is TRUE, but faxNumber is set."); //NOI18N
1290                 }
1291
1292                 // Is a phone number instance set?
1293                 if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() instanceof Long) && (landLineNumber.getPhoneId() > 0)) {
1294                         // Debug message
1295                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: Setting updated timestamp for land-line number ...", this.getClass().getSimpleName())); //NOI18N
1296
1297                         // Set updated timestamp
1298                         landLineNumber.setPhoneEntryUpdated(new Date());
1299                 }
1300
1301                 // Is a fax number instance set?
1302                 if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneId() instanceof Long) && (faxNumber.getPhoneId() > 0)) {
1303                         // Debug message
1304                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: Setting updated timestamp for fax number ...", this.getClass().getSimpleName())); //NOI18N
1305
1306                         // Set updated timestamp
1307                         faxNumber.setPhoneEntryUpdated(new Date());
1308                 }
1309
1310                 // Is a mobile number instance set?
1311                 if ((mobileNumber instanceof DialableMobileNumber) && (mobileNumber.getPhoneId() instanceof Long) && (mobileNumber.getPhoneId() > 0)) {
1312                         // Debug message
1313                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: Setting updated timestamp for mobile number ...", this.getClass().getSimpleName())); //NOI18N
1314
1315                         // Set updated timestamp
1316                         mobileNumber.setPhoneEntryUpdated(new Date());
1317                 }
1318
1319                 // Trace message
1320                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: EXIT!", this.getClass().getSimpleName())); //NOI18N
1321         }
1322
1323 }