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.cellphone.CellphoneNumber;
44 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
45 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
46 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
47 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
48 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
49 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
50 import org.mxchange.pizzaapplication.beans.helper.PizzaAdminWebRequestController;
53 * Administrative user bean (controller)
55 * @author Roland Haeder<roland@mxchange.org>
57 @Named ("adminContactController")
59 public class PizzaAdminContactWebRequestBean implements PizzaAdminContactWebRequestController {
64 private static final long serialVersionUID = 542_145_347_916L;
67 * An event fired when the administrator has added a new contact
71 private Event<AdminAddedContactEvent> addedContactEvent;
74 * Admin helper instance
77 private PizzaAdminWebRequestController adminHelper;
82 private Date birthday;
85 * Cellphone number's carrier
87 private MobileProvider cellphoneCarrier;
92 private Long cellphoneId;
97 private Long cellphoneNumber;
107 private String comment;
110 * Remote contact bean
112 private final ContactSessionBeanRemote contactBean;
115 * General contact controller
118 private PizzaContactWebSessionController contactController;
123 private Long contactId;
128 private Country country;
133 private String emailAddress;
138 private String familyName;
141 * Fax number's area code
143 private Integer faxAreaCode;
146 * Country instance for fax number
148 private Country faxCountry;
158 private Long faxNumber;
163 private String firstName;
168 private Gender gender;
173 private Short houseNumber;
176 * Whether a cellphone entry has been unlinked
178 private boolean isCellphoneUnlinked;
181 * Whether a fax entry has been unlinked
183 private boolean isFaxUnlinked;
186 * Whether a land-line number has been unlinked
188 private boolean isLandLineUnlinked;
191 * Land-line id number
193 private Long landLineId;
196 * Phone number area code
198 private Integer phoneAreaCode;
201 * Country instance for phone number
203 private Country phoneCountry;
208 private Long phoneNumber;
213 private String street;
216 * An event fired when the administrator has updated contact data
220 private Event<AdminUpdatedContactEvent> updatedContactEvent;
225 private Integer zipCode;
228 * Default constructor
230 public PizzaAdminContactWebRequestBean () {
233 // Get initial context
234 Context context = new InitialContext();
237 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
238 } catch (final NamingException e) {
240 throw new FaceletException(e);
245 public String addContact () {
246 // Create new contact instance
247 Contact contact = this.createContactInstance();
249 // Default is not same contact
250 if (this.isSameContactFound(contact)) {
251 // Already registered
252 throw new FaceletException(new ContactAlreadyAddedException(contact));
256 Contact updatedContact;
261 updatedContact = this.contactBean.addContact(contact);
262 } catch (final ContactAlreadyAddedException ex) {
264 throw new FaceletException(ex);
268 this.addedContactEvent.fire(new AdminContactAddedEvent(updatedContact));
274 return "admin_list_contact"; //NOI18N
278 public void copyContactToController (final Contact contact) {
280 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("AdminContactController::copyContactToController(): contact={0} - CALLED!", contact)); //NOI18N
282 // The contact instance must be valid
283 if (null == contact) {
285 throw new NullPointerException("contact is null"); //NOI18N
286 } else if (contact.getContactId() == null) {
288 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
289 } else if (contact.getContactId() < 1) {
291 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
294 // Set all fields: contact
295 this.setContactId(contact.getContactId());
296 this.setBirthday(contact.getContactBirthday());
297 this.setCity(contact.getContactCity());
298 this.setComment(contact.getContactComment());
299 this.setCountry(contact.getContactCountry());
300 this.setEmailAddress(contact.getContactEmailAddress());
301 this.setFamilyName(contact.getContactFamilyName());
302 this.setFirstName(contact.getContactFirstName());
303 this.setGender(contact.getContactGender());
304 this.setHouseNumber(contact.getContactHouseNumber());
305 this.setStreet(contact.getContactStreet());
306 this.setZipCode(contact.getContactZipCode());
308 // Is the cell phone set?
309 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
310 // ... cellphone data
311 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
312 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
313 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
317 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
319 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
320 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
321 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
322 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
325 // Is the land-line number set?
326 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
328 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
329 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
330 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
331 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
335 //* NOISY-DEBUG: */ System.out.println("AdminContactController::copyContactToController(): EXIT!"); //NOI18N
339 public Contact createContactInstance () {
340 // Are all minimum fields set?
341 if (this.getGender() == null) {
343 throw new NullPointerException("gender is null"); //NOI18N
344 } else if (this.getFirstName() == null) {
346 throw new NullPointerException("firstName is null"); //NOI18N
347 } else if (this.getFirstName().isEmpty()) {
349 throw new IllegalStateException("firstName is empty"); //NOI18N
350 } else if (this.getFamilyName() == null) {
352 throw new NullPointerException("familyName is null"); //NOI18N
353 } else if (this.getFamilyName().isEmpty()) {
355 throw new IllegalStateException("familyName is empty"); //NOI18N
358 // Generate phone number
359 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
360 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
361 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
363 // Create new instance
364 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
367 contact.setContactBirthday(this.getBirthday());
368 contact.setContactStreet(this.getStreet());
369 contact.setContactHouseNumber(this.getHouseNumber());
370 contact.setContactZipCode(this.getZipCode());
371 contact.setContactCity(this.getCity());
372 contact.setContactCountry(this.getCountry());
373 contact.setContactEmailAddress(this.getEmailAddress());
374 contact.setContactBirthday(this.getBirthday());
375 contact.setContactComment(this.getComment());
378 contact.setContactOwnContact(Boolean.TRUE);
380 // Don't set null or wrong references
381 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
382 // Now the number must be given
383 if (phone.getPhoneAreaCode() == null) {
385 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
386 } else if (phone.getPhoneAreaCode() < 1) {
388 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
389 } else if (phone.getPhoneNumber() == null) {
391 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
392 } else if (phone.getPhoneNumber() < 1) {
394 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
398 contact.setContactLandLineNumber(phone);
401 // Don't set null or wrong references
402 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
403 // Now the number must be given
404 if (fax.getPhoneAreaCode() == null) {
406 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
407 } else if (fax.getPhoneAreaCode() < 1) {
409 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
410 } else if (fax.getPhoneNumber() == null) {
412 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
413 } else if (fax.getPhoneNumber() < 1) {
415 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
419 contact.setContactFaxNumber(fax);
422 // Is the provider set?
423 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
424 // Is the number set?
425 if (cellphone.getPhoneNumber() == null) {
427 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
428 } else if (cellphone.getPhoneNumber() < 1) {
430 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
433 // Set cellphone number
434 contact.setContactCellphoneNumber(cellphone);
442 public String editContactData () {
443 // Get contact instance
444 Contact contact = this.adminHelper.getContact();
446 // Check if contact instance is in helper and valid
447 if (null == contact) {
449 throw new NullPointerException("adminHelper.contact is null"); //NOI18N
450 } else if (contact.getContactId() == null) {
452 throw new NullPointerException("adminHelper.contact.contactId is null"); //NOI18N //NOI18N
453 } else if (contact.getContactId() < 1) {
455 throw new IllegalStateException(MessageFormat.format("adminHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
458 // Update all data in contact
459 this.updateContactData(contact);
461 // Call EJB for updating contact data
462 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
465 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
470 // Return to contact list (for now)
471 return "admin_list_contact"; //NOI18N
475 @SuppressWarnings ("ReturnOfDateField")
476 public Date getBirthday () {
477 return this.birthday;
481 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
482 public void setBirthday (final Date birthday) {
483 this.birthday = birthday;
487 public MobileProvider getCellphoneCarrier () {
488 return this.cellphoneCarrier;
492 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
493 this.cellphoneCarrier = cellphoneCarrier;
497 public Long getCellphoneId () {
498 return this.cellphoneId;
502 public void setCellphoneId (final Long cellphoneId) {
503 this.cellphoneId = cellphoneId;
507 public Long getCellphoneNumber () {
508 return this.cellphoneNumber;
512 public void setCellphoneNumber (Long cellphoneNumber) {
513 this.cellphoneNumber = cellphoneNumber;
517 public String getCity () {
522 public void setCity (final String city) {
527 public String getComment () {
532 public void setComment (final String comment) {
533 this.comment = comment;
537 public Long getContactId () {
538 return this.contactId;
542 public void setContactId (final Long contactId) {
543 this.contactId = contactId;
547 public Country getCountry () {
552 public void setCountry (final Country country) {
553 this.country = country;
557 public String getEmailAddress () {
558 return this.emailAddress;
562 public void setEmailAddress (final String emailAddress) {
563 this.emailAddress = emailAddress;
567 public String getFamilyName () {
568 return this.familyName;
572 public void setFamilyName (final String familyName) {
573 this.familyName = familyName;
577 public Integer getFaxAreaCode () {
578 return this.faxAreaCode;
582 public void setFaxAreaCode (final Integer faxAreaCode) {
583 this.faxAreaCode = faxAreaCode;
587 public Country getFaxCountry () {
588 return this.faxCountry;
592 public void setFaxCountry (final Country faxCountry) {
593 this.faxCountry = faxCountry;
597 public Long getFaxId () {
602 public void setFaxId (final Long faxId) {
607 public Long getFaxNumber () {
608 return this.faxNumber;
612 public void setFaxNumber (final Long faxNumber) {
613 this.faxNumber = faxNumber;
617 public String getFirstName () {
618 return this.firstName;
622 public void setFirstName (final String firstName) {
623 this.firstName = firstName;
627 public Gender getGender () {
632 public void setGender (final Gender gender) {
633 this.gender = gender;
637 public Short getHouseNumber () {
638 return this.houseNumber;
642 public void setHouseNumber (final Short houseNumber) {
643 this.houseNumber = houseNumber;
647 public Long getLandLineId () {
648 return this.landLineId;
652 public void setLandLineId (final Long landLineId) {
653 this.landLineId = landLineId;
657 public Integer getPhoneAreaCode () {
658 return this.phoneAreaCode;
662 public void setPhoneAreaCode (final Integer phoneAreaCode) {
663 this.phoneAreaCode = phoneAreaCode;
667 public Country getPhoneCountry () {
668 return this.phoneCountry;
672 public void setPhoneCountry (final Country phoneCountry) {
673 this.phoneCountry = phoneCountry;
677 public Long getPhoneNumber () {
678 return this.phoneNumber;
682 public void setPhoneNumber (final Long phoneNumber) {
683 this.phoneNumber = phoneNumber;
687 public String getStreet () {
692 public void setStreet (final String street) {
693 this.street = street;
697 public Integer getZipCode () {
702 public void setZipCode (final Integer zipCode) {
703 this.zipCode = zipCode;
707 * Post-initialization of this class
710 public void init () {
716 private void clear () {
719 this.setGender(Gender.UNKNOWN);
720 this.setFirstName(null);
721 this.setFamilyName(null);
722 this.setStreet(null);
723 this.setHouseNumber(null);
724 this.setZipCode(null);
726 this.setCountry(null);
729 this.setEmailAddress(null);
730 this.setPhoneCountry(null);
731 this.setPhoneAreaCode(null);
732 this.setPhoneNumber(null);
733 this.setCellphoneCarrier(null);
734 this.setCellphoneNumber(null);
735 this.setFaxCountry(null);
736 this.setFaxAreaCode(null);
737 this.setFaxNumber(null);
740 this.setBirthday(null);
741 this.setComment(null);
745 * Checks whether the given contact is found
747 * @param contact Contact inastance
749 * @return Wether contact has been found
751 private boolean isSameContactFound (final Contact contact) {
752 // Default is not found
753 boolean IsFound = false;
756 Iterator<Contact> iterator = this.contactController.allContacts().iterator();
759 while (iterator.hasNext()) {
761 Contact next = iterator.next();
764 if (ContactUtils.isSameContact(contact, next)) {
765 // Yes, then abort loop
776 * Updates all data in contact instance.
778 * @param contact Contact instance
780 private void updateContactData (final Contact contact) {
781 // Contact instance should be valid
782 if (null == contact) {
784 throw new NullPointerException("contact is null"); //NOI18N
785 } else if (contact.getContactId() == null) {
787 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
788 } else if (contact.getContactId() < 1) {
790 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
794 contact.setContactGender(this.getGender());
795 contact.setContactFirstName(this.getFirstName());
796 contact.setContactFamilyName(this.getFamilyName());
797 contact.setContactStreet(this.getStreet());
798 contact.setContactHouseNumber(this.getHouseNumber());
799 contact.setContactZipCode(this.getZipCode());
800 contact.setContactCity(this.getCity());
801 contact.setContactCountry(this.getCountry());
803 // Update contact's cellphone number
804 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
806 // Update contact's land-line number
807 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
809 // Update contact's fax number
810 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());