]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/beans/contact/AddressbookAdminContactWebRequestBean.java
WIP: Please cherry-pick:
[addressbook-war.git] / src / java / org / mxchange / addressbook / beans / contact / AddressbookAdminContactWebRequestBean.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.addressbook.beans.contact;
18
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import java.util.Iterator;
22 import javax.annotation.PostConstruct;
23 import javax.enterprise.context.RequestScoped;
24 import javax.enterprise.event.Event;
25 import javax.enterprise.event.Observes;
26 import javax.enterprise.inject.Any;
27 import javax.faces.context.FacesContext;
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.helper.AddressbookWebRequestHelperController;
36 import org.mxchange.jcontacts.contact.AdminContactSessionBeanRemote;
37 import org.mxchange.jcontacts.contact.Contact;
38 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
39 import org.mxchange.jcontacts.contact.ContactUtils;
40 import org.mxchange.jcontacts.contact.UserContact;
41 import org.mxchange.jcontacts.contact.title.PersonalTitle;
42 import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent;
43 import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent;
44 import org.mxchange.jcontacts.events.contact.helper.created.ObservableHelperCreatedContactEvent;
45 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
46 import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent;
47 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
48 import org.mxchange.jcountry.data.Country;
49 import org.mxchange.jphone.phonenumbers.DialableNumber;
50 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
51 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
52 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
53 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
54 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
55 import org.mxchange.jphone.phonenumbers.mobile.MobileNumber;
56 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
57
58 /**
59  * An administrative user bean (controller)
60  * <p>
61  * @author Roland Häder<roland@mxchange.org>
62  */
63 @Named ("adminContactController")
64 @RequestScoped
65 public class AddressbookAdminContactWebRequestBean extends BaseAddressbookController implements AddressbookAdminContactWebRequestController {
66
67         /**
68          * Serial number
69          */
70         private static final long serialVersionUID = 542_145_347_916L;
71
72         /**
73          * Academic academicTitle
74          */
75         private String academicTitle;
76
77         /**
78          * An event fired when the administrator has added a new contact
79          */
80         @Inject
81         @Any
82         private Event<ObservableAdminAddedContactEvent> addedContactEvent;
83
84         /**
85          * Administrative contact EJB
86          */
87         private AdminContactSessionBeanRemote adminContactBean;
88
89         /**
90          * Bean helper instance
91          */
92         @Inject
93         private AddressbookWebRequestHelperController beanHelper;
94
95         /**
96          * Birth day
97          */
98         private Date birthday;
99
100         /**
101          * City
102          */
103         private String city;
104
105         /**
106          * Optional comments
107          */
108         private String comment;
109
110         /**
111          * Remote contact bean
112          */
113         private ContactSessionBeanRemote contactBean;
114
115         /**
116          * General contact controller
117          */
118         @Inject
119         private AddressbookContactWebSessionController contactController;
120
121         /**
122          * Country instance
123          */
124         private Country contactCountry;
125
126         /**
127          * Contact id
128          */
129         private Long contactId;
130
131         /**
132          * Email address
133          */
134         private String emailAddress;
135
136         /**
137          * Family name
138          */
139         private String familyName;
140
141         /**
142          * Fax number's area code
143          */
144         private Integer faxAreaCode;
145
146         /**
147          * Country instance for fax number
148          */
149         private Country faxCountry;
150
151         /**
152          * Fax id number
153          */
154         private Long faxId;
155
156         /**
157          * Fax number
158          */
159         private Long faxNumber;
160
161         /**
162          * First name
163          */
164         private String firstName;
165
166         /**
167          * House number
168          */
169         private Short houseNumber;
170
171         /**
172          * House number extension
173          */
174         private String houseNumberExtension;
175
176         /**
177          * Whether a fax entry has been unlinked
178          */
179         private boolean isFaxUnlinked;
180
181         /**
182          * Whether a land-line number has been unlinked
183          */
184         private boolean isLandLineUnlinked;
185
186         /**
187          * Whether a cmobile entry has been unlinked
188          */
189         private boolean isMobileNumberUnlinked;
190
191         /**
192          * Phone number area code
193          */
194         private Integer landLineAreaCode;
195
196         /**
197          * Country instance for phone number
198          */
199         private Country landLineCountry;
200
201         /**
202          * Land-line id number
203          */
204         private Long landLineId;
205
206         /**
207          * Phone number
208          */
209         private Long landLineNumber;
210
211         /**
212          * Mobile id number
213          */
214         private Long mobileId;
215
216         /**
217          * Mobile number
218          */
219         private Long mobileNumber;
220
221         /**
222          * Mobile number's provider
223          */
224         private MobileProvider mobileProvider;
225
226         /**
227          * PersonalTitle instance
228          */
229         private PersonalTitle personalTitle;
230
231         /**
232          * Street
233          */
234         private String street;
235
236         /**
237          * An event fired when the administrator has updated contact data
238          */
239         @Inject
240         @Any
241         private Event<ObservableAdminUpdatedContactEvent> updatedContactEvent;
242
243         /**
244          * ZIP code
245          */
246         private Integer zipCode;
247
248         /**
249          * Default constructor
250          */
251         public AddressbookAdminContactWebRequestBean () {
252                 // Call super constructor
253                 super();
254         }
255
256         /**
257          * Adds contact data to database and redirects on success. If the contact is
258          * already found, a proper exception is thrown.
259          * <p>
260          * @return Redirect outcome
261          */
262         public String addContact () {
263                 // Are all minimum fields set?
264                 if (this.getPersonalTitle() == null) {
265                         // Throw NPE
266                         throw new NullPointerException("personalTitle is null"); //NOI18N
267                 } else if (this.getFirstName() == null) {
268                         // Throw NPE
269                         throw new NullPointerException("firstName is null"); //NOI18N
270                 } else if (this.getFirstName().isEmpty()) {
271                         // Empty string
272                         throw new IllegalStateException("firstName is empty"); //NOI18N
273                 } else if (this.getFamilyName() == null) {
274                         // Throw NPE
275                         throw new NullPointerException("familyName is null"); //NOI18N
276                 } else if (this.getFamilyName().isEmpty()) {
277                         // Empty string
278                         throw new IllegalStateException("familyName is empty"); //NOI18N
279                 }
280
281                 // Create new contact instance
282                 Contact contact = this.createContactInstance();
283
284                 // Default is not same contact
285                 if (this.isSameContactFound(contact)) {
286                         // Already registered
287                         throw new FaceletException(new ContactAlreadyAddedException(contact));
288                 }
289
290                 // Init contact
291                 Contact updatedContact;
292
293                 // Try to call EJB
294                 try {
295                         // Call EJB
296                         updatedContact = this.adminContactBean.addContact(contact);
297                 } catch (final ContactAlreadyAddedException ex) {
298                         // Throw again
299                         throw new FaceletException(ex);
300                 }
301
302                 // Fire event
303                 this.addedContactEvent.fire(new AdminAddedContactEvent(updatedContact));
304
305                 // Clear this bean
306                 this.clear();
307
308                 // Return outcome
309                 return "admin_list_contact"; //NOI18N
310         }
311
312         /**
313          * Observer for events being fired when a bean helper has successfully
314          * created a contact instance.
315          * <p>
316          * @param event Event being fired
317          */
318         public void afterHelperCreatedContactEvent (@Observes final ObservableHelperCreatedContactEvent event) {
319                 // Log message
320                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("AdminContactController::afterHelperCreatedContactEvent(): contact={0} - CALLED!", contact)); //NOI18N
321
322                 // The event instance must be valid
323                 if (null == event) {
324                         // Throw NPE again
325                         throw new NullPointerException("event is null"); //NOI18N
326                 } else if (event.getCreatedContact() == null) {
327                         // Throw NPE again
328                         throw new NullPointerException("event.createdContact is null"); //NOI18N //NOI18N
329                 } else if (event.getCreatedContact().getContactId() == null) {
330                         // Throw NPE again
331                         throw new NullPointerException("event.createdContact.contactId is null"); //NOI18N //NOI18N
332                 } else if (event.getCreatedContact().getContactId() < 1) {
333                         // Not valid
334                         throw new IllegalStateException(MessageFormat.format("event.createdContact.contactId={0} is not valid.", event.getCreatedContact().getContactId())); //NOI18N
335                 }
336
337                 // Get contact instance from event
338                 Contact contact = event.getCreatedContact();
339
340                 // Set all fields: contact
341                 this.setContactId(contact.getContactId());
342                 this.setAcademicTitle(contact.getContactTitle());
343                 this.setBirthday(contact.getContactBirthday());
344                 this.setCity(contact.getContactCity());
345                 this.setComment(contact.getContactComment());
346                 this.setContactCountry(contact.getContactCountry());
347                 this.setEmailAddress(contact.getContactEmailAddress());
348                 this.setFamilyName(contact.getContactFamilyName());
349                 this.setFirstName(contact.getContactFirstName());
350                 this.setPersonalTitle(contact.getContactPersonalTitle());
351                 this.setHouseNumber(contact.getContactHouseNumber());
352                 this.setHouseNumberExtension(contact.getContactHouseNumberExtension());
353                 this.setStreet(contact.getContactStreet());
354                 this.setZipCode(contact.getContactZipCode());
355
356                 // Is the cell phone set?
357                 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
358                         // ... cmobile data
359                         this.setMobileId(contact.getContactMobileNumber().getPhoneId());
360                         this.setMobileProvider(contact.getContactMobileNumber().getMobileProvider());
361                         this.setMobileNumber(contact.getContactMobileNumber().getPhoneNumber());
362                 }
363
364                 // Is the fax set?
365                 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
366                         // ... fax data
367                         this.setFaxId(contact.getContactFaxNumber().getPhoneId());
368                         this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
369                         this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
370                         this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
371                 }
372
373                 // Is the land-line number set?
374                 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
375                         // .. land-line data
376                         this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
377                         this.setLandLineAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
378                         this.setLandLineCountry(contact.getContactLandLineNumber().getPhoneCountry());
379                         this.setLandLineNumber(contact.getContactLandLineNumber().getPhoneNumber());
380                 }
381
382                 // Log message
383                 //* NOISY-DEBUG: */ System.out.println("AdminContactController::afterHelperCreatedContactEvent(): EXIT!"); //NOI18N
384         }
385
386         @Override
387         public Contact createContactInstance () {
388                 // Are all minimum fields set?
389                 if (this.getPersonalTitle() == null) {
390                         // Throw NPE
391                         throw new NullPointerException("personalTitle is null"); //NOI18N
392                 } else if (this.getFirstName() == null) {
393                         // Throw NPE
394                         throw new NullPointerException("firstName is null"); //NOI18N
395                 } else if (this.getFirstName().isEmpty()) {
396                         // Empty string
397                         throw new IllegalStateException("firstName is empty"); //NOI18N
398                 } else if (this.getFamilyName() == null) {
399                         // Throw NPE
400                         throw new NullPointerException("familyName is null"); //NOI18N
401                 } else if (this.getFamilyName().isEmpty()) {
402                         // Empty string
403                         throw new IllegalStateException("familyName is empty"); //NOI18N
404                 }
405
406                 // Generate phone number
407                 DialableLandLineNumber landLine = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
408                 DialableMobileNumber mobile = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
409                 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
410
411                 // Create new instance
412                 Contact contact = new UserContact(this.getPersonalTitle(), this.getFirstName(), this.getFamilyName());
413
414                 // Is contact id set?
415                 if (this.getContactId() instanceof Long) {
416                         // Set it, too
417                         contact.setContactId(this.getContactId());
418                 }
419
420                 // Add all others
421                 contact.setContactTitle(this.getAcademicTitle());
422                 contact.setContactBirthday(this.getBirthday());
423                 contact.setContactStreet(this.getStreet());
424                 contact.setContactHouseNumber(this.getHouseNumber());
425                 contact.setContactZipCode(this.getZipCode());
426                 contact.setContactCity(this.getCity());
427                 contact.setContactCountry(this.getContactCountry());
428                 contact.setContactEmailAddress(this.getEmailAddress());
429                 contact.setContactBirthday(this.getBirthday());
430                 contact.setContactComment(this.getComment());
431
432                 // Set ownContact
433                 contact.setContactOwnContact(Boolean.TRUE);
434
435                 // Don't set null or wrong references
436                 if ((landLine instanceof DialableLandLineNumber) && (landLine.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) {
437                         // Now the number must be given
438                         if (landLine.getPhoneAreaCode() == null) {
439                                 // Is null
440                                 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
441                         } else if (landLine.getPhoneAreaCode() < 1) {
442                                 // Abort here
443                                 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
444                         } else if (landLine.getPhoneNumber() == null) {
445                                 // Is null
446                                 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
447                         } else if (landLine.getPhoneNumber() < 1) {
448                                 // Abort here
449                                 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
450                         }
451
452                         // Set phone number
453                         contact.setContactLandLineNumber(landLine);
454                 }
455
456                 // Don't set null or wrong references
457                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
458                         // Now the number must be given
459                         if (fax.getPhoneAreaCode() == null) {
460                                 // Is null
461                                 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
462                         } else if (fax.getPhoneAreaCode() < 1) {
463                                 // Abort here
464                                 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
465                         } else if (fax.getPhoneNumber() == null) {
466                                 // Is null
467                                 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
468                         } else if (fax.getPhoneNumber() < 1) {
469                                 // Abort here
470                                 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
471                         }
472
473                         // Set fax number
474                         contact.setContactFaxNumber(fax);
475                 }
476
477                 // Is the provider set?
478                 if ((mobile instanceof DialableMobileNumber) && (this.getMobileProvider() instanceof MobileProvider) && (this.getMobileNumber() != null) && (this.getMobileNumber() > 0)) {
479                         // Is the number set?
480                         if (mobile.getPhoneNumber() == null) {
481                                 // Is null
482                                 throw new NullPointerException("cmobile.phoneNumber is null"); //NOI18N
483                         } else if (mobile.getPhoneNumber() < 1) {
484                                 // Abort here
485                                 throw new IllegalArgumentException("cmobile.phoneNumber is zero or below."); //NOI18N
486                         }
487
488                         // Set cmobile number
489                         contact.setContactMobileNumber(mobile);
490                 }
491
492                 // Return it
493                 return contact;
494         }
495
496         /**
497          * Edits currently loaded contact's data in database.
498          * <p>
499          * @return Redirect outcome
500          */
501         public String editContactData () {
502                 // Get contact instance
503                 Contact contact = this.createContactInstance();
504
505                 // Check if contact instance is in helper and valid
506                 if (null == contact) {
507                         // Throw NPE
508                         throw new NullPointerException("beanHelper.contact is null"); //NOI18N
509                 } else if (contact.getContactId() == null) {
510                         // Throw NPE again
511                         throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
512                 } else if (contact.getContactId() < 1) {
513                         // Invalid id
514                         throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
515                 }
516
517                 // Update all data in contact
518                 this.updateContactData(contact);
519
520                 // Call EJB for updating contact data
521                 Contact updatedContact = this.contactBean.updateContactData(contact, this.isMobileNumberUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
522
523                 // Fire event
524                 this.updatedContactEvent.fire(new AdminUpdatedContactEvent(updatedContact));
525
526                 // Clear bean
527                 this.clear();
528
529                 // Return to contact list (for now)
530                 return "admin_list_contact"; //NOI18N
531         }
532
533         /**
534          * Returns a text representation of given mobile number or null if not set.
535          * <p>
536          * @param mobileNumber Mobile number
537          * <p>
538          * @return Text representation or null
539          */
540         public String generateMobileNumber (final DialableMobileNumber mobileNumber) {
541                 // Is it null?
542                 if (null == mobileNumber) {
543                         // Return null
544                         return null;
545                 }
546
547                 // Get all data
548                 String number = String.format(
549                            "%s%d%d", //NOI18N
550                            mobileNumber.getMobileProvider().getProviderCountry().getCountryExternalDialPrefix(),
551                            mobileNumber.getMobileProvider().getProviderDialPrefix(),
552                            mobileNumber.getPhoneNumber()
553            );
554
555                 // Return it
556                 return number;
557         }
558
559         /**
560          * Returns a text representation of given land-line or fax number or null if
561          * not set.
562          * <p>
563          * @param phoneNumber Land-line or fax number
564          * <p>
565          * @return Text representation or null
566          */
567         public String generatePhoneNumber (final DialableNumber phoneNumber) {
568                 // Is it null?
569                 if (null == phoneNumber) {
570                         // Return null
571                         return null;
572                 }
573
574                 // Generate it
575                 String number = String.format(
576                            "%s%d%d", //NOI18N
577                            phoneNumber.getPhoneCountry().getCountryExternalDialPrefix(),
578                            phoneNumber.getPhoneAreaCode(),
579                            phoneNumber.getPhoneNumber()
580            );
581
582                 // Return it
583                 return number;
584         }
585
586         /**
587          * Getter for academic title
588          * <p>
589          * @return Academic title
590          */
591         public String getAcademicTitle () {
592                 return this.academicTitle;
593         }
594
595         /**
596          * Setter for academic title
597          * <p>
598          * @param academicTitle Academic title
599          */
600         public void setAcademicTitle (final String academicTitle) {
601                 this.academicTitle = academicTitle;
602         }
603
604         /**
605          * Getter for birth day
606          * <p>
607          * @return Birth day
608          */
609         @SuppressWarnings ("ReturnOfDateField")
610         public Date getBirthday () {
611                 return this.birthday;
612         }
613
614         /**
615          * Setter for birth day
616          * <p>
617          * @param birthday Birth day
618          */
619         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
620         public void setBirthday (final Date birthday) {
621                 this.birthday = birthday;
622         }
623
624         /**
625          * Getter for city name
626          * <p>
627          * @return City name
628          */
629         public String getCity () {
630                 return this.city;
631         }
632
633         /**
634          * Setter for city name
635          * <p>
636          * @param city City name
637          */
638         public void setCity (final String city) {
639                 this.city = city;
640         }
641
642         /**
643          * Getter for comments
644          * <p>
645          * @return Comments
646          */
647         public String getComment () {
648                 return this.comment;
649         }
650
651         /**
652          * Setter for comment
653          * <p>
654          * @param comment Comments
655          */
656         public void setComment (final String comment) {
657                 this.comment = comment;
658         }
659
660         /**
661          * Getter for contactCountry instance
662          * <p>
663          * @return Country instance
664          */
665         public Country getContactCountry () {
666                 return this.contactCountry;
667         }
668
669         /**
670          * Setter for contactCountry instance
671          * <p>
672          * @param contactCountry Country instance
673          */
674         public void setContactCountry (final Country contactCountry) {
675                 this.contactCountry = contactCountry;
676         }
677
678         /**
679          * Getter for contact id
680          * <p>
681          * @return Contact id
682          */
683         public Long getContactId () {
684                 return this.contactId;
685         }
686
687         /**
688          * Setter for contact id
689          * <p>
690          * @param contactId Contact id
691          */
692         public void setContactId (final Long contactId) {
693                 this.contactId = contactId;
694         }
695
696         @Override
697         public String getControllerType () {
698                 return "admin"; //NOI18N
699         }
700
701         @Override
702         @Deprecated
703         public void setControllerType (final String controllerType) {
704                 throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
705         }
706
707         /**
708          * Getter for email address
709          * <p>
710          * @return Email address
711          */
712         public String getEmailAddress () {
713                 return this.emailAddress;
714         }
715
716         /**
717          * Setter for email address
718          * <p>
719          * @param emailAddress Email address
720          */
721         public void setEmailAddress (final String emailAddress) {
722                 this.emailAddress = emailAddress;
723         }
724
725         /**
726          * Family name
727          * <p>
728          * @return the familyName
729          */
730         public String getFamilyName () {
731                 return this.familyName;
732         }
733
734         /**
735          * Family name
736          * <p>
737          * @param familyName the familyName to set
738          */
739         public void setFamilyName (final String familyName) {
740                 this.familyName = familyName;
741         }
742
743         /**
744          * Getter for fax number's area code
745          * <p>
746          * @return Fax number's area code
747          */
748         public Integer getFaxAreaCode () {
749                 return this.faxAreaCode;
750         }
751
752         /**
753          * Setter for fax number's area code
754          * <p>
755          * @param faxAreaCode Fax number's area code
756          */
757         public void setFaxAreaCode (final Integer faxAreaCode) {
758                 this.faxAreaCode = faxAreaCode;
759         }
760
761         /**
762          * Getter for fax's country instance
763          * <p>
764          * @return Fax' country instance
765          */
766         public Country getFaxCountry () {
767                 return this.faxCountry;
768         }
769
770         /**
771          * Setter for fax's country instance
772          * <p>
773          * @param faxCountry Fax' country instance
774          */
775         public void setFaxCountry (final Country faxCountry) {
776                 this.faxCountry = faxCountry;
777         }
778
779         /**
780          * Getter for fax id
781          * <p>
782          * @return Fax id
783          */
784         public Long getFaxId () {
785                 return this.faxId;
786         }
787
788         /**
789          * Setter for fax id
790          * <p>
791          * @param faxId Fax id
792          */
793         public void setFaxId (final Long faxId) {
794                 this.faxId = faxId;
795         }
796
797         /**
798          * Getter for fax number
799          * <p>
800          * @return Fax number
801          */
802         public Long getFaxNumber () {
803                 return this.faxNumber;
804         }
805
806         /**
807          * Setter for fax number
808          * <p>
809          * @param faxNumber Fax number
810          */
811         public void setFaxNumber (final Long faxNumber) {
812                 this.faxNumber = faxNumber;
813         }
814
815         /**
816          * Getter for first name
817          * <p>
818          * @return First name
819          */
820         public String getFirstName () {
821                 return this.firstName;
822         }
823
824         /**
825          * Setter for first name
826          * <p>
827          * @param firstName First name
828          */
829         public void setFirstName (final String firstName) {
830                 this.firstName = firstName;
831         }
832
833         /**
834          * Getter for house number
835          * <p>
836          * @return House number
837          */
838         public Short getHouseNumber () {
839                 return this.houseNumber;
840         }
841
842         /**
843          * Setter for house number
844          * <p>
845          * @param houseNumber House number
846          */
847         public void setHouseNumber (final Short houseNumber) {
848                 this.houseNumber = houseNumber;
849         }
850
851         /**
852          * Getter for house number extension. Example: 123a, 'a' is the extension
853          * and 123 is the house number.
854          * <p>
855          * @return House number extension
856          */
857         public String getHouseNumberExtension () {
858                 return this.houseNumberExtension;
859         }
860
861         /**
862          * Setter for house number extension
863          * <p>
864          * @param houseNumberExtension House number extension
865          */
866         public void setHouseNumberExtension (final String houseNumberExtension) {
867                 this.houseNumberExtension = houseNumberExtension;
868         }
869
870         /**
871          * Getter for land-line number's area code
872          * <p>
873          * @return Land-line number's area code
874          */
875         public Integer getLandLineAreaCode () {
876                 return this.landLineAreaCode;
877         }
878
879         /**
880          * Setter for land-line number's area code
881          * <p>
882          * @param landLineAreaCode Land-line number's area code
883          */
884         public void setLandLineAreaCode (final Integer landLineAreaCode) {
885                 this.landLineAreaCode = landLineAreaCode;
886         }
887
888         /**
889          * Getter for land-line number's country instance
890          * <p>
891          * @return Land-line number's country instance
892          */
893         public Country getLandLineCountry () {
894                 return this.landLineCountry;
895         }
896
897         /**
898          * Setter for land-line number's country instance
899          * <p>
900          * @param landLineCountry Land-line number's country instance
901          */
902         public void setLandLineCountry (final Country landLineCountry) {
903                 this.landLineCountry = landLineCountry;
904         }
905
906         /**
907          * Getter for land-line id
908          * <p>
909          * @return Land-line id
910          */
911         public Long getLandLineId () {
912                 return this.landLineId;
913         }
914
915         /**
916          * Setter for land-line id
917          * <p>
918          * @param landLineId Land-line id
919          */
920         public void setLandLineId (final Long landLineId) {
921                 this.landLineId = landLineId;
922         }
923
924         /**
925          * Getter for land-line number
926          * <p>
927          * @return Land-line number
928          */
929         public Long getLandLineNumber () {
930                 return this.landLineNumber;
931         }
932
933         /**
934          * Setter for land-line number
935          * <p>
936          * @param landLineNumber Land-line number
937          */
938         public void setLandLineNumber (final Long landLineNumber) {
939                 this.landLineNumber = landLineNumber;
940         }
941
942         /**
943          * Getter for mobile id
944          * <p>
945          * @return Mobile id
946          */
947         public Long getMobileId () {
948                 return this.mobileId;
949         }
950
951         /**
952          * Setter for mobile id
953          * <p>
954          * @param mobileId Mobile id
955          */
956         public void setMobileId (final Long mobileId) {
957                 this.mobileId = mobileId;
958         }
959
960         /**
961          * Getter for mobile number
962          * <p>
963          * @return Mobile number
964          */
965         public Long getMobileNumber () {
966                 return this.mobileNumber;
967         }
968
969         /**
970          * Setter for mobile number
971          * <p>
972          * @param mobileNumber Mobile number
973          */
974         public void setMobileNumber (final Long mobileNumber) {
975                 this.mobileNumber = mobileNumber;
976         }
977
978         /**
979          * Getter for mobile number's carrier
980          * <p>
981          * @return Mobile number's carrier
982          */
983         public MobileProvider getMobileProvider () {
984                 return this.mobileProvider;
985         }
986
987         /**
988          * Setter for mobile number's carrier prefix
989          * <p>
990          * @param mobileProvider Mobile number's carrier prefix
991          */
992         public void setMobileProvider (final MobileProvider mobileProvider) {
993                 this.mobileProvider = mobileProvider;
994         }
995
996         /**
997          * Getter for personal title
998          * <p>
999          * @return Personal title
1000          */
1001         public PersonalTitle getPersonalTitle () {
1002                 return this.personalTitle;
1003         }
1004
1005         /**
1006          * Setter for personal title
1007          * <p>
1008          * @param personalTitle Personal title
1009          */
1010         public void setPersonalTitle (final PersonalTitle personalTitle) {
1011                 this.personalTitle = personalTitle;
1012         }
1013
1014         /**
1015          * Getter for street name
1016          * <p>
1017          * @return Street name
1018          */
1019         public String getStreet () {
1020                 return this.street;
1021         }
1022
1023         /**
1024          * Setter for street name
1025          * <p>
1026          * @param street Street name
1027          */
1028         public void setStreet (final String street) {
1029                 this.street = street;
1030         }
1031
1032         /**
1033          * Getter for ZIP code
1034          * <p>
1035          * @return ZIP code
1036          */
1037         public Integer getZipCode () {
1038                 return this.zipCode;
1039         }
1040
1041         /**
1042          * Setter for ZIP code
1043          * <p>
1044          * @param zipCode ZIP code
1045          */
1046         public void setZipCode (final Integer zipCode) {
1047                 this.zipCode = zipCode;
1048         }
1049
1050         /**
1051          * Post-initialization of this class
1052          */
1053         @PostConstruct
1054         public void init () {
1055                 // Try it
1056                 try {
1057                         // Get initial context
1058                         Context context = new InitialContext();
1059
1060                         // Try to lookup
1061                         this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/addressbook-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
1062
1063                         // Try to lookup (administative)
1064                         this.adminContactBean = (AdminContactSessionBeanRemote) context.lookup("java:global/addressbook-ejb/adminContact!org.mxchange.jcontacts.contact.AdminContactSessionBeanRemote"); //NOI18N
1065                 } catch (final NamingException e) {
1066                         // Throw again
1067                         throw new FaceletException(e);
1068                 }
1069         }
1070
1071         @Override
1072         @Deprecated
1073         public boolean isPersonalTitleRequired () {
1074                 // Get context parameter
1075                 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_admin_personal_title_enabled"); //NOI18N
1076
1077                 // Is it set?
1078                 boolean isRequired = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N
1079
1080                 // Return value
1081                 return isRequired;
1082         }
1083
1084         @Override
1085         public void validateContactData () {
1086                 if (this.getPersonalTitle() == null) {
1087                         // Throw NPE again
1088                         throw new NullPointerException("contactController.gender is null"); //NOI18N
1089                 } else if (this.getFirstName() == null) {
1090                         // ... and again
1091                         throw new NullPointerException("contactController.firstName is null"); //NOI18N
1092                 } else if (this.getFirstName().isEmpty()) {
1093                         // ... and again
1094                         throw new IllegalArgumentException("contactController.firstName is empty"); //NOI18N
1095                 } else if (this.getFamilyName() == null) {
1096                         // ... and again
1097                         throw new NullPointerException("contactController.familyName is null"); //NOI18N
1098                 } else if (this.getFamilyName().isEmpty()) {
1099                         // ... and again
1100                         throw new IllegalArgumentException("contactController.familyName is empty"); //NOI18N
1101                 } else if (this.getEmailAddress() == null) {
1102                         // ... and again
1103                         throw new NullPointerException("contactController.emailAddress is null"); //NOI18N
1104                 } else if (this.getEmailAddress().isEmpty()) {
1105                         // ... and again
1106                         throw new IllegalArgumentException("contactController.emailAddress is empty"); //NOI18N
1107                 }
1108         }
1109
1110         /**
1111          * Clears this bean
1112          */
1113         private void clear () {
1114                 // Clear all data
1115                 // - personal data
1116                 this.setAcademicTitle(null);
1117                 this.setFirstName(null);
1118                 this.setFamilyName(null);
1119                 this.setStreet(null);
1120                 this.setHouseNumber(null);
1121                 this.setHouseNumberExtension(null);
1122                 this.setZipCode(null);
1123                 this.setCity(null);
1124                 this.setContactCountry(null);
1125
1126                 // - contact data
1127                 this.setEmailAddress(null);
1128                 this.setLandLineCountry(null);
1129                 this.setLandLineAreaCode(null);
1130                 this.setLandLineNumber(null);
1131                 this.setMobileProvider(null);
1132                 this.setMobileNumber(null);
1133                 this.setFaxCountry(null);
1134                 this.setFaxAreaCode(null);
1135                 this.setFaxNumber(null);
1136
1137                 // - other data
1138                 this.setBirthday(null);
1139                 this.setComment(null);
1140         }
1141
1142         /**
1143          * Checks whether the given contact is found
1144          * <p>
1145          * @param contact Contact inastance
1146          *
1147          * @return Wether contact has been found
1148          */
1149         private boolean isSameContactFound (final Contact contact) {
1150                 // Default is not found
1151                 boolean IsFound = false;
1152
1153                 // Get iterator
1154                 Iterator<Contact> iterator = this.contactController.allContacts().iterator();
1155
1156                 // Loop through all
1157                 while (iterator.hasNext()) {
1158                         // Get next contact
1159                         Contact next = iterator.next();
1160
1161                         // Is the same?
1162                         if (ContactUtils.isSameContact(contact, next)) {
1163                                 // Yes, then abort loop
1164                                 IsFound = false;
1165                                 break;
1166                         }
1167                 }
1168
1169                 // Return status
1170                 return IsFound;
1171         }
1172
1173         /**
1174          * Updates all data in contact instance.
1175          * <p>
1176          * @param contact Contact instance
1177          */
1178         private void updateContactData (final Contact contact) {
1179                 // Contact instance should be valid
1180                 if (null == contact) {
1181                         // Throw NPE
1182                         throw new NullPointerException("contact is null"); //NOI18N
1183                 } else if (contact.getContactId() == null) {
1184                         // Throw NPE again
1185                         throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
1186                 } else if (contact.getContactId() < 1) {
1187                         // Invalid id
1188                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
1189                 }
1190
1191                 // Update all fields
1192                 contact.setContactPersonalTitle(this.getPersonalTitle());
1193                 contact.setContactTitle(this.getAcademicTitle());
1194                 contact.setContactFirstName(this.getFirstName());
1195                 contact.setContactFamilyName(this.getFamilyName());
1196                 contact.setContactStreet(this.getStreet());
1197                 contact.setContactHouseNumber(this.getHouseNumber());
1198                 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
1199                 contact.setContactZipCode(this.getZipCode());
1200                 contact.setContactCity(this.getCity());
1201                 contact.setContactCountry(this.getContactCountry());
1202
1203                 // Update contact's cmobile number
1204                 this.isMobileNumberUnlinked = ContactUtils.updateMobileNumber(contact, this.getMobileProvider(), this.getMobileNumber());
1205
1206                 // Update contact's land-line number
1207                 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
1208
1209                 // Update contact's fax number
1210                 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
1211         }
1212
1213 }