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.BasePizzaController;
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 extends BasePizzaController 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 () {
234 // Get initial context
235 Context context = new InitialContext();
238 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
239 } catch (final NamingException e) {
241 throw new FaceletException(e);
246 public String addContact () {
247 // Create new contact instance
248 Contact contact = this.createContactInstance();
250 // Default is not same contact
251 if (this.isSameContactFound(contact)) {
252 // Already registered
253 throw new FaceletException(new ContactAlreadyAddedException(contact));
257 Contact updatedContact;
262 updatedContact = this.contactBean.addContact(contact);
263 } catch (final ContactAlreadyAddedException ex) {
265 throw new FaceletException(ex);
269 this.addedContactEvent.fire(new AdminContactAddedEvent(updatedContact));
275 return "admin_list_contact"; //NOI18N
279 public void copyContactToController (final Contact contact) {
281 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("AdminContactController::copyContactToController(): contact={0} - CALLED!", contact)); //NOI18N
283 // The contact instance must be valid
284 if (null == contact) {
286 throw new NullPointerException("contact is null"); //NOI18N
287 } else if (contact.getContactId() == null) {
289 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
290 } else if (contact.getContactId() < 1) {
292 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
295 // Set all fields: contact
296 this.setContactId(contact.getContactId());
297 this.setBirthday(contact.getContactBirthday());
298 this.setCity(contact.getContactCity());
299 this.setComment(contact.getContactComment());
300 this.setCountry(contact.getContactCountry());
301 this.setEmailAddress(contact.getContactEmailAddress());
302 this.setFamilyName(contact.getContactFamilyName());
303 this.setFirstName(contact.getContactFirstName());
304 this.setGender(contact.getContactGender());
305 this.setHouseNumber(contact.getContactHouseNumber());
306 this.setStreet(contact.getContactStreet());
307 this.setZipCode(contact.getContactZipCode());
309 // Is the cell phone set?
310 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
311 // ... cellphone data
312 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
313 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
314 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
318 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
320 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
321 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
322 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
323 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
326 // Is the land-line number set?
327 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
329 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
330 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
331 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
332 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
336 //* NOISY-DEBUG: */ System.out.println("AdminContactController::copyContactToController(): EXIT!"); //NOI18N
340 public Contact createContactInstance () {
341 // Are all minimum fields set?
342 if (this.getGender() == null) {
344 throw new NullPointerException("gender is null"); //NOI18N
345 } else if (this.getFirstName() == null) {
347 throw new NullPointerException("firstName is null"); //NOI18N
348 } else if (this.getFirstName().isEmpty()) {
350 throw new IllegalStateException("firstName is empty"); //NOI18N
351 } else if (this.getFamilyName() == null) {
353 throw new NullPointerException("familyName is null"); //NOI18N
354 } else if (this.getFamilyName().isEmpty()) {
356 throw new IllegalStateException("familyName is empty"); //NOI18N
359 // Generate phone number
360 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
361 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
362 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
364 // Create new instance
365 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
368 contact.setContactBirthday(this.getBirthday());
369 contact.setContactStreet(this.getStreet());
370 contact.setContactHouseNumber(this.getHouseNumber());
371 contact.setContactZipCode(this.getZipCode());
372 contact.setContactCity(this.getCity());
373 contact.setContactCountry(this.getCountry());
374 contact.setContactEmailAddress(this.getEmailAddress());
375 contact.setContactBirthday(this.getBirthday());
376 contact.setContactComment(this.getComment());
379 contact.setContactOwnContact(Boolean.TRUE);
381 // Don't set null or wrong references
382 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
383 // Now the number must be given
384 if (phone.getPhoneAreaCode() == null) {
386 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
387 } else if (phone.getPhoneAreaCode() < 1) {
389 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
390 } else if (phone.getPhoneNumber() == null) {
392 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
393 } else if (phone.getPhoneNumber() < 1) {
395 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
399 contact.setContactLandLineNumber(phone);
402 // Don't set null or wrong references
403 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
404 // Now the number must be given
405 if (fax.getPhoneAreaCode() == null) {
407 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
408 } else if (fax.getPhoneAreaCode() < 1) {
410 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
411 } else if (fax.getPhoneNumber() == null) {
413 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
414 } else if (fax.getPhoneNumber() < 1) {
416 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
420 contact.setContactFaxNumber(fax);
423 // Is the provider set?
424 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
425 // Is the number set?
426 if (cellphone.getPhoneNumber() == null) {
428 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
429 } else if (cellphone.getPhoneNumber() < 1) {
431 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
434 // Set cellphone number
435 contact.setContactCellphoneNumber(cellphone);
443 public String editContactData () {
444 // Get contact instance
445 Contact contact = this.adminHelper.getContact();
447 // Check if contact instance is in helper and valid
448 if (null == contact) {
450 throw new NullPointerException("adminHelper.contact is null"); //NOI18N
451 } else if (contact.getContactId() == null) {
453 throw new NullPointerException("adminHelper.contact.contactId is null"); //NOI18N //NOI18N
454 } else if (contact.getContactId() < 1) {
456 throw new IllegalStateException(MessageFormat.format("adminHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
459 // Update all data in contact
460 this.updateContactData(contact);
462 // Call EJB for updating contact data
463 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
466 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
471 // Return to contact list (for now)
472 return "admin_list_contact"; //NOI18N
476 @SuppressWarnings ("ReturnOfDateField")
477 public Date getBirthday () {
478 return this.birthday;
482 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
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 * Post-initialization of this class
711 public void init () {
717 private void clear () {
720 this.setGender(Gender.UNKNOWN);
721 this.setFirstName(null);
722 this.setFamilyName(null);
723 this.setStreet(null);
724 this.setHouseNumber(null);
725 this.setZipCode(null);
727 this.setCountry(null);
730 this.setEmailAddress(null);
731 this.setPhoneCountry(null);
732 this.setPhoneAreaCode(null);
733 this.setPhoneNumber(null);
734 this.setCellphoneCarrier(null);
735 this.setCellphoneNumber(null);
736 this.setFaxCountry(null);
737 this.setFaxAreaCode(null);
738 this.setFaxNumber(null);
741 this.setBirthday(null);
742 this.setComment(null);
746 * Checks whether the given contact is found
748 * @param contact Contact inastance
750 * @return Wether contact has been found
752 private boolean isSameContactFound (final Contact contact) {
753 // Default is not found
754 boolean IsFound = false;
757 Iterator<Contact> iterator = this.contactController.allContacts().iterator();
760 while (iterator.hasNext()) {
762 Contact next = iterator.next();
765 if (ContactUtils.isSameContact(contact, next)) {
766 // Yes, then abort loop
777 * Updates all data in contact instance.
779 * @param contact Contact instance
781 private void updateContactData (final Contact contact) {
782 // Contact instance should be valid
783 if (null == contact) {
785 throw new NullPointerException("contact is null"); //NOI18N
786 } else if (contact.getContactId() == null) {
788 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
789 } else if (contact.getContactId() < 1) {
791 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
795 contact.setContactGender(this.getGender());
796 contact.setContactFirstName(this.getFirstName());
797 contact.setContactFamilyName(this.getFamilyName());
798 contact.setContactStreet(this.getStreet());
799 contact.setContactHouseNumber(this.getHouseNumber());
800 contact.setContactZipCode(this.getZipCode());
801 contact.setContactCity(this.getCity());
802 contact.setContactCountry(this.getCountry());
804 // Update contact's cellphone number
805 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
807 // Update contact's land-line number
808 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
810 // Update contact's fax number
811 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());