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 // Create new contact instance
251 Contact contact = this.createContactInstance();
253 // Default is not same contact
254 if (this.isSameContactFound(contact)) {
255 // Already registered
256 throw new FaceletException(new ContactAlreadyAddedException(contact));
260 Contact updatedContact;
265 updatedContact = this.contactBean.addContact(contact);
266 } catch (final ContactAlreadyAddedException ex) {
268 throw new FaceletException(ex);
272 this.addedContactEvent.fire(new AdminContactAddedEvent(updatedContact));
278 return "admin_list_contact"; //NOI18N
282 public List<Contact> allContacts () {
283 return this.contactController.allContacts();
287 public String changeContactData () {
288 // Get contact instance
289 Contact contact = this.adminHelper.getContact();
291 // Check if contact instance is in helper and valid
292 if (null == contact) {
294 throw new NullPointerException("adminHelper.contact is null"); //NOI18N
295 } else if (contact.getContactId() == null) {
297 throw new NullPointerException("adminHelper.contact.contactId is null"); //NOI18N //NOI18N
298 } else if (contact.getContactId() < 1) {
300 throw new IllegalStateException(MessageFormat.format("adminHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
303 // Update all data in contact
304 this.updateContactData(contact);
306 // Call EJB for updating contact data
307 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
310 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
312 // Return to contact list (for now)
313 return "admin_list_contact"; //NOI18N
317 public void copyContactToController (final Contact contact) {
319 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("AdminContactController::copyContactToController(): contact={0} - CALLED!", contact)); //NOI18N
321 // The contact instance must be valid
322 if (null == contact) {
324 throw new NullPointerException("contact is null"); //NOI18N
325 } else if (contact.getContactId() == null) {
327 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
328 } else if (contact.getContactId() < 1) {
330 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
333 // Set all fields: contact
334 this.setContactId(contact.getContactId());
335 this.setBirthday(contact.getContactBirthday());
336 this.setCity(contact.getContactCity());
337 this.setComment(contact.getContactComment());
338 this.setCountry(contact.getContactCountry());
339 this.setEmailAddress(contact.getContactEmailAddress());
340 this.setFamilyName(contact.getContactFamilyName());
341 this.setFirstName(contact.getContactFirstName());
342 this.setGender(contact.getContactGender());
343 this.setHouseNumber(contact.getContactHouseNumber());
344 this.setStreet(contact.getContactStreet());
345 this.setZipCode(contact.getContactZipCode());
347 // Is the cell phone set?
348 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
349 // ... cellphone data
350 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
351 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
352 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
356 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
358 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
359 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
360 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
361 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
364 // Is the land-line number set?
365 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
367 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
368 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
369 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
370 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
374 //* NOISY-DEBUG: */ System.out.println("AdminContactController::copyContactToController(): EXIT!"); //NOI18N
378 public Contact createContactInstance () {
379 // Are all minimum fields set?
380 if (this.getGender() == null) {
382 throw new NullPointerException("gender is null"); //NOI18N
383 } else if (this.getFirstName() == null) {
385 throw new NullPointerException("firstName is null"); //NOI18N
386 } else if (this.getFirstName().isEmpty()) {
388 throw new IllegalStateException("firstName is empty"); //NOI18N
389 } else if (this.getFamilyName() == null) {
391 throw new NullPointerException("familyName is null"); //NOI18N
392 } else if (this.getFamilyName().isEmpty()) {
394 throw new IllegalStateException("familyName is empty"); //NOI18N
397 // Generate phone number
398 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
399 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
400 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
402 // Create new instance
403 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
406 contact.setContactBirthday(this.getBirthday());
407 contact.setContactStreet(this.getStreet());
408 contact.setContactHouseNumber(this.getHouseNumber());
409 contact.setContactZipCode(this.getZipCode());
410 contact.setContactCity(this.getCity());
411 contact.setContactCountry(this.getCountry());
412 contact.setContactEmailAddress(this.getEmailAddress());
413 contact.setContactBirthday(this.getBirthday());
414 contact.setContactComment(this.getComment());
417 contact.setContactOwnContact(Boolean.TRUE);
419 // Don't set null or wrong references
420 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
421 // Now the number must be given
422 if (phone.getPhoneAreaCode() == null) {
424 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
425 } else if (phone.getPhoneAreaCode() < 1) {
427 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
428 } else if (phone.getPhoneNumber() == null) {
430 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
431 } else if (phone.getPhoneNumber() < 1) {
433 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
437 contact.setContactLandLineNumber(phone);
440 // Don't set null or wrong references
441 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
442 // Now the number must be given
443 if (fax.getPhoneAreaCode() == null) {
445 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
446 } else if (fax.getPhoneAreaCode() < 1) {
448 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
449 } else if (fax.getPhoneNumber() == null) {
451 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
452 } else if (fax.getPhoneNumber() < 1) {
454 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
458 contact.setContactFaxNumber(fax);
461 // Is the provider set?
462 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
463 // Is the number set?
464 if (cellphone.getPhoneNumber() == null) {
466 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
467 } else if (cellphone.getPhoneNumber() < 1) {
469 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
472 // Set cellphone number
473 contact.setContactCellphoneNumber(cellphone);
481 public Date getBirthday () {
482 return this.birthday;
486 public void setBirthday (final Date birthday) {
487 this.birthday = birthday;
491 public MobileProvider getCellphoneCarrier () {
492 return this.cellphoneCarrier;
496 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
497 this.cellphoneCarrier = cellphoneCarrier;
501 public Long getCellphoneId () {
502 return this.cellphoneId;
506 public void setCellphoneId (final Long cellphoneId) {
507 this.cellphoneId = cellphoneId;
511 public Long getCellphoneNumber () {
512 return this.cellphoneNumber;
516 public void setCellphoneNumber (Long cellphoneNumber) {
517 this.cellphoneNumber = cellphoneNumber;
521 public String getCity () {
526 public void setCity (final String city) {
531 public String getComment () {
536 public void setComment (final String comment) {
537 this.comment = comment;
541 public Long getContactId () {
542 return this.contactId;
546 public void setContactId (final Long contactId) {
547 this.contactId = contactId;
551 public Country getCountry () {
556 public void setCountry (final Country country) {
557 this.country = country;
561 public String getEmailAddress () {
562 return this.emailAddress;
566 public void setEmailAddress (final String emailAddress) {
567 this.emailAddress = emailAddress;
571 public String getFamilyName () {
572 return this.familyName;
576 public void setFamilyName (final String familyName) {
577 this.familyName = familyName;
581 public Integer getFaxAreaCode () {
582 return this.faxAreaCode;
586 public void setFaxAreaCode (final Integer faxAreaCode) {
587 this.faxAreaCode = faxAreaCode;
591 public Country getFaxCountry () {
592 return this.faxCountry;
596 public void setFaxCountry (final Country faxCountry) {
597 this.faxCountry = faxCountry;
601 public Long getFaxId () {
606 public void setFaxId (final Long faxId) {
611 public Long getFaxNumber () {
612 return this.faxNumber;
616 public void setFaxNumber (final Long faxNumber) {
617 this.faxNumber = faxNumber;
621 public String getFirstName () {
622 return this.firstName;
626 public void setFirstName (final String firstName) {
627 this.firstName = firstName;
631 public Gender getGender () {
636 public void setGender (final Gender gender) {
637 this.gender = gender;
641 public Short getHouseNumber () {
642 return this.houseNumber;
646 public void setHouseNumber (final Short houseNumber) {
647 this.houseNumber = houseNumber;
651 public Long getLandLineId () {
652 return this.landLineId;
656 public void setLandLineId (final Long landLineId) {
657 this.landLineId = landLineId;
661 public Integer getPhoneAreaCode () {
662 return this.phoneAreaCode;
666 public void setPhoneAreaCode (final Integer phoneAreaCode) {
667 this.phoneAreaCode = phoneAreaCode;
671 public Country getPhoneCountry () {
672 return this.phoneCountry;
676 public void setPhoneCountry (final Country phoneCountry) {
677 this.phoneCountry = phoneCountry;
681 public Long getPhoneNumber () {
682 return this.phoneNumber;
686 public void setPhoneNumber (final Long phoneNumber) {
687 this.phoneNumber = phoneNumber;
691 public String getStreet () {
696 public void setStreet (final String street) {
697 this.street = street;
701 public Integer getZipCode () {
706 public void setZipCode (final Integer zipCode) {
707 this.zipCode = zipCode;
711 public boolean hasContacts () {
712 return (!this.allContacts().isEmpty());
716 * Post-initialization of this class
719 public void init () {
725 private void clear () {
728 this.setGender(Gender.UNKNOWN);
729 this.setFirstName(null);
730 this.setFamilyName(null);
731 this.setStreet(null);
732 this.setHouseNumber(null);
733 this.setZipCode(null);
735 this.setCountry(null);
738 this.setEmailAddress(null);
739 this.setPhoneCountry(null);
740 this.setPhoneAreaCode(null);
741 this.setPhoneNumber(null);
742 this.setCellphoneCarrier(null);
743 this.setCellphoneNumber(null);
744 this.setFaxCountry(null);
745 this.setFaxAreaCode(null);
746 this.setFaxNumber(null);
749 this.setBirthday(null);
750 this.setComment(null);
754 * Checks whether the given contact is found
756 * @param contact Contact inastance
758 * @return Wether contact has been found
760 private boolean isSameContactFound (final Contact contact) {
761 // Default is not found
762 boolean IsFound = false;
765 Iterator<Contact> iterator = this.allContacts().iterator();
768 while (iterator.hasNext()) {
770 Contact next = iterator.next();
773 if (ContactUtils.isSameContact(contact, next)) {
774 // Yes, then abort loop
785 * Updates all data in contact instance.
787 * @param contact Contact instance
789 private void updateContactData (final Contact contact) {
790 // Contact instance should be valid
791 if (null == contact) {
793 throw new NullPointerException("contact is null"); //NOI18N
794 } else if (contact.getContactId() == null) {
796 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
797 } else if (contact.getContactId() < 1) {
799 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
803 contact.setContactGender(this.getGender());
804 contact.setContactFirstName(this.getFirstName());
805 contact.setContactFamilyName(this.getFamilyName());
806 contact.setContactStreet(this.getStreet());
807 contact.setContactHouseNumber(this.getHouseNumber());
808 contact.setContactZipCode(this.getZipCode());
809 contact.setContactCity(this.getCity());
810 contact.setContactCountry(this.getCountry());
812 // Update contact's cellphone number
813 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
815 // Update contact's land-line number
816 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
818 // Update contact's fax number
819 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());