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