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