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.jjobs.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.jjobs.beans.BaseJobsController;
44 import org.mxchange.jphone.phonenumbers.DialableNumber;
45 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
46 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
47 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
48 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
49 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
50 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
51 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
52 import org.mxchange.jjobs.beans.helper.JobsWebRequestController;
55 * An administrative user bean (controller)
57 * @author Roland Haeder<roland@mxchange.org>
59 @Named ("adminContactController")
61 public class JobsAdminContactWebRequestBean extends BaseJobsController implements JobsAdminContactWebRequestController {
66 private static final long serialVersionUID = 542_145_347_916L;
69 * An event fired when the administrator has added a new contact
73 private Event<AdminAddedContactEvent> addedContactEvent;
76 * Admin helper instance
79 private JobsWebRequestController beanHelper;
84 private Date birthday;
87 * Cellphone number's carrier
89 private MobileProvider cellphoneCarrier;
94 private Long cellphoneId;
99 private Long cellphoneNumber;
109 private String comment;
112 * Remote contact bean
114 private final ContactSessionBeanRemote contactBean;
117 * General contact controller
120 private JobsContactWebSessionController contactController;
125 private Long contactId;
130 private Country country;
135 private String emailAddress;
140 private String familyName;
143 * Fax number's area code
145 private Integer faxAreaCode;
148 * Country instance for fax number
150 private Country faxCountry;
160 private Long faxNumber;
165 private String firstName;
170 private Gender gender;
175 private Short houseNumber;
178 * Whether a cellphone entry has been unlinked
180 private boolean isCellphoneUnlinked;
183 * Whether a fax entry has been unlinked
185 private boolean isFaxUnlinked;
188 * Whether a land-line number has been unlinked
190 private boolean isLandLineUnlinked;
193 * Land-line id number
195 private Long landLineId;
198 * Phone number area code
200 private Integer phoneAreaCode;
203 * Country instance for phone number
205 private Country phoneCountry;
210 private Long phoneNumber;
215 private String street;
218 * An event fired when the administrator has updated contact data
222 private Event<AdminUpdatedContactEvent> updatedContactEvent;
227 private Integer zipCode;
230 * Default constructor
232 public JobsAdminContactWebRequestBean () {
235 // Get initial context
236 Context context = new InitialContext();
239 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jratecalc-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
240 } catch (final NamingException e) {
242 throw new FaceletException(e);
247 public String addContact () {
248 // Are all minimum fields set?
249 if (this.getGender() == null) {
251 throw new NullPointerException("gender is null"); //NOI18N
252 } else if (this.getFirstName() == null) {
254 throw new NullPointerException("firstName is null"); //NOI18N
255 } else if (this.getFirstName().isEmpty()) {
257 throw new IllegalStateException("firstName is empty"); //NOI18N
258 } else if (this.getFamilyName() == null) {
260 throw new NullPointerException("familyName is null"); //NOI18N
261 } else if (this.getFamilyName().isEmpty()) {
263 throw new IllegalStateException("familyName is empty"); //NOI18N
266 // Create new contact instance
267 Contact contact = this.createContactInstance();
269 // Default is not same contact
270 if (this.isSameContactFound(contact)) {
271 // Already registered
272 throw new FaceletException(new ContactAlreadyAddedException(contact));
276 Contact updatedContact;
281 updatedContact = this.contactBean.addContact(contact);
282 } catch (final ContactAlreadyAddedException ex) {
284 throw new FaceletException(ex);
288 this.addedContactEvent.fire(new AdminContactAddedEvent(updatedContact));
294 return "admin_list_contact"; //NOI18N
298 public void copyContactToController (final Contact contact) {
299 // The contact instance must be valid
300 if (null == contact) {
302 throw new NullPointerException("contact is null"); //NOI18N
303 } else if (contact.getContactId() == null) {
305 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
306 } else if (contact.getContactId() < 1) {
308 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
311 // Set all fields: contact
312 this.setContactId(contact.getContactId());
313 this.setBirthday(contact.getContactBirthday());
314 this.setCity(contact.getContactCity());
315 this.setComment(contact.getContactComment());
316 this.setCountry(contact.getContactCountry());
317 this.setEmailAddress(contact.getContactEmailAddress());
318 this.setFamilyName(contact.getContactFamilyName());
319 this.setFirstName(contact.getContactFirstName());
320 this.setGender(contact.getContactGender());
321 this.setHouseNumber(contact.getContactHouseNumber());
322 this.setStreet(contact.getContactStreet());
323 this.setZipCode(contact.getContactZipCode());
325 // ... cellphone data
326 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
327 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
328 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
331 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
332 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
333 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
334 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
337 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
338 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
339 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
340 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
344 public Contact createContactInstance () {
345 // Generate phone number
346 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
347 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
348 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
350 // Create new instance
351 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
353 // Check if contact instance is in helper and valid
354 if (null == contact) {
356 throw new NullPointerException("beanHelper.contact is null"); //NOI18N
357 } else if (contact.getContactId() == null) {
359 throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
360 } else if (contact.getContactId() < 1) {
362 throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
365 // Update all data in contact
366 this.updateContactData(contact);
368 // Call EJB for updating contact data
369 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
372 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
382 public String editContactData () {
383 // Get contact instance
384 Contact contact = this.beanHelper.getContact();
386 // Check if contact instance is in helper and valid
387 if (null == contact) {
389 throw new NullPointerException("beanHelper.contact is null"); //NOI18N
390 } else if (contact.getContactId() == null) {
392 throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
393 } else if (contact.getContactId() < 1) {
395 throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
398 // Update all data in contact
399 this.updateContactData(contact);
401 // Call EJB for updating contact data
402 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
405 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
410 // Return to contact list (for now)
411 return "admin_list_contact"; //NOI18N
415 public String generateCellphoneNumber (final DialableCellphoneNumber cellphoneNumber) {
417 if (null == cellphoneNumber) {
423 String number = String.format(
425 cellphoneNumber.getCellphoneProvider().getProviderCountry().getCountryExternalDialPrefix(),
426 cellphoneNumber.getCellphoneProvider().getProviderDialPrefix(),
427 cellphoneNumber.getPhoneNumber()
435 public String generatePhoneNumber (final DialableNumber phoneNumber) {
437 if (null == phoneNumber) {
443 String number = String.format(
445 phoneNumber.getPhoneCountry().getCountryExternalDialPrefix(),
446 phoneNumber.getPhoneAreaCode(),
447 phoneNumber.getPhoneNumber()
455 @SuppressWarnings ("ReturnOfDateField")
456 public Date getBirthday () {
457 return this.birthday;
461 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
462 public void setBirthday (final Date birthday) {
463 this.birthday = birthday;
467 public MobileProvider getCellphoneCarrier () {
468 return this.cellphoneCarrier;
472 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
473 this.cellphoneCarrier = cellphoneCarrier;
477 public Long getCellphoneId () {
478 return this.cellphoneId;
482 public void setCellphoneId (final Long cellphoneId) {
483 this.cellphoneId = cellphoneId;
487 public Long getCellphoneNumber () {
488 return this.cellphoneNumber;
492 public void setCellphoneNumber (Long cellphoneNumber) {
493 this.cellphoneNumber = cellphoneNumber;
497 public String getCity () {
502 public void setCity (final String city) {
507 public String getComment () {
512 public void setComment (final String comment) {
513 this.comment = comment;
517 public Long getContactId () {
518 return this.contactId;
522 public void setContactId (final Long contactId) {
523 this.contactId = contactId;
527 public Country getCountry () {
532 public void setCountry (final Country country) {
533 this.country = country;
537 public String getEmailAddress () {
538 return this.emailAddress;
542 public void setEmailAddress (final String emailAddress) {
543 this.emailAddress = emailAddress;
547 public String getFamilyName () {
548 return this.familyName;
552 public void setFamilyName (final String familyName) {
553 this.familyName = familyName;
557 public Integer getFaxAreaCode () {
558 return this.faxAreaCode;
562 public void setFaxAreaCode (final Integer faxAreaCode) {
563 this.faxAreaCode = faxAreaCode;
567 public Country getFaxCountry () {
568 return this.faxCountry;
572 public void setFaxCountry (final Country faxCountry) {
573 this.faxCountry = faxCountry;
577 public Long getFaxId () {
582 public void setFaxId (final Long faxId) {
587 public Long getFaxNumber () {
588 return this.faxNumber;
592 public void setFaxNumber (final Long faxNumber) {
593 this.faxNumber = faxNumber;
597 public String getFirstName () {
598 return this.firstName;
602 public void setFirstName (final String firstName) {
603 this.firstName = firstName;
607 public Gender getGender () {
612 public void setGender (final Gender gender) {
613 this.gender = gender;
617 public Short getHouseNumber () {
618 return this.houseNumber;
622 public void setHouseNumber (final Short houseNumber) {
623 this.houseNumber = houseNumber;
627 public Long getLandLineId () {
628 return this.landLineId;
632 public void setLandLineId (final Long landLineId) {
633 this.landLineId = landLineId;
637 public Integer getPhoneAreaCode () {
638 return this.phoneAreaCode;
642 public void setPhoneAreaCode (final Integer phoneAreaCode) {
643 this.phoneAreaCode = phoneAreaCode;
647 public Country getPhoneCountry () {
648 return this.phoneCountry;
652 public void setPhoneCountry (final Country phoneCountry) {
653 this.phoneCountry = phoneCountry;
657 public Long getPhoneNumber () {
658 return this.phoneNumber;
662 public void setPhoneNumber (final Long phoneNumber) {
663 this.phoneNumber = phoneNumber;
667 public String getStreet () {
672 public void setStreet (final String street) {
673 this.street = street;
677 public Integer getZipCode () {
682 public void setZipCode (final Integer zipCode) {
683 this.zipCode = zipCode;
687 * Post-initialization of this class
690 public void init () {
696 private void clear () {
699 this.setGender(Gender.UNKNOWN);
700 this.setFirstName(null);
701 this.setFamilyName(null);
702 this.setStreet(null);
703 this.setHouseNumber(null);
704 this.setZipCode(null);
706 this.setCountry(null);
709 this.setEmailAddress(null);
710 this.setPhoneCountry(null);
711 this.setPhoneAreaCode(null);
712 this.setPhoneNumber(null);
713 this.setCellphoneCarrier(null);
714 this.setCellphoneNumber(null);
715 this.setFaxCountry(null);
716 this.setFaxAreaCode(null);
717 this.setFaxNumber(null);
720 this.setBirthday(null);
721 this.setComment(null);
725 * Checks whether the given contact is found
727 * @param contact Contact inastance
729 * @return Wether contact has been found
731 private boolean isSameContactFound (final Contact contact) {
732 // Default is not found
733 boolean IsFound = false;
736 Iterator<Contact> iterator = this.contactController.allContacts().iterator();
739 while (iterator.hasNext()) {
741 Contact next = iterator.next();
744 if (ContactUtils.isSameContact(contact, next)) {
745 // Yes, then abort loop
756 * Updates all data in contact instance.
758 * @param contact Contact instance
760 private void updateContactData (final Contact contact) {
761 // Contact instance should be valid
762 if (null == contact) {
764 throw new NullPointerException("contact is null"); //NOI18N
765 } else if (contact.getContactId() == null) {
767 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
768 } else if (contact.getContactId() < 1) {
770 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
774 contact.setContactGender(this.getGender());
775 contact.setContactFirstName(this.getFirstName());
776 contact.setContactFamilyName(this.getFamilyName());
777 contact.setContactStreet(this.getStreet());
778 contact.setContactHouseNumber(this.getHouseNumber());
779 contact.setContactZipCode(this.getZipCode());
780 contact.setContactCity(this.getCity());
781 contact.setContactCountry(this.getCountry());
783 // Update contact's cellphone number
784 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
786 // Update contact's land-line number
787 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
789 // Update contact's fax number
790 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());