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