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