]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/beans/contact/AddressbookContactWebSessionBean.java
opps, forgot to clear this data (cherry-pick this)
[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 de.chotime.landingpage.beans.login.LandingUserLoginWebSessionController;
20 import java.text.MessageFormat;
21 import java.util.Collections;
22 import java.util.Date;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Objects;
26 import javax.annotation.PostConstruct;
27 import javax.enterprise.context.SessionScoped;
28 import javax.enterprise.event.Observes;
29 import javax.faces.view.facelets.FaceletException;
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 import javax.naming.Context;
33 import javax.naming.InitialContext;
34 import javax.naming.NamingException;
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 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          * Login bean (controller)
178          */
179         @Inject
180         private AddressbookUserLoginWebSessionController loginController;
181
182         /**
183          * Phone number area code
184          */
185         private Integer phoneAreaCode;
186
187         /**
188          * Country instance for phone number
189          */
190         private Country phoneCountry;
191
192         /**
193          * Phone number
194          */
195         private Long phoneNumber;
196
197         /**
198          * Street
199          */
200         private String street;
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                 // Create new contact instance
411                 Contact localContact = new UserContact();
412
413                 // Generate phone number
414                 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
415                 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
416                 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
417
418                 // Create new contact
419                 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
420                 contact.setContactStreet(this.getStreet());
421                 contact.setContactHouseNumber(this.getHouseNumber());
422                 contact.setContactZipCode(this.getZipCode());
423                 contact.setContactCity(this.getCity());
424                 contact.setContactCountry(this.getCountry());
425                 contact.setContactEmailAddress(this.getEmailAddress());
426                 contact.setContactBirthday(this.getBirthday());
427                 contact.setContactComment(this.getComment());
428
429                 // Debug message
430                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: this.emailAddress={1}", this.getClass().getSimpleName(), this.getEmailAddress()));
431                 // Don't set null or wrong references
432                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
433                         // Now the number must be given
434                         if (phone.getPhoneAreaCode() == null) {
435                                 // Is null
436                                 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
437                         } else if (phone.getPhoneAreaCode() < 1) {
438                                 // Abort here
439                                 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
440                         } else if (phone.getPhoneNumber() == null) {
441                                 // Is null
442                                 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
443                         } else if (phone.getPhoneNumber() < 1) {
444                                 // Abort here
445                                 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
446                         }
447
448                         // Set phone number
449                         contact.setContactLandLineNumber(phone);
450                 }
451
452                 // Don't set null or wrong references
453                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
454                         // Now the number must be given
455                         if (fax.getPhoneAreaCode() == null) {
456                                 // Is null
457                                 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
458                         } else if (fax.getPhoneAreaCode() < 1) {
459                                 // Abort here
460                                 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
461                         } else if (fax.getPhoneNumber() == null) {
462                                 // Is null
463                                 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
464                         } else if (fax.getPhoneNumber() < 1) {
465                                 // Abort here
466                                 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
467                         }
468
469                         // Set fax number
470                         contact.setContactFaxNumber(fax);
471                 }
472
473                 // Is the provider set?
474                 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
475                         // Is the number set?
476                         if (cellphone.getPhoneNumber() == null) {
477                                 // Is null
478                                 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
479                         } else if (cellphone.getPhoneNumber() < 1) {
480                                 // Abort here
481                                 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
482                         }
483
484                         // Set cellphone number
485                         contact.setContactCellphoneNumber(cellphone);
486                 }
487
488                 // Trace message
489                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: contact={1} - EXIT!", this.getClass().getSimpleName(), contact));
490                 // Return it
491                 return localContact;
492         }
493
494         @Override
495         public String doChangePersonalContactData () {
496                 // This method shall only be called if the user is logged-in
497                 if (!this.loginController.isUserLoggedIn()) {
498                         // Not logged-in
499                         throw new IllegalStateException("User is not logged-in"); //NOI18N
500                 } else if (!this.isRequiredChangePersonalDataSet()) {
501                         // Not all required fields are set
502                         throw new FaceletException("Not all required fields are set."); //NOI18N
503                 } else if (!this.loginController.ifCurrentPasswordMatches()) {
504                         // Password not matching
505                         throw new FaceletException(new UserPasswordMismatchException(this.loginController.getLoggedInUser()));
506                 }
507
508                 // Get contact instance
509                 Contact contact = this.loginController.getLoggedInUser().getUserContact();
510
511                 // It should be there, so run some tests on it
512                 assert (contact instanceof Contact) : "Instance loginController.loggedInUser.userContact is null"; //NOI18N
513                 assert (contact.getContactId() instanceof Long) : "Instance loginController.userContact.contactId is null"; //NOI18N
514                 assert (contact.getContactId() > 0) : MessageFormat.format("Instance loginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
515
516                 // Update all fields
517                 contact.setContactGender(this.getGender());
518                 contact.setContactFirstName(this.getFirstName());
519                 contact.setContactFamilyName(this.getFamilyName());
520                 contact.setContactStreet(this.getStreet());
521                 contact.setContactHouseNumber(this.getHouseNumber());
522                 contact.setContactZipCode(this.getZipCode());
523                 contact.setContactCity(this.getCity());
524                 contact.setContactCountry(this.getCountry());
525
526                 // Update contact's cellphone number
527                 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
528
529                 // Update contact's land-line number
530                 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
531
532                 // Update contact's fax number
533                 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
534
535                 // Send it to the EJB
536                 this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
537
538                 // All fine
539                 return "contact_data_saved"; //NOI18N
540         }
541
542         @Override
543         @SuppressWarnings ("ReturnOfDateField")
544         public Date getBirthday () {
545                 return this.birthday;
546         }
547
548         @Override
549         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
550         public void setBirthday (final Date birthday) {
551                 this.birthday = birthday;
552         }
553
554         @Override
555         public MobileProvider getCellphoneCarrier () {
556                 return this.cellphoneCarrier;
557         }
558
559         @Override
560         public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
561                 this.cellphoneCarrier = cellphoneCarrier;
562         }
563
564         @Override
565         public Long getCellphoneNumber () {
566                 return this.cellphoneNumber;
567         }
568
569         @Override
570         public void setCellphoneNumber (Long cellphoneNumber) {
571                 this.cellphoneNumber = cellphoneNumber;
572         }
573
574         @Override
575         public String getCity () {
576                 return this.city;
577         }
578
579         @Override
580         public void setCity (final String city) {
581                 this.city = city;
582         }
583
584         @Override
585         public String getComment () {
586                 return this.comment;
587         }
588
589         @Override
590         public void setComment (final String comment) {
591                 this.comment = comment;
592         }
593
594         @Override
595         public Country getCountry () {
596                 return this.country;
597         }
598
599         @Override
600         public void setCountry (final Country country) {
601                 this.country = country;
602         }
603
604         @Override
605         public String getEmailAddress () {
606                 return this.emailAddress;
607         }
608
609         @Override
610         public void setEmailAddress (final String emailAddress) {
611                 this.emailAddress = emailAddress;
612         }
613
614         @Override
615         public String getEmailAddressRepeat () {
616                 return this.emailAddressRepeat;
617         }
618
619         @Override
620         public void setEmailAddressRepeat (final String emailAddressRepeat) {
621                 this.emailAddressRepeat = emailAddressRepeat;
622         }
623
624         @Override
625         public String getFamilyName () {
626                 return this.familyName;
627         }
628
629         @Override
630         public void setFamilyName (final String familyName) {
631                 this.familyName = familyName;
632         }
633
634         @Override
635         public Integer getFaxAreaCode () {
636                 return this.faxAreaCode;
637         }
638
639         @Override
640         public void setFaxAreaCode (final Integer faxAreaCode) {
641                 this.faxAreaCode = faxAreaCode;
642         }
643
644         @Override
645         public Country getFaxCountry () {
646                 return this.faxCountry;
647         }
648
649         @Override
650         public void setFaxCountry (final Country faxCountry) {
651                 this.faxCountry = faxCountry;
652         }
653
654         @Override
655         public Long getFaxNumber () {
656                 return this.faxNumber;
657         }
658
659         @Override
660         public void setFaxNumber (final Long faxNumber) {
661                 this.faxNumber = faxNumber;
662         }
663
664         @Override
665         public String getFirstName () {
666                 return this.firstName;
667         }
668
669         @Override
670         public void setFirstName (final String firstName) {
671                 this.firstName = firstName;
672         }
673
674         @Override
675         public Gender getGender () {
676                 return this.gender;
677         }
678
679         @Override
680         public void setGender (final Gender gender) {
681                 this.gender = gender;
682         }
683
684         @Override
685         public Short getHouseNumber () {
686                 return this.houseNumber;
687         }
688
689         @Override
690         public void setHouseNumber (final Short houseNumber) {
691                 this.houseNumber = houseNumber;
692         }
693
694         @Override
695         public Integer getPhoneAreaCode () {
696                 return this.phoneAreaCode;
697         }
698
699         @Override
700         public void setPhoneAreaCode (final Integer phoneAreaCode) {
701                 this.phoneAreaCode = phoneAreaCode;
702         }
703
704         @Override
705         public Country getPhoneCountry () {
706                 return this.phoneCountry;
707         }
708
709         @Override
710         public void setPhoneCountry (final Country phoneCountry) {
711                 this.phoneCountry = phoneCountry;
712         }
713
714         @Override
715         public Long getPhoneNumber () {
716                 return this.phoneNumber;
717         }
718
719         @Override
720         public void setPhoneNumber (final Long phoneNumber) {
721                 this.phoneNumber = phoneNumber;
722         }
723
724         @Override
725         public String getStreet () {
726                 return this.street;
727         }
728
729         @Override
730         public void setStreet (final String street) {
731                 this.street = street;
732         }
733
734         @Override
735         public Integer getZipCode () {
736                 return this.zipCode;
737         }
738
739         @Override
740         public void setZipCode (final Integer zipCode) {
741                 this.zipCode = zipCode;
742         }
743
744         @Override
745         public boolean hasContacts () {
746                 return (!this.contactList.isEmpty());
747         }
748
749         /**
750          * Post-initialization of this class
751          */
752         @PostConstruct
753         public void init () {
754                 // Get full email address list for reducing EJB calls
755                 this.emailAddressList = this.contactBean.getEmailAddressList();
756
757                 // Get full contact list
758                 this.contactList = this.contactBean.getAllContacts();
759         }
760
761         @Override
762         public boolean isEmailAddressRegistered (final Contact contact) {
763                 // Cherck parameter
764                 if (null == contact) {
765                         // Throw NPE
766                         throw new NullPointerException("contact is null"); //NOI18N
767                 } else if (contact.getContactEmailAddress() == null) {
768                         // Throw again
769                         throw new NullPointerException("contact.contactEmailAddress is null"); //NOI18N
770                 } else if (contact.getContactEmailAddress().isEmpty()) {
771                         // Is empty
772                         throw new IllegalArgumentException("contact.contactEmailAddress is empty."); //NOI18N
773                 }
774
775                 // Determine it
776                 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(contact.getContactEmailAddress())));
777         }
778
779         @Override
780         public boolean isRequiredChangePersonalDataSet () {
781                 return ((this.getGender() != null) &&
782                                 (this.getFirstName() != null) &&
783                                 (this.getFamilyName() != null) &&
784                                 (this.getStreet() != null) &&
785                                 (this.getHouseNumber() != null) &&
786                                 (this.getZipCode() != null) &&
787                                 (this.getCity() != null));
788         }
789
790         @Override
791         public boolean isRequiredPersonalDataSet () {
792                 return ((this.getGender() != null) &&
793                                 (this.getFirstName() != null) &&
794                                 (this.getFamilyName() != null) &&
795                                 (this.getStreet() != null) &&
796                                 (this.getHouseNumber() != null) &&
797                                 (this.getZipCode() != null) &&
798                                 (this.getCity() != null) &&
799                                 (this.getEmailAddress() != null) &&
800                                 (this.getEmailAddressRepeat() != null));
801         }
802
803         @Override
804         public boolean isSameEmailAddressEntered () {
805                 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
806         }
807
808         @Override
809         public Contact lookupContactById (final Long contactId) throws ContactNotFoundException {
810                 // Init variable
811                 Contact localContact = null;
812
813                 // Clear this bean
814                 this.clear();
815
816                 // Try to lookup it in visible user list
817                 for (final Iterator<Contact> iterator = this.contactList.iterator(); iterator.hasNext();) {
818                         // Get next user
819                         Contact next = iterator.next();
820
821                         // Is the user id found?
822                         if (Objects.equals(next.getContactId(), contactId)) {
823                                 // Copy to other variable
824                                 localContact = next;
825                                 break;
826                         }
827                 }
828
829                 // Is it still null?
830                 if (null == localContact) {
831                         // Not visible for the current user
832                         throw new ContactNotFoundException(contactId);
833                 }
834
835                 // Copy all data to this bean
836                 this.copyContact(localContact);
837
838                 // Return it
839                 return localContact;
840         }
841
842         @Override
843         public void updateContactDataFromController (final Contact contact) {
844                 // Is the instance valid?
845                 if (null == contact) {
846                         // Throw NPE
847                         throw new NullPointerException("contact is null"); //NOI18N
848                 } else if (contact.getContactId() == null) {
849                         // Throw NPE
850                         throw new NullPointerException("contact.contactId is null"); //NOI18N
851                 } else if (contact.getContactId() < 1) {
852                         // Not valid id number
853                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
854                 }
855
856                 // Set all
857                 this.copyContact(contact);
858         }
859
860         /**
861          * Adds email address to bean's internal list.
862          * <p>
863          * @param contact Contact instance
864          */
865         private void addUserNameEmailAddress (final Contact contact) {
866                 // Make sure the entry is not added yet
867                 if (this.emailAddressList.contains(contact.getContactEmailAddress())) {
868                         // Already added
869                         throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
870                 }
871
872                 // Add email addres
873                 this.emailAddressList.add(contact.getContactEmailAddress());
874         }
875
876         /**
877          * Clears this bean
878          */
879         private void clear () {
880                 // Clear all data
881                 // - personal data
882                 this.setGender(Gender.UNKNOWN);
883                 this.setFirstName(null);
884                 this.setFamilyName(null);
885                 this.setStreet(null);
886                 this.setHouseNumber(null);
887                 this.setZipCode(null);
888                 this.setCity(null);
889                 this.setCountry(null);
890
891                 // - contact data
892                 this.setEmailAddress(null);
893                 this.setEmailAddressRepeat(null);
894                 this.setPhoneAreaCode(null);
895                 this.setPhoneCountry(null);
896                 this.setPhoneNumber(null);
897                 this.setCellphoneCarrier(null);
898                 this.setCellphoneNumber(null);
899                 this.setFaxAreaCode(null);
900                 this.setFaxCountry(null);
901                 this.setFaxNumber(null);
902
903                 // - other data
904                 this.setBirthday(null);
905                 this.setComment(null);
906         }
907
908         /**
909          * Copies given contact into the controller
910          * <p>
911          * @param contact Contact instance
912          */
913         private void copyContact (final Contact contact) {
914                 // Is the instance valid?
915                 if (null == contact) {
916                         // Throw NPE
917                         throw new NullPointerException("contact is null"); //NOI18N
918                 } else if (contact.getContactId() == null) {
919                         // Throw NPE
920                         throw new NullPointerException("contact.contactId is null"); //NOI18N
921                 } else if (contact.getContactId() < 1) {
922                         // Not valid id number
923                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
924                 }
925
926                 // Copy all fields:
927                 // - base data
928                 this.setGender(contact.getContactGender());
929                 this.setFirstName(contact.getContactFirstName());
930                 this.setFamilyName(contact.getContactFamilyName());
931                 this.setStreet(contact.getContactStreet());
932                 this.setHouseNumber(contact.getContactHouseNumber());
933                 this.setZipCode(contact.getContactZipCode());
934                 this.setCity(contact.getContactCity());
935                 this.setCountry(contact.getContactCountry());
936                 this.setEmailAddress(contact.getContactEmailAddress());
937                 this.setBirthday(contact.getContactBirthday());
938                 this.setComment(contact.getContactComment());
939
940                 // Get cellphone, phone and fax instance
941                 DialableCellphoneNumber cellphone = contact.getContactCellphoneNumber();
942                 DialableFaxNumber fax = contact.getContactFaxNumber();
943                 DialableLandLineNumber phone = contact.getContactLandLineNumber();
944
945                 // - contact data
946                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
947                         this.setPhoneCountry(phone.getPhoneCountry());
948                         this.setPhoneAreaCode(phone.getPhoneAreaCode());
949                         this.setPhoneNumber(phone.getPhoneNumber());
950                 }
951
952                 if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof MobileProvider)) {
953                         this.setCellphoneCarrier(cellphone.getCellphoneProvider());
954                         this.setCellphoneNumber(cellphone.getPhoneNumber());
955                 }
956
957                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
958                         this.setFaxCountry(fax.getPhoneCountry());
959                         this.setFaxAreaCode(fax.getPhoneAreaCode());
960                         this.setFaxNumber(fax.getPhoneNumber());
961                 }
962         }
963
964         /**
965          * Removes given contact from all lists
966          * <p>
967          * @param contact Contact instance to remove
968          */
969         private void removeContact (final Contact contact) {
970                 // Is the instance valid?
971                 if (null == contact) {
972                         // Throw NPE
973                         throw new NullPointerException("contact is null"); //NOI18N
974                 } else if (contact.getContactId() == null) {
975                         // Throw NPE
976                         throw new NullPointerException("contact.contactId is null"); //NOI18N
977                 } else if (contact.getContactId() < 1) {
978                         // Not valid id number
979                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
980                 }
981
982                 // Remove from general list
983                 if (!this.contactList.remove(contact)) {
984                         // Did not remove contact
985                         throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N
986                 }
987
988                 // Remove from other lists
989                 this.emailAddressList.remove(contact.getContactEmailAddress());
990         }
991
992 }