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