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