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