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