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