]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/contact/JobsContactWebSessionBean.java
Please cherry-pick:
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / contact / JobsContactWebSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (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 Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jjobs.beans.contact;
18
19 import java.text.MessageFormat;
20 import java.util.Collections;
21 import java.util.Date;
22 import java.util.Iterator;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Objects;
26 import javax.annotation.PostConstruct;
27 import javax.enterprise.context.SessionScoped;
28 import javax.enterprise.event.Observes;
29 import javax.faces.view.facelets.FaceletException;
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 import javax.naming.Context;
33 import javax.naming.InitialContext;
34 import javax.naming.NamingException;
35 import org.mxchange.jcontacts.contact.Contact;
36 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
37 import org.mxchange.jcontacts.contact.UserContact;
38 import org.mxchange.jcontacts.contact.gender.Gender;
39 import org.mxchange.jcontacts.contact.utils.ContactUtils;
40 import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent;
41 import org.mxchange.jcontacts.events.contact.deleted.AdminDeletedContactEvent;
42 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
43 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
44 import org.mxchange.jcountry.data.Country;
45 import org.mxchange.jjobs.beans.BaseJobsController;
46 import org.mxchange.jjobs.beans.login.JobsUserLoginWebSessionController;
47 import org.mxchange.jjobs.beans.user.JobsUserWebSessionController;
48 import org.mxchange.jphone.events.mobile.remove.AdminRemoveMobileNumberFromListEvent;
49 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
50 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
51 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
52 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
53 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
54 import org.mxchange.jphone.phonenumbers.mobile.MobileNumber;
55 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
56 import org.mxchange.jusercore.events.confirmation.UserConfirmedAccountEvent;
57 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
58 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
59 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
60 import org.mxchange.jusercore.events.user.linked.AdminLinkedUserEvent;
61 import org.mxchange.jusercore.model.user.User;
62
63 /**
64  * A general contact controller (bean)
65  * <p>
66  * @author Roland Haeder<roland@mxchange.org>
67  */
68 @Named ("contactController")
69 @SessionScoped
70 public class JobsContactWebSessionBean extends BaseJobsController implements JobsContactWebSessionController {
71
72         /**
73          * Serial number
74          */
75         private static final long serialVersionUID = 542_145_347_916L;
76
77         /**
78          * Birth day
79          */
80         private Date birthday;
81
82         /**
83          * City
84          */
85         private String city;
86
87         /**
88          * Optional comments
89          */
90         private String comment;
91
92         /**
93          * Remote contact bean
94          */
95         private final ContactSessionBeanRemote contactBean;
96
97         /**
98          * Contact list
99          */
100         private final List<Contact> contactList;
101
102         /**
103          * Country instance
104          */
105         private Country country;
106
107         /**
108          * Email address
109          */
110         private String emailAddress;
111
112         /**
113          * Email address list
114          */
115         private final List<String> emailAddressList;
116
117         /**
118          * Email address repeated
119          */
120         private String emailAddressRepeat;
121
122         /**
123          * Family name
124          */
125         private String familyName;
126
127         /**
128          * Fax number's area code
129          */
130         private Integer faxAreaCode;
131
132         /**
133          * Country instance for fax number
134          */
135         private Country faxCountry;
136
137         /**
138          * Fax number
139          */
140         private Long faxNumber;
141
142         /**
143          * First name
144          */
145         private String firstName;
146
147         /**
148          * Gender instance
149          */
150         private Gender gender;
151
152         /**
153          * House number
154          */
155         private Short houseNumber;
156
157         /**
158          * House number extension
159          */
160         private String houseNumberExtension;
161
162         /**
163          * Whether a fax entry has been unlinked
164          */
165         private boolean isFaxUnlinked;
166
167         /**
168          * Whether a land-line number has been unlinked
169          */
170         private boolean isLandLineUnlinked;
171
172         /**
173          * Whether a mobile entry has been unlinked
174          */
175         private boolean isMobileUnlinked;
176
177         /**
178          * Land-line number area code
179          */
180         private Integer landLineAreaCode;
181
182         /**
183          * Country instance for land-line number
184          */
185         private Country landLineCountry;
186
187         /**
188          * Land-line number
189          */
190         private Long landLineNumber;
191
192         /**
193          * Mobile number
194          */
195         private Long mobileNumber;
196
197         /**
198          * Mobile provider
199          */
200         private MobileProvider mobileProvider;
201
202         /**
203          * A list of all selectable contacts
204          */
205         private List<Contact> selectableContacts;
206
207         /**
208          * Street
209          */
210         private String street;
211
212         /**
213          * Title
214          */
215         private String title;
216
217         /**
218          * Regular user controller
219          */
220         @Inject
221         private JobsUserWebSessionController userController;
222
223         /**
224          * Login controller (bean)
225          */
226         @Inject
227         private JobsUserLoginWebSessionController userLoginController;
228
229         /**
230          * ZIP code
231          */
232         private Integer zipCode;
233
234         /**
235          * Default constructor
236          */
237         public JobsContactWebSessionBean () {
238                 // Try it
239                 try {
240                         // Get initial context
241                         Context context = new InitialContext();
242
243                         // Try to lookup
244                         this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jratecalc-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
245                 } catch (final NamingException e) {
246                         // Throw again
247                         throw new FaceletException(e);
248                 }
249
250                 // Init lists/maps
251                 this.contactList = new LinkedList<>();
252                 this.emailAddressList = new LinkedList<>();
253         }
254
255         @Override
256         public void afterAdminAddedContact (@Observes final AdminAddedContactEvent event) {
257                 // The event must be valid
258                 if (null == event) {
259                         // Throw NPE
260                         throw new NullPointerException("event is null"); //NOI18N
261                 } else if (event.getAddedContact() == null) {
262                         // Throw again ...
263                         throw new NullPointerException("event.addedContact is null"); //NOI18N
264                 } else if (event.getAddedContact().getContactId() == null) {
265                         // ... and again
266                         throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
267                 } else if (event.getAddedContact().getContactId() < 1) {
268                         // Not valid
269                         throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N
270                 }
271
272                 // Clear this bean
273                 this.clear();
274
275                 // Call other method
276                 this.uniqueAddContact(event.getAddedContact());
277
278                 // Add to selectable contacts
279                 this.selectableContacts.add(event.getAddedContact());
280         }
281
282         @Override
283         public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
284                 // event should not be null
285                 if (null == event) {
286                         // Throw NPE
287                         throw new NullPointerException("event is null"); //NOI18N
288                 } else if (event.getAddedUser() == null) {
289                         // Throw NPE again
290                         throw new NullPointerException("event.addedUser is null"); //NOI18N
291                 } else if (event.getAddedUser().getUserId() == null) {
292                         // userId is null
293                         throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
294                 } else if (event.getAddedUser().getUserId() < 1) {
295                         // Not avalid id
296                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
297                 }
298
299                 // Clear all data
300                 this.clear();
301         }
302
303         @Override
304         public void afterAdminDeletedContactEvent (@Observes final AdminDeletedContactEvent event) {
305                 // event should not be null
306                 if (null == event) {
307                         // Throw NPE
308                         throw new NullPointerException("event is null"); //NOI18N
309                 } else if (event.getDeletedContact() == null) {
310                         // Throw NPE again
311                         throw new NullPointerException("event.deletedContact is null"); //NOI18N
312                 } else if (event.getDeletedContact().getContactId() == null) {
313                         // userId is null
314                         throw new NullPointerException("event.deletedContact.contactId is null"); //NOI18N
315                 } else if (event.getDeletedContact().getContactId() < 1) {
316                         // Not avalid id
317                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getDeletedContact(), event.getDeletedContact().getContactId())); //NOI18N
318                 }
319
320                 // Remove from both lists
321                 this.contactList.remove(event.getDeletedContact());
322                 this.selectableContacts.remove(event.getDeletedContact());
323
324                 // Clear all data
325                 this.clear();
326         }
327
328         @Override
329         public void afterAdminLinkedUser (@Observes final AdminLinkedUserEvent event) {
330                 // event should not be null
331                 if (null == event) {
332                         // Throw NPE
333                         throw new NullPointerException("event is null"); //NOI18N
334                 } else if (event.getLinkedUser() == null) {
335                         // Throw NPE again
336                         throw new NullPointerException("event.linkedUser is null"); //NOI18N
337                 } else if (event.getLinkedUser().getUserContact() == null) {
338                         // Throw NPE again
339                         throw new NullPointerException("event.linkedUser.userContact is null"); //NOI18N
340                 } else if (event.getLinkedUser().getUserContact().getContactId() == null) {
341                         // userId is null
342                         throw new NullPointerException("event.linkedUser.userContact.contactId is null"); //NOI18N
343                 } else if (event.getLinkedUser().getUserContact().getContactId() < 1) {
344                         // Not avalid id
345                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserContact().getContactId())); //NOI18N
346                 }
347
348                 // Remove contact from list available contacts list
349                 this.selectableContacts.remove(event.getLinkedUser().getUserContact());
350
351                 // Clear all data
352                 this.clear();
353         }
354
355         @Override
356         public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent event) {
357                 // event should not be null
358                 if (null == event) {
359                         // Throw NPE
360                         throw new NullPointerException("event is null"); //NOI18N
361                 } else if (event.getUpdatedContact() == null) {
362                         // Throw NPE again
363                         throw new NullPointerException("event.updatedContact is null"); //NOI18N
364                 } else if (event.getUpdatedContact().getContactId() == null) {
365                         // userId is null
366                         throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
367                 } else if (event.getUpdatedContact().getContactId() < 1) {
368                         // Not avalid id
369                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
370                 }
371
372                 // Add contact instance only once
373                 this.uniqueAddContact(event.getUpdatedContact());
374
375                 // Add email address to list
376                 this.emailAddressList.add(event.getUpdatedContact().getContactEmailAddress());
377         }
378
379         @Override
380         public void afterRegistrationEvent (@Observes final UserRegisteredEvent event) {
381                 // event should not be null
382                 if (null == event) {
383                         // Throw NPE
384                         throw new NullPointerException("event is null"); //NOI18N
385                 } else if (event.getRegisteredUser() == null) {
386                         // Throw NPE again
387                         throw new NullPointerException("event.registeredUser is null"); //NOI18N
388                 } else if (event.getRegisteredUser().getUserId() == null) {
389                         // userId is null
390                         throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
391                 } else if (event.getRegisteredUser().getUserId() < 1) {
392                         // Not avalid id
393                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
394                 }
395
396                 // Get user instance
397                 Contact registeredContact = event.getRegisteredUser().getUserContact();
398
399                 // Copy all data from registered->user
400                 this.copyContact(registeredContact);
401
402                 // Add contact instance only once
403                 this.uniqueAddContact(registeredContact);
404
405                 // Add user name and email address
406                 this.addUserNameEmailAddress(registeredContact);
407
408                 // Clear all data
409                 this.clear();
410         }
411
412         @Override
413         public void afterUserConfirmedAccountEvent (@Observes final UserConfirmedAccountEvent event) {
414                 // event should not be null
415                 if (null == event) {
416                         // Throw NPE
417                         throw new NullPointerException("event is null"); //NOI18N
418                 } else if (event.getConfirmedUser() == null) {
419                         // Throw NPE again
420                         throw new NullPointerException("event.confirmedUser is null"); //NOI18N
421                 } else if (event.getConfirmedUser().getUserId() == null) {
422                         // userId is null
423                         throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
424                 } else if (event.getConfirmedUser().getUserId() < 1) {
425                         // Not avalid id
426                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
427                 }
428
429                 // Add contact instance only once
430                 this.uniqueAddContact(event.getConfirmedUser().getUserContact());
431         }
432
433         @Override
434         public void afterUserLogin (@Observes final UserLoggedInEvent event) {
435                 // event should not be null
436                 if (null == event) {
437                         // Throw NPE
438                         throw new NullPointerException("event is null"); //NOI18N
439                 } else if (event.getLoggedInUser() == null) {
440                         // Throw NPE again
441                         throw new NullPointerException("event.loggedInUser is null"); //NOI18N
442                 } else if (event.getLoggedInUser().getUserId() == null) {
443                         // userId is null
444                         throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
445                 } else if (event.getLoggedInUser().getUserId() < 1) {
446                         // Not avalid id
447                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
448                 }
449
450                 // Copy all data to this bean
451                 this.copyContact(event.getLoggedInUser().getUserContact());
452         }
453
454         @Override
455         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
456         public List<Contact> allContacts () {
457                 // Debug message
458                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactController.allContacts: contactList.size()={0} - EXIT!", this.contactList.size()));
459
460                 // Return un-modified list
461                 return this.contactList;
462         }
463
464         @Override
465         public Contact createContactInstance () {
466                 // User message
467                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: CALLED!", this.getClass().getSimpleName()));
468
469                 // Is all required data set?
470                 if (!this.isRequiredPersonalDataSet()) {
471                         // No, then abort here
472                         throw new FaceletException(new IllegalArgumentException("Not all personal data is set, but createContactInstance() is called.")); //NOI18N
473                 }
474
475                 // Generate phone number
476                 DialableLandLineNumber phone = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
477                 DialableMobileNumber mobile = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
478                 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
479
480                 // Create new contact
481                 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
482                 contact.setContactStreet(this.getStreet());
483                 contact.setContactHouseNumber(this.getHouseNumber());
484                 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
485                 contact.setContactZipCode(this.getZipCode());
486                 contact.setContactCity(this.getCity());
487                 contact.setContactCountry(this.getCountry());
488                 contact.setContactEmailAddress(this.getEmailAddress());
489                 contact.setContactBirthday(this.getBirthday());
490                 contact.setContactComment(this.getComment());
491
492                 // Debug message
493                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: this.emailAddress={1}", this.getClass().getSimpleName(), this.getEmailAddress()));
494                 // Don't set null or wrong references
495                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) {
496                         // Now the number must be given
497                         if (phone.getPhoneAreaCode() == null) {
498                                 // Is null
499                                 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
500                         } else if (phone.getPhoneAreaCode() < 1) {
501                                 // Abort here
502                                 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
503                         } else if (phone.getPhoneNumber() == null) {
504                                 // Is null
505                                 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
506                         } else if (phone.getPhoneNumber() < 1) {
507                                 // Abort here
508                                 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
509                         }
510
511                         // Set phone number
512                         contact.setContactLandLineNumber(phone);
513                 }
514
515                 // Don't set null or wrong references
516                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
517                         // Now the number must be given
518                         if (fax.getPhoneAreaCode() == null) {
519                                 // Is null
520                                 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
521                         } else if (fax.getPhoneAreaCode() < 1) {
522                                 // Abort here
523                                 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
524                         } else if (fax.getPhoneNumber() == null) {
525                                 // Is null
526                                 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
527                         } else if (fax.getPhoneNumber() < 1) {
528                                 // Abort here
529                                 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
530                         }
531
532                         // Set fax number
533                         contact.setContactFaxNumber(fax);
534                 }
535
536                 // Is the provider set?
537                 if ((mobile instanceof DialableMobileNumber) && (this.getMobileProvider() instanceof MobileProvider) && (this.getMobileNumber() != null) && (this.getMobileNumber() > 0)) {
538                         // Is the number set?
539                         if (mobile.getPhoneNumber() == null) {
540                                 // Is null
541                                 throw new NullPointerException("mobileNumber.phoneNumber is null"); //NOI18N
542                         } else if (mobile.getPhoneNumber() < 1) {
543                                 // Abort here
544                                 throw new IllegalArgumentException("mobileNumber.phoneNumber is zero or below."); //NOI18N
545                         }
546
547                         // Set mobile number
548                         contact.setContactMobileNumber(mobile);
549                 }
550
551                 // Trace message
552                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: contact={1} - EXIT!", this.getClass().getSimpleName(), contact));
553                 // Return it
554                 return contact;
555         }
556
557         @Override
558         public String doChangePersonalContactData () {
559                 // This method shall only be called if the user is logged-in
560                 if (!this.userLoginController.isUserLoggedIn()) {
561                         // Not logged-in
562                         throw new IllegalStateException("User is not logged-in"); //NOI18N
563                 } else if (!this.isRequiredChangePersonalDataSet()) {
564                         // Not all required fields are set
565                         throw new FaceletException("Not all required fields are set."); //NOI18N
566                 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
567                         // Password not matching
568                         this.showFacesMessage("form_login_change_personal:currentPassword", "ERROR_USER_CURRENT_PASSWORD_MISMATCHING"); //NOI18N
569                         return ""; //NOI18N
570                 }
571
572                 // Get contact instance
573                 Contact contact = this.userLoginController.getLoggedInUser().getUserContact();
574
575                 // It should be there, so run some tests on it
576                 assert (contact instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
577                 assert (contact.getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
578                 assert (contact.getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
579
580                 // Update all fields
581                 contact.setContactGender(this.getGender());
582                 contact.setContactFirstName(this.getFirstName());
583                 contact.setContactFamilyName(this.getFamilyName());
584                 contact.setContactStreet(this.getStreet());
585                 contact.setContactHouseNumber(this.getHouseNumber());
586                 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
587                 contact.setContactZipCode(this.getZipCode());
588                 contact.setContactCity(this.getCity());
589                 contact.setContactCountry(this.getCountry());
590
591                 // Update contact's mobile number
592                 this.isMobileUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getMobileProvider(), this.getMobileNumber());
593
594                 // Update contact's land-line number
595                 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
596
597                 // Update contact's fax number
598                 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
599
600                 // Send it to the EJB
601                 this.contactBean.updateContactData(contact, this.isMobileUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
602
603                 // All fine
604                 return "contact_data_saved"; //NOI18N
605         }
606
607         @Override
608         @SuppressWarnings ("ReturnOfDateField")
609         public Date getBirthday () {
610                 return this.birthday;
611         }
612
613         @Override
614         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
615         public void setBirthday (final Date birthday) {
616                 this.birthday = birthday;
617         }
618
619         @Override
620         public String getCity () {
621                 return this.city;
622         }
623
624         @Override
625         public void setCity (final String city) {
626                 this.city = city;
627         }
628
629         @Override
630         public String getComment () {
631                 return this.comment;
632         }
633
634         @Override
635         public void setComment (final String comment) {
636                 this.comment = comment;
637         }
638
639         @Override
640         public String getControllerType () {
641                 return "general"; //NOI18N
642         }
643
644         @Override
645         @Deprecated
646         public void setControllerType (final String controllerType) {
647                 throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
648         }
649
650         @Override
651         public Country getCountry () {
652                 return this.country;
653         }
654
655         @Override
656         public void setCountry (final Country country) {
657                 this.country = country;
658         }
659
660         @Override
661         public String getEmailAddress () {
662                 return this.emailAddress;
663         }
664
665         @Override
666         public void setEmailAddress (final String emailAddress) {
667                 this.emailAddress = emailAddress;
668         }
669
670         @Override
671         public String getEmailAddressRepeat () {
672                 return this.emailAddressRepeat;
673         }
674
675         @Override
676         public void setEmailAddressRepeat (final String emailAddressRepeat) {
677                 this.emailAddressRepeat = emailAddressRepeat;
678         }
679
680         @Override
681         public String getFamilyName () {
682                 return this.familyName;
683         }
684
685         @Override
686         public void setFamilyName (final String familyName) {
687                 this.familyName = familyName;
688         }
689
690         @Override
691         public Integer getFaxAreaCode () {
692                 return this.faxAreaCode;
693         }
694
695         @Override
696         public void setFaxAreaCode (final Integer faxAreaCode) {
697                 this.faxAreaCode = faxAreaCode;
698         }
699
700         @Override
701         public Country getFaxCountry () {
702                 return this.faxCountry;
703         }
704
705         @Override
706         public void setFaxCountry (final Country faxCountry) {
707                 this.faxCountry = faxCountry;
708         }
709
710         @Override
711         public Long getFaxNumber () {
712                 return this.faxNumber;
713         }
714
715         @Override
716         public void setFaxNumber (final Long faxNumber) {
717                 this.faxNumber = faxNumber;
718         }
719
720         @Override
721         public String getFirstName () {
722                 return this.firstName;
723         }
724
725         @Override
726         public void setFirstName (final String firstName) {
727                 this.firstName = firstName;
728         }
729
730         @Override
731         public Gender getGender () {
732                 return this.gender;
733         }
734
735         @Override
736         public void setGender (final Gender gender) {
737                 this.gender = gender;
738         }
739
740         @Override
741         public Short getHouseNumber () {
742                 return this.houseNumber;
743         }
744
745         @Override
746         public void setHouseNumber (final Short houseNumber) {
747                 this.houseNumber = houseNumber;
748         }
749
750         @Override
751         public String getHouseNumberExtension () {
752                 return this.houseNumberExtension;
753         }
754
755         @Override
756         public void setHouseNumberExtension (final String houseNumberExtension) {
757                 this.houseNumberExtension = houseNumberExtension;
758         }
759
760         @Override
761         public Integer getLandLineAreaCode () {
762                 return this.landLineAreaCode;
763         }
764
765         @Override
766         public void setLandLineAreaCode (final Integer landLineAreaCode) {
767                 this.landLineAreaCode = landLineAreaCode;
768         }
769
770         @Override
771         public Country getLandLineCountry () {
772                 return this.landLineCountry;
773         }
774
775         @Override
776         public void setLandLineCountry (final Country landLineCountry) {
777                 this.landLineCountry = landLineCountry;
778         }
779
780         @Override
781         public Long getLandLineNumber () {
782                 return this.landLineNumber;
783         }
784
785         @Override
786         public void setLandLineNumber (final Long landLineNumber) {
787                 this.landLineNumber = landLineNumber;
788         }
789
790         @Override
791         public Long getMobileNumber () {
792                 return this.mobileNumber;
793         }
794
795         @Override
796         public void setMobileNumber (Long mobileNumber) {
797                 this.mobileNumber = mobileNumber;
798         }
799
800         @Override
801         public MobileProvider getMobileProvider () {
802                 return this.mobileProvider;
803         }
804
805         @Override
806         public void setMobileProvider (final MobileProvider mobileProvider) {
807                 this.mobileProvider = mobileProvider;
808         }
809
810         @Override
811         public String getStreet () {
812                 return this.street;
813         }
814
815         @Override
816         public void setStreet (final String street) {
817                 this.street = street;
818         }
819
820         @Override
821         public String getTitle () {
822                 return this.title;
823         }
824
825         @Override
826         public void setTitle (final String title) {
827                 this.title = title;
828         }
829
830         @Override
831         public Integer getZipCode () {
832                 return this.zipCode;
833         }
834
835         @Override
836         public void setZipCode (final Integer zipCode) {
837                 this.zipCode = zipCode;
838         }
839
840         /**
841          * Post-initialization of this class
842          */
843         @PostConstruct
844         public void init () {
845                 // Get full email address list for reducing EJB calls
846                 this.emailAddressList.addAll(this.contactBean.getEmailAddressList());
847
848                 // Get full contact list
849                 this.contactList.addAll(this.contactBean.getAllContacts());
850
851                 // Get all users
852                 List<User> allUsers = this.userController.allUsers();
853
854                 // Get all contacts
855                 List<Contact> allContacts = this.contactBean.getAllContacts();
856
857                 // Get iterator
858                 Iterator<Contact> iterator = allContacts.iterator();
859
860                 // Loop through it
861                 while (iterator.hasNext()) {
862                         // Get next element
863                         Contact next = iterator.next();
864
865                         // Get iterator
866                         Iterator<User> userIterator = allUsers.iterator();
867
868                         // Loop through all users
869                         while (userIterator.hasNext()) {
870                                 // Get user instance
871                                 User nextUser = userIterator.next();
872
873                                 // Is contact same?
874                                 if (Objects.equals(next, nextUser.getUserContact())) {
875                                         // Found same
876                                         iterator.remove();
877                                         break;
878                                 }
879                         }
880                 }
881
882                 // Set contact list
883                 this.selectableContacts = allContacts;
884         }
885
886         @Override
887         public boolean isEmailAddressRegistered (final Contact contact) {
888                 // Cherck parameter
889                 if (null == contact) {
890                         // Throw NPE
891                         throw new NullPointerException("contact is null"); //NOI18N
892                 } else if (contact.getContactEmailAddress() == null) {
893                         // Throw again
894                         throw new NullPointerException("contact.contactEmailAddress is null"); //NOI18N
895                 } else if (contact.getContactEmailAddress().isEmpty()) {
896                         // Is empty
897                         throw new IllegalArgumentException("contact.contactEmailAddress is empty."); //NOI18N
898                 }
899
900                 // Determine it
901                 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(contact.getContactEmailAddress())));
902         }
903
904         @Override
905         public boolean isRequiredChangePersonalDataSet () {
906                 return ((this.getGender() != null) &&
907                                 (this.getFirstName() != null) &&
908                                 (this.getFamilyName() != null) &&
909                                 (this.getStreet() != null) &&
910                                 (this.getHouseNumber() != null) &&
911                                 (this.getZipCode() != null) &&
912                                 (this.getCity() != null));
913         }
914
915         @Override
916         public boolean isRequiredPersonalDataSet () {
917                 return ((this.getGender() != null) &&
918                                 (this.getFirstName() != null) &&
919                                 (this.getFamilyName() != null) &&
920                                 (this.getStreet() != null) &&
921                                 (this.getHouseNumber() != null) &&
922                                 (this.getZipCode() != null) &&
923                                 (this.getCity() != null) &&
924                                 (this.getEmailAddress() != null) &&
925                                 (this.getEmailAddressRepeat() != null));
926         }
927
928         @Override
929         public boolean isSameEmailAddressEntered () {
930                 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
931         }
932
933         @Override
934         public Contact lookupContactById (final Long contactId) throws ContactNotFoundException {
935                 // Init variable
936                 Contact localContact = null;
937
938                 // Clear this bean
939                 this.clear();
940
941                 // Try to lookup it in visible user list
942                 for (final Iterator<Contact> iterator = this.contactList.iterator(); iterator.hasNext();) {
943                         // Get next user
944                         Contact next = iterator.next();
945
946                         // Is the user id found?
947                         if (Objects.equals(next.getContactId(), contactId)) {
948                                 // Copy to other variable
949                                 localContact = next;
950                                 break;
951                         }
952                 }
953
954                 // Is it still null?
955                 if (null == localContact) {
956                         // Not visible for the current user
957                         throw new ContactNotFoundException(contactId);
958                 }
959
960                 // Copy all data to this bean
961                 this.copyContact(localContact);
962
963                 // Return it
964                 return localContact;
965         }
966
967         @Override
968         public void removeMobileNumberFromListEvent (@Observes final AdminRemoveMobileNumberFromListEvent event) {
969                 // event should not be null
970                 if (null == event) {
971                         // Throw NPE
972                         throw new NullPointerException("event is null"); //NOI18N
973                 } else if (event.getMobileNumberList() == null) {
974                         // Throw NPE again
975                         throw new NullPointerException("event.mobileList is null"); //NOI18N
976                 } else if (event.getMobileNumberList().isEmpty()) {
977                         // List is empty, no need to check
978                         return;
979                 }
980
981                 // Check all entries
982                 for (final Contact contact : this.contactList) {
983                         // Is the mobile instance set and in list?
984                         if ((contact.getContactMobileNumber() instanceof DialableMobileNumber) && (event.getMobileNumberList().contains(contact.getContactMobileNumber()))) {
985                                 // Found it, so remvoe it from list
986                                 event.getMobileNumberList().remove(contact.getContactMobileNumber());
987                         }
988                 }
989         }
990
991         @Override
992         public List<Contact> selectableContacts () {
993                 return Collections.unmodifiableList(this.selectableContacts);
994         }
995
996         @Override
997         public void updateContactDataFromController (final Contact contact) {
998                 // Is the instance valid?
999                 if (null == contact) {
1000                         // Throw NPE
1001                         throw new NullPointerException("contact is null"); //NOI18N
1002                 } else if (contact.getContactId() == null) {
1003                         // Throw NPE
1004                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1005                 } else if (contact.getContactId() < 1) {
1006                         // Not valid id number
1007                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1008                 }
1009
1010                 // Set all
1011                 this.copyContact(contact);
1012         }
1013
1014         /**
1015          * Adds email address to bean's internal list.
1016          * <p>
1017          * @param contact Contact instance
1018          */
1019         private void addUserNameEmailAddress (final Contact contact) {
1020                 // Make sure the entry is not added yet
1021                 if (this.emailAddressList.contains(contact.getContactEmailAddress())) {
1022                         // Already added
1023                         throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
1024                 }
1025
1026                 // Add email addres
1027                 this.emailAddressList.add(contact.getContactEmailAddress());
1028         }
1029
1030         /**
1031          * Clears this bean
1032          */
1033         private void clear () {
1034                 // Clear all data
1035                 // - personal data
1036                 this.setGender(null);
1037                 this.setTitle(null);
1038                 this.setFirstName(null);
1039                 this.setFamilyName(null);
1040                 this.setStreet(null);
1041                 this.setHouseNumber(null);
1042                 this.setHouseNumberExtension(null);
1043                 this.setZipCode(null);
1044                 this.setCity(null);
1045                 this.setCountry(null);
1046
1047                 // - contact data
1048                 this.setEmailAddress(null);
1049                 this.setEmailAddressRepeat(null);
1050                 this.setLandLineAreaCode(null);
1051                 this.setLandLineCountry(null);
1052                 this.setLandLineNumber(null);
1053                 this.setMobileProvider(null);
1054                 this.setMobileNumber(null);
1055                 this.setFaxAreaCode(null);
1056                 this.setFaxCountry(null);
1057                 this.setFaxNumber(null);
1058
1059                 // - other data
1060                 this.setBirthday(null);
1061                 this.setComment(null);
1062         }
1063
1064         /**
1065          * Copies given contact into the controller
1066          * <p>
1067          * @param contact Contact instance
1068          */
1069         private void copyContact (final Contact contact) {
1070                 // Is the instance valid?
1071                 if (null == contact) {
1072                         // Throw NPE
1073                         throw new NullPointerException("contact is null"); //NOI18N
1074                 } else if (contact.getContactId() == null) {
1075                         // Throw NPE
1076                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1077                 } else if (contact.getContactId() < 1) {
1078                         // Not valid id number
1079                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1080                 }
1081
1082                 // Copy all fields:
1083                 // - base data
1084                 this.setGender(contact.getContactGender());
1085                 this.setTitle(contact.getContactTitle());
1086                 this.setFirstName(contact.getContactFirstName());
1087                 this.setFamilyName(contact.getContactFamilyName());
1088                 this.setStreet(contact.getContactStreet());
1089                 this.setHouseNumber(contact.getContactHouseNumber());
1090                 this.setHouseNumberExtension(contact.getContactHouseNumberExtension());
1091                 this.setZipCode(contact.getContactZipCode());
1092                 this.setCity(contact.getContactCity());
1093                 this.setCountry(contact.getContactCountry());
1094                 this.setEmailAddress(contact.getContactEmailAddress());
1095                 this.setBirthday(contact.getContactBirthday());
1096                 this.setComment(contact.getContactComment());
1097
1098                 // Get mobile, phone and fax instance
1099                 DialableMobileNumber mobile = contact.getContactMobileNumber();
1100                 DialableFaxNumber fax = contact.getContactFaxNumber();
1101                 DialableLandLineNumber phone = contact.getContactLandLineNumber();
1102
1103                 // - contact data
1104                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
1105                         this.setLandLineCountry(phone.getPhoneCountry());
1106                         this.setLandLineAreaCode(phone.getPhoneAreaCode());
1107                         this.setLandLineNumber(phone.getPhoneNumber());
1108                 }
1109
1110                 if ((mobile instanceof DialableMobileNumber) && (mobile.getMobileProvider() instanceof MobileProvider)) {
1111                         this.setMobileProvider(mobile.getMobileProvider());
1112                         this.setMobileNumber(mobile.getPhoneNumber());
1113                 }
1114
1115                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
1116                         this.setFaxCountry(fax.getPhoneCountry());
1117                         this.setFaxAreaCode(fax.getPhoneAreaCode());
1118                         this.setFaxNumber(fax.getPhoneNumber());
1119                 }
1120         }
1121
1122         /**
1123          * Removes given contact from all lists
1124          * <p>
1125          * @param contact Contact instance to remove
1126          */
1127         private void removeContact (final Contact contact) {
1128                 // Is the instance valid?
1129                 if (null == contact) {
1130                         // Throw NPE
1131                         throw new NullPointerException("contact is null"); //NOI18N
1132                 } else if (contact.getContactId() == null) {
1133                         // Throw NPE
1134                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1135                 } else if (contact.getContactId() < 1) {
1136                         // Not valid id number
1137                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1138                 }
1139
1140                 // Remove from general list
1141                 if (!this.contactList.remove(contact)) {
1142                         // Did not remove contact
1143                         throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N
1144                 }
1145
1146                 // Remove from other lists
1147                 this.emailAddressList.remove(contact.getContactEmailAddress());
1148         }
1149
1150         /**
1151          * Adds unique instance to contact list. First any existing instance is
1152          * being removed, then the new instance is added.
1153          * <p>
1154          * @param contact Contact instance to add uniquely
1155          */
1156         private void uniqueAddContact (final Contact contact) {
1157                 // Is the instance valid?
1158                 if (null == contact) {
1159                         // Throw NPE
1160                         throw new NullPointerException("contact is null"); //NOI18N
1161                 } else if (contact.getContactId() == null) {
1162                         // Throw NPE
1163                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1164                 } else if (contact.getContactId() < 1) {
1165                         // Not valid id number
1166                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1167                 }
1168
1169                 // Get iterator from list
1170                 Iterator<Contact> iterator = this.contactList.iterator();
1171
1172                 // "Walk" through all entries
1173                 while (iterator.hasNext()) {
1174                         // Get next element
1175                         Contact next = iterator.next();
1176
1177                         // Is id number the same?
1178                         if (Objects.equals(contact.getContactId(), next.getContactId())) {
1179                                 // Found entry, so remove it and abort
1180                                 this.removeContact(next);
1181                                 break;
1182                         }
1183                 }
1184
1185                 // Add contact to list
1186                 this.contactList.add(contact);
1187         }
1188
1189 }