]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/contact/JobsContactWebSessionBean.java
Continued a bit: (please cherry-pick)
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / contact / JobsContactWebSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jjobs.beans.contact;
18
19 import java.text.MessageFormat;
20 import java.util.Collections;
21 import java.util.Date;
22 import java.util.Iterator;
23 import java.util.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.jphone.phonenumbers.cellphone.CellphoneNumber;
46 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
47 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
48 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
49 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
50 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
51 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
52 import org.mxchange.jusercore.events.confirmation.UserConfirmedAccountEvent;
53 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
54 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
55 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
56 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
57
58 /**
59  * A general contact bean (controller)
60  * <p>
61  * @author Roland Haeder<roland@mxchange.org>
62  */
63 @Named ("contactController")
64 @SessionScoped
65 public class JobsContactWebSessionBean extends BaseJobsController implements JobsContactWebSessionController {
66
67         /**
68          * Serial number
69          */
70         private static final long serialVersionUID = 542_145_347_916L;
71
72         /**
73          * Birth day
74          */
75         private Date birthday;
76
77         /**
78          * Cellphone number's carrier
79          */
80         private MobileProvider cellphoneCarrier;
81
82         /**
83          * Cellphone number
84          */
85         private Long cellphoneNumber;
86
87         /**
88          * City
89          */
90         private String city;
91
92         /**
93          * Optional comments
94          */
95         private String comment;
96
97         /**
98          * Remote contact bean
99          */
100         private final ContactSessionBeanRemote contactBean;
101
102         /**
103          * Contact list
104          */
105         private List<Contact> contactList;
106
107         /**
108          * Country instance
109          */
110         private Country country;
111
112         /**
113          * Email address
114          */
115         private String emailAddress;
116
117         /**
118          * Email address list
119          */
120         private List<String> emailAddressList;
121
122         /**
123          * Email address repeated
124          */
125         private String emailAddressRepeat;
126
127         /**
128          * Family name
129          */
130         private String familyName;
131
132         /**
133          * Fax number's area code
134          */
135         private Integer faxAreaCode;
136
137         /**
138          * Country instance for fax number
139          */
140         private Country faxCountry;
141
142         /**
143          * Fax number
144          */
145         private Long faxNumber;
146
147         /**
148          * First name
149          */
150         private String firstName;
151
152         /**
153          * Gender instance
154          */
155         private Gender gender;
156
157         /**
158          * House number
159          */
160         private Short houseNumber;
161
162         /**
163          * Whether a cellphone entry has been unlinked
164          */
165         private boolean isCellphoneUnlinked;
166
167         /**
168          * Whether a fax entry has been unlinked
169          */
170         private boolean isFaxUnlinked;
171
172         /**
173          * Whether a land-line number has been unlinked
174          */
175         private boolean isLandLineUnlinked;
176
177         /**
178          * Phone number area code
179          */
180         private Integer phoneAreaCode;
181
182         /**
183          * Country instance for phone number
184          */
185         private Country phoneCountry;
186
187         /**
188          * Phone number
189          */
190         private Long phoneNumber;
191
192         /**
193          * Street
194          */
195         private String street;
196
197         /**
198          * Login bean (controller)
199          */
200         @Inject
201         private JobsUserLoginWebSessionController userLoginController;
202
203         /**
204          * ZIP code
205          */
206         private Integer zipCode;
207
208         /**
209          * Default constructor
210          */
211         public JobsContactWebSessionBean () {
212                 // Try it
213                 try {
214                         // Get initial context
215                         Context context = new InitialContext();
216
217                         // Try to lookup
218                         this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jratecalc-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
219                 } catch (final NamingException e) {
220                         // Throw again
221                         throw new FaceletException(e);
222                 }
223         }
224
225         @Override
226         public void afterAdminAddedContact (@Observes final AdminAddedContactEvent event) {
227                 // The event must be valid
228                 if (null == event) {
229                         // Throw NPE
230                         throw new NullPointerException("event is null"); //NOI18N
231                 } else if (event.getAddedContact() == null) {
232                         // Throw again ...
233                         throw new NullPointerException("event.addedContact is null"); //NOI18N
234                 } else if (event.getAddedContact().getContactId() == null) {
235                         // ... and again
236                         throw new NullPointerException("event.addedContact.customerId is null"); //NOI18N
237                 } else if (event.getAddedContact().getContactId() < 1) {
238                         // Not valid
239                         throw new IllegalArgumentException(MessageFormat.format("event.addedContact.customerId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N
240                 }
241
242                 // Clear this bean
243                 this.clear();
244
245                 // Call other method
246                 this.contactList.add(event.getAddedContact());
247         }
248
249         @Override
250         public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
251                 // Trace message
252                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterAdminAddedUserEvent: event={0} - CALLED!", event)); //NOI18N
253
254                 // event should not be null
255                 if (null == event) {
256                         // Throw NPE
257                         throw new NullPointerException("event is null"); //NOI18N
258                 } else if (event.getAddedUser() == null) {
259                         // Throw NPE again
260                         throw new NullPointerException("event.addedUser is null"); //NOI18N
261                 } else if (event.getAddedUser().getUserId() == null) {
262                         // userId is null
263                         throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
264                 } else if (event.getAddedUser().getUserId() < 1) {
265                         // Not avalid id
266                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
267                 }
268
269                 // Clear all data
270                 this.clear();
271
272                 // Trace message
273                 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterAdminAddedUserEvent: EXIT!"); //NOI18N
274         }
275
276         @Override
277         public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent event) {
278                 // Trace message
279                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterAdminUpdatedContactDataEvent: event={0} - CALLED!", event)); //NOI18N
280
281                 // event should not be null
282                 if (null == event) {
283                         // Throw NPE
284                         throw new NullPointerException("event is null"); //NOI18N
285                 } else if (event.getUpdatedContact() == null) {
286                         // Throw NPE again
287                         throw new NullPointerException("event.updatedContact is null"); //NOI18N
288                 } else if (event.getUpdatedContact().getContactId() == null) {
289                         // userId is null
290                         throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
291                 } else if (event.getUpdatedContact().getContactId() < 1) {
292                         // Not avalid id
293                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
294                 }
295
296                 // Get iterator from list
297                 Iterator<Contact> iterator = this.contactList.iterator();
298
299                 // "Walk" through all entries
300                 while (iterator.hasNext()) {
301                         // Get next element
302                         Contact next = iterator.next();
303
304                         // Is id number the same?
305                         if (Objects.equals(event.getUpdatedContact().getContactId(), next.getContactId())) {
306                                 // Found entry, so remove it and abort
307                                 this.contactList.remove(next);
308
309                                 // Remove also email from list
310                                 this.emailAddressList.remove(next.getContactEmailAddress());
311                                 break;
312                         }
313                 }
314
315                 // Add contact to list
316                 this.contactList.add(event.getUpdatedContact());
317
318                 // Add email address to list
319                 this.emailAddressList.add(event.getUpdatedContact().getContactEmailAddress());
320         }
321
322         @Override
323         public void afterRegistrationEvent (@Observes final UserRegisteredEvent event) {
324                 // Trace message
325                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
326
327                 // event should not be null
328                 if (null == event) {
329                         // Throw NPE
330                         throw new NullPointerException("event is null"); //NOI18N
331                 } else if (event.getRegisteredUser() == null) {
332                         // Throw NPE again
333                         throw new NullPointerException("event.registeredUser is null"); //NOI18N
334                 } else if (event.getRegisteredUser().getUserId() == null) {
335                         // userId is null
336                         throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
337                 } else if (event.getRegisteredUser().getUserId() < 1) {
338                         // Not avalid id
339                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
340                 }
341
342                 // Get user instance
343                 Contact registeredContact = event.getRegisteredUser().getUserContact();
344
345                 // Debug message
346                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterRegistration: registeredContact={0}", registeredContact)); //NOI18N
347
348                 // Copy all data from registered->user
349                 this.copyContact(registeredContact);
350
351                 // Add user name and email address
352                 this.addUserNameEmailAddress(registeredContact);
353
354                 // Clear all data
355                 this.clear();
356
357                 // Trace message
358                 //* NOISY-DEBUG: */ System.out.println("ContactWebBean:afterRegistration: EXIT!"); //NOI18N
359         }
360
361         @Override
362         public void afterUserConfirmedAccount (@Observes final UserConfirmedAccountEvent event) {
363                 // Trace message
364                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterAdminUpdatedContactDataEvent: event={0} - CALLED!", event)); //NOI18N
365
366                 // event should not be null
367                 if (null == event) {
368                         // Throw NPE
369                         throw new NullPointerException("event is null"); //NOI18N
370                 } else if (event.getConfirmedUser() == null) {
371                         // Throw NPE again
372                         throw new NullPointerException("event.confirmedUser is null"); //NOI18N
373                 } else if (event.getConfirmedUser().getUserId() == null) {
374                         // userId is null
375                         throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
376                 } else if (event.getConfirmedUser().getUserId() < 1) {
377                         // Not avalid id
378                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
379                 }
380
381                 // "Cache" contact instance
382                 Contact contact = event.getConfirmedUser().getUserContact();
383
384                 // Get iterator from list
385                 Iterator<Contact> iterator = this.contactList.iterator();
386
387                 // "Walk" through all entries
388                 while (iterator.hasNext()) {
389                         // Get next element
390                         Contact next = iterator.next();
391
392                         // Is id number the same?
393                         if (Objects.equals(contact.getContactId(), next.getContactId())) {
394                                 // Found entry, so remove it and abort
395                                 this.contactList.remove(next);
396                                 break;
397                         }
398                 }
399
400                 // Add contact to list
401                 this.contactList.add(contact);
402         }
403
404         @Override
405         public void afterUserLogin (@Observes final UserLoggedInEvent event) {
406                 // Trace message
407                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
408
409                 // event should not be null
410                 if (null == event) {
411                         // Throw NPE
412                         throw new NullPointerException("event is null"); //NOI18N
413                 } else if (event.getLoggedInUser() == null) {
414                         // Throw NPE again
415                         throw new NullPointerException("event.loggedInUser is null"); //NOI18N
416                 } else if (event.getLoggedInUser().getUserId() == null) {
417                         // userId is null
418                         throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
419                 } else if (event.getLoggedInUser().getUserId() < 1) {
420                         // Not avalid id
421                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
422                 }
423
424                 // Copy all data to this bean
425                 this.copyContact(event.getLoggedInUser().getUserContact());
426
427                 // Trace message
428                 //* NOISY-DEBUG: */ System.out.println("ContactWebBean:afterUserLogin - EXIT!"); //NOI18N
429         }
430
431         @Override
432         public List<Contact> allContacts () {
433                 // Debug message
434                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactController.allContacts: contactList.size()={0} - EXIT!", this.contactList.size()));
435
436                 // Return un-modified list
437                 return Collections.unmodifiableList(this.contactList);
438         }
439
440         @Override
441         public Contact createContactInstance () {
442                 // User message
443                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: CALLED!", this.getClass().getSimpleName()));
444
445                 // Is all required data set?
446                 if (!this.isRequiredPersonalDataSet()) {
447                         // No, then abort here
448                         throw new FaceletException(new IllegalArgumentException("Not all personal data is set, but createContactInstance() is called.")); //NOI18N
449                 }
450
451                 // Generate phone number
452                 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
453                 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
454                 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
455
456                 // Create new contact
457                 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
458                 contact.setContactStreet(this.getStreet());
459                 contact.setContactHouseNumber(this.getHouseNumber());
460                 contact.setContactZipCode(this.getZipCode());
461                 contact.setContactCity(this.getCity());
462                 contact.setContactCountry(this.getCountry());
463                 contact.setContactEmailAddress(this.getEmailAddress());
464                 contact.setContactBirthday(this.getBirthday());
465                 contact.setContactComment(this.getComment());
466
467                 // Debug message
468                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: this.emailAddress={1}", this.getClass().getSimpleName(), this.getEmailAddress()));
469
470                 // Don't set null or wrong references
471                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
472                         // Now the number must be given
473                         if (phone.getPhoneAreaCode() == null) {
474                                 // Is null
475                                 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
476                         } else if (phone.getPhoneAreaCode() < 1) {
477                                 // Abort here
478                                 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
479                         } else if (phone.getPhoneNumber() == null) {
480                                 // Is null
481                                 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
482                         } else if (phone.getPhoneNumber() < 1) {
483                                 // Abort here
484                                 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
485                         }
486
487                         // Set phone number
488                         contact.setContactLandLineNumber(phone);
489                 }
490
491                 // Don't set null or wrong references
492                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
493                         // Now the number must be given
494                         if (fax.getPhoneAreaCode() == null) {
495                                 // Is null
496                                 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
497                         } else if (fax.getPhoneAreaCode() < 1) {
498                                 // Abort here
499                                 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
500                         } else if (fax.getPhoneNumber() == null) {
501                                 // Is null
502                                 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
503                         } else if (fax.getPhoneNumber() < 1) {
504                                 // Abort here
505                                 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
506                         }
507
508                         // Set fax number
509                         contact.setContactFaxNumber(fax);
510                 }
511
512                 // Is the provider set?
513                 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
514                         // Is the number set?
515                         if (cellphone.getPhoneNumber() == null) {
516                                 // Is null
517                                 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
518                         } else if (cellphone.getPhoneNumber() < 1) {
519                                 // Abort here
520                                 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
521                         }
522
523                         // Set cellphone number
524                         contact.setContactCellphoneNumber(cellphone);
525                 }
526
527                 // Trace message
528                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: contact={1} - EXIT!", this.getClass().getSimpleName(), contact));
529
530                 // Return it
531                 return contact;
532         }
533
534         @Override
535         public String doChangePersonalContactData () {
536                 // This method shall only be called if the user is logged-in
537                 if (!this.userLoginController.isUserLoggedIn()) {
538                         // Not logged-in
539                         throw new IllegalStateException("User is not logged-in"); //NOI18N
540                 } else if (!this.isRequiredChangePersonalDataSet()) {
541                         // Not all required fields are set
542                         throw new FaceletException("Not all required fields are set."); //NOI18N
543                 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
544                         // Password not matching
545                         this.showFacesMessage("form_login_change_personal:currentPassword", new UserPasswordMismatchException(this.userLoginController.getLoggedInUser())); //NOI18N
546                         return ""; //NOI18N
547                 }
548
549                 // Get contact instance
550                 Contact contact = this.userLoginController.getLoggedInUser().getUserContact();
551
552                 // It should be there, so run some tests on it
553                 assert (contact instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
554                 assert (contact.getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
555                 assert (contact.getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
556
557                 // Update all fields
558                 contact.setContactGender(this.getGender());
559                 contact.setContactFirstName(this.getFirstName());
560                 contact.setContactFamilyName(this.getFamilyName());
561                 contact.setContactStreet(this.getStreet());
562                 contact.setContactHouseNumber(this.getHouseNumber());
563                 contact.setContactZipCode(this.getZipCode());
564                 contact.setContactCity(this.getCity());
565                 contact.setContactCountry(this.getCountry());
566
567                 // Update contact's cellphone number
568                 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
569
570                 // Update contact's land-line number
571                 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
572
573                 // Update contact's fax number
574                 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
575
576                 // Send it to the EJB
577                 this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
578
579                 // All fine
580                 return "contact_data_saved"; //NOI18N
581         }
582
583         @Override
584         @SuppressWarnings ("ReturnOfDateField")
585         public Date getBirthday () {
586                 return this.birthday;
587         }
588
589         @Override
590         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
591         public void setBirthday (final Date birthday) {
592                 this.birthday = birthday;
593         }
594
595         @Override
596         public MobileProvider getCellphoneCarrier () {
597                 return this.cellphoneCarrier;
598         }
599
600         @Override
601         public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
602                 this.cellphoneCarrier = cellphoneCarrier;
603         }
604
605         @Override
606         public Long getCellphoneNumber () {
607                 return this.cellphoneNumber;
608         }
609
610         @Override
611         public void setCellphoneNumber (Long cellphoneNumber) {
612                 this.cellphoneNumber = cellphoneNumber;
613         }
614
615         @Override
616         public String getCity () {
617                 return this.city;
618         }
619
620         @Override
621         public void setCity (final String city) {
622                 this.city = city;
623         }
624
625         @Override
626         public String getComment () {
627                 return this.comment;
628         }
629
630         @Override
631         public void setComment (final String comment) {
632                 this.comment = comment;
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 Integer getPhoneAreaCode () {
737                 return this.phoneAreaCode;
738         }
739
740         @Override
741         public void setPhoneAreaCode (final Integer phoneAreaCode) {
742                 this.phoneAreaCode = phoneAreaCode;
743         }
744
745         @Override
746         public Country getPhoneCountry () {
747                 return this.phoneCountry;
748         }
749
750         @Override
751         public void setPhoneCountry (final Country phoneCountry) {
752                 this.phoneCountry = phoneCountry;
753         }
754
755         @Override
756         public Long getPhoneNumber () {
757                 return this.phoneNumber;
758         }
759
760         @Override
761         public void setPhoneNumber (final Long phoneNumber) {
762                 this.phoneNumber = phoneNumber;
763         }
764
765         @Override
766         public String getStreet () {
767                 return this.street;
768         }
769
770         @Override
771         public void setStreet (final String street) {
772                 this.street = street;
773         }
774
775         @Override
776         public Integer getZipCode () {
777                 return this.zipCode;
778         }
779
780         @Override
781         public void setZipCode (final Integer zipCode) {
782                 this.zipCode = zipCode;
783         }
784
785         @Override
786         public boolean hasContacts () {
787                 return (!this.contactList.isEmpty());
788         }
789
790         /**
791          * Post-initialization of this class
792          */
793         @PostConstruct
794         public void init () {
795                 // Get full email address list for reducing EJB calls
796                 this.emailAddressList = this.contactBean.getEmailAddressList();
797
798                 // Get full contact list
799                 this.contactList = this.contactBean.getAllContacts();
800         }
801
802         @Override
803         public boolean isEmailAddressRegistered (final Contact contact) {
804                 // Cherck parameter
805                 if (null == contact) {
806                         // Throw NPE
807                         throw new NullPointerException("contact is null"); //NOI18N
808                 } else if (contact.getContactEmailAddress() == null) {
809                         // Throw again
810                         throw new NullPointerException("contact.contactEmailAddress is null"); //NOI18N
811                 } else if (contact.getContactEmailAddress().isEmpty()) {
812                         // Is empty
813                         throw new IllegalArgumentException("contact.contactEmailAddress is empty."); //NOI18N
814                 }
815
816                 // Determine it
817                 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(contact.getContactEmailAddress())));
818         }
819
820         @Override
821         public boolean isRequiredChangePersonalDataSet () {
822                 return ((this.getGender() != null) &&
823                                 (this.getFirstName() != null) &&
824                                 (this.getFamilyName() != null) &&
825                                 (this.getStreet() != null) &&
826                                 (this.getHouseNumber() != null) &&
827                                 (this.getZipCode() != null) &&
828                                 (this.getCity() != null));
829         }
830
831         @Override
832         public boolean isRequiredPersonalDataSet () {
833                 return ((this.getGender() != null) &&
834                                 (this.getFirstName() != null) &&
835                                 (this.getFamilyName() != null) &&
836                                 (this.getStreet() != null) &&
837                                 (this.getHouseNumber() != null) &&
838                                 (this.getZipCode() != null) &&
839                                 (this.getCity() != null) &&
840                                 (this.getEmailAddress() != null) &&
841                                 (this.getEmailAddressRepeat() != null));
842         }
843
844         @Override
845         public boolean isSameEmailAddressEntered () {
846                 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
847         }
848
849         @Override
850         public Contact lookupContactById (final Long contactId) throws ContactNotFoundException {
851                 // Init variable
852                 Contact localContact = null;
853
854                 // Clear this bean
855                 this.clear();
856
857                 // Try to lookup it in visible user list
858                 for (final Iterator<Contact> iterator = this.contactList.iterator(); iterator.hasNext();) {
859                         // Get next user
860                         Contact next = iterator.next();
861
862                         // Is the user id found?
863                         if (Objects.equals(next.getContactId(), contactId)) {
864                                 // Copy to other variable
865                                 localContact = next;
866                                 break;
867                         }
868                 }
869
870                 // Is it still null?
871                 if (null == localContact) {
872                         // Not visible for the current user
873                         throw new ContactNotFoundException(contactId);
874                 }
875
876                 // Copy all data to this bean
877                 this.copyContact(localContact);
878
879                 // Return it
880                 return localContact;
881         }
882
883         @Override
884         public void updateContactDataFromController (final Contact contact) {
885                 // Is the instance valid?
886                 if (null == contact) {
887                         // Throw NPE
888                         throw new NullPointerException("contact is null"); //NOI18N
889                 } else if (contact.getContactId() == null) {
890                         // Throw NPE
891                         throw new NullPointerException("contact.contactId is null"); //NOI18N
892                 } else if (contact.getContactId() < 1) {
893                         // Not valid id number
894                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
895                 }
896
897                 // Set all
898                 this.copyContact(contact);
899         }
900
901         /**
902          * Adds email address to bean's internal list.
903          * <p>
904          * @param contact Contact instance
905          */
906         private void addUserNameEmailAddress (final Contact contact) {
907                 // Make sure the entry is not added yet
908                 if (this.emailAddressList.contains(contact.getContactEmailAddress())) {
909                         // Already added
910                         throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
911                 }
912
913                 // Add email addres
914                 this.emailAddressList.add(contact.getContactEmailAddress());
915         }
916
917         /**
918          * Clears this bean
919          */
920         private void clear () {
921                 // Clear all data
922                 // - personal data
923                 this.setGender(Gender.UNKNOWN);
924                 this.setFirstName(null);
925                 this.setFamilyName(null);
926                 this.setStreet(null);
927                 this.setHouseNumber(null);
928                 this.setZipCode(null);
929                 this.setCity(null);
930                 this.setCountry(null);
931
932                 // - contact data
933                 this.setEmailAddress(null);
934                 this.setEmailAddressRepeat(null);
935                 this.setPhoneAreaCode(null);
936                 this.setPhoneCountry(null);
937                 this.setPhoneNumber(null);
938                 this.setCellphoneCarrier(null);
939                 this.setCellphoneNumber(null);
940                 this.setFaxAreaCode(null);
941                 this.setFaxCountry(null);
942                 this.setFaxNumber(null);
943
944                 // - other data
945                 this.setBirthday(null);
946                 this.setComment(null);
947         }
948
949         /**
950          * Copies given contact into the controller
951          * <p>
952          * @param contact Contact instance
953          */
954         private void copyContact (final Contact contact) {
955                 // Is the instance valid?
956                 if (null == contact) {
957                         // Throw NPE
958                         throw new NullPointerException("contact is null"); //NOI18N
959                 } else if (contact.getContactId() == null) {
960                         // Throw NPE
961                         throw new NullPointerException("contact.contactId is null"); //NOI18N
962                 } else if (contact.getContactId() < 1) {
963                         // Not valid id number
964                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
965                 }
966
967                 // Copy all fields:
968                 // - base data
969                 this.setGender(contact.getContactGender());
970                 this.setFirstName(contact.getContactFirstName());
971                 this.setFamilyName(contact.getContactFamilyName());
972                 this.setStreet(contact.getContactStreet());
973                 this.setHouseNumber(contact.getContactHouseNumber());
974                 this.setZipCode(contact.getContactZipCode());
975                 this.setCity(contact.getContactCity());
976                 this.setCountry(contact.getContactCountry());
977                 this.setEmailAddress(contact.getContactEmailAddress());
978                 this.setBirthday(contact.getContactBirthday());
979                 this.setComment(contact.getContactComment());
980
981                 // Get cellphone, phone and fax instance
982                 DialableCellphoneNumber cellphone = contact.getContactCellphoneNumber();
983                 DialableFaxNumber fax = contact.getContactFaxNumber();
984                 DialableLandLineNumber phone = contact.getContactLandLineNumber();
985
986                 // - contact data
987                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
988                         this.setPhoneCountry(phone.getPhoneCountry());
989                         this.setPhoneAreaCode(phone.getPhoneAreaCode());
990                         this.setPhoneNumber(phone.getPhoneNumber());
991                 }
992
993                 if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof MobileProvider)) {
994                         this.setCellphoneCarrier(cellphone.getCellphoneProvider());
995                         this.setCellphoneNumber(cellphone.getPhoneNumber());
996                 }
997
998                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
999                         this.setFaxCountry(fax.getPhoneCountry());
1000                         this.setFaxAreaCode(fax.getPhoneAreaCode());
1001                         this.setFaxNumber(fax.getPhoneNumber());
1002                 }
1003         }
1004
1005         /**
1006          * Removes given contact from all lists
1007          * <p>
1008          * @param contact Contact instance to remove
1009          */
1010         private void removeContact (final Contact contact) {
1011                 // Is the instance valid?
1012                 if (null == contact) {
1013                         // Throw NPE
1014                         throw new NullPointerException("contact is null"); //NOI18N
1015                 } else if (contact.getContactId() == null) {
1016                         // Throw NPE
1017                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1018                 } else if (contact.getContactId() < 1) {
1019                         // Not valid id number
1020                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1021                 }
1022
1023                 // Remove from general list
1024                 if (!this.contactList.remove(contact)) {
1025                         // Did not remove contact
1026                         throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N
1027                 }
1028
1029                 // Remove from other lists
1030                 this.emailAddressList.remove(contact.getContactEmailAddress());
1031         }
1032
1033 }