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.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.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.jjobs.beans.BaseJobsController;
45 import org.mxchange.jjobs.beans.helper.JobsWebRequestController;
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 controller (bean)
58 * @author Roland Haeder<roland@mxchange.org>
60 @Named ("adminContactController")
62 public class JobsAdminContactWebRequestBean extends BaseJobsController implements JobsAdminContactWebRequestController {
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 JobsWebRequestController 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 JobsContactWebSessionController 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 JobsAdminContactWebRequestBean () {
246 // Get initial context
247 Context context = new InitialContext();
250 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jratecalc-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.setHouseNumberExtension(contact.getContactHouseNumberExtension());
335 this.setStreet(contact.getContactStreet());
336 this.setZipCode(contact.getContactZipCode());
338 // ... cellphone data
339 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
340 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
341 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
342 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
346 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
347 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
348 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
349 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
350 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
354 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
355 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
356 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
357 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
358 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
363 public Contact createContactInstance () {
364 // Generate phone number
365 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
366 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
367 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
369 // Create new instance
370 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
372 // Check if contact instance is in helper and valid
373 if (null == contact) {
375 throw new NullPointerException("beanHelper.contact is null"); //NOI18N
376 } else if (contact.getContactId() == null) {
378 throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
379 } else if (contact.getContactId() < 1) {
381 throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
384 // Update all data in contact
385 this.updateContactData(contact);
387 // Call EJB for updating contact data
388 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
391 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
401 public String editContactData () {
402 // Get contact instance
403 Contact contact = this.beanHelper.getContact();
405 // Check if contact instance is in helper and valid
406 if (null == contact) {
408 throw new NullPointerException("beanHelper.contact is null"); //NOI18N
409 } else if (contact.getContactId() == null) {
411 throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
412 } else if (contact.getContactId() < 1) {
414 throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
417 // Update all data in contact
418 this.updateContactData(contact);
420 // Call EJB for updating contact data
421 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
424 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
429 // Return to contact list (for now)
430 return "admin_list_contact"; //NOI18N
434 public String generateCellphoneNumber (final DialableCellphoneNumber cellphoneNumber) {
436 if (null == cellphoneNumber) {
442 String number = String.format(
444 cellphoneNumber.getCellphoneProvider().getProviderCountry().getCountryExternalDialPrefix(),
445 cellphoneNumber.getCellphoneProvider().getProviderDialPrefix(),
446 cellphoneNumber.getPhoneNumber()
454 public String generatePhoneNumber (final DialableNumber phoneNumber) {
456 if (null == phoneNumber) {
462 String number = String.format(
464 phoneNumber.getPhoneCountry().getCountryExternalDialPrefix(),
465 phoneNumber.getPhoneAreaCode(),
466 phoneNumber.getPhoneNumber()
474 @SuppressWarnings ("ReturnOfDateField")
475 public Date getBirthday () {
476 return this.birthday;
480 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
481 public void setBirthday (final Date birthday) {
482 this.birthday = birthday;
486 public MobileProvider getCellphoneCarrier () {
487 return this.cellphoneCarrier;
491 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
492 this.cellphoneCarrier = cellphoneCarrier;
496 public Long getCellphoneId () {
497 return this.cellphoneId;
501 public void setCellphoneId (final Long cellphoneId) {
502 this.cellphoneId = cellphoneId;
506 public Long getCellphoneNumber () {
507 return this.cellphoneNumber;
511 public void setCellphoneNumber (Long cellphoneNumber) {
512 this.cellphoneNumber = cellphoneNumber;
516 public String getCity () {
521 public void setCity (final String city) {
526 public String getComment () {
531 public void setComment (final String comment) {
532 this.comment = comment;
536 public Long getContactId () {
537 return this.contactId;
541 public void setContactId (final Long contactId) {
542 this.contactId = contactId;
546 public Country getCountry () {
551 public void setCountry (final Country country) {
552 this.country = country;
556 public String getEmailAddress () {
557 return this.emailAddress;
561 public void setEmailAddress (final String emailAddress) {
562 this.emailAddress = emailAddress;
566 public String getFamilyName () {
567 return this.familyName;
571 public void setFamilyName (final String familyName) {
572 this.familyName = familyName;
576 public Integer getFaxAreaCode () {
577 return this.faxAreaCode;
581 public void setFaxAreaCode (final Integer faxAreaCode) {
582 this.faxAreaCode = faxAreaCode;
586 public Country getFaxCountry () {
587 return this.faxCountry;
591 public void setFaxCountry (final Country faxCountry) {
592 this.faxCountry = faxCountry;
596 public Long getFaxId () {
601 public void setFaxId (final Long faxId) {
606 public Long getFaxNumber () {
607 return this.faxNumber;
611 public void setFaxNumber (final Long faxNumber) {
612 this.faxNumber = faxNumber;
616 public String getFirstName () {
617 return this.firstName;
621 public void setFirstName (final String firstName) {
622 this.firstName = firstName;
626 public Gender getGender () {
631 public void setGender (final Gender gender) {
632 this.gender = gender;
636 public Short getHouseNumber () {
637 return this.houseNumber;
641 public void setHouseNumber (final Short houseNumber) {
642 this.houseNumber = houseNumber;
646 public String getHouseNumberExtension () {
647 return this.houseNumberExtension;
651 public void setHouseNumberExtension (final String houseNumberExtension) {
652 this.houseNumberExtension = houseNumberExtension;
656 public Long getLandLineId () {
657 return this.landLineId;
661 public void setLandLineId (final Long landLineId) {
662 this.landLineId = landLineId;
666 public Integer getPhoneAreaCode () {
667 return this.phoneAreaCode;
671 public void setPhoneAreaCode (final Integer phoneAreaCode) {
672 this.phoneAreaCode = phoneAreaCode;
676 public Country getPhoneCountry () {
677 return this.phoneCountry;
681 public void setPhoneCountry (final Country phoneCountry) {
682 this.phoneCountry = phoneCountry;
686 public Long getPhoneNumber () {
687 return this.phoneNumber;
691 public void setPhoneNumber (final Long phoneNumber) {
692 this.phoneNumber = phoneNumber;
696 public String getStreet () {
701 public void setStreet (final String street) {
702 this.street = street;
706 public String getTitle () {
711 public void setTitle (final String title) {
716 public Integer getZipCode () {
721 public void setZipCode (final Integer zipCode) {
722 this.zipCode = zipCode;
726 * Post-initialization of this class
729 public void init () {
733 public boolean isGenderRequired () {
734 // Get context parameter
735 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_admin_gender_enabled"); //NOI18N
738 boolean isRequired = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N
747 private void clear () {
750 this.setGender(null);
752 this.setFirstName(null);
753 this.setFamilyName(null);
754 this.setStreet(null);
755 this.setHouseNumber(null);
756 this.setHouseNumberExtension(null);
757 this.setZipCode(null);
759 this.setCountry(null);
762 this.setEmailAddress(null);
763 this.setPhoneCountry(null);
764 this.setPhoneAreaCode(null);
765 this.setPhoneNumber(null);
766 this.setCellphoneCarrier(null);
767 this.setCellphoneNumber(null);
768 this.setFaxCountry(null);
769 this.setFaxAreaCode(null);
770 this.setFaxNumber(null);
773 this.setBirthday(null);
774 this.setComment(null);
778 * Checks whether the given contact is found
780 * @param contact Contact inastance
782 * @return Wether contact has been found
784 private boolean isSameContactFound (final Contact contact) {
785 // Default is not found
786 boolean IsFound = false;
789 Iterator<Contact> iterator = this.contactController.allContacts().iterator();
792 while (iterator.hasNext()) {
794 Contact next = iterator.next();
797 if (ContactUtils.isSameContact(contact, next)) {
798 // Yes, then abort loop
809 * Updates all data in contact instance.
811 * @param contact Contact instance
813 private void updateContactData (final Contact contact) {
814 // Contact instance should be valid
815 if (null == contact) {
817 throw new NullPointerException("contact is null"); //NOI18N
818 } else if (contact.getContactId() == null) {
820 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
821 } else if (contact.getContactId() < 1) {
823 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
827 contact.setContactGender(this.getGender());
828 contact.setContactTitle(this.getTitle());
829 contact.setContactFirstName(this.getFirstName());
830 contact.setContactFamilyName(this.getFamilyName());
831 contact.setContactStreet(this.getStreet());
832 contact.setContactHouseNumber(this.getHouseNumber());
833 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
834 contact.setContactZipCode(this.getZipCode());
835 contact.setContactCity(this.getCity());
836 contact.setContactCountry(this.getCountry());
838 // Update contact's cellphone number
839 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
841 // Update contact's land-line number
842 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
844 // Update contact's fax number
845 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());