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.view.facelets.FaceletException;
27 import javax.inject.Inject;
28 import javax.inject.Named;
29 import javax.naming.Context;
30 import javax.naming.InitialContext;
31 import javax.naming.NamingException;
32 import org.mxchange.jcontacts.contact.Contact;
33 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
34 import org.mxchange.jcontacts.contact.UserContact;
35 import org.mxchange.jcontacts.contact.gender.Gender;
36 import org.mxchange.jcontacts.contact.utils.ContactUtils;
37 import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent;
38 import org.mxchange.jcontacts.events.contact.add.AdminContactAddedEvent;
39 import org.mxchange.jcontacts.events.contact.update.AdminContactUpdatedEvent;
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.cellphone.CellphoneNumber;
45 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
46 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
47 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
48 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
49 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
50 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
51 import org.mxchange.pizzaapplication.beans.BasePizzaController;
52 import org.mxchange.pizzaapplication.beans.helper.PizzaWebRequestController;
55 * Administrative user bean (controller)
57 * @author Roland Haeder<roland@mxchange.org>
59 @Named ("adminContactController")
61 public class PizzaAdminContactWebRequestBean extends BasePizzaController implements PizzaAdminContactWebRequestController {
66 private static final long serialVersionUID = 542_145_347_916L;
69 * An event fired when the administrator has added a new contact
73 private Event<AdminAddedContactEvent> addedContactEvent;
76 * Admin helper instance
79 private PizzaWebRequestController beanHelper;
84 private Date birthday;
87 * Cellphone number's carrier
89 private MobileProvider cellphoneCarrier;
94 private Long cellphoneId;
99 private Long cellphoneNumber;
109 private String comment;
112 * Remote contact bean
114 private final ContactSessionBeanRemote contactBean;
117 * General contact controller
120 private PizzaContactWebSessionController contactController;
125 private Long contactId;
130 private Country country;
135 private String emailAddress;
140 private String familyName;
143 * Fax number's area code
145 private Integer faxAreaCode;
148 * Country instance for fax number
150 private Country faxCountry;
160 private Long faxNumber;
165 private String firstName;
170 private Gender gender;
175 private Short houseNumber;
178 * Whether a cellphone entry has been unlinked
180 private boolean isCellphoneUnlinked;
183 * Whether a fax entry has been unlinked
185 private boolean isFaxUnlinked;
188 * Whether a land-line number has been unlinked
190 private boolean isLandLineUnlinked;
193 * Land-line id number
195 private Long landLineId;
198 * Phone number area code
200 private Integer phoneAreaCode;
203 * Country instance for phone number
205 private Country phoneCountry;
210 private Long phoneNumber;
215 private String street;
218 * An event fired when the administrator has updated contact data
222 private Event<AdminUpdatedContactEvent> updatedContactEvent;
227 private Integer zipCode;
230 * Default constructor
232 public PizzaAdminContactWebRequestBean () {
235 // Get initial context
236 Context context = new InitialContext();
239 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
240 } catch (final NamingException e) {
242 throw new FaceletException(e);
247 public String addContact () {
248 // Create new contact instance
249 Contact contact = this.createContactInstance();
251 // Default is not same contact
252 if (this.isSameContactFound(contact)) {
253 // Already registered
254 throw new FaceletException(new ContactAlreadyAddedException(contact));
258 Contact updatedContact;
263 updatedContact = this.contactBean.addContact(contact);
264 } catch (final ContactAlreadyAddedException ex) {
266 throw new FaceletException(ex);
270 this.addedContactEvent.fire(new AdminContactAddedEvent(updatedContact));
276 return "admin_list_contact"; //NOI18N
280 public void copyContactToController (final Contact contact) {
282 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("AdminContactController::copyContactToController(): contact={0} - CALLED!", contact)); //NOI18N
284 // The contact instance must be valid
285 if (null == contact) {
287 throw new NullPointerException("contact is null"); //NOI18N
288 } else if (contact.getContactId() == null) {
290 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
291 } else if (contact.getContactId() < 1) {
293 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
296 // Set all fields: contact
297 this.setContactId(contact.getContactId());
298 this.setBirthday(contact.getContactBirthday());
299 this.setCity(contact.getContactCity());
300 this.setComment(contact.getContactComment());
301 this.setCountry(contact.getContactCountry());
302 this.setEmailAddress(contact.getContactEmailAddress());
303 this.setFamilyName(contact.getContactFamilyName());
304 this.setFirstName(contact.getContactFirstName());
305 this.setGender(contact.getContactGender());
306 this.setHouseNumber(contact.getContactHouseNumber());
307 this.setStreet(contact.getContactStreet());
308 this.setZipCode(contact.getContactZipCode());
310 // Is the cell phone set?
311 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
312 // ... cellphone data
313 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
314 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
315 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
319 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
321 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
322 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
323 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
324 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
327 // Is the land-line number set?
328 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
330 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
331 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
332 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
333 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
337 //* NOISY-DEBUG: */ System.out.println("AdminContactController::copyContactToController(): EXIT!"); //NOI18N
341 public Contact createContactInstance () {
342 // Are all minimum fields set?
343 if (this.getGender() == null) {
345 throw new NullPointerException("gender is null"); //NOI18N
346 } else if (this.getFirstName() == null) {
348 throw new NullPointerException("firstName is null"); //NOI18N
349 } else if (this.getFirstName().isEmpty()) {
351 throw new IllegalStateException("firstName is empty"); //NOI18N
352 } else if (this.getFamilyName() == null) {
354 throw new NullPointerException("familyName is null"); //NOI18N
355 } else if (this.getFamilyName().isEmpty()) {
357 throw new IllegalStateException("familyName is empty"); //NOI18N
360 // Generate phone number
361 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
362 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
363 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
365 // Create new instance
366 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
369 contact.setContactBirthday(this.getBirthday());
370 contact.setContactStreet(this.getStreet());
371 contact.setContactHouseNumber(this.getHouseNumber());
372 contact.setContactZipCode(this.getZipCode());
373 contact.setContactCity(this.getCity());
374 contact.setContactCountry(this.getCountry());
375 contact.setContactEmailAddress(this.getEmailAddress());
376 contact.setContactBirthday(this.getBirthday());
377 contact.setContactComment(this.getComment());
380 contact.setContactOwnContact(Boolean.TRUE);
382 // Don't set null or wrong references
383 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
384 // Now the number must be given
385 if (phone.getPhoneAreaCode() == null) {
387 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
388 } else if (phone.getPhoneAreaCode() < 1) {
390 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
391 } else if (phone.getPhoneNumber() == null) {
393 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
394 } else if (phone.getPhoneNumber() < 1) {
396 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
400 contact.setContactLandLineNumber(phone);
403 // Don't set null or wrong references
404 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
405 // Now the number must be given
406 if (fax.getPhoneAreaCode() == null) {
408 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
409 } else if (fax.getPhoneAreaCode() < 1) {
411 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
412 } else if (fax.getPhoneNumber() == null) {
414 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
415 } else if (fax.getPhoneNumber() < 1) {
417 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
421 contact.setContactFaxNumber(fax);
424 // Is the provider set?
425 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
426 // Is the number set?
427 if (cellphone.getPhoneNumber() == null) {
429 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
430 } else if (cellphone.getPhoneNumber() < 1) {
432 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
435 // Set cellphone number
436 contact.setContactCellphoneNumber(cellphone);
444 public String editContactData () {
445 // Get contact instance
446 Contact contact = this.beanHelper.getContact();
448 // Check if contact instance is in helper and valid
449 if (null == contact) {
451 throw new NullPointerException("beanHelper.contact is null"); //NOI18N
452 } else if (contact.getContactId() == null) {
454 throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
455 } else if (contact.getContactId() < 1) {
457 throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
460 // Update all data in contact
461 this.updateContactData(contact);
463 // Call EJB for updating contact data
464 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
467 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
472 // Return to contact list (for now)
473 return "admin_list_contact"; //NOI18N
477 public String generateCellphoneNumber (final DialableCellphoneNumber cellphoneNumber) {
479 if (null == cellphoneNumber) {
485 String number = String.format(
487 cellphoneNumber.getCellphoneProvider().getProviderCountry().getCountryExternalDialPrefix(),
488 cellphoneNumber.getCellphoneProvider().getProviderDialPrefix(),
489 cellphoneNumber.getPhoneNumber()
497 public String generatePhoneNumber (final DialableNumber phoneNumber) {
499 if (null == phoneNumber) {
505 String number = String.format(
507 phoneNumber.getPhoneCountry().getCountryExternalDialPrefix(),
508 phoneNumber.getPhoneAreaCode(),
509 phoneNumber.getPhoneNumber()
517 @SuppressWarnings ("ReturnOfDateField")
518 public Date getBirthday () {
519 return this.birthday;
523 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
524 public void setBirthday (final Date birthday) {
525 this.birthday = birthday;
529 public MobileProvider getCellphoneCarrier () {
530 return this.cellphoneCarrier;
534 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
535 this.cellphoneCarrier = cellphoneCarrier;
539 public Long getCellphoneId () {
540 return this.cellphoneId;
544 public void setCellphoneId (final Long cellphoneId) {
545 this.cellphoneId = cellphoneId;
549 public Long getCellphoneNumber () {
550 return this.cellphoneNumber;
554 public void setCellphoneNumber (Long cellphoneNumber) {
555 this.cellphoneNumber = cellphoneNumber;
559 public String getCity () {
564 public void setCity (final String city) {
569 public String getComment () {
574 public void setComment (final String comment) {
575 this.comment = comment;
579 public Long getContactId () {
580 return this.contactId;
584 public void setContactId (final Long contactId) {
585 this.contactId = contactId;
589 public Country getCountry () {
594 public void setCountry (final Country country) {
595 this.country = country;
599 public String getEmailAddress () {
600 return this.emailAddress;
604 public void setEmailAddress (final String emailAddress) {
605 this.emailAddress = emailAddress;
609 public String getFamilyName () {
610 return this.familyName;
614 public void setFamilyName (final String familyName) {
615 this.familyName = familyName;
619 public Integer getFaxAreaCode () {
620 return this.faxAreaCode;
624 public void setFaxAreaCode (final Integer faxAreaCode) {
625 this.faxAreaCode = faxAreaCode;
629 public Country getFaxCountry () {
630 return this.faxCountry;
634 public void setFaxCountry (final Country faxCountry) {
635 this.faxCountry = faxCountry;
639 public Long getFaxId () {
644 public void setFaxId (final Long faxId) {
649 public Long getFaxNumber () {
650 return this.faxNumber;
654 public void setFaxNumber (final Long faxNumber) {
655 this.faxNumber = faxNumber;
659 public String getFirstName () {
660 return this.firstName;
664 public void setFirstName (final String firstName) {
665 this.firstName = firstName;
669 public Gender getGender () {
674 public void setGender (final Gender gender) {
675 this.gender = gender;
679 public Short getHouseNumber () {
680 return this.houseNumber;
684 public void setHouseNumber (final Short houseNumber) {
685 this.houseNumber = houseNumber;
689 public Long getLandLineId () {
690 return this.landLineId;
694 public void setLandLineId (final Long landLineId) {
695 this.landLineId = landLineId;
699 public Integer getPhoneAreaCode () {
700 return this.phoneAreaCode;
704 public void setPhoneAreaCode (final Integer phoneAreaCode) {
705 this.phoneAreaCode = phoneAreaCode;
709 public Country getPhoneCountry () {
710 return this.phoneCountry;
714 public void setPhoneCountry (final Country phoneCountry) {
715 this.phoneCountry = phoneCountry;
719 public Long getPhoneNumber () {
720 return this.phoneNumber;
724 public void setPhoneNumber (final Long phoneNumber) {
725 this.phoneNumber = phoneNumber;
729 public String getStreet () {
734 public void setStreet (final String street) {
735 this.street = street;
739 public Integer getZipCode () {
744 public void setZipCode (final Integer zipCode) {
745 this.zipCode = zipCode;
749 * Post-initialization of this class
752 public void init () {
758 private void clear () {
761 this.setGender(Gender.UNKNOWN);
762 this.setFirstName(null);
763 this.setFamilyName(null);
764 this.setStreet(null);
765 this.setHouseNumber(null);
766 this.setZipCode(null);
768 this.setCountry(null);
771 this.setEmailAddress(null);
772 this.setPhoneCountry(null);
773 this.setPhoneAreaCode(null);
774 this.setPhoneNumber(null);
775 this.setCellphoneCarrier(null);
776 this.setCellphoneNumber(null);
777 this.setFaxCountry(null);
778 this.setFaxAreaCode(null);
779 this.setFaxNumber(null);
782 this.setBirthday(null);
783 this.setComment(null);
787 * Checks whether the given contact is found
789 * @param contact Contact inastance
791 * @return Wether contact has been found
793 private boolean isSameContactFound (final Contact contact) {
794 // Default is not found
795 boolean IsFound = false;
798 Iterator<Contact> iterator = this.contactController.allContacts().iterator();
801 while (iterator.hasNext()) {
803 Contact next = iterator.next();
806 if (ContactUtils.isSameContact(contact, next)) {
807 // Yes, then abort loop
818 * Updates all data in contact instance.
820 * @param contact Contact instance
822 private void updateContactData (final Contact contact) {
823 // Contact instance should be valid
824 if (null == contact) {
826 throw new NullPointerException("contact is null"); //NOI18N
827 } else if (contact.getContactId() == null) {
829 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
830 } else if (contact.getContactId() < 1) {
832 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
836 contact.setContactGender(this.getGender());
837 contact.setContactFirstName(this.getFirstName());
838 contact.setContactFamilyName(this.getFamilyName());
839 contact.setContactStreet(this.getStreet());
840 contact.setContactHouseNumber(this.getHouseNumber());
841 contact.setContactZipCode(this.getZipCode());
842 contact.setContactCity(this.getCity());
843 contact.setContactCountry(this.getCountry());
845 // Update contact's cellphone number
846 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
848 // Update contact's land-line number
849 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
851 // Update contact's fax number
852 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());