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.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.add.AdminAddedContactEvent;
39 import org.mxchange.jcontacts.events.contact.add.AdminContactAddedEvent;
40 import org.mxchange.jcontacts.events.contact.update.AdminContactUpdatedEvent;
41 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
42 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
43 import org.mxchange.jcountry.data.Country;
44 import org.mxchange.jphone.phonenumbers.DialableNumber;
45 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
46 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
47 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
48 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
49 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
50 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
51 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
52 import org.mxchange.pizzaapplication.beans.BasePizzaController;
53 import org.mxchange.pizzaapplication.beans.helper.PizzaWebRequestController;
56 * Administrative user bean (controller)
58 * @author Roland Häder<roland@mxchange.org>
60 @Named ("adminContactController")
62 public class PizzaAdminContactWebRequestBean extends BasePizzaController implements PizzaAdminContactWebRequestController {
67 private static final long serialVersionUID = 542_145_347_916L;
70 * An event fired when the administrator has added a new contact
74 private Event<AdminAddedContactEvent> addedContactEvent;
77 * Admin helper instance
80 private PizzaWebRequestController beanHelper;
85 private Date birthday;
88 * Cellphone number's carrier
90 private MobileProvider cellphoneCarrier;
95 private Long cellphoneId;
100 private Long cellphoneNumber;
110 private String comment;
113 * Remote contact bean
115 private final ContactSessionBeanRemote contactBean;
118 * General contact controller
121 private PizzaContactWebSessionController contactController;
126 private Long contactId;
131 private Country country;
136 private String emailAddress;
141 private String familyName;
144 * Fax number's area code
146 private Integer faxAreaCode;
149 * Country instance for fax number
151 private Country faxCountry;
161 private Long faxNumber;
166 private String firstName;
171 private Gender gender;
176 private Short houseNumber;
179 * House number extension
181 private String houseNumberExtension;
184 * Whether a cellphone entry has been unlinked
186 private boolean isCellphoneUnlinked;
189 * Whether a fax entry has been unlinked
191 private boolean isFaxUnlinked;
194 * Whether a land-line number has been unlinked
196 private boolean isLandLineUnlinked;
199 * Land-line id number
201 private Long landLineId;
204 * Phone number area code
206 private Integer phoneAreaCode;
209 * Country instance for phone number
211 private Country phoneCountry;
216 private Long phoneNumber;
221 private String street;
226 private String title;
229 * An event fired when the administrator has updated contact data
233 private Event<AdminUpdatedContactEvent> updatedContactEvent;
238 private Integer zipCode;
241 * Default constructor
243 public PizzaAdminContactWebRequestBean () {
246 // Get initial context
247 Context context = new InitialContext();
250 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
251 } catch (final NamingException e) {
253 throw new FaceletException(e);
258 public String addContact () {
259 // Create new contact instance
260 Contact contact = this.createContactInstance();
262 // Default is not same contact
263 if (this.isSameContactFound(contact)) {
264 // Already registered
265 throw new FaceletException(new ContactAlreadyAddedException(contact));
269 Contact updatedContact;
274 updatedContact = this.contactBean.addContact(contact);
275 } catch (final ContactAlreadyAddedException ex) {
277 throw new FaceletException(ex);
281 this.addedContactEvent.fire(new AdminContactAddedEvent(updatedContact));
287 return "admin_list_contact"; //NOI18N
291 public void copyContactToController (final Contact contact) {
293 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("AdminContactController::copyContactToController(): contact={0} - CALLED!", contact)); //NOI18N
295 // The contact instance must be valid
296 if (null == contact) {
298 throw new NullPointerException("contact is null"); //NOI18N
299 } else if (contact.getContactId() == null) {
301 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
302 } else if (contact.getContactId() < 1) {
304 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
307 // Set all fields: contact
308 this.setContactId(contact.getContactId());
309 this.setTitle(contact.getContactTitle());
310 this.setBirthday(contact.getContactBirthday());
311 this.setCity(contact.getContactCity());
312 this.setComment(contact.getContactComment());
313 this.setCountry(contact.getContactCountry());
314 this.setEmailAddress(contact.getContactEmailAddress());
315 this.setFamilyName(contact.getContactFamilyName());
316 this.setFirstName(contact.getContactFirstName());
317 this.setGender(contact.getContactGender());
318 this.setHouseNumber(contact.getContactHouseNumber());
319 this.setHouseNumberExtension(contact.getContactHouseNumberExtension());
320 this.setStreet(contact.getContactStreet());
321 this.setZipCode(contact.getContactZipCode());
323 // Is the cell phone set?
324 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
325 // ... cellphone data
326 this.setCellphoneId(contact.getContactMobileNumber().getPhoneId());
327 this.setCellphoneCarrier(contact.getContactMobileNumber().getCellphoneProvider());
328 this.setCellphoneNumber(contact.getContactMobileNumber().getPhoneNumber());
332 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
334 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
335 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
336 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
337 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
340 // Is the land-line number set?
341 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
343 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
344 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
345 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
346 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
350 //* NOISY-DEBUG: */ System.out.println("AdminContactController::copyContactToController(): EXIT!"); //NOI18N
354 public Contact createContactInstance () {
355 // Are all minimum fields set?
356 if (this.getGender() == null) {
358 throw new NullPointerException("gender is null"); //NOI18N
359 } else if (this.getFirstName() == null) {
361 throw new NullPointerException("firstName is null"); //NOI18N
362 } else if (this.getFirstName().isEmpty()) {
364 throw new IllegalStateException("firstName is empty"); //NOI18N
365 } else if (this.getFamilyName() == null) {
367 throw new NullPointerException("familyName is null"); //NOI18N
368 } else if (this.getFamilyName().isEmpty()) {
370 throw new IllegalStateException("familyName is empty"); //NOI18N
373 // Generate phone number
374 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
375 DialableMobileNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
376 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
378 // Create new instance
379 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
382 contact.setContactBirthday(this.getBirthday());
383 contact.setContactStreet(this.getStreet());
384 contact.setContactHouseNumber(this.getHouseNumber());
385 contact.setContactZipCode(this.getZipCode());
386 contact.setContactCity(this.getCity());
387 contact.setContactCountry(this.getCountry());
388 contact.setContactEmailAddress(this.getEmailAddress());
389 contact.setContactBirthday(this.getBirthday());
390 contact.setContactComment(this.getComment());
393 contact.setContactOwnContact(Boolean.TRUE);
395 // Don't set null or wrong references
396 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
397 // Now the number must be given
398 if (phone.getPhoneAreaCode() == null) {
400 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
401 } else if (phone.getPhoneAreaCode() < 1) {
403 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
404 } else if (phone.getPhoneNumber() == null) {
406 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
407 } else if (phone.getPhoneNumber() < 1) {
409 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
413 contact.setContactLandLineNumber(phone);
416 // Don't set null or wrong references
417 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
418 // Now the number must be given
419 if (fax.getPhoneAreaCode() == null) {
421 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
422 } else if (fax.getPhoneAreaCode() < 1) {
424 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
425 } else if (fax.getPhoneNumber() == null) {
427 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
428 } else if (fax.getPhoneNumber() < 1) {
430 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
434 contact.setContactFaxNumber(fax);
437 // Is the provider set?
438 if ((cellphone instanceof DialableMobileNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
439 // Is the number set?
440 if (cellphone.getPhoneNumber() == null) {
442 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
443 } else if (cellphone.getPhoneNumber() < 1) {
445 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
448 // Set cellphone number
449 contact.setContactCellphoneNumber(cellphone);
457 public String editContactData () {
458 // Get contact instance
459 Contact contact = this.beanHelper.getContact();
461 // Check if contact instance is in helper and valid
462 if (null == contact) {
464 throw new NullPointerException("beanHelper.contact is null"); //NOI18N
465 } else if (contact.getContactId() == null) {
467 throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
468 } else if (contact.getContactId() < 1) {
470 throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
473 // Update all data in contact
474 this.updateContactData(contact);
476 // Call EJB for updating contact data
477 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
480 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
485 // Return to contact list (for now)
486 return "admin_list_contact"; //NOI18N
490 public String generateCellphoneNumber (final DialableMobileNumber cellphoneNumber) {
492 if (null == cellphoneNumber) {
498 String number = String.format(
500 cellphoneNumber.getCellphoneProvider().getProviderCountry().getCountryExternalDialPrefix(),
501 cellphoneNumber.getCellphoneProvider().getProviderDialPrefix(),
502 cellphoneNumber.getPhoneNumber()
510 public String generatePhoneNumber (final DialableNumber phoneNumber) {
512 if (null == phoneNumber) {
518 String number = String.format(
520 phoneNumber.getPhoneCountry().getCountryExternalDialPrefix(),
521 phoneNumber.getPhoneAreaCode(),
522 phoneNumber.getPhoneNumber()
530 @SuppressWarnings ("ReturnOfDateField")
531 public Date getBirthday () {
532 return this.birthday;
536 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
537 public void setBirthday (final Date birthday) {
538 this.birthday = birthday;
542 public MobileProvider getCellphoneCarrier () {
543 return this.cellphoneCarrier;
547 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
548 this.cellphoneCarrier = cellphoneCarrier;
552 public Long getCellphoneId () {
553 return this.cellphoneId;
557 public void setCellphoneId (final Long cellphoneId) {
558 this.cellphoneId = cellphoneId;
562 public Long getCellphoneNumber () {
563 return this.cellphoneNumber;
567 public void setCellphoneNumber (Long cellphoneNumber) {
568 this.cellphoneNumber = cellphoneNumber;
572 public String getCity () {
577 public void setCity (final String city) {
582 public String getComment () {
587 public void setComment (final String comment) {
588 this.comment = comment;
592 public Long getContactId () {
593 return this.contactId;
597 public void setContactId (final Long contactId) {
598 this.contactId = contactId;
602 public String getControllerType () {
603 return "admin"; //NOI18N
608 public void setControllerType (final String controllerType) {
609 throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
613 public Country getCountry () {
618 public void setCountry (final Country country) {
619 this.country = country;
623 public String getEmailAddress () {
624 return this.emailAddress;
628 public void setEmailAddress (final String emailAddress) {
629 this.emailAddress = emailAddress;
633 public String getFamilyName () {
634 return this.familyName;
638 public void setFamilyName (final String familyName) {
639 this.familyName = familyName;
643 public Integer getFaxAreaCode () {
644 return this.faxAreaCode;
648 public void setFaxAreaCode (final Integer faxAreaCode) {
649 this.faxAreaCode = faxAreaCode;
653 public Country getFaxCountry () {
654 return this.faxCountry;
658 public void setFaxCountry (final Country faxCountry) {
659 this.faxCountry = faxCountry;
663 public Long getFaxId () {
668 public void setFaxId (final Long faxId) {
673 public Long getFaxNumber () {
674 return this.faxNumber;
678 public void setFaxNumber (final Long faxNumber) {
679 this.faxNumber = faxNumber;
683 public String getFirstName () {
684 return this.firstName;
688 public void setFirstName (final String firstName) {
689 this.firstName = firstName;
693 public Gender getGender () {
698 public void setGender (final Gender gender) {
699 this.gender = gender;
703 public Short getHouseNumber () {
704 return this.houseNumber;
708 public void setHouseNumber (final Short houseNumber) {
709 this.houseNumber = houseNumber;
713 public String getHouseNumberExtension () {
714 return this.houseNumberExtension;
718 public void setHouseNumberExtension (final String houseNumberExtension) {
719 this.houseNumberExtension = houseNumberExtension;
723 public Long getLandLineId () {
724 return this.landLineId;
728 public void setLandLineId (final Long landLineId) {
729 this.landLineId = landLineId;
733 public Integer getPhoneAreaCode () {
734 return this.phoneAreaCode;
738 public void setPhoneAreaCode (final Integer phoneAreaCode) {
739 this.phoneAreaCode = phoneAreaCode;
743 public Country getPhoneCountry () {
744 return this.phoneCountry;
748 public void setPhoneCountry (final Country phoneCountry) {
749 this.phoneCountry = phoneCountry;
753 public Long getPhoneNumber () {
754 return this.phoneNumber;
758 public void setPhoneNumber (final Long phoneNumber) {
759 this.phoneNumber = phoneNumber;
763 public String getStreet () {
768 public void setStreet (final String street) {
769 this.street = street;
773 public String getTitle () {
778 public void setTitle (final String title) {
783 public Integer getZipCode () {
788 public void setZipCode (final Integer zipCode) {
789 this.zipCode = zipCode;
793 * Post-initialization of this class
796 public void init () {
800 public boolean isGenderRequired () {
801 // Get context parameter
802 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_admin_gender_enabled"); //NOI18N
805 boolean isRequired = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N
814 private void clear () {
818 this.setFirstName(null);
819 this.setFamilyName(null);
820 this.setStreet(null);
821 this.setHouseNumber(null);
822 this.setHouseNumberExtension(null);
823 this.setZipCode(null);
825 this.setCountry(null);
828 this.setEmailAddress(null);
829 this.setPhoneCountry(null);
830 this.setPhoneAreaCode(null);
831 this.setPhoneNumber(null);
832 this.setCellphoneCarrier(null);
833 this.setCellphoneNumber(null);
834 this.setFaxCountry(null);
835 this.setFaxAreaCode(null);
836 this.setFaxNumber(null);
839 this.setBirthday(null);
840 this.setComment(null);
844 * Checks whether the given contact is found
846 * @param contact Contact inastance
848 * @return Wether contact has been found
850 private boolean isSameContactFound (final Contact contact) {
851 // Default is not found
852 boolean IsFound = false;
855 Iterator<Contact> iterator = this.contactController.allContacts().iterator();
858 while (iterator.hasNext()) {
860 Contact next = iterator.next();
863 if (ContactUtils.isSameContact(contact, next)) {
864 // Yes, then abort loop
875 * Updates all data in contact instance.
877 * @param contact Contact instance
879 private void updateContactData (final Contact contact) {
880 // Contact instance should be valid
881 if (null == contact) {
883 throw new NullPointerException("contact is null"); //NOI18N
884 } else if (contact.getContactId() == null) {
886 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
887 } else if (contact.getContactId() < 1) {
889 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
893 contact.setContactGender(this.getGender());
894 contact.setContactTitle(this.getTitle());
895 contact.setContactFirstName(this.getFirstName());
896 contact.setContactFamilyName(this.getFamilyName());
897 contact.setContactStreet(this.getStreet());
898 contact.setContactHouseNumber(this.getHouseNumber());
899 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
900 contact.setContactZipCode(this.getZipCode());
901 contact.setContactCity(this.getCity());
902 contact.setContactCountry(this.getCountry());
904 // Update contact's cellphone number
905 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
907 // Update contact's land-line number
908 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
910 // Update contact's fax number
911 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());