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.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.cellphone.DialableCellphoneNumber;
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 Haeder<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.setStreet(contact.getContactStreet());
320 this.setZipCode(contact.getContactZipCode());
322 // Is the cell phone set?
323 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
324 // ... cellphone data
325 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
326 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
327 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
331 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
333 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
334 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
335 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
336 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
339 // Is the land-line number set?
340 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
342 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
343 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
344 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
345 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
349 //* NOISY-DEBUG: */ System.out.println("AdminContactController::copyContactToController(): EXIT!"); //NOI18N
353 public Contact createContactInstance () {
354 // Are all minimum fields set?
355 if (this.getGender() == null) {
357 throw new NullPointerException("gender is null"); //NOI18N
358 } else if (this.getFirstName() == null) {
360 throw new NullPointerException("firstName is null"); //NOI18N
361 } else if (this.getFirstName().isEmpty()) {
363 throw new IllegalStateException("firstName is empty"); //NOI18N
364 } else if (this.getFamilyName() == null) {
366 throw new NullPointerException("familyName is null"); //NOI18N
367 } else if (this.getFamilyName().isEmpty()) {
369 throw new IllegalStateException("familyName is empty"); //NOI18N
372 // Generate phone number
373 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
374 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
375 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
377 // Create new instance
378 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
381 contact.setContactBirthday(this.getBirthday());
382 contact.setContactStreet(this.getStreet());
383 contact.setContactHouseNumber(this.getHouseNumber());
384 contact.setContactZipCode(this.getZipCode());
385 contact.setContactCity(this.getCity());
386 contact.setContactCountry(this.getCountry());
387 contact.setContactEmailAddress(this.getEmailAddress());
388 contact.setContactBirthday(this.getBirthday());
389 contact.setContactComment(this.getComment());
392 contact.setContactOwnContact(Boolean.TRUE);
394 // Don't set null or wrong references
395 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
396 // Now the number must be given
397 if (phone.getPhoneAreaCode() == null) {
399 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
400 } else if (phone.getPhoneAreaCode() < 1) {
402 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
403 } else if (phone.getPhoneNumber() == null) {
405 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
406 } else if (phone.getPhoneNumber() < 1) {
408 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
412 contact.setContactLandLineNumber(phone);
415 // Don't set null or wrong references
416 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
417 // Now the number must be given
418 if (fax.getPhoneAreaCode() == null) {
420 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
421 } else if (fax.getPhoneAreaCode() < 1) {
423 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
424 } else if (fax.getPhoneNumber() == null) {
426 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
427 } else if (fax.getPhoneNumber() < 1) {
429 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
433 contact.setContactFaxNumber(fax);
436 // Is the provider set?
437 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
438 // Is the number set?
439 if (cellphone.getPhoneNumber() == null) {
441 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
442 } else if (cellphone.getPhoneNumber() < 1) {
444 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
447 // Set cellphone number
448 contact.setContactCellphoneNumber(cellphone);
456 public String editContactData () {
457 // Get contact instance
458 Contact contact = this.beanHelper.getContact();
460 // Check if contact instance is in helper and valid
461 if (null == contact) {
463 throw new NullPointerException("beanHelper.contact is null"); //NOI18N
464 } else if (contact.getContactId() == null) {
466 throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
467 } else if (contact.getContactId() < 1) {
469 throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
472 // Update all data in contact
473 this.updateContactData(contact);
475 // Call EJB for updating contact data
476 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
479 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
484 // Return to contact list (for now)
485 return "admin_list_contact"; //NOI18N
489 public String generateCellphoneNumber (final DialableCellphoneNumber cellphoneNumber) {
491 if (null == cellphoneNumber) {
497 String number = String.format(
499 cellphoneNumber.getCellphoneProvider().getProviderCountry().getCountryExternalDialPrefix(),
500 cellphoneNumber.getCellphoneProvider().getProviderDialPrefix(),
501 cellphoneNumber.getPhoneNumber()
509 public String generatePhoneNumber (final DialableNumber phoneNumber) {
511 if (null == phoneNumber) {
517 String number = String.format(
519 phoneNumber.getPhoneCountry().getCountryExternalDialPrefix(),
520 phoneNumber.getPhoneAreaCode(),
521 phoneNumber.getPhoneNumber()
529 @SuppressWarnings ("ReturnOfDateField")
530 public Date getBirthday () {
531 return this.birthday;
535 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
536 public void setBirthday (final Date birthday) {
537 this.birthday = birthday;
541 public MobileProvider getCellphoneCarrier () {
542 return this.cellphoneCarrier;
546 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
547 this.cellphoneCarrier = cellphoneCarrier;
551 public Long getCellphoneId () {
552 return this.cellphoneId;
556 public void setCellphoneId (final Long cellphoneId) {
557 this.cellphoneId = cellphoneId;
561 public Long getCellphoneNumber () {
562 return this.cellphoneNumber;
566 public void setCellphoneNumber (Long cellphoneNumber) {
567 this.cellphoneNumber = cellphoneNumber;
571 public String getCity () {
576 public void setCity (final String city) {
581 public String getComment () {
586 public void setComment (final String comment) {
587 this.comment = comment;
591 public Long getContactId () {
592 return this.contactId;
596 public void setContactId (final Long contactId) {
597 this.contactId = contactId;
601 public String getControllerType () {
602 return "admin"; //NOI18N
607 public void setControllerType (final String controllerType) {
608 throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
612 public Country getCountry () {
617 public void setCountry (final Country country) {
618 this.country = country;
622 public String getEmailAddress () {
623 return this.emailAddress;
627 public void setEmailAddress (final String emailAddress) {
628 this.emailAddress = emailAddress;
632 public String getFamilyName () {
633 return this.familyName;
637 public void setFamilyName (final String familyName) {
638 this.familyName = familyName;
642 public Integer getFaxAreaCode () {
643 return this.faxAreaCode;
647 public void setFaxAreaCode (final Integer faxAreaCode) {
648 this.faxAreaCode = faxAreaCode;
652 public Country getFaxCountry () {
653 return this.faxCountry;
657 public void setFaxCountry (final Country faxCountry) {
658 this.faxCountry = faxCountry;
662 public Long getFaxId () {
667 public void setFaxId (final Long faxId) {
672 public Long getFaxNumber () {
673 return this.faxNumber;
677 public void setFaxNumber (final Long faxNumber) {
678 this.faxNumber = faxNumber;
682 public String getFirstName () {
683 return this.firstName;
687 public void setFirstName (final String firstName) {
688 this.firstName = firstName;
692 public Gender getGender () {
697 public void setGender (final Gender gender) {
698 this.gender = gender;
702 public Short getHouseNumber () {
703 return this.houseNumber;
707 public void setHouseNumber (final Short houseNumber) {
708 this.houseNumber = houseNumber;
712 public String getHouseNumberExtension () {
713 return this.houseNumberExtension;
717 public void setHouseNumberExtension (final String houseNumberExtension) {
718 this.houseNumberExtension = houseNumberExtension;
722 public Long getLandLineId () {
723 return this.landLineId;
727 public void setLandLineId (final Long landLineId) {
728 this.landLineId = landLineId;
732 public Integer getPhoneAreaCode () {
733 return this.phoneAreaCode;
737 public void setPhoneAreaCode (final Integer phoneAreaCode) {
738 this.phoneAreaCode = phoneAreaCode;
742 public Country getPhoneCountry () {
743 return this.phoneCountry;
747 public void setPhoneCountry (final Country phoneCountry) {
748 this.phoneCountry = phoneCountry;
752 public Long getPhoneNumber () {
753 return this.phoneNumber;
757 public void setPhoneNumber (final Long phoneNumber) {
758 this.phoneNumber = phoneNumber;
762 public String getStreet () {
767 public void setStreet (final String street) {
768 this.street = street;
772 public String getTitle () {
777 public void setTitle (final String title) {
782 public Integer getZipCode () {
787 public void setZipCode (final Integer zipCode) {
788 this.zipCode = zipCode;
792 * Post-initialization of this class
795 public void init () {
799 public boolean isGenderRequired () {
800 // Get context parameter
801 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_admin_gender_enabled"); //NOI18N
804 boolean isRequired = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N
813 private void clear () {
817 this.setFirstName(null);
818 this.setFamilyName(null);
819 this.setStreet(null);
820 this.setHouseNumber(null);
821 this.setZipCode(null);
823 this.setCountry(null);
826 this.setEmailAddress(null);
827 this.setPhoneCountry(null);
828 this.setPhoneAreaCode(null);
829 this.setPhoneNumber(null);
830 this.setCellphoneCarrier(null);
831 this.setCellphoneNumber(null);
832 this.setFaxCountry(null);
833 this.setFaxAreaCode(null);
834 this.setFaxNumber(null);
837 this.setBirthday(null);
838 this.setComment(null);
842 * Checks whether the given contact is found
844 * @param contact Contact inastance
846 * @return Wether contact has been found
848 private boolean isSameContactFound (final Contact contact) {
849 // Default is not found
850 boolean IsFound = false;
853 Iterator<Contact> iterator = this.contactController.allContacts().iterator();
856 while (iterator.hasNext()) {
858 Contact next = iterator.next();
861 if (ContactUtils.isSameContact(contact, next)) {
862 // Yes, then abort loop
873 * Updates all data in contact instance.
875 * @param contact Contact instance
877 private void updateContactData (final Contact contact) {
878 // Contact instance should be valid
879 if (null == contact) {
881 throw new NullPointerException("contact is null"); //NOI18N
882 } else if (contact.getContactId() == null) {
884 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
885 } else if (contact.getContactId() < 1) {
887 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
891 contact.setContactGender(this.getGender());
892 contact.setContactTitle(this.getTitle());
893 contact.setContactFirstName(this.getFirstName());
894 contact.setContactFamilyName(this.getFamilyName());
895 contact.setContactStreet(this.getStreet());
896 contact.setContactHouseNumber(this.getHouseNumber());
897 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
898 contact.setContactZipCode(this.getZipCode());
899 contact.setContactCity(this.getCity());
900 contact.setContactCountry(this.getCountry());
902 // Update contact's cellphone number
903 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
905 // Update contact's land-line number
906 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
908 // Update contact's fax number
909 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());