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.helper.AddressbookAdminWebRequestController;
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;
53 * An administrative user bean (controller)
55 * @author Roland Haeder<roland@mxchange.org>
57 @Named ("adminContactController")
59 public class AddressbookAdminContactWebRequestBean implements AddressbookAdminContactWebRequestController {
64 private static final long serialVersionUID = 542_145_347_916L;
67 * An event fired when the administrator has added a new contact
71 private Event<AdminAddedContactEvent> addedContactEvent;
74 * Admin helper instance
77 private AddressbookAdminWebRequestController adminHelper;
82 private Date birthday;
85 * Cellphone number's carrier
87 private MobileProvider cellphoneCarrier;
92 private Long cellphoneId;
97 private Long cellphoneNumber;
107 private String comment;
110 * Remote contact bean
112 private final ContactSessionBeanRemote contactBean;
115 * General contact controller
118 private AddressbookContactWebSessionController contactController;
123 private Long contactId;
128 private Country country;
133 private String emailAddress;
138 private String familyName;
141 * Fax number's area code
143 private Integer faxAreaCode;
146 * Country instance for fax number
148 private Country faxCountry;
158 private Long faxNumber;
163 private String firstName;
168 private Gender gender;
173 private Short houseNumber;
176 * Whether a cellphone entry has been unlinked
178 private boolean isCellphoneUnlinked;
181 * Whether a fax entry has been unlinked
183 private boolean isFaxUnlinked;
186 * Whether a land-line number has been unlinked
188 private boolean isLandLineUnlinked;
191 * Land-line id number
193 private Long landLineId;
196 * Phone number area code
198 private Integer phoneAreaCode;
201 * Country instance for phone number
203 private Country phoneCountry;
208 private Long phoneNumber;
213 private String street;
216 * An event fired when the administrator has updated contact data
220 private Event<AdminUpdatedContactEvent> updatedContactEvent;
225 private Integer zipCode;
228 * Default constructor
230 public AddressbookAdminContactWebRequestBean () {
233 // Get initial context
234 Context context = new InitialContext();
237 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/addressbook-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
238 } catch (final NamingException e) {
240 throw new FaceletException(e);
245 public String addContact () {
246 // Are all minimum fields set?
247 if (this.getGender() == null) {
249 throw new NullPointerException("gender is null"); //NOI18N
250 } else if (this.getFirstName() == null) {
252 throw new NullPointerException("firstName is null"); //NOI18N
253 } else if (this.getFirstName().isEmpty()) {
255 throw new IllegalStateException("firstName is empty"); //NOI18N
256 } else if (this.getFamilyName() == null) {
258 throw new NullPointerException("familyName is null"); //NOI18N
259 } else if (this.getFamilyName().isEmpty()) {
261 throw new IllegalStateException("familyName is empty"); //NOI18N
264 // Create new contact instance
265 Contact contact = this.createContactInstance();
267 // Default is not same contact
268 if (this.isSameContactFound(contact)) {
269 // Already registered
270 throw new FaceletException(new ContactAlreadyAddedException(contact));
274 Contact updatedContact;
279 updatedContact = this.contactBean.addContact(contact);
280 } catch (final ContactAlreadyAddedException ex) {
282 throw new FaceletException(ex);
286 this.addedContactEvent.fire(new AdminContactAddedEvent(updatedContact));
292 return "admin_list_contact"; //NOI18N
296 public void copyContactToController (final Contact contact) {
297 // The contact instance must be valid
298 if (null == contact) {
300 throw new NullPointerException("contact is null"); //NOI18N
301 } else if (contact.getContactId() == null) {
303 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
304 } else if (contact.getContactId() < 1) {
306 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
309 // Set all fields: contact
310 this.setContactId(contact.getContactId());
311 this.setBirthday(contact.getContactBirthday());
312 this.setCity(contact.getContactCity());
313 this.setComment(contact.getContactComment());
314 this.setCountry(contact.getContactCountry());
315 this.setEmailAddress(contact.getContactEmailAddress());
316 this.setFamilyName(contact.getContactFamilyName());
317 this.setFirstName(contact.getContactFirstName());
318 this.setGender(contact.getContactGender());
319 this.setHouseNumber(contact.getContactHouseNumber());
320 this.setStreet(contact.getContactStreet());
321 this.setZipCode(contact.getContactZipCode());
323 // ... cellphone data
324 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
325 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
326 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
329 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
330 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
331 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
332 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
335 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
336 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
337 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
338 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
342 public Contact createContactInstance () {
343 // Generate phone number
344 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
345 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
346 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
348 // Create new instance
349 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
351 // Check if contact instance is in helper and valid
352 if (null == contact) {
354 throw new NullPointerException("adminHelper.contact is null"); //NOI18N
355 } else if (contact.getContactId() == null) {
357 throw new NullPointerException("adminHelper.contact.contactId is null"); //NOI18N //NOI18N
358 } else if (contact.getContactId() < 1) {
360 throw new IllegalStateException(MessageFormat.format("adminHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
363 // Update all data in contact
364 this.updateContactData(contact);
366 // Call EJB for updating contact data
367 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
370 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
380 public String editContactData () {
381 // Get contact instance
382 Contact contact = this.adminHelper.getContact();
384 // Check if contact instance is in helper and valid
385 if (null == contact) {
387 throw new NullPointerException("adminHelper.contact is null"); //NOI18N
388 } else if (contact.getContactId() == null) {
390 throw new NullPointerException("adminHelper.contact.contactId is null"); //NOI18N //NOI18N
391 } else if (contact.getContactId() < 1) {
393 throw new IllegalStateException(MessageFormat.format("adminHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
396 // Update all data in contact
397 this.updateContactData(contact);
399 // Call EJB for updating contact data
400 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
403 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
408 // Return to contact list (for now)
409 return "admin_list_contact"; //NOI18N
413 @SuppressWarnings ("ReturnOfDateField")
414 public Date getBirthday () {
415 return this.birthday;
419 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
420 public void setBirthday (final Date birthday) {
421 this.birthday = birthday;
425 public MobileProvider getCellphoneCarrier () {
426 return this.cellphoneCarrier;
430 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
431 this.cellphoneCarrier = cellphoneCarrier;
435 public Long getCellphoneId () {
436 return this.cellphoneId;
440 public void setCellphoneId (final Long cellphoneId) {
441 this.cellphoneId = cellphoneId;
445 public Long getCellphoneNumber () {
446 return this.cellphoneNumber;
450 public void setCellphoneNumber (Long cellphoneNumber) {
451 this.cellphoneNumber = cellphoneNumber;
455 public String getCity () {
460 public void setCity (final String city) {
465 public String getComment () {
470 public void setComment (final String comment) {
471 this.comment = comment;
475 public Long getContactId () {
476 return this.contactId;
480 public void setContactId (final Long contactId) {
481 this.contactId = contactId;
485 public Country getCountry () {
490 public void setCountry (final Country country) {
491 this.country = country;
495 public String getEmailAddress () {
496 return this.emailAddress;
500 public void setEmailAddress (final String emailAddress) {
501 this.emailAddress = emailAddress;
505 public String getFamilyName () {
506 return this.familyName;
510 public void setFamilyName (final String familyName) {
511 this.familyName = familyName;
515 public Integer getFaxAreaCode () {
516 return this.faxAreaCode;
520 public void setFaxAreaCode (final Integer faxAreaCode) {
521 this.faxAreaCode = faxAreaCode;
525 public Country getFaxCountry () {
526 return this.faxCountry;
530 public void setFaxCountry (final Country faxCountry) {
531 this.faxCountry = faxCountry;
535 public Long getFaxId () {
540 public void setFaxId (final Long faxId) {
545 public Long getFaxNumber () {
546 return this.faxNumber;
550 public void setFaxNumber (final Long faxNumber) {
551 this.faxNumber = faxNumber;
555 public String getFirstName () {
556 return this.firstName;
560 public void setFirstName (final String firstName) {
561 this.firstName = firstName;
565 public Gender getGender () {
570 public void setGender (final Gender gender) {
571 this.gender = gender;
575 public Short getHouseNumber () {
576 return this.houseNumber;
580 public void setHouseNumber (final Short houseNumber) {
581 this.houseNumber = houseNumber;
585 public Long getLandLineId () {
586 return this.landLineId;
590 public void setLandLineId (final Long landLineId) {
591 this.landLineId = landLineId;
595 public Integer getPhoneAreaCode () {
596 return this.phoneAreaCode;
600 public void setPhoneAreaCode (final Integer phoneAreaCode) {
601 this.phoneAreaCode = phoneAreaCode;
605 public Country getPhoneCountry () {
606 return this.phoneCountry;
610 public void setPhoneCountry (final Country phoneCountry) {
611 this.phoneCountry = phoneCountry;
615 public Long getPhoneNumber () {
616 return this.phoneNumber;
620 public void setPhoneNumber (final Long phoneNumber) {
621 this.phoneNumber = phoneNumber;
625 public String getStreet () {
630 public void setStreet (final String street) {
631 this.street = street;
635 public Integer getZipCode () {
640 public void setZipCode (final Integer zipCode) {
641 this.zipCode = zipCode;
645 * Post-initialization of this class
648 public void init () {
654 private void clear () {
657 this.setGender(Gender.UNKNOWN);
658 this.setFirstName(null);
659 this.setFamilyName(null);
660 this.setStreet(null);
661 this.setHouseNumber(null);
662 this.setZipCode(null);
664 this.setCountry(null);
667 this.setEmailAddress(null);
668 this.setPhoneCountry(null);
669 this.setPhoneAreaCode(null);
670 this.setPhoneNumber(null);
671 this.setCellphoneCarrier(null);
672 this.setCellphoneNumber(null);
673 this.setFaxCountry(null);
674 this.setFaxAreaCode(null);
675 this.setFaxNumber(null);
678 this.setBirthday(null);
679 this.setComment(null);
683 * Checks whether the given contact is found
685 * @param contact Contact inastance
687 * @return Wether contact has been found
689 private boolean isSameContactFound (final Contact contact) {
690 // Default is not found
691 boolean IsFound = false;
694 Iterator<Contact> iterator = this.contactController.allContacts().iterator();
697 while (iterator.hasNext()) {
699 Contact next = iterator.next();
702 if (ContactUtils.isSameContact(contact, next)) {
703 // Yes, then abort loop
714 * Updates all data in contact instance.
716 * @param contact Contact instance
718 private void updateContactData (final Contact contact) {
719 // Contact instance should be valid
720 if (null == contact) {
722 throw new NullPointerException("contact is null"); //NOI18N
723 } else if (contact.getContactId() == null) {
725 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
726 } else if (contact.getContactId() < 1) {
728 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
732 contact.setContactGender(this.getGender());
733 contact.setContactFirstName(this.getFirstName());
734 contact.setContactFamilyName(this.getFamilyName());
735 contact.setContactStreet(this.getStreet());
736 contact.setContactHouseNumber(this.getHouseNumber());
737 contact.setContactZipCode(this.getZipCode());
738 contact.setContactCity(this.getCity());
739 contact.setContactCountry(this.getCountry());
741 // Update contact's cellphone number
742 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
744 // Update contact's land-line number
745 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
747 // Update contact's fax number
748 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());