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