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