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