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.List;
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.update.AdminContactUpdatedEvent;
39 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
40 import org.mxchange.jcountry.data.Country;
41 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
42 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
43 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
44 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
45 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
46 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
47 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
48 import org.mxchange.pizzaapplication.beans.helper.PizzaAdminWebRequestController;
51 * Administrative user bean (controller)
53 * @author Roland Haeder<roland@mxchange.org>
55 @Named ("adminContactController")
57 public class PizzaAdminContactWebRequestBean implements PizzaAdminContactWebRequestController {
62 private static final long serialVersionUID = 542_145_347_916L;
65 * An event fired when the administrator has added a new contact
69 private Event<AdminAddedContactEvent> addedContactEvent;
72 * Admin helper instance
75 private PizzaAdminWebRequestController adminHelper;
80 private Date birthday;
83 * Cellphone number's carrier
85 private MobileProvider cellphoneCarrier;
90 private Long cellphoneId;
95 private Long cellphoneNumber;
105 private String comment;
108 * Remote contact bean
110 private final ContactSessionBeanRemote contactBean;
113 * General contact controller
116 private PizzaContactWebSessionController contactController;
121 private Long contactId;
126 private Country country;
131 private String emailAddress;
136 private String familyName;
139 * Fax number's area code
141 private Integer faxAreaCode;
144 * Country instance for fax number
146 private Country faxCountry;
156 private Long faxNumber;
161 private String firstName;
166 private Gender gender;
171 private Short houseNumber;
174 * Whether a cellphone entry has been unlinked
176 private boolean isCellphoneUnlinked;
179 * Whether a fax entry has been unlinked
181 private boolean isFaxUnlinked;
184 * Whether a land-line number has been unlinked
186 private boolean isLandLineUnlinked;
189 * Land-line id number
191 private Long landLineId;
194 * Phone number area code
196 private Integer phoneAreaCode;
199 * Country instance for phone number
201 private Country phoneCountry;
206 private Long phoneNumber;
211 private String street;
214 * An event fired when the administrator has updated contact data
218 private Event<AdminUpdatedContactEvent> updatedContactEvent;
223 private Integer zipCode;
226 * Default constructor
228 public PizzaAdminContactWebRequestBean () {
229 // Set gender to UNKNOWN
230 this.gender = Gender.UNKNOWN;
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 List<Contact> allContacts () {
247 return this.contactController.allContacts();
251 public String changeContactData () {
252 // Get contact instance
253 Contact contact = this.adminHelper.getContact();
255 // Check if contact instance is in helper and valid
256 if (null == contact) {
258 throw new NullPointerException("adminHelper.contact is null"); //NOI18N
259 } else if (contact.getContactId() == null) {
261 throw new NullPointerException("adminHelper.contact.contactId is null"); //NOI18N //NOI18N
262 } else if (contact.getContactId() < 1) {
264 throw new IllegalStateException(MessageFormat.format("adminHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
267 // Update all data in contact
268 this.updateContactData(contact);
270 // Call EJB for updating contact data
271 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
274 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
276 // Return to contact list (for now)
277 return "admin_list_contact"; //NOI18N
281 public void copyContactToController (final Contact contact) {
283 System.out.println(MessageFormat.format("AdminContactController::copyContactToController(): contact={0} - CALLED!", contact)); //NOI18N
285 // The contact instance must be valid
286 if (null == contact) {
288 throw new NullPointerException("contact is null"); //NOI18N
289 } else if (contact.getContactId() == null) {
291 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
292 } else if (contact.getContactId() < 1) {
294 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
297 // Set all fields: contact
298 this.setContactId(contact.getContactId());
299 this.setBirthday(contact.getContactBirthday());
300 this.setCity(contact.getContactCity());
301 this.setComment(contact.getContactComment());
302 this.setCountry(contact.getContactCountry());
303 this.setEmailAddress(contact.getContactEmailAddress());
304 this.setFamilyName(contact.getContactFamilyName());
305 this.setFirstName(contact.getContactFirstName());
306 this.setGender(contact.getContactGender());
307 this.setHouseNumber(contact.getContactHouseNumber());
308 this.setStreet(contact.getContactStreet());
309 this.setZipCode(contact.getContactZipCode());
311 // Is the cell phone set?
312 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
313 // ... cellphone data
314 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
315 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
316 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
320 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
322 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
323 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
324 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
325 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
328 // Is the land-line number set?
329 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
331 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
332 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
333 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
334 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
338 System.out.println("AdminContactController::copyContactToController(): EXIT!"); //NOI18N
342 public Contact createContactInstance () {
344 //this.getLogger().logTrace("createContactInstance: CALLED!");
346 // Generate phone number
347 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
348 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
349 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
351 // Create new instance
352 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
355 contact.setContactBirthday(this.getBirthday());
356 contact.setContactStreet(this.getStreet());
357 contact.setContactHouseNumber(this.getHouseNumber());
358 contact.setContactZipCode(this.getZipCode());
359 contact.setContactCity(this.getCity());
360 contact.setContactCountry(this.getCountry());
361 contact.setContactEmailAddress(this.getEmailAddress());
362 contact.setContactBirthday(this.getBirthday());
363 contact.setContactComment(this.getComment());
366 contact.setContactOwnContact(Boolean.TRUE);
368 // Don't set null or wrong references
369 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
370 // Now the number must be given
371 if (phone.getPhoneAreaCode() == null) {
373 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
374 } else if (phone.getPhoneAreaCode() < 1) {
376 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
377 } else if (phone.getPhoneNumber() == null) {
379 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
380 } else if (phone.getPhoneNumber() < 1) {
382 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
386 contact.setContactLandLineNumber(phone);
389 // Don't set null or wrong references
390 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
391 // Now the number must be given
392 if (fax.getPhoneAreaCode() == null) {
394 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
395 } else if (fax.getPhoneAreaCode() < 1) {
397 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
398 } else if (fax.getPhoneNumber() == null) {
400 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
401 } else if (fax.getPhoneNumber() < 1) {
403 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
407 contact.setContactFaxNumber(fax);
410 // Is the provider set?
411 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
412 // Is the number set?
413 if (cellphone.getPhoneNumber() == null) {
415 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
416 } else if (cellphone.getPhoneNumber() < 1) {
418 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
421 // Set cellphone number
422 contact.setContactCellphoneNumber(cellphone);
426 //this.getLogger().logTrace(MessageFormat.format("createContactInstance: localContact={0} - EXIT!", localContact));
433 public Date getBirthday () {
434 return this.birthday;
438 public void setBirthday (final Date birthday) {
439 this.birthday = birthday;
443 public MobileProvider getCellphoneCarrier () {
444 return this.cellphoneCarrier;
448 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
449 this.cellphoneCarrier = cellphoneCarrier;
453 public Long getCellphoneId () {
454 return this.cellphoneId;
458 public void setCellphoneId (final Long cellphoneId) {
459 this.cellphoneId = cellphoneId;
463 public Long getCellphoneNumber () {
464 return this.cellphoneNumber;
468 public void setCellphoneNumber (Long cellphoneNumber) {
469 this.cellphoneNumber = cellphoneNumber;
473 public String getCity () {
478 public void setCity (final String city) {
483 public String getComment () {
488 public void setComment (final String comment) {
489 this.comment = comment;
493 public Long getContactId () {
494 return this.contactId;
498 public void setContactId (final Long contactId) {
499 this.contactId = contactId;
503 public Country getCountry () {
508 public void setCountry (final Country country) {
509 this.country = country;
513 public String getEmailAddress () {
514 return this.emailAddress;
518 public void setEmailAddress (final String emailAddress) {
519 this.emailAddress = emailAddress;
523 public String getFamilyName () {
524 return this.familyName;
528 public void setFamilyName (final String familyName) {
529 this.familyName = familyName;
533 public Integer getFaxAreaCode () {
534 return this.faxAreaCode;
538 public void setFaxAreaCode (final Integer faxAreaCode) {
539 this.faxAreaCode = faxAreaCode;
543 public Country getFaxCountry () {
544 return this.faxCountry;
548 public void setFaxCountry (final Country faxCountry) {
549 this.faxCountry = faxCountry;
553 public Long getFaxId () {
558 public void setFaxId (final Long faxId) {
563 public Long getFaxNumber () {
564 return this.faxNumber;
568 public void setFaxNumber (final Long faxNumber) {
569 this.faxNumber = faxNumber;
573 public String getFirstName () {
574 return this.firstName;
578 public void setFirstName (final String firstName) {
579 this.firstName = firstName;
583 public Gender getGender () {
588 public void setGender (final Gender gender) {
589 this.gender = gender;
593 public Short getHouseNumber () {
594 return this.houseNumber;
598 public void setHouseNumber (final Short houseNumber) {
599 this.houseNumber = houseNumber;
603 public Long getLandLineId () {
604 return this.landLineId;
608 public void setLandLineId (final Long landLineId) {
609 this.landLineId = landLineId;
613 public Integer getPhoneAreaCode () {
614 return this.phoneAreaCode;
618 public void setPhoneAreaCode (final Integer phoneAreaCode) {
619 this.phoneAreaCode = phoneAreaCode;
623 public Country getPhoneCountry () {
624 return this.phoneCountry;
628 public void setPhoneCountry (final Country phoneCountry) {
629 this.phoneCountry = phoneCountry;
633 public Long getPhoneNumber () {
634 return this.phoneNumber;
638 public void setPhoneNumber (final Long phoneNumber) {
639 this.phoneNumber = phoneNumber;
643 public String getStreet () {
648 public void setStreet (final String street) {
649 this.street = street;
653 public Integer getZipCode () {
658 public void setZipCode (final Integer zipCode) {
659 this.zipCode = zipCode;
663 public boolean hasContacts () {
664 return (!this.allContacts().isEmpty());
668 * Post-initialization of this class
671 public void init () {
677 private void clear () {
680 this.setGender(Gender.UNKNOWN);
681 this.setFirstName(null);
682 this.setFamilyName(null);
683 this.setStreet(null);
684 this.setHouseNumber(null);
685 this.setZipCode(null);
687 this.setCountry(null);
690 this.setEmailAddress(null);
691 this.setPhoneCountry(null);
692 this.setPhoneAreaCode(null);
693 this.setPhoneNumber(null);
694 this.setCellphoneCarrier(null);
695 this.setCellphoneNumber(null);
696 this.setFaxCountry(null);
697 this.setFaxAreaCode(null);
698 this.setFaxNumber(null);
701 this.setBirthday(null);
702 this.setComment(null);
706 * Updates all data in contact instance.
708 * @param contact Contact instance
710 private void updateContactData (final Contact contact) {
711 // Contact instance should be valid
712 if (null == contact) {
714 throw new NullPointerException("contact is null"); //NOI18N
715 } else if (contact.getContactId() == null) {
717 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
718 } else if (contact.getContactId() < 1) {
720 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
724 contact.setContactGender(this.getGender());
725 contact.setContactFirstName(this.getFirstName());
726 contact.setContactFamilyName(this.getFamilyName());
727 contact.setContactStreet(this.getStreet());
728 contact.setContactHouseNumber(this.getHouseNumber());
729 contact.setContactZipCode(this.getZipCode());
730 contact.setContactCity(this.getCity());
731 contact.setContactCountry(this.getCountry());
733 // Update contact's cellphone number
734 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
736 // Update contact's land-line number
737 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
739 // Update contact's fax number
740 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());