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.add.ObservableAdminAddedContactEvent;
41 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
42 import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent;
43 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
44 import org.mxchange.jcountry.data.Country;
45 import org.mxchange.jphone.phonenumbers.DialableNumber;
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.mobile.DialableMobileNumber;
51 import org.mxchange.jphone.phonenumbers.mobile.MobileNumber;
52 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
53 import org.mxchange.pizzaapplication.beans.BasePizzaController;
54 import org.mxchange.pizzaapplication.beans.helper.PizzaWebRequestHelperController;
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 PizzaWebRequestHelperController beanHelper;
91 private Date birthday;
94 * Cellphone number's carrier
96 private MobileProvider mobileCarrier;
101 private Long mobileId;
106 private Long mobileNumber;
116 private String comment;
119 * Remote contact bean
121 private 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 () {
253 public String addContact () {
254 // Are all minimum fields set?
255 if (this.getGender() == null) {
257 throw new NullPointerException("gender is null"); //NOI18N
258 } else if (this.getFirstName() == null) {
260 throw new NullPointerException("firstName is null"); //NOI18N
261 } else if (this.getFirstName().isEmpty()) {
263 throw new IllegalStateException("firstName is empty"); //NOI18N
264 } else if (this.getFamilyName() == null) {
266 throw new NullPointerException("familyName is null"); //NOI18N
267 } else if (this.getFamilyName().isEmpty()) {
269 throw new IllegalStateException("familyName is empty"); //NOI18N
272 // Create new contact instance
273 Contact contact = this.createContactInstance();
275 // Default is not same contact
276 if (this.isSameContactFound(contact)) {
277 // Already registered
278 throw new FaceletException(new ContactAlreadyAddedException(contact));
282 Contact updatedContact;
287 updatedContact = this.adminContactBean.addContact(contact);
288 } catch (final ContactAlreadyAddedException ex) {
290 throw new FaceletException(ex);
294 this.addedContactEvent.fire(new AdminAddedContactEvent(updatedContact));
300 return "admin_list_contact"; //NOI18N
304 public void copyContactToController (final Contact contact) {
306 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("AdminContactController::copyContactToController(): contact={0} - CALLED!", contact)); //NOI18N
308 // The contact instance must be valid
309 if (null == contact) {
311 throw new NullPointerException("contact is null"); //NOI18N
312 } else if (contact.getContactId() == null) {
314 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
315 } else if (contact.getContactId() < 1) {
317 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
320 // Set all fields: contact
321 this.setContactId(contact.getContactId());
322 this.setTitle(contact.getContactTitle());
323 this.setBirthday(contact.getContactBirthday());
324 this.setCity(contact.getContactCity());
325 this.setComment(contact.getContactComment());
326 this.setCountry(contact.getContactCountry());
327 this.setEmailAddress(contact.getContactEmailAddress());
328 this.setFamilyName(contact.getContactFamilyName());
329 this.setFirstName(contact.getContactFirstName());
330 this.setGender(contact.getContactGender());
331 this.setHouseNumber(contact.getContactHouseNumber());
332 this.setHouseNumberExtension(contact.getContactHouseNumberExtension());
333 this.setStreet(contact.getContactStreet());
334 this.setZipCode(contact.getContactZipCode());
336 // Is the cell phone set?
337 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
338 // ... cellphone data
339 this.setMobileId(contact.getContactMobileNumber().getPhoneId());
340 this.setMobileCarrier(contact.getContactMobileNumber().getMobileProvider());
341 this.setMobileNumber(contact.getContactMobileNumber().getPhoneNumber());
345 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
347 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
348 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
349 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
350 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
353 // Is the land-line number set?
354 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
356 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
357 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
358 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
359 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
363 //* NOISY-DEBUG: */ System.out.println("AdminContactController::copyContactToController(): EXIT!"); //NOI18N
367 public Contact createContactInstance () {
368 // Are all minimum fields set?
369 if (this.getGender() == null) {
371 throw new NullPointerException("gender is null"); //NOI18N
372 } else if (this.getFirstName() == null) {
374 throw new NullPointerException("firstName is null"); //NOI18N
375 } else if (this.getFirstName().isEmpty()) {
377 throw new IllegalStateException("firstName is empty"); //NOI18N
378 } else if (this.getFamilyName() == null) {
380 throw new NullPointerException("familyName is null"); //NOI18N
381 } else if (this.getFamilyName().isEmpty()) {
383 throw new IllegalStateException("familyName is empty"); //NOI18N
386 // Generate phone number
387 DialableLandLineNumber landLine = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
388 DialableMobileNumber mobile = new MobileNumber(this.getMobileCarrier(), this.getMobileNumber());
389 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
391 // Create new instance
392 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
395 contact.setContactBirthday(this.getBirthday());
396 contact.setContactStreet(this.getStreet());
397 contact.setContactHouseNumber(this.getHouseNumber());
398 contact.setContactZipCode(this.getZipCode());
399 contact.setContactCity(this.getCity());
400 contact.setContactCountry(this.getCountry());
401 contact.setContactEmailAddress(this.getEmailAddress());
402 contact.setContactBirthday(this.getBirthday());
403 contact.setContactComment(this.getComment());
406 contact.setContactOwnContact(Boolean.TRUE);
408 // Don't set null or wrong references
409 if ((landLine instanceof DialableLandLineNumber) && (landLine.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
410 // Now the number must be given
411 if (landLine.getPhoneAreaCode() == null) {
413 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
414 } else if (landLine.getPhoneAreaCode() < 1) {
416 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
417 } else if (landLine.getPhoneNumber() == null) {
419 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
420 } else if (landLine.getPhoneNumber() < 1) {
422 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
426 contact.setContactLandLineNumber(landLine);
429 // Don't set null or wrong references
430 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
431 // Now the number must be given
432 if (fax.getPhoneAreaCode() == null) {
434 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
435 } else if (fax.getPhoneAreaCode() < 1) {
437 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
438 } else if (fax.getPhoneNumber() == null) {
440 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
441 } else if (fax.getPhoneNumber() < 1) {
443 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
447 contact.setContactFaxNumber(fax);
450 // Is the provider set?
451 if ((mobile instanceof DialableMobileNumber) && (this.getMobileCarrier() instanceof MobileProvider) && (this.getMobileNumber() != null) && (this.getMobileNumber() > 0)) {
452 // Is the number set?
453 if (mobile.getPhoneNumber() == null) {
455 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
456 } else if (mobile.getPhoneNumber() < 1) {
458 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
461 // Set cellphone number
462 contact.setContactMobileNumber(mobile);
470 public String editContactData () {
471 // Get contact instance
472 Contact contact = this.beanHelper.getContact();
474 // Check if contact instance is in helper and valid
475 if (null == contact) {
477 throw new NullPointerException("beanHelper.contact is null"); //NOI18N
478 } else if (contact.getContactId() == null) {
480 throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
481 } else if (contact.getContactId() < 1) {
483 throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
486 // Update all data in contact
487 this.updateContactData(contact);
489 // Call EJB for updating contact data
490 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
493 this.updatedContactEvent.fire(new AdminUpdatedContactEvent(updatedContact));
498 // Return to contact list (for now)
499 return "admin_list_contact"; //NOI18N
503 public String generateCellphoneNumber (final DialableMobileNumber cellphoneNumber) {
505 if (null == cellphoneNumber) {
511 String number = String.format(
513 cellphoneNumber.getMobileProvider().getProviderCountry().getCountryExternalDialPrefix(),
514 cellphoneNumber.getMobileProvider().getProviderDialPrefix(),
515 cellphoneNumber.getPhoneNumber()
523 public String generatePhoneNumber (final DialableNumber phoneNumber) {
525 if (null == phoneNumber) {
531 String number = String.format(
533 phoneNumber.getPhoneCountry().getCountryExternalDialPrefix(),
534 phoneNumber.getPhoneAreaCode(),
535 phoneNumber.getPhoneNumber()
543 @SuppressWarnings ("ReturnOfDateField")
544 public Date getBirthday () {
545 return this.birthday;
549 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
550 public void setBirthday (final Date birthday) {
551 this.birthday = birthday;
555 public MobileProvider getMobileCarrier () {
556 return this.mobileCarrier;
560 public void setMobileCarrier (final MobileProvider mobileCarrier) {
561 this.mobileCarrier = mobileCarrier;
565 public Long getMobileId () {
566 return this.mobileId;
570 public void setMobileId (final Long mobileId) {
571 this.mobileId = mobileId;
575 public Long getMobileNumber () {
576 return this.mobileNumber;
580 public void setMobileNumber (Long mobileNumber) {
581 this.mobileNumber = mobileNumber;
585 public String getCity () {
590 public void setCity (final String city) {
595 public String getComment () {
600 public void setComment (final String comment) {
601 this.comment = comment;
605 public Long getContactId () {
606 return this.contactId;
610 public void setContactId (final Long contactId) {
611 this.contactId = contactId;
615 public String getControllerType () {
616 return "admin"; //NOI18N
621 public void setControllerType (final String controllerType) {
622 throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
626 public Country getCountry () {
631 public void setCountry (final Country country) {
632 this.country = country;
636 public String getEmailAddress () {
637 return this.emailAddress;
641 public void setEmailAddress (final String emailAddress) {
642 this.emailAddress = emailAddress;
646 public String getFamilyName () {
647 return this.familyName;
651 public void setFamilyName (final String familyName) {
652 this.familyName = familyName;
656 public Integer getFaxAreaCode () {
657 return this.faxAreaCode;
661 public void setFaxAreaCode (final Integer faxAreaCode) {
662 this.faxAreaCode = faxAreaCode;
666 public Country getFaxCountry () {
667 return this.faxCountry;
671 public void setFaxCountry (final Country faxCountry) {
672 this.faxCountry = faxCountry;
676 public Long getFaxId () {
681 public void setFaxId (final Long faxId) {
686 public Long getFaxNumber () {
687 return this.faxNumber;
691 public void setFaxNumber (final Long faxNumber) {
692 this.faxNumber = faxNumber;
696 public String getFirstName () {
697 return this.firstName;
701 public void setFirstName (final String firstName) {
702 this.firstName = firstName;
706 public Gender getGender () {
711 public void setGender (final Gender gender) {
712 this.gender = gender;
716 public Short getHouseNumber () {
717 return this.houseNumber;
721 public void setHouseNumber (final Short houseNumber) {
722 this.houseNumber = houseNumber;
726 public String getHouseNumberExtension () {
727 return this.houseNumberExtension;
731 public void setHouseNumberExtension (final String houseNumberExtension) {
732 this.houseNumberExtension = houseNumberExtension;
736 public Long getLandLineId () {
737 return this.landLineId;
741 public void setLandLineId (final Long landLineId) {
742 this.landLineId = landLineId;
746 public Integer getPhoneAreaCode () {
747 return this.phoneAreaCode;
751 public void setPhoneAreaCode (final Integer phoneAreaCode) {
752 this.phoneAreaCode = phoneAreaCode;
756 public Country getPhoneCountry () {
757 return this.phoneCountry;
761 public void setPhoneCountry (final Country phoneCountry) {
762 this.phoneCountry = phoneCountry;
766 public Long getPhoneNumber () {
767 return this.phoneNumber;
771 public void setPhoneNumber (final Long phoneNumber) {
772 this.phoneNumber = phoneNumber;
776 public String getStreet () {
781 public void setStreet (final String street) {
782 this.street = street;
786 public String getTitle () {
791 public void setTitle (final String title) {
796 public Integer getZipCode () {
801 public void setZipCode (final Integer zipCode) {
802 this.zipCode = zipCode;
806 * Post-initialization of this class
809 public void init () {
812 // Get initial context
813 Context context = new InitialContext();
816 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
818 // Try to lookup (administative)
819 this.adminContactBean = (AdminContactSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/adminContact!org.mxchange.jcontacts.contact.AdminContactSessionBeanRemote"); //NOI18N
820 } catch (final NamingException e) {
822 throw new FaceletException(e);
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.setMobileCarrier(null);
860 this.setMobileNumber(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.getMobileCarrier(), this.getMobileNumber());
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());