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