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