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