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 * House number extension
180 private String houseNumberExtension;
183 * Whether a cellphone entry has been unlinked
185 private boolean isCellphoneUnlinked;
188 * Whether a fax entry has been unlinked
190 private boolean isFaxUnlinked;
193 * Whether a land-line number has been unlinked
195 private boolean isLandLineUnlinked;
198 * Land-line id number
200 private Long landLineId;
203 * Phone number area code
205 private Integer phoneAreaCode;
208 * Country instance for phone number
210 private Country phoneCountry;
215 private Long phoneNumber;
220 private String street;
225 private String title;
228 * An event fired when the administrator has updated contact data
232 private Event<AdminUpdatedContactEvent> updatedContactEvent;
237 private Integer zipCode;
240 * Default constructor
242 public PizzaAdminContactWebRequestBean () {
245 // Get initial context
246 Context context = new InitialContext();
249 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
250 } catch (final NamingException e) {
252 throw new FaceletException(e);
257 public String addContact () {
258 // Create new contact instance
259 Contact contact = this.createContactInstance();
261 // Default is not same contact
262 if (this.isSameContactFound(contact)) {
263 // Already registered
264 throw new FaceletException(new ContactAlreadyAddedException(contact));
268 Contact updatedContact;
273 updatedContact = this.contactBean.addContact(contact);
274 } catch (final ContactAlreadyAddedException ex) {
276 throw new FaceletException(ex);
280 this.addedContactEvent.fire(new AdminContactAddedEvent(updatedContact));
286 return "admin_list_contact"; //NOI18N
290 public void copyContactToController (final Contact contact) {
292 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("AdminContactController::copyContactToController(): contact={0} - CALLED!", contact)); //NOI18N
294 // The contact instance must be valid
295 if (null == contact) {
297 throw new NullPointerException("contact is null"); //NOI18N
298 } else if (contact.getContactId() == null) {
300 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
301 } else if (contact.getContactId() < 1) {
303 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
306 // Set all fields: contact
307 this.setContactId(contact.getContactId());
308 this.setTitle(contact.getContactTitle());
309 this.setBirthday(contact.getContactBirthday());
310 this.setCity(contact.getContactCity());
311 this.setComment(contact.getContactComment());
312 this.setCountry(contact.getContactCountry());
313 this.setEmailAddress(contact.getContactEmailAddress());
314 this.setFamilyName(contact.getContactFamilyName());
315 this.setFirstName(contact.getContactFirstName());
316 this.setGender(contact.getContactGender());
317 this.setHouseNumber(contact.getContactHouseNumber());
318 this.setStreet(contact.getContactStreet());
319 this.setZipCode(contact.getContactZipCode());
321 // Is the cell phone set?
322 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
323 // ... cellphone data
324 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
325 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
326 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
330 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
332 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
333 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
334 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
335 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
338 // Is the land-line number set?
339 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
341 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
342 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
343 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
344 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
348 //* NOISY-DEBUG: */ System.out.println("AdminContactController::copyContactToController(): EXIT!"); //NOI18N
352 public Contact createContactInstance () {
353 // Are all minimum fields set?
354 if (this.getGender() == null) {
356 throw new NullPointerException("gender is null"); //NOI18N
357 } else if (this.getFirstName() == null) {
359 throw new NullPointerException("firstName is null"); //NOI18N
360 } else if (this.getFirstName().isEmpty()) {
362 throw new IllegalStateException("firstName is empty"); //NOI18N
363 } else if (this.getFamilyName() == null) {
365 throw new NullPointerException("familyName is null"); //NOI18N
366 } else if (this.getFamilyName().isEmpty()) {
368 throw new IllegalStateException("familyName is empty"); //NOI18N
371 // Generate phone number
372 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
373 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
374 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
376 // Create new instance
377 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
380 contact.setContactBirthday(this.getBirthday());
381 contact.setContactStreet(this.getStreet());
382 contact.setContactHouseNumber(this.getHouseNumber());
383 contact.setContactZipCode(this.getZipCode());
384 contact.setContactCity(this.getCity());
385 contact.setContactCountry(this.getCountry());
386 contact.setContactEmailAddress(this.getEmailAddress());
387 contact.setContactBirthday(this.getBirthday());
388 contact.setContactComment(this.getComment());
391 contact.setContactOwnContact(Boolean.TRUE);
393 // Don't set null or wrong references
394 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
395 // Now the number must be given
396 if (phone.getPhoneAreaCode() == null) {
398 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
399 } else if (phone.getPhoneAreaCode() < 1) {
401 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
402 } else if (phone.getPhoneNumber() == null) {
404 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
405 } else if (phone.getPhoneNumber() < 1) {
407 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
411 contact.setContactLandLineNumber(phone);
414 // Don't set null or wrong references
415 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
416 // Now the number must be given
417 if (fax.getPhoneAreaCode() == null) {
419 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
420 } else if (fax.getPhoneAreaCode() < 1) {
422 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
423 } else if (fax.getPhoneNumber() == null) {
425 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
426 } else if (fax.getPhoneNumber() < 1) {
428 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
432 contact.setContactFaxNumber(fax);
435 // Is the provider set?
436 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
437 // Is the number set?
438 if (cellphone.getPhoneNumber() == null) {
440 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
441 } else if (cellphone.getPhoneNumber() < 1) {
443 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
446 // Set cellphone number
447 contact.setContactCellphoneNumber(cellphone);
455 public String editContactData () {
456 // Get contact instance
457 Contact contact = this.beanHelper.getContact();
459 // Check if contact instance is in helper and valid
460 if (null == contact) {
462 throw new NullPointerException("beanHelper.contact is null"); //NOI18N
463 } else if (contact.getContactId() == null) {
465 throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
466 } else if (contact.getContactId() < 1) {
468 throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
471 // Update all data in contact
472 this.updateContactData(contact);
474 // Call EJB for updating contact data
475 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
478 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
483 // Return to contact list (for now)
484 return "admin_list_contact"; //NOI18N
488 public String generateCellphoneNumber (final DialableCellphoneNumber cellphoneNumber) {
490 if (null == cellphoneNumber) {
496 String number = String.format(
498 cellphoneNumber.getCellphoneProvider().getProviderCountry().getCountryExternalDialPrefix(),
499 cellphoneNumber.getCellphoneProvider().getProviderDialPrefix(),
500 cellphoneNumber.getPhoneNumber()
508 public String generatePhoneNumber (final DialableNumber phoneNumber) {
510 if (null == phoneNumber) {
516 String number = String.format(
518 phoneNumber.getPhoneCountry().getCountryExternalDialPrefix(),
519 phoneNumber.getPhoneAreaCode(),
520 phoneNumber.getPhoneNumber()
528 @SuppressWarnings ("ReturnOfDateField")
529 public Date getBirthday () {
530 return this.birthday;
534 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
535 public void setBirthday (final Date birthday) {
536 this.birthday = birthday;
540 public MobileProvider getCellphoneCarrier () {
541 return this.cellphoneCarrier;
545 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
546 this.cellphoneCarrier = cellphoneCarrier;
550 public Long getCellphoneId () {
551 return this.cellphoneId;
555 public void setCellphoneId (final Long cellphoneId) {
556 this.cellphoneId = cellphoneId;
560 public Long getCellphoneNumber () {
561 return this.cellphoneNumber;
565 public void setCellphoneNumber (Long cellphoneNumber) {
566 this.cellphoneNumber = cellphoneNumber;
570 public String getCity () {
575 public void setCity (final String city) {
580 public String getComment () {
585 public void setComment (final String comment) {
586 this.comment = comment;
590 public Long getContactId () {
591 return this.contactId;
595 public void setContactId (final Long contactId) {
596 this.contactId = contactId;
600 public String getControllerType () {
601 return "admin"; //NOI18N
606 public void setControllerType (final String controllerType) {
607 throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
611 public Country getCountry () {
616 public void setCountry (final Country country) {
617 this.country = country;
621 public String getEmailAddress () {
622 return this.emailAddress;
626 public void setEmailAddress (final String emailAddress) {
627 this.emailAddress = emailAddress;
631 public String getFamilyName () {
632 return this.familyName;
636 public void setFamilyName (final String familyName) {
637 this.familyName = familyName;
641 public Integer getFaxAreaCode () {
642 return this.faxAreaCode;
646 public void setFaxAreaCode (final Integer faxAreaCode) {
647 this.faxAreaCode = faxAreaCode;
651 public Country getFaxCountry () {
652 return this.faxCountry;
656 public void setFaxCountry (final Country faxCountry) {
657 this.faxCountry = faxCountry;
661 public Long getFaxId () {
666 public void setFaxId (final Long faxId) {
671 public Long getFaxNumber () {
672 return this.faxNumber;
676 public void setFaxNumber (final Long faxNumber) {
677 this.faxNumber = faxNumber;
681 public String getFirstName () {
682 return this.firstName;
686 public void setFirstName (final String firstName) {
687 this.firstName = firstName;
691 public Gender getGender () {
696 public void setGender (final Gender gender) {
697 this.gender = gender;
701 public Short getHouseNumber () {
702 return this.houseNumber;
706 public void setHouseNumber (final Short houseNumber) {
707 this.houseNumber = houseNumber;
711 public String getHouseNumberExtension () {
712 return this.houseNumberExtension;
716 public void setHouseNumberExtension (final String houseNumberExtension) {
717 this.houseNumberExtension = houseNumberExtension;
721 public Long getLandLineId () {
722 return this.landLineId;
726 public void setLandLineId (final Long landLineId) {
727 this.landLineId = landLineId;
731 public Integer getPhoneAreaCode () {
732 return this.phoneAreaCode;
736 public void setPhoneAreaCode (final Integer phoneAreaCode) {
737 this.phoneAreaCode = phoneAreaCode;
741 public Country getPhoneCountry () {
742 return this.phoneCountry;
746 public void setPhoneCountry (final Country phoneCountry) {
747 this.phoneCountry = phoneCountry;
751 public Long getPhoneNumber () {
752 return this.phoneNumber;
756 public void setPhoneNumber (final Long phoneNumber) {
757 this.phoneNumber = phoneNumber;
761 public String getStreet () {
766 public void setStreet (final String street) {
767 this.street = street;
771 public String getTitle () {
776 public void setTitle (final String title) {
781 public Integer getZipCode () {
786 public void setZipCode (final Integer zipCode) {
787 this.zipCode = zipCode;
791 * Post-initialization of this class
794 public void init () {
800 private void clear () {
804 this.setFirstName(null);
805 this.setFamilyName(null);
806 this.setStreet(null);
807 this.setHouseNumber(null);
808 this.setZipCode(null);
810 this.setCountry(null);
813 this.setEmailAddress(null);
814 this.setPhoneCountry(null);
815 this.setPhoneAreaCode(null);
816 this.setPhoneNumber(null);
817 this.setCellphoneCarrier(null);
818 this.setCellphoneNumber(null);
819 this.setFaxCountry(null);
820 this.setFaxAreaCode(null);
821 this.setFaxNumber(null);
824 this.setBirthday(null);
825 this.setComment(null);
829 * Checks whether the given contact is found
831 * @param contact Contact inastance
833 * @return Wether contact has been found
835 private boolean isSameContactFound (final Contact contact) {
836 // Default is not found
837 boolean IsFound = false;
840 Iterator<Contact> iterator = this.contactController.allContacts().iterator();
843 while (iterator.hasNext()) {
845 Contact next = iterator.next();
848 if (ContactUtils.isSameContact(contact, next)) {
849 // Yes, then abort loop
860 * Updates all data in contact instance.
862 * @param contact Contact instance
864 private void updateContactData (final Contact contact) {
865 // Contact instance should be valid
866 if (null == contact) {
868 throw new NullPointerException("contact is null"); //NOI18N
869 } else if (contact.getContactId() == null) {
871 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
872 } else if (contact.getContactId() < 1) {
874 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
878 contact.setContactGender(this.getGender());
879 contact.setContactTitle(this.getTitle());
880 contact.setContactFirstName(this.getFirstName());
881 contact.setContactFamilyName(this.getFamilyName());
882 contact.setContactStreet(this.getStreet());
883 contact.setContactHouseNumber(this.getHouseNumber());
884 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
885 contact.setContactZipCode(this.getZipCode());
886 contact.setContactCity(this.getCity());
887 contact.setContactCountry(this.getCountry());
889 // Update contact's cellphone number
890 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
892 // Update contact's land-line number
893 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
895 // Update contact's fax number
896 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());