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