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