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.addressbook.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.addressbook.beans.BaseAddressbookController;
33 import org.mxchange.addressbook.beans.helper.AddressbookWebRequestController;
34 import org.mxchange.jcontacts.contact.Contact;
35 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
36 import org.mxchange.jcontacts.contact.UserContact;
37 import org.mxchange.jcontacts.contact.gender.Gender;
38 import org.mxchange.jcontacts.contact.utils.ContactUtils;
39 import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent;
40 import org.mxchange.jcontacts.events.contact.add.AdminContactAddedEvent;
41 import org.mxchange.jcontacts.events.contact.update.AdminContactUpdatedEvent;
42 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
43 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
44 import org.mxchange.jcountry.data.Country;
45 import org.mxchange.jphone.phonenumbers.DialableNumber;
46 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
47 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
48 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
49 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
50 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
51 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
52 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
55 * An administrative user bean (controller)
57 * @author Roland Haeder<roland@mxchange.org>
59 @Named ("adminContactController")
61 public class AddressbookAdminContactWebRequestBean extends BaseAddressbookController implements AddressbookAdminContactWebRequestController {
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 AddressbookWebRequestController 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 AddressbookContactWebSessionController 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;
220 private String title;
223 * An event fired when the administrator has updated contact data
227 private Event<AdminUpdatedContactEvent> updatedContactEvent;
232 private Integer zipCode;
235 * Default constructor
237 public AddressbookAdminContactWebRequestBean () {
240 // Get initial context
241 Context context = new InitialContext();
244 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/addressbook-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
245 } catch (final NamingException e) {
247 throw new FaceletException(e);
252 public String addContact () {
253 // Are all minimum fields set?
254 if (this.getGender() == null) {
256 throw new NullPointerException("gender is null"); //NOI18N
257 } else if (this.getFirstName() == null) {
259 throw new NullPointerException("firstName is null"); //NOI18N
260 } else if (this.getFirstName().isEmpty()) {
262 throw new IllegalStateException("firstName is empty"); //NOI18N
263 } else if (this.getFamilyName() == null) {
265 throw new NullPointerException("familyName is null"); //NOI18N
266 } else if (this.getFamilyName().isEmpty()) {
268 throw new IllegalStateException("familyName is empty"); //NOI18N
271 // Create new contact instance
272 Contact contact = this.createContactInstance();
274 // Default is not same contact
275 if (this.isSameContactFound(contact)) {
276 // Already registered
277 throw new FaceletException(new ContactAlreadyAddedException(contact));
281 Contact updatedContact;
286 updatedContact = this.contactBean.addContact(contact);
287 } catch (final ContactAlreadyAddedException ex) {
289 throw new FaceletException(ex);
293 this.addedContactEvent.fire(new AdminContactAddedEvent(updatedContact));
299 return "admin_list_contact"; //NOI18N
303 public void copyContactToController (final Contact contact) {
304 // The contact instance must be valid
305 if (null == contact) {
307 throw new NullPointerException("contact is null"); //NOI18N
308 } else if (contact.getContactId() == null) {
310 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
311 } else if (contact.getContactId() < 1) {
313 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
316 // Set all fields: contact
317 this.setContactId(contact.getContactId());
318 this.setTitle(contact.getContactTitle());
319 this.setBirthday(contact.getContactBirthday());
320 this.setCity(contact.getContactCity());
321 this.setComment(contact.getContactComment());
322 this.setCountry(contact.getContactCountry());
323 this.setEmailAddress(contact.getContactEmailAddress());
324 this.setFamilyName(contact.getContactFamilyName());
325 this.setFirstName(contact.getContactFirstName());
326 this.setGender(contact.getContactGender());
327 this.setHouseNumber(contact.getContactHouseNumber());
328 this.setStreet(contact.getContactStreet());
329 this.setZipCode(contact.getContactZipCode());
331 // ... cellphone data
332 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
333 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
334 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
335 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
339 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
340 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
341 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
342 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
343 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
347 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
348 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
349 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
350 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
351 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
356 public Contact createContactInstance () {
357 // Generate phone number
358 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
359 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
360 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
362 // Create new instance
363 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
365 // Check if contact instance is in helper and valid
366 if (null == contact) {
368 throw new NullPointerException("beanHelper.contact is null"); //NOI18N
369 } else if (contact.getContactId() == null) {
371 throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
372 } else if (contact.getContactId() < 1) {
374 throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
377 // Update all data in contact
378 this.updateContactData(contact);
380 // Call EJB for updating contact data
381 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
384 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
394 public String editContactData () {
395 // Get contact instance
396 Contact contact = this.beanHelper.getContact();
398 // Check if contact instance is in helper and valid
399 if (null == contact) {
401 throw new NullPointerException("beanHelper.contact is null"); //NOI18N
402 } else if (contact.getContactId() == null) {
404 throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
405 } else if (contact.getContactId() < 1) {
407 throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
410 // Update all data in contact
411 this.updateContactData(contact);
413 // Call EJB for updating contact data
414 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
417 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
422 // Return to contact list (for now)
423 return "admin_list_contact"; //NOI18N
427 public String generateCellphoneNumber (final DialableCellphoneNumber cellphoneNumber) {
429 if (null == cellphoneNumber) {
435 String number = String.format(
437 cellphoneNumber.getCellphoneProvider().getProviderCountry().getCountryExternalDialPrefix(),
438 cellphoneNumber.getCellphoneProvider().getProviderDialPrefix(),
439 cellphoneNumber.getPhoneNumber()
447 public String generatePhoneNumber (final DialableNumber phoneNumber) {
449 if (null == phoneNumber) {
455 String number = String.format(
457 phoneNumber.getPhoneCountry().getCountryExternalDialPrefix(),
458 phoneNumber.getPhoneAreaCode(),
459 phoneNumber.getPhoneNumber()
467 @SuppressWarnings ("ReturnOfDateField")
468 public Date getBirthday () {
469 return this.birthday;
473 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
474 public void setBirthday (final Date birthday) {
475 this.birthday = birthday;
479 public MobileProvider getCellphoneCarrier () {
480 return this.cellphoneCarrier;
484 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
485 this.cellphoneCarrier = cellphoneCarrier;
489 public Long getCellphoneId () {
490 return this.cellphoneId;
494 public void setCellphoneId (final Long cellphoneId) {
495 this.cellphoneId = cellphoneId;
499 public Long getCellphoneNumber () {
500 return this.cellphoneNumber;
504 public void setCellphoneNumber (Long cellphoneNumber) {
505 this.cellphoneNumber = cellphoneNumber;
509 public String getCity () {
514 public void setCity (final String city) {
519 public String getComment () {
524 public void setComment (final String comment) {
525 this.comment = comment;
529 public Long getContactId () {
530 return this.contactId;
534 public void setContactId (final Long contactId) {
535 this.contactId = contactId;
539 public Country getCountry () {
544 public void setCountry (final Country country) {
545 this.country = country;
549 public String getEmailAddress () {
550 return this.emailAddress;
554 public void setEmailAddress (final String emailAddress) {
555 this.emailAddress = emailAddress;
559 public String getFamilyName () {
560 return this.familyName;
564 public void setFamilyName (final String familyName) {
565 this.familyName = familyName;
569 public Integer getFaxAreaCode () {
570 return this.faxAreaCode;
574 public void setFaxAreaCode (final Integer faxAreaCode) {
575 this.faxAreaCode = faxAreaCode;
579 public Country getFaxCountry () {
580 return this.faxCountry;
584 public void setFaxCountry (final Country faxCountry) {
585 this.faxCountry = faxCountry;
589 public Long getFaxId () {
594 public void setFaxId (final Long faxId) {
599 public Long getFaxNumber () {
600 return this.faxNumber;
604 public void setFaxNumber (final Long faxNumber) {
605 this.faxNumber = faxNumber;
609 public String getFirstName () {
610 return this.firstName;
614 public void setFirstName (final String firstName) {
615 this.firstName = firstName;
619 public Gender getGender () {
624 public void setGender (final Gender gender) {
625 this.gender = gender;
629 public Short getHouseNumber () {
630 return this.houseNumber;
634 public void setHouseNumber (final Short houseNumber) {
635 this.houseNumber = houseNumber;
639 public Long getLandLineId () {
640 return this.landLineId;
644 public void setLandLineId (final Long landLineId) {
645 this.landLineId = landLineId;
649 public Integer getPhoneAreaCode () {
650 return this.phoneAreaCode;
654 public void setPhoneAreaCode (final Integer phoneAreaCode) {
655 this.phoneAreaCode = phoneAreaCode;
659 public Country getPhoneCountry () {
660 return this.phoneCountry;
664 public void setPhoneCountry (final Country phoneCountry) {
665 this.phoneCountry = phoneCountry;
669 public Long getPhoneNumber () {
670 return this.phoneNumber;
674 public void setPhoneNumber (final Long phoneNumber) {
675 this.phoneNumber = phoneNumber;
679 public String getStreet () {
684 public void setStreet (final String street) {
685 this.street = street;
689 public String getTitle () {
694 public void setTitle (final String title) {
699 public Integer getZipCode () {
704 public void setZipCode (final Integer zipCode) {
705 this.zipCode = zipCode;
709 * Post-initialization of this class
712 public void init () {
718 private void clear () {
721 this.setGender(Gender.UNKNOWN);
723 this.setFirstName(null);
724 this.setFamilyName(null);
725 this.setStreet(null);
726 this.setHouseNumber(null);
727 this.setZipCode(null);
729 this.setCountry(null);
732 this.setEmailAddress(null);
733 this.setPhoneCountry(null);
734 this.setPhoneAreaCode(null);
735 this.setPhoneNumber(null);
736 this.setCellphoneCarrier(null);
737 this.setCellphoneNumber(null);
738 this.setFaxCountry(null);
739 this.setFaxAreaCode(null);
740 this.setFaxNumber(null);
743 this.setBirthday(null);
744 this.setComment(null);
748 * Checks whether the given contact is found
750 * @param contact Contact inastance
752 * @return Wether contact has been found
754 private boolean isSameContactFound (final Contact contact) {
755 // Default is not found
756 boolean IsFound = false;
759 Iterator<Contact> iterator = this.contactController.allContacts().iterator();
762 while (iterator.hasNext()) {
764 Contact next = iterator.next();
767 if (ContactUtils.isSameContact(contact, next)) {
768 // Yes, then abort loop
779 * Updates all data in contact instance.
781 * @param contact Contact instance
783 private void updateContactData (final Contact contact) {
784 // Contact instance should be valid
785 if (null == contact) {
787 throw new NullPointerException("contact is null"); //NOI18N
788 } else if (contact.getContactId() == null) {
790 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
791 } else if (contact.getContactId() < 1) {
793 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
797 contact.setContactGender(this.getGender());
798 contact.setContactTitle(this.getTitle());
799 contact.setContactFirstName(this.getFirstName());
800 contact.setContactFamilyName(this.getFamilyName());
801 contact.setContactStreet(this.getStreet());
802 contact.setContactHouseNumber(this.getHouseNumber());
803 contact.setContactZipCode(this.getZipCode());
804 contact.setContactCity(this.getCity());
805 contact.setContactCountry(this.getCountry());
807 // Update contact's cellphone number
808 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
810 // Update contact's land-line number
811 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
813 // Update contact's fax number
814 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());