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