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