2 * Copyright (C) 2016 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.jjobs.beans.contact;
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.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.AdminContactSessionBeanRemote;
34 import org.mxchange.jcontacts.contact.Contact;
35 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
36 import org.mxchange.jcontacts.contact.UserContact;
37 import org.mxchange.jcontacts.contact.gender.Gender;
38 import org.mxchange.jcontacts.contact.utils.ContactUtils;
39 import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent;
40 import org.mxchange.jcontacts.events.contact.add.AdminContactAddedEvent;
41 import org.mxchange.jcontacts.events.contact.deleted.AdminContactDeletedEvent;
42 import org.mxchange.jcontacts.events.contact.deleted.AdminDeletedContactEvent;
43 import org.mxchange.jcontacts.events.contact.update.AdminContactUpdatedEvent;
44 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
45 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
46 import org.mxchange.jcountry.data.Country;
47 import org.mxchange.jjobs.beans.BaseJobsController;
48 import org.mxchange.jjobs.beans.helper.JobsWebViewHelperController;
49 import org.mxchange.jphone.phonenumbers.DialableNumber;
50 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
51 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
52 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
53 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
54 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
57 * An administrative user controller (bean)
59 * @author Roland Haeder<roland@mxchange.org>
61 @Named ("adminContactController")
63 public class JobsAdminContactWebRequestBean extends BaseJobsController implements JobsAdminContactWebRequestController {
68 private static final long serialVersionUID = 542_145_347_916L;
71 * An event fired when the administrator has added a new contact
75 private Event<AdminAddedContactEvent> addedContactEvent;
78 * Administrative contact EJB
80 private AdminContactSessionBeanRemote adminContactBean;
86 private JobsWebViewHelperController beanHelper;
91 private Date birthday;
101 private String comment;
104 * General contact EJB
106 private final ContactSessionBeanRemote contactBean;
109 * General contact controller
112 private JobsContactWebSessionController contactController;
117 private Long contactId;
122 private Country country;
125 * Event being fired when contact data has been deleted
129 private Event<AdminDeletedContactEvent> deletedContactEvent;
134 private String emailAddress;
139 private String familyName;
142 * Fax number's area code
144 private Integer faxAreaCode;
147 * Country instance for fax number
149 private Country faxCountry;
154 private Long faxNumber;
159 private Long faxNumberId;
164 private String firstName;
169 private Gender gender;
174 private Short houseNumber;
177 * House number extension
179 private String houseNumberExtension;
182 * Whether a fax entry has been unlinked
184 private boolean isFaxUnlinked;
187 * Whether a land-line number has been unlinked
189 private boolean isLandLineUnlinked;
192 * Whether a mobile entry has been unlinked
194 private boolean isMobileUnlinked;
197 * Land-line number area code
199 private Integer landLineAreaCode;
202 * Country instance for land-line number
204 private Country landLineCountry;
207 * Land-line id number
209 private Long landLineId;
214 private Long landLineNumber;
219 private Long mobileNumber;
224 private Long mobileNumberId;
229 private MobileProvider mobileProvider;
234 private String street;
239 private String title;
242 * An event fired when the administrator has updated contact data
246 private Event<AdminUpdatedContactEvent> updatedContactEvent;
251 private Integer zipCode;
254 * Default constructor
256 public JobsAdminContactWebRequestBean () {
259 // Get initial context
260 Context context = new InitialContext();
262 // Try to lookup (general)
263 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jjobs-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
265 // Try to lookup (administative)
266 this.adminContactBean = (AdminContactSessionBeanRemote) context.lookup("java:global/jjobs-ejb/adminContact!org.mxchange.jcontacts.contact.AdminContactSessionBeanRemote"); //NOI18N
267 } catch (final NamingException e) {
269 throw new FaceletException(e);
274 public String addContact () {
275 // Are all minimum fields set?
276 if (this.getGender() == null) {
278 throw new NullPointerException("gender is null"); //NOI18N
279 } else if (this.getFirstName() == null) {
281 throw new NullPointerException("firstName is null"); //NOI18N
282 } else if (this.getFirstName().isEmpty()) {
284 throw new IllegalStateException("firstName is empty"); //NOI18N
285 } else if (this.getFamilyName() == null) {
287 throw new NullPointerException("familyName is null"); //NOI18N
288 } else if (this.getFamilyName().isEmpty()) {
290 throw new IllegalStateException("familyName is empty"); //NOI18N
293 // Create new contact instance
294 Contact contact = this.createContactInstance();
296 // Default is not same contact
297 if (this.isSameContactFound(contact)) {
298 // Already registered
299 throw new FaceletException(new ContactAlreadyAddedException(contact));
303 Contact updatedContact;
308 updatedContact = this.adminContactBean.addContact(contact);
309 } catch (final ContactAlreadyAddedException ex) {
311 throw new FaceletException(ex);
315 this.addedContactEvent.fire(new AdminContactAddedEvent(updatedContact));
321 return "admin_list_contact"; //NOI18N
325 public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
326 // event should not be null
329 throw new NullPointerException("event is null"); //NOI18N
330 } else if (event.getAddedUser() == null) {
332 throw new NullPointerException("event.addedUser is null"); //NOI18N
333 } else if (event.getAddedUser().getUserId() == null) {
335 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
336 } else if (event.getAddedUser().getUserId() < 1) {
338 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
346 public void copyContactToController (final Contact contact) {
347 // The contact instance must be valid
348 if (null == contact) {
350 throw new NullPointerException("contact is null"); //NOI18N
351 } else if (contact.getContactId() == null) {
353 throw new NullPointerException("contact.contactId is null"); //NOI18N
354 } else if (contact.getContactId() < 1) {
356 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
359 // Set all fields: contact
360 this.setContactId(contact.getContactId());
361 this.setTitle(contact.getContactTitle());
362 this.setBirthday(contact.getContactBirthday());
363 this.setCity(contact.getContactCity());
364 this.setComment(contact.getContactComment());
365 this.setCountry(contact.getContactCountry());
366 this.setEmailAddress(contact.getContactEmailAddress());
367 this.setFamilyName(contact.getContactFamilyName());
368 this.setFirstName(contact.getContactFirstName());
369 this.setGender(contact.getContactGender());
370 this.setHouseNumber(contact.getContactHouseNumber());
371 this.setHouseNumberExtension(contact.getContactHouseNumberExtension());
372 this.setStreet(contact.getContactStreet());
373 this.setZipCode(contact.getContactZipCode());
376 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
377 this.setMobileNumberId(contact.getContactMobileNumber().getPhoneId());
378 this.setMobileProvider(contact.getContactMobileNumber().getMobileProvider());
379 this.setMobileNumber(contact.getContactMobileNumber().getPhoneNumber());
383 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
384 this.setFaxNumberId(contact.getContactFaxNumber().getPhoneId());
385 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
386 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
387 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
391 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
392 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
393 this.setLandLineAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
394 this.setLandLineCountry(contact.getContactLandLineNumber().getPhoneCountry());
395 this.setLandLineNumber(contact.getContactLandLineNumber().getPhoneNumber());
400 public Contact createContactInstance () {
401 // Create new instance
402 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
404 // Update all data in contact
405 this.updateContactData(contact);
412 public String deleteContactData (final Contact contact) {
413 // The instance must be valid
414 if (null == contact) {
416 throw new NullPointerException("contact is null"); //NOI18N
417 } else if (contact.getContactId() == null) {
419 throw new NullPointerException("contact.contactId is null"); //NOI18N
420 } else if (contact.getContactId() < 1) {
422 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
426 this.adminContactBean.deleteContactData(contact);
429 this.deletedContactEvent.fire(new AdminContactDeletedEvent(contact));
432 return "admin_list_contact"; //NOI18N
436 public String editContactData () {
437 // Get contact instance
438 Contact contact = this.beanHelper.getContact();
440 // Check if contact instance is in helper and valid
441 if (null == contact) {
443 throw new NullPointerException("beanHelper.contact is null"); //NOI18N
444 } else if (contact.getContactId() == null) {
446 throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N
447 } else if (contact.getContactId() < 1) {
449 throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
452 // Update all data in contact
453 this.updateContactData(contact);
455 // Call EJB for updating contact data
456 Contact updatedContact = this.contactBean.updateContactData(contact, this.isMobileUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
459 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
464 // Return to contact list (for now)
465 return "admin_list_contact"; //NOI18N
469 public String generateMobileNumber (final DialableMobileNumber mobileNumber) {
471 if (null == mobileNumber) {
477 String number = String.format(
479 mobileNumber.getMobileProvider().getProviderCountry().getCountryExternalDialPrefix(),
480 mobileNumber.getMobileProvider().getProviderDialPrefix(),
481 mobileNumber.getPhoneNumber()
489 public String generatePhoneNumber (final DialableNumber phoneNumber) {
491 if (null == phoneNumber) {
497 String number = String.format(
499 phoneNumber.getPhoneCountry().getCountryExternalDialPrefix(),
500 phoneNumber.getPhoneAreaCode(),
501 phoneNumber.getPhoneNumber()
509 @SuppressWarnings ("ReturnOfDateField")
510 public Date getBirthday () {
511 return this.birthday;
515 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
516 public void setBirthday (final Date birthday) {
517 this.birthday = birthday;
521 public String getCity () {
526 public void setCity (final String city) {
531 public String getComment () {
536 public void setComment (final String comment) {
537 this.comment = comment;
541 public Long getContactId () {
542 return this.contactId;
546 public void setContactId (final Long contactId) {
547 this.contactId = contactId;
551 public String getControllerType () {
552 return "admin"; //NOI18N
557 public void setControllerType (final String controllerType) {
558 throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
562 public Country getCountry () {
567 public void setCountry (final Country country) {
568 this.country = country;
572 public String getEmailAddress () {
573 return this.emailAddress;
577 public void setEmailAddress (final String emailAddress) {
578 this.emailAddress = emailAddress;
582 public String getFamilyName () {
583 return this.familyName;
587 public void setFamilyName (final String familyName) {
588 this.familyName = familyName;
592 public Integer getFaxAreaCode () {
593 return this.faxAreaCode;
597 public void setFaxAreaCode (final Integer faxAreaCode) {
598 this.faxAreaCode = faxAreaCode;
602 public Country getFaxCountry () {
603 return this.faxCountry;
607 public void setFaxCountry (final Country faxCountry) {
608 this.faxCountry = faxCountry;
612 public Long getFaxNumber () {
613 return this.faxNumber;
617 public void setFaxNumber (final Long faxNumber) {
618 this.faxNumber = faxNumber;
622 public Long getFaxNumberId () {
623 return this.faxNumberId;
627 public void setFaxNumberId (final Long faxNumberId) {
628 this.faxNumberId = faxNumberId;
632 public String getFirstName () {
633 return this.firstName;
637 public void setFirstName (final String firstName) {
638 this.firstName = firstName;
642 public Gender getGender () {
647 public void setGender (final Gender gender) {
648 this.gender = gender;
652 public Short getHouseNumber () {
653 return this.houseNumber;
657 public void setHouseNumber (final Short houseNumber) {
658 this.houseNumber = houseNumber;
662 public String getHouseNumberExtension () {
663 return this.houseNumberExtension;
667 public void setHouseNumberExtension (final String houseNumberExtension) {
668 this.houseNumberExtension = houseNumberExtension;
672 public Integer getLandLineAreaCode () {
673 return this.landLineAreaCode;
677 public void setLandLineAreaCode (final Integer landLineAreaCode) {
678 this.landLineAreaCode = landLineAreaCode;
682 public Country getLandLineCountry () {
683 return this.landLineCountry;
687 public void setLandLineCountry (final Country landLineCountry) {
688 this.landLineCountry = landLineCountry;
692 public Long getLandLineId () {
693 return this.landLineId;
697 public void setLandLineId (final Long landLineId) {
698 this.landLineId = landLineId;
702 public Long getLandLineNumber () {
703 return this.landLineNumber;
707 public void setLandLineNumber (final Long landLineNumber) {
708 this.landLineNumber = landLineNumber;
712 public Long getMobileNumber () {
713 return this.mobileNumber;
717 public void setMobileNumber (Long mobileNumber) {
718 this.mobileNumber = mobileNumber;
722 public Long getMobileNumberId () {
723 return this.mobileNumberId;
727 public void setMobileNumberId (final Long mobileNumberId) {
728 this.mobileNumberId = mobileNumberId;
732 public MobileProvider getMobileProvider () {
733 return this.mobileProvider;
737 public void setMobileProvider (final MobileProvider mobileProvider) {
738 this.mobileProvider = mobileProvider;
742 public String getStreet () {
747 public void setStreet (final String street) {
748 this.street = street;
752 public String getTitle () {
757 public void setTitle (final String title) {
762 public Integer getZipCode () {
767 public void setZipCode (final Integer zipCode) {
768 this.zipCode = zipCode;
772 * Post-initialization of this class
775 public void init () {
781 private void clear () {
784 this.setGender(null);
786 this.setFirstName(null);
787 this.setFamilyName(null);
788 this.setStreet(null);
789 this.setHouseNumber(null);
790 this.setHouseNumberExtension(null);
791 this.setZipCode(null);
793 this.setCountry(null);
796 this.setEmailAddress(null);
797 this.setLandLineCountry(null);
798 this.setLandLineAreaCode(null);
799 this.setLandLineNumber(null);
800 this.setMobileProvider(null);
801 this.setMobileNumber(null);
802 this.setFaxCountry(null);
803 this.setFaxAreaCode(null);
804 this.setFaxNumber(null);
807 this.setBirthday(null);
808 this.setComment(null);
812 * Checks whether the given contact is found
814 * @param contact Contact inastance
816 * @return Wether contact has been found
818 private boolean isSameContactFound (final Contact contact) {
819 // Default is not found
820 boolean IsFound = false;
823 Iterator<Contact> iterator = this.contactController.allContacts().iterator();
826 while (iterator.hasNext()) {
828 Contact next = iterator.next();
831 if (ContactUtils.isSameContact(contact, next)) {
832 // Yes, then abort loop
843 * Updates all data in contact instance.
845 * @param contact Contact instance
847 private void updateContactData (final Contact contact) {
848 // Contact instance should be valid
849 if (null == contact) {
851 throw new NullPointerException("contact is null"); //NOI18N
855 contact.setContactGender(this.getGender());
856 contact.setContactTitle(this.getTitle());
857 contact.setContactFirstName(this.getFirstName());
858 contact.setContactFamilyName(this.getFamilyName());
859 contact.setContactStreet(this.getStreet());
860 contact.setContactHouseNumber(this.getHouseNumber());
861 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
862 contact.setContactZipCode(this.getZipCode());
863 contact.setContactCity(this.getCity());
864 contact.setContactCountry(this.getCountry());
865 contact.setContactEmailAddress(this.getEmailAddress());
866 contact.setContactBirthday(this.getBirthday());
867 contact.setContactComment(this.getComment());
869 // Update contact's mobile number
870 this.isMobileUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getMobileProvider(), this.getMobileNumber());
872 // Update contact's land-line number
873 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
875 // Update contact's fax number
876 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());