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.context.FacesContext;
27 import javax.faces.view.facelets.FaceletException;
28 import javax.inject.Inject;
29 import javax.inject.Named;
30 import javax.naming.Context;
31 import javax.naming.InitialContext;
32 import javax.naming.NamingException;
33 import org.mxchange.addressbook.beans.BaseAddressbookController;
34 import org.mxchange.addressbook.beans.helper.AddressbookWebRequestController;
35 import org.mxchange.jcontacts.contact.Contact;
36 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
37 import org.mxchange.jcontacts.contact.UserContact;
38 import org.mxchange.jcontacts.contact.gender.Gender;
39 import org.mxchange.jcontacts.contact.utils.ContactUtils;
40 import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent;
41 import org.mxchange.jcontacts.events.contact.add.AdminContactAddedEvent;
42 import org.mxchange.jcontacts.events.contact.update.AdminContactUpdatedEvent;
43 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
44 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
45 import org.mxchange.jcountry.data.Country;
46 import org.mxchange.jphone.phonenumbers.DialableNumber;
47 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
48 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
49 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
50 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
51 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
52 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
53 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
56 * An administrative user bean (controller)
58 * @author Roland Haeder<roland@mxchange.org>
60 @Named ("adminContactController")
62 public class AddressbookAdminContactWebRequestBean extends BaseAddressbookController implements AddressbookAdminContactWebRequestController {
67 private static final long serialVersionUID = 542_145_347_916L;
70 * An event fired when the administrator has added a new contact
74 private Event<AdminAddedContactEvent> addedContactEvent;
77 * Admin helper instance
80 private AddressbookWebRequestController beanHelper;
85 private Date birthday;
88 * Cellphone number's carrier
90 private MobileProvider cellphoneCarrier;
95 private Long cellphoneId;
100 private Long cellphoneNumber;
110 private String comment;
113 * Remote contact bean
115 private final ContactSessionBeanRemote contactBean;
118 * General contact controller
121 private AddressbookContactWebSessionController contactController;
126 private Long contactId;
131 private Country country;
136 private String emailAddress;
141 private String familyName;
144 * Fax number's area code
146 private Integer faxAreaCode;
149 * Country instance for fax number
151 private Country faxCountry;
161 private Long faxNumber;
166 private String firstName;
171 private Gender gender;
176 private Short houseNumber;
179 * House number extension
181 private String houseNumberExtension;
184 * Whether a cellphone entry has been unlinked
186 private boolean isCellphoneUnlinked;
189 * Whether a fax entry has been unlinked
191 private boolean isFaxUnlinked;
194 * Whether a land-line number has been unlinked
196 private boolean isLandLineUnlinked;
199 * Land-line id number
201 private Long landLineId;
204 * Phone number area code
206 private Integer phoneAreaCode;
209 * Country instance for phone number
211 private Country phoneCountry;
216 private Long phoneNumber;
221 private String street;
226 private String title;
229 * An event fired when the administrator has updated contact data
233 private Event<AdminUpdatedContactEvent> updatedContactEvent;
238 private Integer zipCode;
241 * Default constructor
243 public AddressbookAdminContactWebRequestBean () {
246 // Get initial context
247 Context context = new InitialContext();
250 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/addressbook-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
251 } catch (final NamingException e) {
253 throw new FaceletException(e);
258 public String addContact () {
259 // Are all minimum fields set?
260 if (this.getGender() == null) {
262 throw new NullPointerException("gender is null"); //NOI18N
263 } else if (this.getFirstName() == null) {
265 throw new NullPointerException("firstName is null"); //NOI18N
266 } else if (this.getFirstName().isEmpty()) {
268 throw new IllegalStateException("firstName is empty"); //NOI18N
269 } else if (this.getFamilyName() == null) {
271 throw new NullPointerException("familyName is null"); //NOI18N
272 } else if (this.getFamilyName().isEmpty()) {
274 throw new IllegalStateException("familyName is empty"); //NOI18N
277 // Create new contact instance
278 Contact contact = this.createContactInstance();
280 // Default is not same contact
281 if (this.isSameContactFound(contact)) {
282 // Already registered
283 throw new FaceletException(new ContactAlreadyAddedException(contact));
287 Contact updatedContact;
292 updatedContact = this.contactBean.addContact(contact);
293 } catch (final ContactAlreadyAddedException ex) {
295 throw new FaceletException(ex);
299 this.addedContactEvent.fire(new AdminContactAddedEvent(updatedContact));
305 return "admin_list_contact"; //NOI18N
309 public void copyContactToController (final Contact contact) {
310 // The contact instance must be valid
311 if (null == contact) {
313 throw new NullPointerException("contact is null"); //NOI18N
314 } else if (contact.getContactId() == null) {
316 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
317 } else if (contact.getContactId() < 1) {
319 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
322 // Set all fields: contact
323 this.setContactId(contact.getContactId());
324 this.setTitle(contact.getContactTitle());
325 this.setBirthday(contact.getContactBirthday());
326 this.setCity(contact.getContactCity());
327 this.setComment(contact.getContactComment());
328 this.setCountry(contact.getContactCountry());
329 this.setEmailAddress(contact.getContactEmailAddress());
330 this.setFamilyName(contact.getContactFamilyName());
331 this.setFirstName(contact.getContactFirstName());
332 this.setGender(contact.getContactGender());
333 this.setHouseNumber(contact.getContactHouseNumber());
334 this.setStreet(contact.getContactStreet());
335 this.setZipCode(contact.getContactZipCode());
337 // ... cellphone data
338 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
339 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
340 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
341 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
345 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
346 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
347 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
348 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
349 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
353 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
354 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
355 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
356 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
357 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
362 public Contact createContactInstance () {
363 // Generate phone number
364 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
365 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
366 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
368 // Create new instance
369 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
371 // Check if contact instance is in helper and valid
372 if (null == contact) {
374 throw new NullPointerException("beanHelper.contact is null"); //NOI18N
375 } else if (contact.getContactId() == null) {
377 throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
378 } else if (contact.getContactId() < 1) {
380 throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
383 // Update all data in contact
384 this.updateContactData(contact);
386 // Call EJB for updating contact data
387 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
390 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
400 public String editContactData () {
401 // Get contact instance
402 Contact contact = this.beanHelper.getContact();
404 // Check if contact instance is in helper and valid
405 if (null == contact) {
407 throw new NullPointerException("beanHelper.contact is null"); //NOI18N
408 } else if (contact.getContactId() == null) {
410 throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
411 } else if (contact.getContactId() < 1) {
413 throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
416 // Update all data in contact
417 this.updateContactData(contact);
419 // Call EJB for updating contact data
420 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
423 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
428 // Return to contact list (for now)
429 return "admin_list_contact"; //NOI18N
433 public String generateCellphoneNumber (final DialableCellphoneNumber cellphoneNumber) {
435 if (null == cellphoneNumber) {
441 String number = String.format(
443 cellphoneNumber.getCellphoneProvider().getProviderCountry().getCountryExternalDialPrefix(),
444 cellphoneNumber.getCellphoneProvider().getProviderDialPrefix(),
445 cellphoneNumber.getPhoneNumber()
453 public String generatePhoneNumber (final DialableNumber phoneNumber) {
455 if (null == phoneNumber) {
461 String number = String.format(
463 phoneNumber.getPhoneCountry().getCountryExternalDialPrefix(),
464 phoneNumber.getPhoneAreaCode(),
465 phoneNumber.getPhoneNumber()
473 @SuppressWarnings ("ReturnOfDateField")
474 public Date getBirthday () {
475 return this.birthday;
479 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
480 public void setBirthday (final Date birthday) {
481 this.birthday = birthday;
485 public MobileProvider getCellphoneCarrier () {
486 return this.cellphoneCarrier;
490 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
491 this.cellphoneCarrier = cellphoneCarrier;
495 public Long getCellphoneId () {
496 return this.cellphoneId;
500 public void setCellphoneId (final Long cellphoneId) {
501 this.cellphoneId = cellphoneId;
505 public Long getCellphoneNumber () {
506 return this.cellphoneNumber;
510 public void setCellphoneNumber (Long cellphoneNumber) {
511 this.cellphoneNumber = cellphoneNumber;
515 public String getCity () {
520 public void setCity (final String city) {
525 public String getComment () {
530 public void setComment (final String comment) {
531 this.comment = comment;
535 public Long getContactId () {
536 return this.contactId;
540 public void setContactId (final Long contactId) {
541 this.contactId = contactId;
545 public Country getCountry () {
550 public void setCountry (final Country country) {
551 this.country = country;
555 public String getEmailAddress () {
556 return this.emailAddress;
560 public void setEmailAddress (final String emailAddress) {
561 this.emailAddress = emailAddress;
565 public String getFamilyName () {
566 return this.familyName;
570 public void setFamilyName (final String familyName) {
571 this.familyName = familyName;
575 public Integer getFaxAreaCode () {
576 return this.faxAreaCode;
580 public void setFaxAreaCode (final Integer faxAreaCode) {
581 this.faxAreaCode = faxAreaCode;
585 public Country getFaxCountry () {
586 return this.faxCountry;
590 public void setFaxCountry (final Country faxCountry) {
591 this.faxCountry = faxCountry;
595 public Long getFaxId () {
600 public void setFaxId (final Long faxId) {
605 public Long getFaxNumber () {
606 return this.faxNumber;
610 public void setFaxNumber (final Long faxNumber) {
611 this.faxNumber = faxNumber;
615 public String getFirstName () {
616 return this.firstName;
620 public void setFirstName (final String firstName) {
621 this.firstName = firstName;
625 public Gender getGender () {
630 public void setGender (final Gender gender) {
631 this.gender = gender;
635 public Short getHouseNumber () {
636 return this.houseNumber;
640 public void setHouseNumber (final Short houseNumber) {
641 this.houseNumber = houseNumber;
645 public String getHouseNumberExtension () {
646 return this.houseNumberExtension;
650 public void setHouseNumberExtension (final String houseNumberExtension) {
651 this.houseNumberExtension = houseNumberExtension;
655 public Long getLandLineId () {
656 return this.landLineId;
660 public void setLandLineId (final Long landLineId) {
661 this.landLineId = landLineId;
665 public Integer getPhoneAreaCode () {
666 return this.phoneAreaCode;
670 public void setPhoneAreaCode (final Integer phoneAreaCode) {
671 this.phoneAreaCode = phoneAreaCode;
675 public Country getPhoneCountry () {
676 return this.phoneCountry;
680 public void setPhoneCountry (final Country phoneCountry) {
681 this.phoneCountry = phoneCountry;
685 public Long getPhoneNumber () {
686 return this.phoneNumber;
690 public void setPhoneNumber (final Long phoneNumber) {
691 this.phoneNumber = phoneNumber;
695 public String getStreet () {
700 public void setStreet (final String street) {
701 this.street = street;
705 public String getTitle () {
710 public void setTitle (final String title) {
715 public Integer getZipCode () {
720 public void setZipCode (final Integer zipCode) {
721 this.zipCode = zipCode;
725 * Post-initialization of this class
728 public void init () {
732 public boolean isGenderRequired () {
733 // Get context parameter
734 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_admin_gender_enabled"); //NOI18N
737 boolean isRequired = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N
746 private void clear () {
749 this.setGender(null);
751 this.setFirstName(null);
752 this.setFamilyName(null);
753 this.setStreet(null);
754 this.setHouseNumber(null);
755 this.setZipCode(null);
757 this.setCountry(null);
760 this.setEmailAddress(null);
761 this.setPhoneCountry(null);
762 this.setPhoneAreaCode(null);
763 this.setPhoneNumber(null);
764 this.setCellphoneCarrier(null);
765 this.setCellphoneNumber(null);
766 this.setFaxCountry(null);
767 this.setFaxAreaCode(null);
768 this.setFaxNumber(null);
771 this.setBirthday(null);
772 this.setComment(null);
776 * Checks whether the given contact is found
778 * @param contact Contact inastance
780 * @return Wether contact has been found
782 private boolean isSameContactFound (final Contact contact) {
783 // Default is not found
784 boolean IsFound = false;
787 Iterator<Contact> iterator = this.contactController.allContacts().iterator();
790 while (iterator.hasNext()) {
792 Contact next = iterator.next();
795 if (ContactUtils.isSameContact(contact, next)) {
796 // Yes, then abort loop
807 * Updates all data in contact instance.
809 * @param contact Contact instance
811 private void updateContactData (final Contact contact) {
812 // Contact instance should be valid
813 if (null == contact) {
815 throw new NullPointerException("contact is null"); //NOI18N
816 } else if (contact.getContactId() == null) {
818 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
819 } else if (contact.getContactId() < 1) {
821 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
825 contact.setContactGender(this.getGender());
826 contact.setContactTitle(this.getTitle());
827 contact.setContactFirstName(this.getFirstName());
828 contact.setContactFamilyName(this.getFamilyName());
829 contact.setContactStreet(this.getStreet());
830 contact.setContactHouseNumber(this.getHouseNumber());
831 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
832 contact.setContactZipCode(this.getZipCode());
833 contact.setContactCity(this.getCity());
834 contact.setContactCountry(this.getCountry());
836 // Update contact's cellphone number
837 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
839 // Update contact's land-line number
840 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
842 // Update contact's fax number
843 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());