]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/contact/PizzaContactWebSessionBean.java
28a37f4c180fd09235f446c9f1b42a40b3a7cadd
[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                 // Copy all data from registered->user
357                 this.copyContact(registeredContact);
358
359                 // Add user name and email address
360                 this.addUserNameEmailAddress(registeredContact);
361
362                 // Clear all data
363                 this.clear();
364
365                 // Trace message
366                 //* NOISY-DEBUG: */ System.out.println("ContactWebBean:afterRegistration: EXIT!"); //NOI18N
367         }
368
369         @Override
370         public void afterUserLogin (final @Observes UserLoggedInEvent event) {
371                 // Trace message
372                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
373
374                 // event should not be null
375                 if (null == event) {
376                         // Throw NPE
377                         throw new NullPointerException("event is null"); //NOI18N
378                 } else if (event.getLoggedInUser() == null) {
379                         // Throw NPE again
380                         throw new NullPointerException("event.user is null"); //NOI18N
381                 } else if (event.getLoggedInUser().getUserId() == null) {
382                         // userId is null
383                         throw new NullPointerException("event.user.userId is null"); //NOI18N
384                 } else if (event.getLoggedInUser().getUserId() < 1) {
385                         // Not avalid id
386                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
387                 }
388
389                 // Copy all data to this bean
390                 this.copyContact(event.getLoggedInUser().getUserContact());
391
392                 // Trace message
393                 //* NOISY-DEBUG: */ System.out.println("ContactWebBean:afterUserLogin - EXIT!"); //NOI18N
394         }
395
396         @Override
397         public List<Contact> allContacts () {
398                 return this.contactList;
399         }
400
401         @Override
402         public Contact createContactInstance () {
403                 // Required personal data must be set
404                 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
405
406                 // Generate phone number
407                 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
408                 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
409                 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
410
411                 // Create new contact
412                 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
413                 contact.setContactStreet(this.getStreet());
414                 contact.setContactHouseNumber(this.getHouseNumber());
415                 contact.setContactZipCode(this.getZipCode());
416                 contact.setContactCity(this.getCity());
417                 contact.setContactCountry(this.getCountry());
418                 contact.setContactEmailAddress(this.getEmailAddress());
419                 contact.setContactBirthday(this.getBirthday());
420                 contact.setContactComment(this.getComment());
421
422                 // Set ownContact
423                 contact.setContactOwnContact(Boolean.TRUE);
424
425                 // Don't set null or wrong references
426                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
427                         // Now the number must be given
428                         if (phone.getPhoneAreaCode() == null) {
429                                 // Is null
430                                 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
431                         } else if (phone.getPhoneAreaCode() < 1) {
432                                 // Abort here
433                                 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
434                         } else if (phone.getPhoneNumber() == null) {
435                                 // Is null
436                                 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
437                         } else if (phone.getPhoneNumber() < 1) {
438                                 // Abort here
439                                 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
440                         }
441
442                         // Set phone number
443                         contact.setContactLandLineNumber(phone);
444                 }
445
446                 // Don't set null or wrong references
447                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
448                         // Now the number must be given
449                         if (fax.getPhoneAreaCode() == null) {
450                                 // Is null
451                                 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
452                         } else if (fax.getPhoneAreaCode() < 1) {
453                                 // Abort here
454                                 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
455                         } else if (fax.getPhoneNumber() == null) {
456                                 // Is null
457                                 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
458                         } else if (fax.getPhoneNumber() < 1) {
459                                 // Abort here
460                                 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
461                         }
462
463                         // Set fax number
464                         contact.setContactFaxNumber(fax);
465                 }
466
467                 // Is the provider set?
468                 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
469                         // Is the number set?
470                         if (cellphone.getPhoneNumber() == null) {
471                                 // Is null
472                                 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
473                         } else if (cellphone.getPhoneNumber() < 1) {
474                                 // Abort here
475                                 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
476                         }
477
478                         // Set cellphone number
479                         contact.setContactCellphoneNumber(cellphone);
480                 }
481
482                 // Return it
483                 return contact;
484         }
485
486         @Override
487         public String doChangePersonalContactData () {
488                 // This method shall only be called if the user is logged-in
489                 if (!this.loginController.isUserLoggedIn()) {
490                         // Not logged-in
491                         throw new IllegalStateException("User is not logged-in"); //NOI18N
492                 } else if (!this.isRequiredChangePersonalDataSet()) {
493                         // Not all required fields are set
494                         throw new FaceletException("Not all required fields are set."); //NOI18N
495                 } else if (!this.loginController.ifCurrentPasswordMatches()) {
496                         // Password not matching
497                         throw new FaceletException(new UserPasswordMismatchException(this.loginController.getLoggedInUser()));
498                 }
499
500                 // Get contact instance
501                 Contact contact = this.loginController.getLoggedInUser().getUserContact();
502
503                 // It should be there, so run some tests on it
504                 assert (contact instanceof Contact) : "Instance loginController.loggedInUser.userContact is null"; //NOI18N
505                 assert (contact.getContactId() instanceof Long) : "Instance loginController.userContact.contactId is null"; //NOI18N
506                 assert (contact.getContactId() > 0) : MessageFormat.format("Instance loginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
507
508                 // Update all fields
509                 contact.setContactGender(this.getGender());
510                 contact.setContactFirstName(this.getFirstName());
511                 contact.setContactFamilyName(this.getFamilyName());
512                 contact.setContactStreet(this.getStreet());
513                 contact.setContactHouseNumber(this.getHouseNumber());
514                 contact.setContactZipCode(this.getZipCode());
515                 contact.setContactCity(this.getCity());
516                 contact.setContactCountry(this.getCountry());
517
518                 // Update contact's cellphone number
519                 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
520
521                 // Update contact's land-line number
522                 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
523
524                 // Update contact's fax number
525                 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
526
527                 // Send it to the EJB
528                 this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
529
530                 // All fine
531                 return "contact_data_saved"; //NOI18N
532         }
533
534         @Override
535         public Date getBirthday () {
536                 return this.birthday;
537         }
538
539         @Override
540         public void setBirthday (final Date birthday) {
541                 this.birthday = birthday;
542         }
543
544         @Override
545         public MobileProvider getCellphoneCarrier () {
546                 return this.cellphoneCarrier;
547         }
548
549         @Override
550         public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
551                 this.cellphoneCarrier = cellphoneCarrier;
552         }
553
554         @Override
555         public Long getCellphoneNumber () {
556                 return this.cellphoneNumber;
557         }
558
559         @Override
560         public void setCellphoneNumber (Long cellphoneNumber) {
561                 this.cellphoneNumber = cellphoneNumber;
562         }
563
564         @Override
565         public String getCity () {
566                 return this.city;
567         }
568
569         @Override
570         public void setCity (final String city) {
571                 this.city = city;
572         }
573
574         @Override
575         public String getComment () {
576                 return this.comment;
577         }
578
579         @Override
580         public void setComment (final String comment) {
581                 this.comment = comment;
582         }
583
584         @Override
585         public Country getCountry () {
586                 return this.country;
587         }
588
589         @Override
590         public void setCountry (final Country country) {
591                 this.country = country;
592         }
593
594         @Override
595         public String getEmailAddress () {
596                 return this.emailAddress;
597         }
598
599         @Override
600         public void setEmailAddress (final String emailAddress) {
601                 this.emailAddress = emailAddress;
602         }
603
604         @Override
605         public String getEmailAddressRepeat () {
606                 return this.emailAddressRepeat;
607         }
608
609         @Override
610         public void setEmailAddressRepeat (final String emailAddressRepeat) {
611                 this.emailAddressRepeat = emailAddressRepeat;
612         }
613
614         @Override
615         public String getFamilyName () {
616                 return this.familyName;
617         }
618
619         @Override
620         public void setFamilyName (final String familyName) {
621                 this.familyName = familyName;
622         }
623
624         @Override
625         public Integer getFaxAreaCode () {
626                 return this.faxAreaCode;
627         }
628
629         @Override
630         public void setFaxAreaCode (final Integer faxAreaCode) {
631                 this.faxAreaCode = faxAreaCode;
632         }
633
634         @Override
635         public Country getFaxCountry () {
636                 return this.faxCountry;
637         }
638
639         @Override
640         public void setFaxCountry (final Country faxCountry) {
641                 this.faxCountry = faxCountry;
642         }
643
644         @Override
645         public Long getFaxNumber () {
646                 return this.faxNumber;
647         }
648
649         @Override
650         public void setFaxNumber (final Long faxNumber) {
651                 this.faxNumber = faxNumber;
652         }
653
654         @Override
655         public String getFirstName () {
656                 return this.firstName;
657         }
658
659         @Override
660         public void setFirstName (final String firstName) {
661                 this.firstName = firstName;
662         }
663
664         @Override
665         public Gender getGender () {
666                 return this.gender;
667         }
668
669         @Override
670         public void setGender (final Gender gender) {
671                 this.gender = gender;
672         }
673
674         @Override
675         public Short getHouseNumber () {
676                 return this.houseNumber;
677         }
678
679         @Override
680         public void setHouseNumber (final Short houseNumber) {
681                 this.houseNumber = houseNumber;
682         }
683
684         @Override
685         public Integer getPhoneAreaCode () {
686                 return this.phoneAreaCode;
687         }
688
689         @Override
690         public void setPhoneAreaCode (final Integer phoneAreaCode) {
691                 this.phoneAreaCode = phoneAreaCode;
692         }
693
694         @Override
695         public Country getPhoneCountry () {
696                 return this.phoneCountry;
697         }
698
699         @Override
700         public void setPhoneCountry (final Country phoneCountry) {
701                 this.phoneCountry = phoneCountry;
702         }
703
704         @Override
705         public Long getPhoneNumber () {
706                 return this.phoneNumber;
707         }
708
709         @Override
710         public void setPhoneNumber (final Long phoneNumber) {
711                 this.phoneNumber = phoneNumber;
712         }
713
714         @Override
715         public String getStreet () {
716                 return this.street;
717         }
718
719         @Override
720         public void setStreet (final String street) {
721                 this.street = street;
722         }
723
724         @Override
725         public Integer getZipCode () {
726                 return this.zipCode;
727         }
728
729         @Override
730         public void setZipCode (final Integer zipCode) {
731                 this.zipCode = zipCode;
732         }
733
734         /**
735          * Post-initialization of this class
736          */
737         @PostConstruct
738         public void init () {
739                 // Get full email address list for reducing EJB calls
740                 this.emailAddressList = this.contactBean.getEmailAddressList();
741
742                 // Get full contact list
743                 this.contactList = this.contactBean.getAllContacts();
744         }
745
746         @Override
747         public boolean isEmailAddressRegistered (final Contact contact) {
748                 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(contact.getContactEmailAddress())));
749         }
750
751         @Override
752         public boolean isRequiredChangePersonalDataSet () {
753                 return ((this.getGender() != null) &&
754                                 (this.getFirstName() != null) &&
755                                 (this.getFamilyName() != null) &&
756                                 (this.getStreet() != null) &&
757                                 (this.getHouseNumber() != null) &&
758                                 (this.getZipCode() != null) &&
759                                 (this.getCity() != null));
760         }
761
762         @Override
763         public boolean isRequiredPersonalDataSet () {
764                 return ((this.getGender() != null) &&
765                                 (this.getFirstName() != null) &&
766                                 (this.getFamilyName() != null) &&
767                                 (this.getStreet() != null) &&
768                                 (this.getHouseNumber() != null) &&
769                                 (this.getZipCode() != null) &&
770                                 (this.getCity() != null) &&
771                                 (this.getEmailAddress() != null) &&
772                                 (this.getEmailAddressRepeat() != null));
773         }
774
775         @Override
776         public boolean isSameEmailAddressEntered () {
777                 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
778         }
779
780         @Override
781         public Contact lookupContactById (final Long contactId) throws ContactNotFoundException {
782                 // Init variable
783                 Contact localContact = null;
784
785                 // Clear this bean
786                 this.clear();
787
788                 // Try to lookup it in visible user list
789                 for (final Iterator<Contact> iterator = this.contactList.iterator(); iterator.hasNext();) {
790                         // Get next user
791                         Contact next = iterator.next();
792
793                         // Is the user id found?
794                         if (Objects.equals(next.getContactId(), contactId)) {
795                                 // Copy to other variable
796                                 localContact = next;
797                                 break;
798                         }
799                 }
800
801                 // Is it still null?
802                 if (null == localContact) {
803                         // Not visible for the current user
804                         throw new ContactNotFoundException(contactId);
805                 }
806
807                 // Copy all data to this bean
808                 this.copyContact(localContact);
809
810                 // Return it
811                 return localContact;
812         }
813
814         @Override
815         public void updateContactDataFromController (final Contact userContact) {
816                 // Is the instance valid?
817                 if (null == userContact) {
818                         // Throw NPE
819                         throw new NullPointerException("userContact is null"); //NOI18N
820                 } else if (userContact.getContactId() == null) {
821                         // Throw NPE
822                         throw new NullPointerException("userContact.contactId is null"); //NOI18N
823                 } else if (userContact.getContactId() < 1) {
824                         // Not valid id number
825                         throw new IllegalArgumentException(MessageFormat.format("userContact.contactId={0} is not valid.", userContact.getContactId())); //NOI18N
826                 }
827
828                 // Set all
829                 this.copyContact(userContact);
830         }
831
832         /**
833          * Adds email address to bean's internal list.
834          * <p>
835          * @param contact Contact instance
836          */
837         private void addUserNameEmailAddress (final Contact contact) {
838                 // Make sure the entry is not added yet
839                 if (this.emailAddressList.contains(contact.getContactEmailAddress())) {
840                         // Already added
841                         throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
842                 }
843
844                 // Add email addres
845                 this.emailAddressList.add(contact.getContactEmailAddress());
846         }
847
848         /**
849          * Clears this bean
850          */
851         private void clear () {
852                 // Clear all data
853                 // - personal data
854                 this.setGender(Gender.UNKNOWN);
855                 this.setFirstName(null);
856                 this.setFamilyName(null);
857                 this.setStreet(null);
858                 this.setHouseNumber(null);
859                 this.setZipCode(null);
860                 this.setCity(null);
861                 this.setCountry(null);
862
863                 // - contact data
864                 this.setEmailAddress(null);
865                 this.setEmailAddressRepeat(null);
866                 this.setPhoneCountry(null);
867                 this.setPhoneAreaCode(null);
868                 this.setPhoneNumber(null);
869                 this.setCellphoneCarrier(null);
870                 this.setCellphoneNumber(null);
871                 this.setFaxCountry(null);
872                 this.setFaxAreaCode(null);
873                 this.setFaxNumber(null);
874
875                 // - other data
876                 this.setBirthday(null);
877                 this.setComment(null);
878         }
879
880         /**
881          * Copies given contact into the controller
882          * <p>
883          * @param contact Contact instance
884          */
885         private void copyContact (final Contact contact) {
886                 // Copy all fields:
887                 // - base data
888                 this.setGender(contact.getContactGender());
889                 this.setFirstName(contact.getContactFirstName());
890                 this.setFamilyName(contact.getContactFamilyName());
891                 this.setStreet(contact.getContactStreet());
892                 this.setHouseNumber(contact.getContactHouseNumber());
893                 this.setZipCode(contact.getContactZipCode());
894                 this.setCity(contact.getContactCity());
895                 this.setCountry(contact.getContactCountry());
896
897                 // Get cellphone, phone and fax instance
898                 DialableCellphoneNumber cellphone = contact.getContactCellphoneNumber();
899                 DialableFaxNumber fax = contact.getContactFaxNumber();
900                 DialableLandLineNumber phone = contact.getContactLandLineNumber();
901
902                 // - contact data
903                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
904                         this.setPhoneCountry(phone.getPhoneCountry());
905                         this.setPhoneAreaCode(phone.getPhoneAreaCode());
906                         this.setPhoneNumber(phone.getPhoneNumber());
907                 }
908                 if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof MobileProvider)) {
909                         this.setCellphoneCarrier(cellphone.getCellphoneProvider());
910                         this.setCellphoneNumber(cellphone.getPhoneNumber());
911                 }
912                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
913                         this.setFaxCountry(fax.getPhoneCountry());
914                         this.setFaxAreaCode(fax.getPhoneAreaCode());
915                         this.setFaxNumber(fax.getPhoneNumber());
916                 }
917                 this.setEmailAddress(contact.getContactEmailAddress());
918
919                 // -- other data
920                 this.setBirthday(contact.getContactBirthday());
921                 this.setComment(contact.getContactComment());
922         }
923
924 }