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