]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/contact/JobsContactWebSessionBean.java
removed double //NOI18N (please cherry-pick)
[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.LinkedList;
24 import java.util.List;
25 import java.util.Objects;
26 import javax.annotation.PostConstruct;
27 import javax.enterprise.context.SessionScoped;
28 import javax.enterprise.event.Observes;
29 import javax.faces.view.facelets.FaceletException;
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 import javax.naming.Context;
33 import javax.naming.InitialContext;
34 import javax.naming.NamingException;
35 import org.mxchange.jcontacts.contact.Contact;
36 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
37 import org.mxchange.jcontacts.contact.UserContact;
38 import org.mxchange.jcontacts.contact.gender.Gender;
39 import org.mxchange.jcontacts.contact.utils.ContactUtils;
40 import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent;
41 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
42 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
43 import org.mxchange.jcountry.data.Country;
44 import org.mxchange.jjobs.beans.BaseJobsController;
45 import org.mxchange.jjobs.beans.login.JobsUserLoginWebSessionController;
46 import org.mxchange.jjobs.beans.user.JobsUserWebSessionController;
47 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
48 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
49 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
50 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
51 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
52 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
53 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
54 import org.mxchange.jusercore.events.confirmation.UserConfirmedAccountEvent;
55 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
56 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
57 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
58 import org.mxchange.jusercore.events.user.linked.AdminLinkedUserEvent;
59 import org.mxchange.jusercore.model.user.User;
60
61 /**
62  * A general contact controller (bean)
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 final 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 final 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 controller (bean)
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                 // Init lists/maps
249                 this.contactList = new LinkedList<>();
250                 this.emailAddressList = new LinkedList<>();
251         }
252
253         @Override
254         public void afterAdminAddedContact (@Observes final AdminAddedContactEvent event) {
255                 // The event must be valid
256                 if (null == event) {
257                         // Throw NPE
258                         throw new NullPointerException("event is null"); //NOI18N
259                 } else if (event.getAddedContact() == null) {
260                         // Throw again ...
261                         throw new NullPointerException("event.addedContact is null"); //NOI18N
262                 } else if (event.getAddedContact().getContactId() == null) {
263                         // ... and again
264                         throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
265                 } else if (event.getAddedContact().getContactId() < 1) {
266                         // Not valid
267                         throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N
268                 }
269
270                 // Clear this bean
271                 this.clear();
272
273                 // Call other method
274                 this.uniqueAddContact(event.getAddedContact());
275
276                 // Add to selectable contacts
277                 this.selectableContacts.add(event.getAddedContact());
278         }
279
280         @Override
281         public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
282                 // event should not be null
283                 if (null == event) {
284                         // Throw NPE
285                         throw new NullPointerException("event is null"); //NOI18N
286                 } else if (event.getAddedUser() == null) {
287                         // Throw NPE again
288                         throw new NullPointerException("event.addedUser is null"); //NOI18N
289                 } else if (event.getAddedUser().getUserId() == null) {
290                         // userId is null
291                         throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
292                 } else if (event.getAddedUser().getUserId() < 1) {
293                         // Not avalid id
294                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
295                 }
296
297                 // Clear all data
298                 this.clear();
299         }
300
301         @Override
302         public void afterAdminLinkedUser (@Observes final AdminLinkedUserEvent event) {
303                 // event should not be null
304                 if (null == event) {
305                         // Throw NPE
306                         throw new NullPointerException("event is null"); //NOI18N
307                 } else if (event.getLinkedUser() == null) {
308                         // Throw NPE again
309                         throw new NullPointerException("event.linkedUser is null"); //NOI18N
310                 } else if (event.getLinkedUser().getUserContact() == null) {
311                         // Throw NPE again
312                         throw new NullPointerException("event.linkedUser.userContact is null"); //NOI18N
313                 } else if (event.getLinkedUser().getUserContact().getContactId() == null) {
314                         // userId is null
315                         throw new NullPointerException("event.linkedUser.userContact.contactId is null"); //NOI18N
316                 } else if (event.getLinkedUser().getUserContact().getContactId() < 1) {
317                         // Not avalid id
318                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserContact().getContactId())); //NOI18N
319                 }
320
321                 // Remove contact from list available contacts list
322                 this.selectableContacts.remove(event.getLinkedUser().getUserContact());
323
324                 // Clear all data
325                 this.clear();
326         }
327
328         @Override
329         public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent event) {
330                 // event should not be null
331                 if (null == event) {
332                         // Throw NPE
333                         throw new NullPointerException("event is null"); //NOI18N
334                 } else if (event.getUpdatedContact() == null) {
335                         // Throw NPE again
336                         throw new NullPointerException("event.updatedContact is null"); //NOI18N
337                 } else if (event.getUpdatedContact().getContactId() == null) {
338                         // userId is null
339                         throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
340                 } else if (event.getUpdatedContact().getContactId() < 1) {
341                         // Not avalid id
342                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
343                 }
344
345                 // Add contact instance only once
346                 this.uniqueAddContact(event.getUpdatedContact());
347
348                 // Add email address to list
349                 this.emailAddressList.add(event.getUpdatedContact().getContactEmailAddress());
350         }
351
352         @Override
353         public void afterRegistrationEvent (@Observes final UserRegisteredEvent event) {
354                 // event should not be null
355                 if (null == event) {
356                         // Throw NPE
357                         throw new NullPointerException("event is null"); //NOI18N
358                 } else if (event.getRegisteredUser() == null) {
359                         // Throw NPE again
360                         throw new NullPointerException("event.registeredUser is null"); //NOI18N
361                 } else if (event.getRegisteredUser().getUserId() == null) {
362                         // userId is null
363                         throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
364                 } else if (event.getRegisteredUser().getUserId() < 1) {
365                         // Not avalid id
366                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
367                 }
368
369                 // Get user instance
370                 Contact registeredContact = event.getRegisteredUser().getUserContact();
371
372                 // Copy all data from registered->user
373                 this.copyContact(registeredContact);
374
375                 // Add contact instance only once
376                 this.uniqueAddContact(registeredContact);
377
378                 // Add user name and email address
379                 this.addUserNameEmailAddress(registeredContact);
380
381                 // Clear all data
382                 this.clear();
383         }
384
385         @Override
386         public void afterUserConfirmedAccount (@Observes final UserConfirmedAccountEvent event) {
387                 // event should not be null
388                 if (null == event) {
389                         // Throw NPE
390                         throw new NullPointerException("event is null"); //NOI18N
391                 } else if (event.getConfirmedUser() == null) {
392                         // Throw NPE again
393                         throw new NullPointerException("event.confirmedUser is null"); //NOI18N
394                 } else if (event.getConfirmedUser().getUserId() == null) {
395                         // userId is null
396                         throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
397                 } else if (event.getConfirmedUser().getUserId() < 1) {
398                         // Not avalid id
399                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
400                 }
401
402                 // Add contact instance only once
403                 this.uniqueAddContact(event.getConfirmedUser().getUserContact());
404         }
405
406         @Override
407         public void afterUserLogin (@Observes final UserLoggedInEvent event) {
408                 // event should not be null
409                 if (null == event) {
410                         // Throw NPE
411                         throw new NullPointerException("event is null"); //NOI18N
412                 } else if (event.getLoggedInUser() == null) {
413                         // Throw NPE again
414                         throw new NullPointerException("event.loggedInUser is null"); //NOI18N
415                 } else if (event.getLoggedInUser().getUserId() == null) {
416                         // userId is null
417                         throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
418                 } else if (event.getLoggedInUser().getUserId() < 1) {
419                         // Not avalid id
420                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
421                 }
422
423                 // Copy all data to this bean
424                 this.copyContact(event.getLoggedInUser().getUserContact());
425         }
426
427         @Override
428         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
429         public List<Contact> allContacts () {
430                 // Debug message
431                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactController.allContacts: contactList.size()={0} - EXIT!", this.contactList.size()));
432
433                 // Return un-modified list
434                 return this.contactList;
435         }
436
437         @Override
438         public Contact createContactInstance () {
439                 // User message
440                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: CALLED!", this.getClass().getSimpleName()));
441
442                 // Is all required data set?
443                 if (!this.isRequiredPersonalDataSet()) {
444                         // No, then abort here
445                         throw new FaceletException(new IllegalArgumentException("Not all personal data is set, but createContactInstance() is called.")); //NOI18N
446                 }
447
448                 // Generate phone number
449                 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
450                 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
451                 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
452
453                 // Create new contact
454                 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
455                 contact.setContactStreet(this.getStreet());
456                 contact.setContactHouseNumber(this.getHouseNumber());
457                 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
458                 contact.setContactZipCode(this.getZipCode());
459                 contact.setContactCity(this.getCity());
460                 contact.setContactCountry(this.getCountry());
461                 contact.setContactEmailAddress(this.getEmailAddress());
462                 contact.setContactBirthday(this.getBirthday());
463                 contact.setContactComment(this.getComment());
464
465                 // Debug message
466                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: this.emailAddress={1}", this.getClass().getSimpleName(), this.getEmailAddress()));
467                 // Don't set null or wrong references
468                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
469                         // Now the number must be given
470                         if (phone.getPhoneAreaCode() == null) {
471                                 // Is null
472                                 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
473                         } else if (phone.getPhoneAreaCode() < 1) {
474                                 // Abort here
475                                 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
476                         } else if (phone.getPhoneNumber() == null) {
477                                 // Is null
478                                 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
479                         } else if (phone.getPhoneNumber() < 1) {
480                                 // Abort here
481                                 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
482                         }
483
484                         // Set phone number
485                         contact.setContactLandLineNumber(phone);
486                 }
487
488                 // Don't set null or wrong references
489                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
490                         // Now the number must be given
491                         if (fax.getPhoneAreaCode() == null) {
492                                 // Is null
493                                 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
494                         } else if (fax.getPhoneAreaCode() < 1) {
495                                 // Abort here
496                                 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
497                         } else if (fax.getPhoneNumber() == null) {
498                                 // Is null
499                                 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
500                         } else if (fax.getPhoneNumber() < 1) {
501                                 // Abort here
502                                 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
503                         }
504
505                         // Set fax number
506                         contact.setContactFaxNumber(fax);
507                 }
508
509                 // Is the provider set?
510                 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
511                         // Is the number set?
512                         if (cellphone.getPhoneNumber() == null) {
513                                 // Is null
514                                 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
515                         } else if (cellphone.getPhoneNumber() < 1) {
516                                 // Abort here
517                                 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
518                         }
519
520                         // Set cellphone number
521                         contact.setContactCellphoneNumber(cellphone);
522                 }
523
524                 // Trace message
525                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: contact={1} - EXIT!", this.getClass().getSimpleName(), contact));
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", "ERROR_USER_CURRENT_PASSWORD_MISMATCHING"); //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         /**
814          * Post-initialization of this class
815          */
816         @PostConstruct
817         public void init () {
818                 // Get full email address list for reducing EJB calls
819                 this.emailAddressList.addAll(this.contactBean.getEmailAddressList());
820
821                 // Get full contact list
822                 this.contactList.addAll(this.contactBean.getAllContacts());
823
824                 // Get all users
825                 List<User> allUsers = this.userController.allUsers();
826
827                 // Get all contacts
828                 List<Contact> allContacts = this.contactBean.getAllContacts();
829
830                 // Get iterator
831                 Iterator<Contact> iterator = allContacts.iterator();
832
833                 // Loop through it
834                 while (iterator.hasNext()) {
835                         // Get next element
836                         Contact next = iterator.next();
837
838                         // Get iterator
839                         Iterator<User> userIterator = allUsers.iterator();
840
841                         // Loop through all users
842                         while (userIterator.hasNext()) {
843                                 // Get user instance
844                                 User nextUser = userIterator.next();
845
846                                 // Is contact same?
847                                 if (Objects.equals(next, nextUser.getUserContact())) {
848                                         // Found same
849                                         iterator.remove();
850                                         break;
851                                 }
852                         }
853                 }
854
855                 // Set contact list
856                 this.selectableContacts = allContacts;
857         }
858
859         @Override
860         public boolean isEmailAddressRegistered (final Contact contact) {
861                 // Cherck parameter
862                 if (null == contact) {
863                         // Throw NPE
864                         throw new NullPointerException("contact is null"); //NOI18N
865                 } else if (contact.getContactEmailAddress() == null) {
866                         // Throw again
867                         throw new NullPointerException("contact.contactEmailAddress is null"); //NOI18N
868                 } else if (contact.getContactEmailAddress().isEmpty()) {
869                         // Is empty
870                         throw new IllegalArgumentException("contact.contactEmailAddress is empty."); //NOI18N
871                 }
872
873                 // Determine it
874                 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(contact.getContactEmailAddress())));
875         }
876
877         @Override
878         public boolean isRequiredChangePersonalDataSet () {
879                 return ((this.getGender() != null) &&
880                                 (this.getFirstName() != null) &&
881                                 (this.getFamilyName() != null) &&
882                                 (this.getStreet() != null) &&
883                                 (this.getHouseNumber() != null) &&
884                                 (this.getZipCode() != null) &&
885                                 (this.getCity() != null));
886         }
887
888         @Override
889         public boolean isRequiredPersonalDataSet () {
890                 return ((this.getGender() != null) &&
891                                 (this.getFirstName() != null) &&
892                                 (this.getFamilyName() != null) &&
893                                 (this.getStreet() != null) &&
894                                 (this.getHouseNumber() != null) &&
895                                 (this.getZipCode() != null) &&
896                                 (this.getCity() != null) &&
897                                 (this.getEmailAddress() != null) &&
898                                 (this.getEmailAddressRepeat() != null));
899         }
900
901         @Override
902         public boolean isSameEmailAddressEntered () {
903                 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
904         }
905
906         @Override
907         public Contact lookupContactById (final Long contactId) throws ContactNotFoundException {
908                 // Init variable
909                 Contact localContact = null;
910
911                 // Clear this bean
912                 this.clear();
913
914                 // Try to lookup it in visible user list
915                 for (final Iterator<Contact> iterator = this.contactList.iterator(); iterator.hasNext();) {
916                         // Get next user
917                         Contact next = iterator.next();
918
919                         // Is the user id found?
920                         if (Objects.equals(next.getContactId(), contactId)) {
921                                 // Copy to other variable
922                                 localContact = next;
923                                 break;
924                         }
925                 }
926
927                 // Is it still null?
928                 if (null == localContact) {
929                         // Not visible for the current user
930                         throw new ContactNotFoundException(contactId);
931                 }
932
933                 // Copy all data to this bean
934                 this.copyContact(localContact);
935
936                 // Return it
937                 return localContact;
938         }
939
940         @Override
941         public List<Contact> selectableContacts () {
942                 return Collections.unmodifiableList(this.selectableContacts);
943         }
944
945         @Override
946         public void updateContactDataFromController (final Contact contact) {
947                 // Is the instance valid?
948                 if (null == contact) {
949                         // Throw NPE
950                         throw new NullPointerException("contact is null"); //NOI18N
951                 } else if (contact.getContactId() == null) {
952                         // Throw NPE
953                         throw new NullPointerException("contact.contactId is null"); //NOI18N
954                 } else if (contact.getContactId() < 1) {
955                         // Not valid id number
956                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
957                 }
958
959                 // Set all
960                 this.copyContact(contact);
961         }
962
963         /**
964          * Adds email address to bean's internal list.
965          * <p>
966          * @param contact Contact instance
967          */
968         private void addUserNameEmailAddress (final Contact contact) {
969                 // Make sure the entry is not added yet
970                 if (this.emailAddressList.contains(contact.getContactEmailAddress())) {
971                         // Already added
972                         throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
973                 }
974
975                 // Add email addres
976                 this.emailAddressList.add(contact.getContactEmailAddress());
977         }
978
979         /**
980          * Clears this bean
981          */
982         private void clear () {
983                 // Clear all data
984                 // - personal data
985                 this.setGender(null);
986                 this.setTitle(null);
987                 this.setFirstName(null);
988                 this.setFamilyName(null);
989                 this.setStreet(null);
990                 this.setHouseNumber(null);
991                 this.setHouseNumberExtension(null);
992                 this.setZipCode(null);
993                 this.setCity(null);
994                 this.setCountry(null);
995
996                 // - contact data
997                 this.setEmailAddress(null);
998                 this.setEmailAddressRepeat(null);
999                 this.setPhoneAreaCode(null);
1000                 this.setPhoneCountry(null);
1001                 this.setPhoneNumber(null);
1002                 this.setCellphoneCarrier(null);
1003                 this.setCellphoneNumber(null);
1004                 this.setFaxAreaCode(null);
1005                 this.setFaxCountry(null);
1006                 this.setFaxNumber(null);
1007
1008                 // - other data
1009                 this.setBirthday(null);
1010                 this.setComment(null);
1011         }
1012
1013         /**
1014          * Copies given contact into the controller
1015          * <p>
1016          * @param contact Contact instance
1017          */
1018         private void copyContact (final Contact contact) {
1019                 // Is the instance valid?
1020                 if (null == contact) {
1021                         // Throw NPE
1022                         throw new NullPointerException("contact is null"); //NOI18N
1023                 } else if (contact.getContactId() == null) {
1024                         // Throw NPE
1025                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1026                 } else if (contact.getContactId() < 1) {
1027                         // Not valid id number
1028                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1029                 }
1030
1031                 // Copy all fields:
1032                 // - base data
1033                 this.setGender(contact.getContactGender());
1034                 this.setTitle(contact.getContactTitle());
1035                 this.setFirstName(contact.getContactFirstName());
1036                 this.setFamilyName(contact.getContactFamilyName());
1037                 this.setStreet(contact.getContactStreet());
1038                 this.setHouseNumber(contact.getContactHouseNumber());
1039                 this.setHouseNumberExtension(contact.getContactHouseNumberExtension());
1040                 this.setZipCode(contact.getContactZipCode());
1041                 this.setCity(contact.getContactCity());
1042                 this.setCountry(contact.getContactCountry());
1043                 this.setEmailAddress(contact.getContactEmailAddress());
1044                 this.setBirthday(contact.getContactBirthday());
1045                 this.setComment(contact.getContactComment());
1046
1047                 // Get cellphone, phone and fax instance
1048                 DialableCellphoneNumber cellphone = contact.getContactCellphoneNumber();
1049                 DialableFaxNumber fax = contact.getContactFaxNumber();
1050                 DialableLandLineNumber phone = contact.getContactLandLineNumber();
1051
1052                 // - contact data
1053                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
1054                         this.setPhoneCountry(phone.getPhoneCountry());
1055                         this.setPhoneAreaCode(phone.getPhoneAreaCode());
1056                         this.setPhoneNumber(phone.getPhoneNumber());
1057                 }
1058
1059                 if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof MobileProvider)) {
1060                         this.setCellphoneCarrier(cellphone.getCellphoneProvider());
1061                         this.setCellphoneNumber(cellphone.getPhoneNumber());
1062                 }
1063
1064                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
1065                         this.setFaxCountry(fax.getPhoneCountry());
1066                         this.setFaxAreaCode(fax.getPhoneAreaCode());
1067                         this.setFaxNumber(fax.getPhoneNumber());
1068                 }
1069         }
1070
1071         /**
1072          * Removes given contact from all lists
1073          * <p>
1074          * @param contact Contact instance to remove
1075          */
1076         private void removeContact (final Contact contact) {
1077                 // Is the instance valid?
1078                 if (null == contact) {
1079                         // Throw NPE
1080                         throw new NullPointerException("contact is null"); //NOI18N
1081                 } else if (contact.getContactId() == null) {
1082                         // Throw NPE
1083                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1084                 } else if (contact.getContactId() < 1) {
1085                         // Not valid id number
1086                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1087                 }
1088
1089                 // Remove from general list
1090                 if (!this.contactList.remove(contact)) {
1091                         // Did not remove contact
1092                         throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N
1093                 }
1094
1095                 // Remove from other lists
1096                 this.emailAddressList.remove(contact.getContactEmailAddress());
1097         }
1098
1099         /**
1100          * Adds unique instance to contact list. First any existing instance is
1101          * being removed, then the new instance is added.
1102          * <p>
1103          * @param contact Contact instance to add uniquely
1104          */
1105         private void uniqueAddContact (final Contact contact) {
1106                 // Is the instance valid?
1107                 if (null == contact) {
1108                         // Throw NPE
1109                         throw new NullPointerException("contact is null"); //NOI18N
1110                 } else if (contact.getContactId() == null) {
1111                         // Throw NPE
1112                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1113                 } else if (contact.getContactId() < 1) {
1114                         // Not valid id number
1115                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1116                 }
1117
1118                 // Get iterator from list
1119                 Iterator<Contact> iterator = this.contactList.iterator();
1120
1121                 // "Walk" through all entries
1122                 while (iterator.hasNext()) {
1123                         // Get next element
1124                         Contact next = iterator.next();
1125
1126                         // Is id number the same?
1127                         if (Objects.equals(contact.getContactId(), next.getContactId())) {
1128                                 // Found entry, so remove it and abort
1129                                 this.removeContact(next);
1130                                 break;
1131                         }
1132                 }
1133
1134                 // Add contact to list
1135                 this.contactList.add(contact);
1136         }
1137
1138 }