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.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.jcontacts.contact.Contact;
33 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
34 import org.mxchange.jcontacts.contact.UserContact;
35 import org.mxchange.jcontacts.contact.gender.Gender;
36 import org.mxchange.jcontacts.contact.utils.ContactUtils;
37 import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent;
38 import org.mxchange.jcontacts.events.contact.add.AdminContactAddedEvent;
39 import org.mxchange.jcontacts.events.contact.update.AdminContactUpdatedEvent;
40 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
41 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
42 import org.mxchange.jcountry.data.Country;
43 import org.mxchange.jjobs.beans.BaseJobsController;
44 import org.mxchange.jjobs.beans.helper.JobsWebRequestController;
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 JobsAdminContactWebRequestBean extends BaseJobsController implements JobsAdminContactWebRequestController {
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 JobsWebRequestController 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 JobsContactWebSessionController 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 JobsAdminContactWebRequestBean () {
240 // Get initial context
241 Context context = new InitialContext();
244 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jratecalc-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.setHouseNumberExtension(contact.getContactHouseNumberExtension());
329 this.setStreet(contact.getContactStreet());
330 this.setZipCode(contact.getContactZipCode());
332 // ... cellphone data
333 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
334 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
335 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
336 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
340 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
341 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
342 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
343 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
344 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
348 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
349 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
350 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
351 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
352 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
357 public Contact createContactInstance () {
358 // Generate phone number
359 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
360 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
361 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
363 // Create new instance
364 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
366 // Check if contact instance is in helper and valid
367 if (null == contact) {
369 throw new NullPointerException("beanHelper.contact is null"); //NOI18N
370 } else if (contact.getContactId() == null) {
372 throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
373 } else if (contact.getContactId() < 1) {
375 throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
378 // Update all data in contact
379 this.updateContactData(contact);
381 // Call EJB for updating contact data
382 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
385 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
395 public String editContactData () {
396 // Get contact instance
397 Contact contact = this.beanHelper.getContact();
399 // Check if contact instance is in helper and valid
400 if (null == contact) {
402 throw new NullPointerException("beanHelper.contact is null"); //NOI18N
403 } else if (contact.getContactId() == null) {
405 throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
406 } else if (contact.getContactId() < 1) {
408 throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
411 // Update all data in contact
412 this.updateContactData(contact);
414 // Call EJB for updating contact data
415 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
418 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
423 // Return to contact list (for now)
424 return "admin_list_contact"; //NOI18N
428 public String generateCellphoneNumber (final DialableCellphoneNumber cellphoneNumber) {
430 if (null == cellphoneNumber) {
436 String number = String.format(
438 cellphoneNumber.getCellphoneProvider().getProviderCountry().getCountryExternalDialPrefix(),
439 cellphoneNumber.getCellphoneProvider().getProviderDialPrefix(),
440 cellphoneNumber.getPhoneNumber()
448 public String generatePhoneNumber (final DialableNumber phoneNumber) {
450 if (null == phoneNumber) {
456 String number = String.format(
458 phoneNumber.getPhoneCountry().getCountryExternalDialPrefix(),
459 phoneNumber.getPhoneAreaCode(),
460 phoneNumber.getPhoneNumber()
468 @SuppressWarnings ("ReturnOfDateField")
469 public Date getBirthday () {
470 return this.birthday;
474 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
475 public void setBirthday (final Date birthday) {
476 this.birthday = birthday;
480 public MobileProvider getCellphoneCarrier () {
481 return this.cellphoneCarrier;
485 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
486 this.cellphoneCarrier = cellphoneCarrier;
490 public Long getCellphoneId () {
491 return this.cellphoneId;
495 public void setCellphoneId (final Long cellphoneId) {
496 this.cellphoneId = cellphoneId;
500 public Long getCellphoneNumber () {
501 return this.cellphoneNumber;
505 public void setCellphoneNumber (Long cellphoneNumber) {
506 this.cellphoneNumber = cellphoneNumber;
510 public String getCity () {
515 public void setCity (final String city) {
520 public String getComment () {
525 public void setComment (final String comment) {
526 this.comment = comment;
530 public Long getContactId () {
531 return this.contactId;
535 public void setContactId (final Long contactId) {
536 this.contactId = contactId;
540 public Country getCountry () {
545 public void setCountry (final Country country) {
546 this.country = country;
550 public String getEmailAddress () {
551 return this.emailAddress;
555 public void setEmailAddress (final String emailAddress) {
556 this.emailAddress = emailAddress;
560 public String getFamilyName () {
561 return this.familyName;
565 public void setFamilyName (final String familyName) {
566 this.familyName = familyName;
570 public Integer getFaxAreaCode () {
571 return this.faxAreaCode;
575 public void setFaxAreaCode (final Integer faxAreaCode) {
576 this.faxAreaCode = faxAreaCode;
580 public Country getFaxCountry () {
581 return this.faxCountry;
585 public void setFaxCountry (final Country faxCountry) {
586 this.faxCountry = faxCountry;
590 public Long getFaxId () {
595 public void setFaxId (final Long faxId) {
600 public Long getFaxNumber () {
601 return this.faxNumber;
605 public void setFaxNumber (final Long faxNumber) {
606 this.faxNumber = faxNumber;
610 public String getFirstName () {
611 return this.firstName;
615 public void setFirstName (final String firstName) {
616 this.firstName = firstName;
620 public Gender getGender () {
625 public void setGender (final Gender gender) {
626 this.gender = gender;
630 public Short getHouseNumber () {
631 return this.houseNumber;
635 public void setHouseNumber (final Short houseNumber) {
636 this.houseNumber = houseNumber;
640 public Long getLandLineId () {
641 return this.landLineId;
645 public void setLandLineId (final Long landLineId) {
646 this.landLineId = landLineId;
650 public Integer getPhoneAreaCode () {
651 return this.phoneAreaCode;
655 public void setPhoneAreaCode (final Integer phoneAreaCode) {
656 this.phoneAreaCode = phoneAreaCode;
660 public Country getPhoneCountry () {
661 return this.phoneCountry;
665 public void setPhoneCountry (final Country phoneCountry) {
666 this.phoneCountry = phoneCountry;
670 public Long getPhoneNumber () {
671 return this.phoneNumber;
675 public void setPhoneNumber (final Long phoneNumber) {
676 this.phoneNumber = phoneNumber;
680 public String getStreet () {
685 public void setStreet (final String street) {
686 this.street = street;
690 public String getTitle () {
695 public void setTitle (final String title) {
700 public Integer getZipCode () {
705 public void setZipCode (final Integer zipCode) {
706 this.zipCode = zipCode;
710 * Post-initialization of this class
713 public void init () {
719 private void clear () {
722 this.setGender(null);
724 this.setFirstName(null);
725 this.setFamilyName(null);
726 this.setStreet(null);
727 this.setHouseNumber(null);
728 this.setHouseNumberExtension(null);
729 this.setZipCode(null);
731 this.setCountry(null);
734 this.setEmailAddress(null);
735 this.setPhoneCountry(null);
736 this.setPhoneAreaCode(null);
737 this.setPhoneNumber(null);
738 this.setCellphoneCarrier(null);
739 this.setCellphoneNumber(null);
740 this.setFaxCountry(null);
741 this.setFaxAreaCode(null);
742 this.setFaxNumber(null);
745 this.setBirthday(null);
746 this.setComment(null);
750 * Checks whether the given contact is found
752 * @param contact Contact inastance
754 * @return Wether contact has been found
756 private boolean isSameContactFound (final Contact contact) {
757 // Default is not found
758 boolean IsFound = false;
761 Iterator<Contact> iterator = this.contactController.allContacts().iterator();
764 while (iterator.hasNext()) {
766 Contact next = iterator.next();
769 if (ContactUtils.isSameContact(contact, next)) {
770 // Yes, then abort loop
781 * Updates all data in contact instance.
783 * @param contact Contact instance
785 private void updateContactData (final Contact contact) {
786 // Contact instance should be valid
787 if (null == contact) {
789 throw new NullPointerException("contact is null"); //NOI18N
790 } else if (contact.getContactId() == null) {
792 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
793 } else if (contact.getContactId() < 1) {
795 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
799 contact.setContactGender(this.getGender());
800 contact.setContactTitle(this.getTitle());
801 contact.setContactFirstName(this.getFirstName());
802 contact.setContactFamilyName(this.getFamilyName());
803 contact.setContactStreet(this.getStreet());
804 contact.setContactHouseNumber(this.getHouseNumber());
805 contact.setContactZipCode(this.getZipCode());
806 contact.setContactCity(this.getCity());
807 contact.setContactCountry(this.getCountry());
809 // Update contact's cellphone number
810 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
812 // Update contact's land-line number
813 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
815 // Update contact's fax number
816 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());