]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/contact/PizzaContactWebSessionBean.java
Continued with observers:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / contact / PizzaContactWebSessionBean.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.pizzaapplication.beans.contact;
18
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Objects;
24 import javax.annotation.PostConstruct;
25 import javax.enterprise.context.SessionScoped;
26 import javax.enterprise.event.Observes;
27 import javax.faces.view.facelets.FaceletException;
28 import javax.inject.Inject;
29 import javax.inject.Named;
30 import javax.naming.Context;
31 import javax.naming.InitialContext;
32 import javax.naming.NamingException;
33 import org.mxchange.jcontacts.contact.Contact;
34 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
35 import org.mxchange.jcontacts.contact.UserContact;
36 import org.mxchange.jcontacts.contact.gender.Gender;
37 import org.mxchange.jcontacts.contact.utils.ContactUtils;
38 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
39 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
40 import org.mxchange.jcountry.data.Country;
41 import org.mxchange.jcustomercore.events.AdminAddedCustomerEvent;
42 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
43 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
44 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
45 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
46 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
47 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
48 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
49 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
50 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
51 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
52 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
53 import org.mxchange.pizzaapplication.beans.login.PizzaUserLoginWebSessionController;
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 PizzaContactWebSessionBean implements PizzaContactWebSessionController {
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 PizzaUserLoginWebSessionController 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 PizzaContactWebSessionBean () {
209                 // Set gender to UNKNOWN
210                 this.gender = Gender.UNKNOWN;
211
212                 // Try it
213                 try {
214                         // Get initial context
215                         Context context = new InitialContext();
216
217                         // Try to lookup
218                         this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
219                 } catch (final NamingException e) {
220                         // Throw again
221                         throw new FaceletException(e);
222                 }
223         }
224
225         @Override
226         public void addEmailAddress (final String contactEmailAddress) {
227                 // Add it
228                 this.emailAddressList.add(contactEmailAddress);
229         }
230
231         @Override
232         public void afterAdminAddedCustomer (@Observes final AdminAddedCustomerEvent event) {
233                 // The event must be valid
234                 if (null == event) {
235                         // Throw NPE
236                         throw new NullPointerException("event is null"); //NOI18N
237                 } else if (event.getAddedCustomer() == null) {
238                         // Throw again ...
239                         throw new NullPointerException("event.addedCustomer is null"); //NOI18N
240                 } else if (event.getAddedCustomer().getCustomerId() == null) {
241                         // ... and again
242                         throw new NullPointerException("event.addedCustomer.customerId is null"); //NOI18N
243                 } else if (event.getAddedCustomer().getCustomerId() < 1) {
244                         // Not valid
245                         throw new IllegalArgumentException(MessageFormat.format("event.addedCustomer.customerId={0} is not valid", event.getAddedCustomer().getCustomerId())); //NOI18N //NOI18N
246                 }
247
248                 // Clear this bean
249                 this.clear();
250
251                 // Call other method
252                 this.contactList.add(event.getAddedCustomer().getCustomerContact());
253         }
254
255         @Override
256         public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
257                 // The event must be valid
258                 if (null == event) {
259                         // Throw NPE
260                         throw new NullPointerException("event is null"); //NOI18N
261                 } else if (event.getAddedUser() == null) {
262                         // Throw NPE again
263                         throw new NullPointerException("event.addedUser is null"); //NOI18N
264                 } else if (event.getAddedUser().getUserId() == null) {
265                         // userId is null
266                         throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
267                 } else if (event.getAddedUser().getUserId() < 1) {
268                         // Not avalid id
269                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
270                 } else if (event.getAddedUser().getUserContact() == null) {
271                         // userId is null
272                         throw new NullPointerException("event.addedUser.userContact is null"); //NOI18N
273                 } else if (event.getAddedUser().getUserContact().getContactId() == null) {
274                         // userId is null
275                         throw new NullPointerException("event.addedUser.userContact.contactId is null"); //NOI18N
276                 } else if (event.getAddedUser().getUserContact().getContactId() < 1) {
277                         // userId is null
278                         throw new IllegalArgumentException(MessageFormat.format("event.addedUser.userContact.contactId={0} is not valid", event.getAddedUser().getUserContact().getContactId())); //NOI18N
279                 }
280
281                 // Add user to local list
282                 this.contactList.add(event.getAddedUser().getUserContact());
283         }
284
285         @Override
286         public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent event) {
287                 // Trace message
288                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterAdminUpdatedContactDataEvent: event={0} - CALLED!", event)); //NOI18N
289
290                 // event should not be null
291                 if (null == event) {
292                         // Throw NPE
293                         throw new NullPointerException("event is null"); //NOI18N
294                 } else if (event.getUpdatedContact()== null) {
295                         // Throw NPE again
296                         throw new NullPointerException("event.updatedUser is null"); //NOI18N
297                 } else if (event.getUpdatedContact().getContactId() == null) {
298                         // userId is null
299                         throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
300                 } else if (event.getUpdatedContact().getContactId() < 1) {
301                         // Not avalid id
302                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
303                 }
304
305                 // Get iterator from list
306                 Iterator<Contact> iterator = this.contactList.iterator();
307
308                 // "Walk" through all entries
309                 while (iterator.hasNext()) {
310                         // Get next element
311                         Contact next = iterator.next();
312
313                         // Is id number the same?
314                         if (Objects.equals(event.getUpdatedContact().getContactId(), next.getContactId())) {
315                                 // Found entry, so remove it and abort
316                                 this.contactList.remove(next);
317
318                                 // Remove also email from list
319                                 this.emailAddressList.remove(next.getContactEmailAddress());
320                                 break;
321                         }
322                 }
323
324                 // Add contact to list
325                 this.contactList.add(event.getUpdatedContact());
326
327                 // Add email address to list
328                 this.emailAddressList.add(event.getUpdatedContact().getContactEmailAddress());
329         }
330
331         @Override
332         public void afterRegistrationEvent (final @Observes UserRegisteredEvent event) {
333                 // Trace message
334                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
335
336                 // event should not be null
337                 if (null == event) {
338                         // Throw NPE
339                         throw new NullPointerException("event is null"); //NOI18N
340                 } else if (event.getRegisteredUser() == null) {
341                         // Throw NPE again
342                         throw new NullPointerException("event.user is null"); //NOI18N
343                 } else if (event.getRegisteredUser().getUserId() == null) {
344                         // userId is null
345                         throw new NullPointerException("event.user.userId is null"); //NOI18N
346                 } else if (event.getRegisteredUser().getUserId() < 1) {
347                         // Not avalid id
348                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
349                 }
350
351                 // Get user instance
352                 Contact registeredContact = event.getRegisteredUser().getUserContact();
353
354                 // Debug message
355                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterRegistration: registeredContact={0}", registeredContact)); //NOI18N
356
357                 // Copy all data from registered->user
358                 this.copyContact(registeredContact);
359
360                 // Add user name and email address
361                 this.addUserNameEmailAddress(registeredContact);
362
363                 // Clear all data
364                 this.clear();
365
366                 // Trace message
367                 //* NOISY-DEBUG: */ System.out.println("ContactWebBean:afterRegistration: EXIT!"); //NOI18N
368         }
369
370         @Override
371         public void afterUserLogin (final @Observes UserLoggedInEvent event) {
372                 // Trace message
373                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
374
375                 // event should not be null
376                 if (null == event) {
377                         // Throw NPE
378                         throw new NullPointerException("event is null"); //NOI18N
379                 } else if (event.getLoggedInUser() == null) {
380                         // Throw NPE again
381                         throw new NullPointerException("event.user is null"); //NOI18N
382                 } else if (event.getLoggedInUser().getUserId() == null) {
383                         // userId is null
384                         throw new NullPointerException("event.user.userId is null"); //NOI18N
385                 } else if (event.getLoggedInUser().getUserId() < 1) {
386                         // Not avalid id
387                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
388                 }
389
390                 // Copy all data to this bean
391                 this.copyContact(event.getLoggedInUser().getUserContact());
392
393                 // Trace message
394                 //* NOISY-DEBUG: */ System.out.println("ContactWebBean:afterUserLogin - EXIT!"); //NOI18N
395         }
396
397         @Override
398         public List<Contact> allContacts () {
399                 return this.contactList;
400         }
401
402         /**
403          * Clears this bean
404          */
405         @Override
406         public void clear () {
407                 // Clear all data
408                 // - personal data
409                 this.setGender(Gender.UNKNOWN);
410                 this.setFirstName(null);
411                 this.setFamilyName(null);
412                 this.setStreet(null);
413                 this.setHouseNumber(null);
414                 this.setZipCode(null);
415                 this.setCity(null);
416                 this.setCountry(null);
417
418                 // - contact data
419                 this.setEmailAddress(null);
420                 this.setEmailAddressRepeat(null);
421                 this.setPhoneAreaCode(null);
422                 this.setCellphoneCarrier(null);
423                 this.setFaxAreaCode(null);
424
425                 // - other data
426                 this.setBirthday(null);
427                 this.setComment(null);
428         }
429
430         @Override
431         public Contact createContactInstance () {
432                 // Required personal data must be set
433                 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
434
435                 // Generate phone number
436                 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
437                 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
438                 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
439
440                 // Create new contact
441                 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
442                 contact.setContactStreet(this.getStreet());
443                 contact.setContactHouseNumber(this.getHouseNumber());
444                 contact.setContactZipCode(this.getZipCode());
445                 contact.setContactCity(this.getCity());
446                 contact.setContactCountry(this.getCountry());
447                 contact.setContactEmailAddress(this.getEmailAddress());
448                 contact.setContactBirthday(this.getBirthday());
449                 contact.setContactComment(this.getComment());
450
451                 // Set ownContact
452                 contact.setContactOwnContact(Boolean.TRUE);
453
454                 // Don't set null or wrong references
455                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
456                         // Now the number must be given
457                         if (phone.getPhoneAreaCode() == null) {
458                                 // Is null
459                                 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
460                         } else if (phone.getPhoneAreaCode() < 1) {
461                                 // Abort here
462                                 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
463                         } else if (phone.getPhoneNumber() == null) {
464                                 // Is null
465                                 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
466                         } else if (phone.getPhoneNumber() < 1) {
467                                 // Abort here
468                                 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
469                         }
470
471                         // Set phone number
472                         contact.setContactLandLineNumber(phone);
473                 }
474
475                 // Don't set null or wrong references
476                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
477                         // Now the number must be given
478                         if (fax.getPhoneAreaCode() == null) {
479                                 // Is null
480                                 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
481                         } else if (fax.getPhoneAreaCode() < 1) {
482                                 // Abort here
483                                 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
484                         } else if (fax.getPhoneNumber() == null) {
485                                 // Is null
486                                 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
487                         } else if (fax.getPhoneNumber() < 1) {
488                                 // Abort here
489                                 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
490                         }
491
492                         // Set fax number
493                         contact.setContactFaxNumber(fax);
494                 }
495
496                 // Is the provider set?
497                 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
498                         // Is the number set?
499                         if (cellphone.getPhoneNumber() == null) {
500                                 // Is null
501                                 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
502                         } else if (cellphone.getPhoneNumber() < 1) {
503                                 // Abort here
504                                 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
505                         }
506
507                         // Set cellphone number
508                         contact.setContactCellphoneNumber(cellphone);
509                 }
510
511                 // Return it
512                 return contact;
513         }
514
515         @Override
516         public String doChangePersonalContactData () {
517                 // This method shall only be called if the user is logged-in
518                 if (!this.loginController.isUserLoggedIn()) {
519                         // Not logged-in
520                         throw new IllegalStateException("User is not logged-in"); //NOI18N
521                 } else if (!this.isRequiredChangePersonalDataSet()) {
522                         // Not all required fields are set
523                         throw new FaceletException("Not all required fields are set."); //NOI18N
524                 } else if (!this.loginController.ifCurrentPasswordMatches()) {
525                         // Password not matching
526                         throw new FaceletException(new UserPasswordMismatchException(this.loginController.getLoggedInUser()));
527                 }
528
529                 // Get contact instance
530                 Contact contact = this.loginController.getLoggedInUser().getUserContact();
531
532                 // It should be there, so run some tests on it
533                 assert (contact instanceof Contact) : "Instance loginController.loggedInUser.userContact is null"; //NOI18N
534                 assert (contact.getContactId() instanceof Long) : "Instance loginController.userContact.contactId is null"; //NOI18N
535                 assert (contact.getContactId() > 0) : MessageFormat.format("Instance loginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
536
537                 // Update all fields
538                 contact.setContactGender(this.getGender());
539                 contact.setContactFirstName(this.getFirstName());
540                 contact.setContactFamilyName(this.getFamilyName());
541                 contact.setContactStreet(this.getStreet());
542                 contact.setContactHouseNumber(this.getHouseNumber());
543                 contact.setContactZipCode(this.getZipCode());
544                 contact.setContactCity(this.getCity());
545                 contact.setContactCountry(this.getCountry());
546
547                 // Update contact's cellphone number
548                 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
549
550                 // Update contact's land-line number
551                 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
552
553                 // Update contact's fax number
554                 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
555
556                 // Send it to the EJB
557                 this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
558
559                 // All fine
560                 return "contact_data_saved"; //NOI18N
561         }
562
563         @Override
564         public Date getBirthday () {
565                 return this.birthday;
566         }
567
568         @Override
569         public void setBirthday (final Date birthday) {
570                 this.birthday = birthday;
571         }
572
573         @Override
574         public MobileProvider getCellphoneCarrier () {
575                 return this.cellphoneCarrier;
576         }
577
578         @Override
579         public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
580                 this.cellphoneCarrier = cellphoneCarrier;
581         }
582
583         @Override
584         public Long getCellphoneNumber () {
585                 return this.cellphoneNumber;
586         }
587
588         @Override
589         public void setCellphoneNumber (Long cellphoneNumber) {
590                 this.cellphoneNumber = cellphoneNumber;
591         }
592
593         @Override
594         public String getCity () {
595                 return this.city;
596         }
597
598         @Override
599         public void setCity (final String city) {
600                 this.city = city;
601         }
602
603         @Override
604         public String getComment () {
605                 return this.comment;
606         }
607
608         @Override
609         public void setComment (final String comment) {
610                 this.comment = comment;
611         }
612
613         @Override
614         public Country getCountry () {
615                 return this.country;
616         }
617
618         @Override
619         public void setCountry (final Country country) {
620                 this.country = country;
621         }
622
623         @Override
624         public String getEmailAddress () {
625                 return this.emailAddress;
626         }
627
628         @Override
629         public void setEmailAddress (final String emailAddress) {
630                 this.emailAddress = emailAddress;
631         }
632
633         @Override
634         public String getEmailAddressRepeat () {
635                 return this.emailAddressRepeat;
636         }
637
638         @Override
639         public void setEmailAddressRepeat (final String emailAddressRepeat) {
640                 this.emailAddressRepeat = emailAddressRepeat;
641         }
642
643         @Override
644         public String getFamilyName () {
645                 return this.familyName;
646         }
647
648         @Override
649         public void setFamilyName (final String familyName) {
650                 this.familyName = familyName;
651         }
652
653         @Override
654         public Integer getFaxAreaCode () {
655                 return this.faxAreaCode;
656         }
657
658         @Override
659         public void setFaxAreaCode (final Integer faxAreaCode) {
660                 this.faxAreaCode = faxAreaCode;
661         }
662
663         @Override
664         public Country getFaxCountry () {
665                 return this.faxCountry;
666         }
667
668         @Override
669         public void setFaxCountry (final Country faxCountry) {
670                 this.faxCountry = faxCountry;
671         }
672
673         @Override
674         public Long getFaxNumber () {
675                 return this.faxNumber;
676         }
677
678         @Override
679         public void setFaxNumber (final Long faxNumber) {
680                 this.faxNumber = faxNumber;
681         }
682
683         @Override
684         public String getFirstName () {
685                 return this.firstName;
686         }
687
688         @Override
689         public void setFirstName (final String firstName) {
690                 this.firstName = firstName;
691         }
692
693         @Override
694         public Gender getGender () {
695                 return this.gender;
696         }
697
698         @Override
699         public void setGender (final Gender gender) {
700                 this.gender = gender;
701         }
702
703         @Override
704         public Short getHouseNumber () {
705                 return this.houseNumber;
706         }
707
708         @Override
709         public void setHouseNumber (final Short houseNumber) {
710                 this.houseNumber = houseNumber;
711         }
712
713         @Override
714         public Integer getPhoneAreaCode () {
715                 return this.phoneAreaCode;
716         }
717
718         @Override
719         public void setPhoneAreaCode (final Integer phoneAreaCode) {
720                 this.phoneAreaCode = phoneAreaCode;
721         }
722
723         @Override
724         public Country getPhoneCountry () {
725                 return this.phoneCountry;
726         }
727
728         @Override
729         public void setPhoneCountry (final Country phoneCountry) {
730                 this.phoneCountry = phoneCountry;
731         }
732
733         @Override
734         public Long getPhoneNumber () {
735                 return this.phoneNumber;
736         }
737
738         @Override
739         public void setPhoneNumber (final Long phoneNumber) {
740                 this.phoneNumber = phoneNumber;
741         }
742
743         @Override
744         public String getStreet () {
745                 return this.street;
746         }
747
748         @Override
749         public void setStreet (final String street) {
750                 this.street = street;
751         }
752
753         @Override
754         public Integer getZipCode () {
755                 return this.zipCode;
756         }
757
758         @Override
759         public void setZipCode (final Integer zipCode) {
760                 this.zipCode = zipCode;
761         }
762
763         /**
764          * Post-initialization of this class
765          */
766         @PostConstruct
767         public void init () {
768                 // Get full email address list for reducing EJB calls
769                 this.emailAddressList = this.contactBean.getEmailAddressList();
770
771                 // Get full contact list
772                 this.contactList = this.contactBean.getAllContacts();
773         }
774
775         @Override
776         public boolean isEmailAddressRegistered (final Contact contact) {
777                 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(contact.getContactEmailAddress())));
778         }
779
780         @Override
781         public boolean isRequiredChangePersonalDataSet () {
782                 return ((this.getGender() != null) &&
783                                 (this.getFirstName() != null) &&
784                                 (this.getFamilyName() != null) &&
785                                 (this.getStreet() != null) &&
786                                 (this.getHouseNumber() != null) &&
787                                 (this.getZipCode() != null) &&
788                                 (this.getCity() != null));
789         }
790
791         @Override
792         public boolean isRequiredPersonalDataSet () {
793                 return ((this.getGender() != null) &&
794                                 (this.getFirstName() != null) &&
795                                 (this.getFamilyName() != null) &&
796                                 (this.getStreet() != null) &&
797                                 (this.getHouseNumber() != null) &&
798                                 (this.getZipCode() != null) &&
799                                 (this.getCity() != null) &&
800                                 (this.getEmailAddress() != null) &&
801                                 (this.getEmailAddressRepeat() != null));
802         }
803
804         @Override
805         public boolean isSameEmailAddressEntered () {
806                 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
807         }
808
809         @Override
810         public Contact lookupContactById (final Long contactId) throws ContactNotFoundException {
811                 // Init variable
812                 Contact localContact = null;
813
814                 // Clear this bean
815                 this.clear();
816
817                 // Try to lookup it in visible user list
818                 for (final Iterator<Contact> iterator = this.contactList.iterator(); iterator.hasNext();) {
819                         // Get next user
820                         Contact next = iterator.next();
821
822                         // Is the user id found?
823                         if (Objects.equals(next.getContactId(), contactId)) {
824                                 // Copy to other variable
825                                 localContact = next;
826                                 break;
827                         }
828                 }
829
830                 // Is it still null?
831                 if (null == localContact) {
832                         // Not visible for the current user
833                         throw new ContactNotFoundException(contactId);
834                 }
835
836                 // Copy all data to this bean
837                 this.copyContact(localContact);
838
839                 // Return it
840                 return localContact;
841         }
842
843         @Override
844         public void updateContactDataFromController (final Contact userContact) {
845                 // Is the instance valid?
846                 if (null == userContact) {
847                         // Throw NPE
848                         throw new NullPointerException("userContact is null"); //NOI18N
849                 } else if (userContact.getContactId() == null) {
850                         // Throw NPE
851                         throw new NullPointerException("userContact.contactId is null"); //NOI18N
852                 } else if (userContact.getContactId() < 1) {
853                         // Not valid id number
854                         throw new IllegalArgumentException(MessageFormat.format("userContact.contactId={0} is not valid.", userContact.getContactId())); //NOI18N
855                 }
856
857                 // Set all
858                 this.copyContact(userContact);
859         }
860
861         /**
862          * Adds email address to bean's internal list.
863          * <p>
864          * @param contact Contact instance
865          */
866         private void addUserNameEmailAddress (final Contact contact) {
867                 // Make sure the entry is not added yet
868                 if (this.emailAddressList.contains(contact.getContactEmailAddress())) {
869                         // Already added
870                         throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
871                 }
872
873                 // Add email addres
874                 this.emailAddressList.add(contact.getContactEmailAddress());
875         }
876
877         /**
878          * Copies given contact into the controller
879          * <p>
880          * @param contact Contact instance
881          */
882         private void copyContact (final Contact contact) {
883                 // Copy all fields:
884                 // - base data
885                 this.setGender(contact.getContactGender());
886                 this.setFirstName(contact.getContactFirstName());
887                 this.setFamilyName(contact.getContactFamilyName());
888                 this.setStreet(contact.getContactStreet());
889                 this.setHouseNumber(contact.getContactHouseNumber());
890                 this.setZipCode(contact.getContactZipCode());
891                 this.setCity(contact.getContactCity());
892                 this.setCountry(contact.getContactCountry());
893
894                 // Get cellphone, phone and fax instance
895                 DialableCellphoneNumber cellphone = contact.getContactCellphoneNumber();
896                 DialableFaxNumber fax = contact.getContactFaxNumber();
897                 DialableLandLineNumber phone = contact.getContactLandLineNumber();
898
899                 // - contact data
900                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
901                         this.setPhoneCountry(phone.getPhoneCountry());
902                         this.setPhoneAreaCode(phone.getPhoneAreaCode());
903                         this.setPhoneNumber(phone.getPhoneNumber());
904                 }
905                 if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof MobileProvider)) {
906                         this.setCellphoneCarrier(cellphone.getCellphoneProvider());
907                         this.setCellphoneNumber(cellphone.getPhoneNumber());
908                 }
909                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
910                         this.setFaxCountry(fax.getPhoneCountry());
911                         this.setFaxAreaCode(fax.getPhoneAreaCode());
912                         this.setFaxNumber(fax.getPhoneNumber());
913                 }
914                 this.setEmailAddress(contact.getContactEmailAddress());
915
916                 // -- other data
917                 this.setBirthday(contact.getContactBirthday());
918                 this.setComment(contact.getContactComment());
919         }
920
921 }