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 * House number extension
180 private String houseNumberExtension;
183 * Whether a cellphone entry has been unlinked
185 private boolean isCellphoneUnlinked;
188 * Whether a fax entry has been unlinked
190 private boolean isFaxUnlinked;
193 * Whether a land-line number has been unlinked
195 private boolean isLandLineUnlinked;
198 * Land-line id number
200 private Long landLineId;
203 * Phone number area code
205 private Integer phoneAreaCode;
208 * Country instance for phone number
210 private Country phoneCountry;
215 private Long phoneNumber;
220 private String street;
225 private String title;
228 * An event fired when the administrator has updated contact data
232 private Event<AdminUpdatedContactEvent> updatedContactEvent;
237 private Integer zipCode;
240 * Default constructor
242 public JobsAdminContactWebRequestBean () {
245 // Get initial context
246 Context context = new InitialContext();
249 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jratecalc-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
250 } catch (final NamingException e) {
252 throw new FaceletException(e);
257 public String addContact () {
258 // Are all minimum fields set?
259 if (this.getGender() == null) {
261 throw new NullPointerException("gender is null"); //NOI18N
262 } else if (this.getFirstName() == null) {
264 throw new NullPointerException("firstName is null"); //NOI18N
265 } else if (this.getFirstName().isEmpty()) {
267 throw new IllegalStateException("firstName is empty"); //NOI18N
268 } else if (this.getFamilyName() == null) {
270 throw new NullPointerException("familyName is null"); //NOI18N
271 } else if (this.getFamilyName().isEmpty()) {
273 throw new IllegalStateException("familyName is empty"); //NOI18N
276 // Create new contact instance
277 Contact contact = this.createContactInstance();
279 // Default is not same contact
280 if (this.isSameContactFound(contact)) {
281 // Already registered
282 throw new FaceletException(new ContactAlreadyAddedException(contact));
286 Contact updatedContact;
291 updatedContact = this.contactBean.addContact(contact);
292 } catch (final ContactAlreadyAddedException ex) {
294 throw new FaceletException(ex);
298 this.addedContactEvent.fire(new AdminContactAddedEvent(updatedContact));
304 return "admin_list_contact"; //NOI18N
308 public void copyContactToController (final Contact contact) {
309 // The contact instance must be valid
310 if (null == contact) {
312 throw new NullPointerException("contact is null"); //NOI18N
313 } else if (contact.getContactId() == null) {
315 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
316 } else if (contact.getContactId() < 1) {
318 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
321 // Set all fields: contact
322 this.setContactId(contact.getContactId());
323 this.setTitle(contact.getContactTitle());
324 this.setBirthday(contact.getContactBirthday());
325 this.setCity(contact.getContactCity());
326 this.setComment(contact.getContactComment());
327 this.setCountry(contact.getContactCountry());
328 this.setEmailAddress(contact.getContactEmailAddress());
329 this.setFamilyName(contact.getContactFamilyName());
330 this.setFirstName(contact.getContactFirstName());
331 this.setGender(contact.getContactGender());
332 this.setHouseNumber(contact.getContactHouseNumber());
333 this.setHouseNumberExtension(contact.getContactHouseNumberExtension());
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 () {
734 private void clear () {
737 this.setGender(null);
739 this.setFirstName(null);
740 this.setFamilyName(null);
741 this.setStreet(null);
742 this.setHouseNumber(null);
743 this.setHouseNumberExtension(null);
744 this.setZipCode(null);
746 this.setCountry(null);
749 this.setEmailAddress(null);
750 this.setPhoneCountry(null);
751 this.setPhoneAreaCode(null);
752 this.setPhoneNumber(null);
753 this.setCellphoneCarrier(null);
754 this.setCellphoneNumber(null);
755 this.setFaxCountry(null);
756 this.setFaxAreaCode(null);
757 this.setFaxNumber(null);
760 this.setBirthday(null);
761 this.setComment(null);
765 * Checks whether the given contact is found
767 * @param contact Contact inastance
769 * @return Wether contact has been found
771 private boolean isSameContactFound (final Contact contact) {
772 // Default is not found
773 boolean IsFound = false;
776 Iterator<Contact> iterator = this.contactController.allContacts().iterator();
779 while (iterator.hasNext()) {
781 Contact next = iterator.next();
784 if (ContactUtils.isSameContact(contact, next)) {
785 // Yes, then abort loop
796 * Updates all data in contact instance.
798 * @param contact Contact instance
800 private void updateContactData (final Contact contact) {
801 // Contact instance should be valid
802 if (null == contact) {
804 throw new NullPointerException("contact is null"); //NOI18N
805 } else if (contact.getContactId() == null) {
807 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
808 } else if (contact.getContactId() < 1) {
810 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
814 contact.setContactGender(this.getGender());
815 contact.setContactTitle(this.getTitle());
816 contact.setContactFirstName(this.getFirstName());
817 contact.setContactFamilyName(this.getFamilyName());
818 contact.setContactStreet(this.getStreet());
819 contact.setContactHouseNumber(this.getHouseNumber());
820 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
821 contact.setContactZipCode(this.getZipCode());
822 contact.setContactCity(this.getCity());
823 contact.setContactCountry(this.getCountry());
825 // Update contact's cellphone number
826 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
828 // Update contact's land-line number
829 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
831 // Update contact's fax number
832 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());