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