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