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/jratecalc-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));
289 return "admin_list_contact"; //NOI18N
293 public void copyContactToController (final Contact contact) {
294 // The contact instance must be valid
295 if (null == contact) {
297 throw new NullPointerException("contact is null"); //NOI18N
298 } else if (contact.getContactId() == null) {
300 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
301 } else if (contact.getContactId() < 1) {
303 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
306 // Set all fields: contact
307 this.setContactId(contact.getContactId());
308 this.setBirthday(contact.getContactBirthday());
309 this.setCity(contact.getContactCity());
310 this.setComment(contact.getContactComment());
311 this.setCountry(contact.getContactCountry());
312 this.setEmailAddress(contact.getContactEmailAddress());
313 this.setFamilyName(contact.getContactFamilyName());
314 this.setFirstName(contact.getContactFirstName());
315 this.setGender(contact.getContactGender());
316 this.setHouseNumber(contact.getContactHouseNumber());
317 this.setStreet(contact.getContactStreet());
318 this.setZipCode(contact.getContactZipCode());
320 // ... cellphone data
321 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
322 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
323 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
326 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
327 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
328 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
329 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
332 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
333 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
334 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
335 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
339 public Contact createContactInstance () {
340 // Generate phone number
341 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
342 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
343 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
345 // Create new instance
346 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
348 // Check if contact instance is in helper and valid
349 if (null == contact) {
351 throw new NullPointerException("adminHelper.contact is null"); //NOI18N
352 } else if (contact.getContactId() == null) {
354 throw new NullPointerException("adminHelper.contact.contactId is null"); //NOI18N //NOI18N
355 } else if (contact.getContactId() < 1) {
357 throw new IllegalStateException(MessageFormat.format("adminHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
360 // Update all data in contact
361 this.updateContactData(contact);
363 // Call EJB for updating contact data
364 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
367 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
377 public String editContactData () {
378 // Get contact instance
379 Contact contact = this.adminHelper.getContact();
381 // Check if contact instance is in helper and valid
382 if (null == contact) {
384 throw new NullPointerException("adminHelper.contact is null"); //NOI18N
385 } else if (contact.getContactId() == null) {
387 throw new NullPointerException("adminHelper.contact.contactId is null"); //NOI18N //NOI18N
388 } else if (contact.getContactId() < 1) {
390 throw new IllegalStateException(MessageFormat.format("adminHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
393 // Update all data in contact
394 this.updateContactData(contact);
396 // Call EJB for updating contact data
397 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
400 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
405 // Return to contact list (for now)
406 return "admin_list_contact"; //NOI18N
410 @SuppressWarnings ("ReturnOfDateField")
411 public Date getBirthday () {
412 return this.birthday;
416 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
417 public void setBirthday (final Date birthday) {
418 this.birthday = birthday;
422 public MobileProvider getCellphoneCarrier () {
423 return this.cellphoneCarrier;
427 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
428 this.cellphoneCarrier = cellphoneCarrier;
432 public Long getCellphoneId () {
433 return this.cellphoneId;
437 public void setCellphoneId (final Long cellphoneId) {
438 this.cellphoneId = cellphoneId;
442 public Long getCellphoneNumber () {
443 return this.cellphoneNumber;
447 public void setCellphoneNumber (Long cellphoneNumber) {
448 this.cellphoneNumber = cellphoneNumber;
452 public String getCity () {
457 public void setCity (final String city) {
462 public String getComment () {
467 public void setComment (final String comment) {
468 this.comment = comment;
472 public Long getContactId () {
473 return this.contactId;
477 public void setContactId (final Long contactId) {
478 this.contactId = contactId;
482 public Country getCountry () {
487 public void setCountry (final Country country) {
488 this.country = country;
492 public String getEmailAddress () {
493 return this.emailAddress;
497 public void setEmailAddress (final String emailAddress) {
498 this.emailAddress = emailAddress;
502 public String getFamilyName () {
503 return this.familyName;
507 public void setFamilyName (final String familyName) {
508 this.familyName = familyName;
512 public Integer getFaxAreaCode () {
513 return this.faxAreaCode;
517 public void setFaxAreaCode (final Integer faxAreaCode) {
518 this.faxAreaCode = faxAreaCode;
522 public Country getFaxCountry () {
523 return this.faxCountry;
527 public void setFaxCountry (final Country faxCountry) {
528 this.faxCountry = faxCountry;
532 public Long getFaxId () {
537 public void setFaxId (final Long faxId) {
542 public Long getFaxNumber () {
543 return this.faxNumber;
547 public void setFaxNumber (final Long faxNumber) {
548 this.faxNumber = faxNumber;
552 public String getFirstName () {
553 return this.firstName;
557 public void setFirstName (final String firstName) {
558 this.firstName = firstName;
562 public Gender getGender () {
567 public void setGender (final Gender gender) {
568 this.gender = gender;
572 public Short getHouseNumber () {
573 return this.houseNumber;
577 public void setHouseNumber (final Short houseNumber) {
578 this.houseNumber = houseNumber;
582 public Long getLandLineId () {
583 return this.landLineId;
587 public void setLandLineId (final Long landLineId) {
588 this.landLineId = landLineId;
592 public Integer getPhoneAreaCode () {
593 return this.phoneAreaCode;
597 public void setPhoneAreaCode (final Integer phoneAreaCode) {
598 this.phoneAreaCode = phoneAreaCode;
602 public Country getPhoneCountry () {
603 return this.phoneCountry;
607 public void setPhoneCountry (final Country phoneCountry) {
608 this.phoneCountry = phoneCountry;
612 public Long getPhoneNumber () {
613 return this.phoneNumber;
617 public void setPhoneNumber (final Long phoneNumber) {
618 this.phoneNumber = phoneNumber;
622 public String getStreet () {
627 public void setStreet (final String street) {
628 this.street = street;
632 public Integer getZipCode () {
637 public void setZipCode (final Integer zipCode) {
638 this.zipCode = zipCode;
642 * Post-initialization of this class
645 public void init () {
651 private void clear () {
654 this.setGender(Gender.UNKNOWN);
655 this.setFirstName(null);
656 this.setFamilyName(null);
657 this.setStreet(null);
658 this.setHouseNumber(null);
659 this.setZipCode(null);
661 this.setCountry(null);
664 this.setEmailAddress(null);
665 this.setPhoneCountry(null);
666 this.setPhoneAreaCode(null);
667 this.setPhoneNumber(null);
668 this.setCellphoneCarrier(null);
669 this.setCellphoneNumber(null);
670 this.setFaxCountry(null);
671 this.setFaxAreaCode(null);
672 this.setFaxNumber(null);
675 this.setBirthday(null);
676 this.setComment(null);
680 * Checks whether the given contact is found
682 * @param contact Contact inastance
684 * @return Wether contact has been found
686 private boolean isSameContactFound (final Contact contact) {
687 // Default is not found
688 boolean IsFound = false;
691 Iterator<Contact> iterator = this.contactController.allContacts().iterator();
694 while (iterator.hasNext()) {
696 Contact next = iterator.next();
699 if (ContactUtils.isSameContact(contact, next)) {
700 // Yes, then abort loop
711 * Updates all data in contact instance.
713 * @param contact Contact instance
715 private void updateContactData (final Contact contact) {
716 // Contact instance should be valid
717 if (null == contact) {
719 throw new NullPointerException("contact is null"); //NOI18N
720 } else if (contact.getContactId() == null) {
722 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
723 } else if (contact.getContactId() < 1) {
725 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
729 contact.setContactGender(this.getGender());
730 contact.setContactFirstName(this.getFirstName());
731 contact.setContactFamilyName(this.getFamilyName());
732 contact.setContactStreet(this.getStreet());
733 contact.setContactHouseNumber(this.getHouseNumber());
734 contact.setContactZipCode(this.getZipCode());
735 contact.setContactCity(this.getCity());
736 contact.setContactCountry(this.getCountry());
738 // Update contact's cellphone number
739 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
741 // Update contact's land-line number
742 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
744 // Update contact's fax number
745 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());