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 java.util.List;
23 import javax.annotation.PostConstruct;
24 import javax.enterprise.context.RequestScoped;
25 import javax.enterprise.event.Event;
26 import javax.enterprise.inject.Any;
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.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.helper.PizzaAdminWebRequestController;
54 * Administrative user bean (controller)
56 * @author Roland Haeder<roland@mxchange.org>
58 @Named ("adminContactController")
60 public class PizzaAdminContactWebRequestBean implements PizzaAdminContactWebRequestController {
65 private static final long serialVersionUID = 542_145_347_916L;
68 * An event fired when the administrator has added a new contact
72 private Event<AdminAddedContactEvent> addedContactEvent;
75 * Admin helper instance
78 private PizzaAdminWebRequestController adminHelper;
83 private Date birthday;
86 * Cellphone number's carrier
88 private MobileProvider cellphoneCarrier;
93 private Long cellphoneId;
98 private Long cellphoneNumber;
108 private String comment;
111 * Remote contact bean
113 private final ContactSessionBeanRemote contactBean;
116 * General contact controller
119 private PizzaContactWebSessionController contactController;
124 private Long contactId;
129 private Country country;
134 private String emailAddress;
139 private String familyName;
142 * Fax number's area code
144 private Integer faxAreaCode;
147 * Country instance for fax number
149 private Country faxCountry;
159 private Long faxNumber;
164 private String firstName;
169 private Gender gender;
174 private Short houseNumber;
177 * Whether a cellphone entry has been unlinked
179 private boolean isCellphoneUnlinked;
182 * Whether a fax entry has been unlinked
184 private boolean isFaxUnlinked;
187 * Whether a land-line number has been unlinked
189 private boolean isLandLineUnlinked;
192 * Land-line id number
194 private Long landLineId;
197 * Phone number area code
199 private Integer phoneAreaCode;
202 * Country instance for phone number
204 private Country phoneCountry;
209 private Long phoneNumber;
214 private String street;
217 * An event fired when the administrator has updated contact data
221 private Event<AdminUpdatedContactEvent> updatedContactEvent;
226 private Integer zipCode;
229 * Default constructor
231 public PizzaAdminContactWebRequestBean () {
232 // Set gender to UNKNOWN
233 this.gender = Gender.UNKNOWN;
237 // Get initial context
238 Context context = new InitialContext();
241 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
242 } catch (final NamingException e) {
244 throw new FaceletException(e);
249 public String addContact () {
250 // Are all minimum fields set?
251 if (this.getGender() == null) {
253 throw new NullPointerException("gender is null"); //NOI18N
254 } else if (this.getFirstName() == null) {
256 throw new NullPointerException("firstName is null"); //NOI18N
257 } else if (this.getFirstName().isEmpty()) {
259 throw new IllegalStateException("firstName is empty"); //NOI18N
260 } else if (this.getFamilyName() == null) {
262 throw new NullPointerException("familyName is null"); //NOI18N
263 } else if (this.getFamilyName().isEmpty()) {
265 throw new IllegalStateException("familyName is empty"); //NOI18N
268 // Create new contact instance
269 Contact contact = this.createContactInstance();
271 // Default is not same contact
272 if (this.isSameContactFound(contact)) {
273 // Already registered
274 throw new FaceletException(new ContactAlreadyAddedException(contact));
278 Contact updatedContact;
283 updatedContact = this.contactBean.addContact(contact);
284 } catch (final ContactAlreadyAddedException ex) {
286 throw new FaceletException(ex);
290 this.addedContactEvent.fire(new AdminContactAddedEvent(updatedContact));
293 return "admin_list_contact"; //NOI18N
297 public List<Contact> allContacts () {
298 return this.contactController.allContacts();
302 public String changeContactData () {
303 // Get contact instance
304 Contact contact = this.adminHelper.getContact();
306 // Check if contact instance is in helper and valid
307 if (null == contact) {
309 throw new NullPointerException("adminHelper.contact is null"); //NOI18N
310 } else if (contact.getContactId() == null) {
312 throw new NullPointerException("adminHelper.contact.contactId is null"); //NOI18N //NOI18N
313 } else if (contact.getContactId() < 1) {
315 throw new IllegalStateException(MessageFormat.format("adminHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
318 // Update all data in contact
319 this.updateContactData(contact);
321 // Call EJB for updating contact data
322 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
325 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
327 // Return to contact list (for now)
328 return "admin_list_contact"; //NOI18N
332 public void copyContactToController (final Contact contact) {
334 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("AdminContactController::copyContactToController(): contact={0} - CALLED!", contact)); //NOI18N
336 // The contact instance must be valid
337 if (null == contact) {
339 throw new NullPointerException("contact is null"); //NOI18N
340 } else if (contact.getContactId() == null) {
342 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
343 } else if (contact.getContactId() < 1) {
345 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
348 // Set all fields: contact
349 this.setContactId(contact.getContactId());
350 this.setBirthday(contact.getContactBirthday());
351 this.setCity(contact.getContactCity());
352 this.setComment(contact.getContactComment());
353 this.setCountry(contact.getContactCountry());
354 this.setEmailAddress(contact.getContactEmailAddress());
355 this.setFamilyName(contact.getContactFamilyName());
356 this.setFirstName(contact.getContactFirstName());
357 this.setGender(contact.getContactGender());
358 this.setHouseNumber(contact.getContactHouseNumber());
359 this.setStreet(contact.getContactStreet());
360 this.setZipCode(contact.getContactZipCode());
362 // Is the cell phone set?
363 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
364 // ... cellphone data
365 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
366 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
367 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
371 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
373 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
374 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
375 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
376 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
379 // Is the land-line number set?
380 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
382 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
383 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
384 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
385 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
389 //* NOISY-DEBUG: */ System.out.println("AdminContactController::copyContactToController(): EXIT!"); //NOI18N
393 public Contact createContactInstance () {
394 // Generate phone number
395 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
396 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
397 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
399 // Create new instance
400 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
403 contact.setContactBirthday(this.getBirthday());
404 contact.setContactStreet(this.getStreet());
405 contact.setContactHouseNumber(this.getHouseNumber());
406 contact.setContactZipCode(this.getZipCode());
407 contact.setContactCity(this.getCity());
408 contact.setContactCountry(this.getCountry());
409 contact.setContactEmailAddress(this.getEmailAddress());
410 contact.setContactBirthday(this.getBirthday());
411 contact.setContactComment(this.getComment());
414 contact.setContactOwnContact(Boolean.TRUE);
416 // Don't set null or wrong references
417 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
418 // Now the number must be given
419 if (phone.getPhoneAreaCode() == null) {
421 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
422 } else if (phone.getPhoneAreaCode() < 1) {
424 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
425 } else if (phone.getPhoneNumber() == null) {
427 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
428 } else if (phone.getPhoneNumber() < 1) {
430 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
434 contact.setContactLandLineNumber(phone);
437 // Don't set null or wrong references
438 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
439 // Now the number must be given
440 if (fax.getPhoneAreaCode() == null) {
442 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
443 } else if (fax.getPhoneAreaCode() < 1) {
445 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
446 } else if (fax.getPhoneNumber() == null) {
448 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
449 } else if (fax.getPhoneNumber() < 1) {
451 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
455 contact.setContactFaxNumber(fax);
458 // Is the provider set?
459 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
460 // Is the number set?
461 if (cellphone.getPhoneNumber() == null) {
463 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
464 } else if (cellphone.getPhoneNumber() < 1) {
466 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
469 // Set cellphone number
470 contact.setContactCellphoneNumber(cellphone);
478 public Date getBirthday () {
479 return this.birthday;
483 public void setBirthday (final Date birthday) {
484 this.birthday = birthday;
488 public MobileProvider getCellphoneCarrier () {
489 return this.cellphoneCarrier;
493 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
494 this.cellphoneCarrier = cellphoneCarrier;
498 public Long getCellphoneId () {
499 return this.cellphoneId;
503 public void setCellphoneId (final Long cellphoneId) {
504 this.cellphoneId = cellphoneId;
508 public Long getCellphoneNumber () {
509 return this.cellphoneNumber;
513 public void setCellphoneNumber (Long cellphoneNumber) {
514 this.cellphoneNumber = cellphoneNumber;
518 public String getCity () {
523 public void setCity (final String city) {
528 public String getComment () {
533 public void setComment (final String comment) {
534 this.comment = comment;
538 public Long getContactId () {
539 return this.contactId;
543 public void setContactId (final Long contactId) {
544 this.contactId = contactId;
548 public Country getCountry () {
553 public void setCountry (final Country country) {
554 this.country = country;
558 public String getEmailAddress () {
559 return this.emailAddress;
563 public void setEmailAddress (final String emailAddress) {
564 this.emailAddress = emailAddress;
568 public String getFamilyName () {
569 return this.familyName;
573 public void setFamilyName (final String familyName) {
574 this.familyName = familyName;
578 public Integer getFaxAreaCode () {
579 return this.faxAreaCode;
583 public void setFaxAreaCode (final Integer faxAreaCode) {
584 this.faxAreaCode = faxAreaCode;
588 public Country getFaxCountry () {
589 return this.faxCountry;
593 public void setFaxCountry (final Country faxCountry) {
594 this.faxCountry = faxCountry;
598 public Long getFaxId () {
603 public void setFaxId (final Long faxId) {
608 public Long getFaxNumber () {
609 return this.faxNumber;
613 public void setFaxNumber (final Long faxNumber) {
614 this.faxNumber = faxNumber;
618 public String getFirstName () {
619 return this.firstName;
623 public void setFirstName (final String firstName) {
624 this.firstName = firstName;
628 public Gender getGender () {
633 public void setGender (final Gender gender) {
634 this.gender = gender;
638 public Short getHouseNumber () {
639 return this.houseNumber;
643 public void setHouseNumber (final Short houseNumber) {
644 this.houseNumber = houseNumber;
648 public Long getLandLineId () {
649 return this.landLineId;
653 public void setLandLineId (final Long landLineId) {
654 this.landLineId = landLineId;
658 public Integer getPhoneAreaCode () {
659 return this.phoneAreaCode;
663 public void setPhoneAreaCode (final Integer phoneAreaCode) {
664 this.phoneAreaCode = phoneAreaCode;
668 public Country getPhoneCountry () {
669 return this.phoneCountry;
673 public void setPhoneCountry (final Country phoneCountry) {
674 this.phoneCountry = phoneCountry;
678 public Long getPhoneNumber () {
679 return this.phoneNumber;
683 public void setPhoneNumber (final Long phoneNumber) {
684 this.phoneNumber = phoneNumber;
688 public String getStreet () {
693 public void setStreet (final String street) {
694 this.street = street;
698 public Integer getZipCode () {
703 public void setZipCode (final Integer zipCode) {
704 this.zipCode = zipCode;
708 public boolean hasContacts () {
709 return (!this.allContacts().isEmpty());
713 * Post-initialization of this class
716 public void init () {
722 private void clear () {
725 this.setGender(Gender.UNKNOWN);
726 this.setFirstName(null);
727 this.setFamilyName(null);
728 this.setStreet(null);
729 this.setHouseNumber(null);
730 this.setZipCode(null);
732 this.setCountry(null);
735 this.setEmailAddress(null);
736 this.setPhoneCountry(null);
737 this.setPhoneAreaCode(null);
738 this.setPhoneNumber(null);
739 this.setCellphoneCarrier(null);
740 this.setCellphoneNumber(null);
741 this.setFaxCountry(null);
742 this.setFaxAreaCode(null);
743 this.setFaxNumber(null);
746 this.setBirthday(null);
747 this.setComment(null);
751 * Checks whether the given contact is found
753 * @param contact Contact inastance
755 * @return Wether contact has been found
757 private boolean isSameContactFound (final Contact contact) {
758 // Default is not found
759 boolean IsFound = false;
762 Iterator<Contact> iterator = this.allContacts().iterator();
765 while (iterator.hasNext()) {
767 Contact next = iterator.next();
770 if (ContactUtils.isSameContact(contact, next)) {
771 // Yes, then abort loop
782 * Updates all data in contact instance.
784 * @param contact Contact instance
786 private void updateContactData (final Contact contact) {
787 // Contact instance should be valid
788 if (null == contact) {
790 throw new NullPointerException("contact is null"); //NOI18N
791 } else if (contact.getContactId() == null) {
793 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
794 } else if (contact.getContactId() < 1) {
796 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
800 contact.setContactGender(this.getGender());
801 contact.setContactFirstName(this.getFirstName());
802 contact.setContactFamilyName(this.getFamilyName());
803 contact.setContactStreet(this.getStreet());
804 contact.setContactHouseNumber(this.getHouseNumber());
805 contact.setContactZipCode(this.getZipCode());
806 contact.setContactCity(this.getCity());
807 contact.setContactCountry(this.getCountry());
809 // Update contact's cellphone number
810 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
812 // Update contact's land-line number
813 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
815 // Update contact's fax number
816 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());