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