2 * Copyright (C) 2016 Roland Häder
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.pizzaapplication.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.inject.Any;
26 import javax.faces.context.FacesContext;
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.update.AdminUpdatedContactEvent;
41 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
42 import org.mxchange.jcountry.data.Country;
43 import org.mxchange.jphone.phonenumbers.DialableNumber;
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.mobile.DialableMobileNumber;
49 import org.mxchange.jphone.phonenumbers.mobile.MobileNumber;
50 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
51 import org.mxchange.pizzaapplication.beans.BasePizzaController;
52 import org.mxchange.pizzaapplication.beans.helper.PizzaWebRequestController;
53 import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent;
54 import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent;
57 * Administrative user bean (controller)
59 * @author Roland Häder<roland@mxchange.org>
61 @Named ("adminContactController")
63 public class PizzaAdminContactWebRequestBean extends BasePizzaController implements PizzaAdminContactWebRequestController {
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<ObservableAdminAddedContactEvent> addedContactEvent;
78 * Administrative contact EJB
80 private AdminContactSessionBeanRemote adminContactBean;
83 * Bean helper instance
86 private PizzaWebRequestController beanHelper;
91 private Date birthday;
94 * Cellphone number's carrier
96 private MobileProvider cellphoneCarrier;
101 private Long cellphoneId;
106 private Long cellphoneNumber;
116 private String comment;
119 * Remote contact bean
121 private final ContactSessionBeanRemote contactBean;
124 * General contact controller
127 private PizzaContactWebSessionController contactController;
132 private Long contactId;
137 private Country country;
142 private String emailAddress;
147 private String familyName;
150 * Fax number's area code
152 private Integer faxAreaCode;
155 * Country instance for fax number
157 private Country faxCountry;
167 private Long faxNumber;
172 private String firstName;
177 private Gender gender;
182 private Short houseNumber;
185 * House number extension
187 private String houseNumberExtension;
190 * Whether a cellphone entry has been unlinked
192 private boolean isCellphoneUnlinked;
195 * Whether a fax entry has been unlinked
197 private boolean isFaxUnlinked;
200 * Whether a land-line number has been unlinked
202 private boolean isLandLineUnlinked;
205 * Land-line id number
207 private Long landLineId;
210 * Phone number area code
212 private Integer phoneAreaCode;
215 * Country instance for phone number
217 private Country phoneCountry;
222 private Long phoneNumber;
227 private String street;
232 private String title;
235 * An event fired when the administrator has updated contact data
239 private Event<ObservableAdminUpdatedContactEvent> updatedContactEvent;
244 private Integer zipCode;
247 * Default constructor
249 public PizzaAdminContactWebRequestBean () {
252 // Get initial context
253 Context context = new InitialContext();
256 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
258 // Try to lookup (administative)
259 this.adminContactBean = (AdminContactSessionBeanRemote) context.lookup("java:global/jjobs-ejb/adminContact!org.mxchange.jcontacts.contact.AdminContactSessionBeanRemote"); //NOI18N
260 } catch (final NamingException e) {
262 throw new FaceletException(e);
267 public String addContact () {
268 // Are all minimum fields set?
269 if (this.getGender() == null) {
271 throw new NullPointerException("gender is null"); //NOI18N
272 } else if (this.getFirstName() == null) {
274 throw new NullPointerException("firstName is null"); //NOI18N
275 } else if (this.getFirstName().isEmpty()) {
277 throw new IllegalStateException("firstName is empty"); //NOI18N
278 } else if (this.getFamilyName() == null) {
280 throw new NullPointerException("familyName is null"); //NOI18N
281 } else if (this.getFamilyName().isEmpty()) {
283 throw new IllegalStateException("familyName is empty"); //NOI18N
286 // Create new contact instance
287 Contact contact = this.createContactInstance();
289 // Default is not same contact
290 if (this.isSameContactFound(contact)) {
291 // Already registered
292 throw new FaceletException(new ContactAlreadyAddedException(contact));
296 Contact updatedContact;
301 updatedContact = this.adminContactBean.addContact(contact);
302 } catch (final ContactAlreadyAddedException ex) {
304 throw new FaceletException(ex);
308 this.addedContactEvent.fire(new AdminAddedContactEvent(updatedContact));
314 return "admin_list_contact"; //NOI18N
318 public void copyContactToController (final Contact contact) {
320 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("AdminContactController::copyContactToController(): contact={0} - CALLED!", contact)); //NOI18N
322 // The contact instance must be valid
323 if (null == contact) {
325 throw new NullPointerException("contact is null"); //NOI18N
326 } else if (contact.getContactId() == null) {
328 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
329 } else if (contact.getContactId() < 1) {
331 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
334 // Set all fields: contact
335 this.setContactId(contact.getContactId());
336 this.setTitle(contact.getContactTitle());
337 this.setBirthday(contact.getContactBirthday());
338 this.setCity(contact.getContactCity());
339 this.setComment(contact.getContactComment());
340 this.setCountry(contact.getContactCountry());
341 this.setEmailAddress(contact.getContactEmailAddress());
342 this.setFamilyName(contact.getContactFamilyName());
343 this.setFirstName(contact.getContactFirstName());
344 this.setGender(contact.getContactGender());
345 this.setHouseNumber(contact.getContactHouseNumber());
346 this.setHouseNumberExtension(contact.getContactHouseNumberExtension());
347 this.setStreet(contact.getContactStreet());
348 this.setZipCode(contact.getContactZipCode());
350 // Is the cell phone set?
351 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
352 // ... cellphone data
353 this.setCellphoneId(contact.getContactMobileNumber().getPhoneId());
354 this.setCellphoneCarrier(contact.getContactMobileNumber().getMobileProvider());
355 this.setCellphoneNumber(contact.getContactMobileNumber().getPhoneNumber());
359 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
361 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
362 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
363 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
364 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
367 // Is the land-line number set?
368 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
370 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
371 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
372 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
373 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
377 //* NOISY-DEBUG: */ System.out.println("AdminContactController::copyContactToController(): EXIT!"); //NOI18N
381 public Contact createContactInstance () {
382 // Are all minimum fields set?
383 if (this.getGender() == null) {
385 throw new NullPointerException("gender is null"); //NOI18N
386 } else if (this.getFirstName() == null) {
388 throw new NullPointerException("firstName is null"); //NOI18N
389 } else if (this.getFirstName().isEmpty()) {
391 throw new IllegalStateException("firstName is empty"); //NOI18N
392 } else if (this.getFamilyName() == null) {
394 throw new NullPointerException("familyName is null"); //NOI18N
395 } else if (this.getFamilyName().isEmpty()) {
397 throw new IllegalStateException("familyName is empty"); //NOI18N
400 // Generate phone number
401 DialableLandLineNumber landLineNumber = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
402 DialableMobileNumber mobileNumber = new MobileNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
403 DialableFaxNumber faxNumber = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
405 // Create new instance
406 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
409 contact.setContactBirthday(this.getBirthday());
410 contact.setContactStreet(this.getStreet());
411 contact.setContactHouseNumber(this.getHouseNumber());
412 contact.setContactZipCode(this.getZipCode());
413 contact.setContactCity(this.getCity());
414 contact.setContactCountry(this.getCountry());
415 contact.setContactEmailAddress(this.getEmailAddress());
416 contact.setContactBirthday(this.getBirthday());
417 contact.setContactComment(this.getComment());
420 contact.setContactOwnContact(Boolean.TRUE);
422 // Don't set null or wrong references
423 if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
424 // Now the number must be given
425 if (landLineNumber.getPhoneAreaCode() == null) {
427 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
428 } else if (landLineNumber.getPhoneAreaCode() < 1) {
430 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
431 } else if (landLineNumber.getPhoneNumber() == null) {
433 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
434 } else if (landLineNumber.getPhoneNumber() < 1) {
436 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
440 contact.setContactLandLineNumber(landLineNumber);
443 // Don't set null or wrong references
444 if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
445 // Now the number must be given
446 if (faxNumber.getPhoneAreaCode() == null) {
448 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
449 } else if (faxNumber.getPhoneAreaCode() < 1) {
451 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
452 } else if (faxNumber.getPhoneNumber() == null) {
454 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
455 } else if (faxNumber.getPhoneNumber() < 1) {
457 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
461 contact.setContactFaxNumber(faxNumber);
464 // Is the provider set?
465 if ((mobileNumber instanceof DialableMobileNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
466 // Is the number set?
467 if (mobileNumber.getPhoneNumber() == null) {
469 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
470 } else if (mobileNumber.getPhoneNumber() < 1) {
472 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
475 // Set cellphone number
476 contact.setContactMobileNumber(mobileNumber);
484 public String editContactData () {
485 // Get contact instance
486 Contact contact = this.beanHelper.getContact();
488 // Check if contact instance is in helper and valid
489 if (null == contact) {
491 throw new NullPointerException("beanHelper.contact is null"); //NOI18N
492 } else if (contact.getContactId() == null) {
494 throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
495 } else if (contact.getContactId() < 1) {
497 throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
500 // Update all data in contact
501 this.updateContactData(contact);
503 // Call EJB for updating contact data
504 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
507 this.updatedContactEvent.fire(new AdminUpdatedContactEvent(updatedContact));
512 // Return to contact list (for now)
513 return "admin_list_contact"; //NOI18N
517 public String generateCellphoneNumber (final DialableMobileNumber cellphoneNumber) {
519 if (null == cellphoneNumber) {
525 String number = String.format(
527 cellphoneNumber.getMobileProvider().getProviderCountry().getCountryExternalDialPrefix(),
528 cellphoneNumber.getMobileProvider().getProviderDialPrefix(),
529 cellphoneNumber.getPhoneNumber()
537 public String generatePhoneNumber (final DialableNumber phoneNumber) {
539 if (null == phoneNumber) {
545 String number = String.format(
547 phoneNumber.getPhoneCountry().getCountryExternalDialPrefix(),
548 phoneNumber.getPhoneAreaCode(),
549 phoneNumber.getPhoneNumber()
557 @SuppressWarnings ("ReturnOfDateField")
558 public Date getBirthday () {
559 return this.birthday;
563 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
564 public void setBirthday (final Date birthday) {
565 this.birthday = birthday;
569 public MobileProvider getCellphoneCarrier () {
570 return this.cellphoneCarrier;
574 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
575 this.cellphoneCarrier = cellphoneCarrier;
579 public Long getCellphoneId () {
580 return this.cellphoneId;
584 public void setCellphoneId (final Long cellphoneId) {
585 this.cellphoneId = cellphoneId;
589 public Long getCellphoneNumber () {
590 return this.cellphoneNumber;
594 public void setCellphoneNumber (Long cellphoneNumber) {
595 this.cellphoneNumber = cellphoneNumber;
599 public String getCity () {
604 public void setCity (final String city) {
609 public String getComment () {
614 public void setComment (final String comment) {
615 this.comment = comment;
619 public Long getContactId () {
620 return this.contactId;
624 public void setContactId (final Long contactId) {
625 this.contactId = contactId;
629 public String getControllerType () {
630 return "admin"; //NOI18N
635 public void setControllerType (final String controllerType) {
636 throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
640 public Country getCountry () {
645 public void setCountry (final Country country) {
646 this.country = country;
650 public String getEmailAddress () {
651 return this.emailAddress;
655 public void setEmailAddress (final String emailAddress) {
656 this.emailAddress = emailAddress;
660 public String getFamilyName () {
661 return this.familyName;
665 public void setFamilyName (final String familyName) {
666 this.familyName = familyName;
670 public Integer getFaxAreaCode () {
671 return this.faxAreaCode;
675 public void setFaxAreaCode (final Integer faxAreaCode) {
676 this.faxAreaCode = faxAreaCode;
680 public Country getFaxCountry () {
681 return this.faxCountry;
685 public void setFaxCountry (final Country faxCountry) {
686 this.faxCountry = faxCountry;
690 public Long getFaxId () {
695 public void setFaxId (final Long faxId) {
700 public Long getFaxNumber () {
701 return this.faxNumber;
705 public void setFaxNumber (final Long faxNumber) {
706 this.faxNumber = faxNumber;
710 public String getFirstName () {
711 return this.firstName;
715 public void setFirstName (final String firstName) {
716 this.firstName = firstName;
720 public Gender getGender () {
725 public void setGender (final Gender gender) {
726 this.gender = gender;
730 public Short getHouseNumber () {
731 return this.houseNumber;
735 public void setHouseNumber (final Short houseNumber) {
736 this.houseNumber = houseNumber;
740 public String getHouseNumberExtension () {
741 return this.houseNumberExtension;
745 public void setHouseNumberExtension (final String houseNumberExtension) {
746 this.houseNumberExtension = houseNumberExtension;
750 public Long getLandLineId () {
751 return this.landLineId;
755 public void setLandLineId (final Long landLineId) {
756 this.landLineId = landLineId;
760 public Integer getPhoneAreaCode () {
761 return this.phoneAreaCode;
765 public void setPhoneAreaCode (final Integer phoneAreaCode) {
766 this.phoneAreaCode = phoneAreaCode;
770 public Country getPhoneCountry () {
771 return this.phoneCountry;
775 public void setPhoneCountry (final Country phoneCountry) {
776 this.phoneCountry = phoneCountry;
780 public Long getPhoneNumber () {
781 return this.phoneNumber;
785 public void setPhoneNumber (final Long phoneNumber) {
786 this.phoneNumber = phoneNumber;
790 public String getStreet () {
795 public void setStreet (final String street) {
796 this.street = street;
800 public String getTitle () {
805 public void setTitle (final String title) {
810 public Integer getZipCode () {
815 public void setZipCode (final Integer zipCode) {
816 this.zipCode = zipCode;
820 * Post-initialization of this class
823 public void init () {
827 public boolean isGenderRequired () {
828 // Get context parameter
829 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_admin_gender_enabled"); //NOI18N
832 boolean isRequired = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N
841 private void clear () {
845 this.setFirstName(null);
846 this.setFamilyName(null);
847 this.setStreet(null);
848 this.setHouseNumber(null);
849 this.setHouseNumberExtension(null);
850 this.setZipCode(null);
852 this.setCountry(null);
855 this.setEmailAddress(null);
856 this.setPhoneCountry(null);
857 this.setPhoneAreaCode(null);
858 this.setPhoneNumber(null);
859 this.setCellphoneCarrier(null);
860 this.setCellphoneNumber(null);
861 this.setFaxCountry(null);
862 this.setFaxAreaCode(null);
863 this.setFaxNumber(null);
866 this.setBirthday(null);
867 this.setComment(null);
871 * Checks whether the given contact is found
873 * @param contact Contact inastance
875 * @return Wether contact has been found
877 private boolean isSameContactFound (final Contact contact) {
878 // Default is not found
879 boolean IsFound = false;
882 Iterator<Contact> iterator = this.contactController.allContacts().iterator();
885 while (iterator.hasNext()) {
887 Contact next = iterator.next();
890 if (ContactUtils.isSameContact(contact, next)) {
891 // Yes, then abort loop
902 * Updates all data in contact instance.
904 * @param contact Contact instance
906 private void updateContactData (final Contact contact) {
907 // Contact instance should be valid
908 if (null == contact) {
910 throw new NullPointerException("contact is null"); //NOI18N
911 } else if (contact.getContactId() == null) {
913 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
914 } else if (contact.getContactId() < 1) {
916 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
920 contact.setContactGender(this.getGender());
921 contact.setContactTitle(this.getTitle());
922 contact.setContactFirstName(this.getFirstName());
923 contact.setContactFamilyName(this.getFamilyName());
924 contact.setContactStreet(this.getStreet());
925 contact.setContactHouseNumber(this.getHouseNumber());
926 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
927 contact.setContactZipCode(this.getZipCode());
928 contact.setContactCity(this.getCity());
929 contact.setContactCountry(this.getCountry());
931 // Update contact's cellphone number
932 this.isCellphoneUnlinked = ContactUtils.updateMobileNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
934 // Update contact's land-line number
935 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
937 // Update contact's fax number
938 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());