2 * Copyright (C) 2016 Cho-Time GmbH
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 java.util.List;
23 import javax.annotation.PostConstruct;
24 import javax.enterprise.context.RequestScoped;
25 import javax.enterprise.event.Event;
26 import javax.enterprise.inject.Any;
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.helper.JobsAdminWebRequestController;
45 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
46 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
47 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
48 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
49 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
50 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
51 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
54 * An administrative user bean (controller)
56 * @author Roland Haeder<roland@mxchange.org>
58 @Named ("adminContactController")
60 public class JobsAdminContactWebRequestBean implements JobsAdminContactWebRequestController {
65 private static final long serialVersionUID = 542_145_347_916L;
68 * An event fired when the administrator has added a new contact
72 private Event<AdminAddedContactEvent> addedContactEvent;
75 * Admin helper instance
78 private JobsAdminWebRequestController adminHelper;
83 private Date birthday;
86 * Cellphone number's carrier
88 private MobileProvider cellphoneCarrier;
93 private Long cellphoneId;
98 private Long cellphoneNumber;
108 private String comment;
111 * Remote contact bean
113 private final ContactSessionBeanRemote contactBean;
118 private Long contactId;
123 private Country country;
128 private String emailAddress;
133 private String familyName;
136 * Fax number's area code
138 private Integer faxAreaCode;
141 * Country instance for fax number
143 private Country faxCountry;
153 private Long faxNumber;
158 private String firstName;
163 private Gender gender;
168 private Short houseNumber;
171 * Whether a cellphone entry has been unlinked
173 private boolean isCellphoneUnlinked;
176 * Whether a fax entry has been unlinked
178 private boolean isFaxUnlinked;
181 * Whether a land-line number has been unlinked
183 private boolean isLandLineUnlinked;
186 * Land-line id number
188 private Long landLineId;
191 * Phone number area code
193 private Integer phoneAreaCode;
196 * Country instance for phone number
198 private Country phoneCountry;
203 private Long phoneNumber;
208 private String street;
211 * An event fired when the administrator has updated contact data
215 private Event<AdminUpdatedContactEvent> updatedContactEvent;
220 private Integer zipCode;
223 * Default constructor
225 public JobsAdminContactWebRequestBean () {
226 // Set gender to UNKNOWN
227 this.gender = Gender.UNKNOWN;
231 // Get initial context
232 Context context = new InitialContext();
235 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jratecalc-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
236 } catch (final NamingException e) {
238 throw new FaceletException(e);
243 public String addContact () {
244 // Are all minimum fields set?
245 if (this.getGender() == null) {
247 throw new NullPointerException("gender is null"); //NOI18N
248 } else if (this.getFirstName() == null) {
250 throw new NullPointerException("firstName is null"); //NOI18N
251 } else if (this.getFirstName().isEmpty()) {
253 throw new IllegalStateException("firstName is empty"); //NOI18N
254 } else if (this.getFamilyName() == null) {
256 throw new NullPointerException("familyName is null"); //NOI18N
257 } else if (this.getFamilyName().isEmpty()) {
259 throw new IllegalStateException("familyName is empty"); //NOI18N
262 // Create new contact instance
263 Contact contact = this.createContactInstance();
265 // Default is not same contact
266 if (this.isSameContactFound(contact)) {
267 // Already registered
268 throw new FaceletException(new ContactAlreadyAddedException(contact));
272 Contact updatedContact;
277 updatedContact = this.contactBean.addContact(contact);
278 } catch (final ContactAlreadyAddedException ex) {
280 throw new FaceletException(ex);
284 this.addedContactEvent.fire(new AdminContactAddedEvent(updatedContact));
287 return "admin_list_contact"; //NOI18N
291 public List<Contact> allContacts () {
292 return this.contactController.allContacts();
296 public String changeContactData () {
297 // Get contact instance
298 Contact contact = this.adminHelper.getContact();
300 // Default is not same contact
301 if (this.isSameContactFound(contact)) {
302 // Already registered
303 throw new FaceletException(new ContactAlreadyAddedException(contact));
307 Contact updatedContact;
312 updatedContact = this.contactBean.addContact(contact);
313 } catch (final ContactAlreadyAddedException ex) {
315 throw new FaceletException(ex);
319 this.addedContactEvent.fire(new AdminContactAddedEvent(updatedContact));
325 return "admin_list_contact"; //NOI18N
329 public void copyContactToController (final Contact contact) {
330 // The contact instance must be valid
331 if (null == contact) {
333 throw new NullPointerException("contact is null"); //NOI18N
334 } else if (contact.getContactId() == null) {
336 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
337 } else if (contact.getContactId() < 1) {
339 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
342 // Set all fields: contact
343 this.setContactId(contact.getContactId());
344 this.setBirthday(contact.getContactBirthday());
345 this.setCity(contact.getContactCity());
346 this.setComment(contact.getContactComment());
347 this.setCountry(contact.getContactCountry());
348 this.setEmailAddress(contact.getContactEmailAddress());
349 this.setFamilyName(contact.getContactFamilyName());
350 this.setFirstName(contact.getContactFirstName());
351 this.setGender(contact.getContactGender());
352 this.setHouseNumber(contact.getContactHouseNumber());
353 this.setStreet(contact.getContactStreet());
354 this.setZipCode(contact.getContactZipCode());
356 // ... cellphone data
357 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
358 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
359 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
362 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
363 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
364 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
365 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
368 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
369 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
370 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
371 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
375 public String editContactData () {
376 // Get contact instance
377 Contact contact = this.adminHelper.getContact();
379 // Is the land-line number set?
380 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
382 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
383 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
384 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
385 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
389 //* NOISY-DEBUG: */ System.out.println("AdminContactController::copyContactToController(): EXIT!"); //NOI18N
393 public Contact createContactInstance () {
394 // Generate phone number
395 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
396 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
397 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
399 // Create new instance
400 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
402 // Check if contact instance is in helper and valid
403 if (null == contact) {
405 throw new NullPointerException("adminHelper.contact is null"); //NOI18N
406 } else if (contact.getContactId() == null) {
408 throw new NullPointerException("adminHelper.contact.contactId is null"); //NOI18N //NOI18N
409 } else if (contact.getContactId() < 1) {
411 throw new IllegalStateException(MessageFormat.format("adminHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
414 // Update all data in contact
415 this.updateContactData(contact);
417 // Call EJB for updating contact data
418 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
421 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
431 @SuppressWarnings ("ReturnOfDateField")
432 public Date getBirthday () {
433 return this.birthday;
437 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
438 public void setBirthday (final Date birthday) {
439 this.birthday = birthday;
443 public MobileProvider getCellphoneCarrier () {
444 return this.cellphoneCarrier;
448 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
449 this.cellphoneCarrier = cellphoneCarrier;
453 public Long getCellphoneId () {
454 return this.cellphoneId;
458 public void setCellphoneId (final Long cellphoneId) {
459 this.cellphoneId = cellphoneId;
463 public Long getCellphoneNumber () {
464 return this.cellphoneNumber;
468 public void setCellphoneNumber (Long cellphoneNumber) {
469 this.cellphoneNumber = cellphoneNumber;
473 public String getCity () {
478 public void setCity (final String city) {
483 public String getComment () {
488 public void setComment (final String comment) {
489 this.comment = comment;
493 public Long getContactId () {
494 return this.contactId;
498 public void setContactId (final Long contactId) {
499 this.contactId = contactId;
503 public Country getCountry () {
508 public void setCountry (final Country country) {
509 this.country = country;
513 public String getEmailAddress () {
514 return this.emailAddress;
518 public void setEmailAddress (final String emailAddress) {
519 this.emailAddress = emailAddress;
523 public String getFamilyName () {
524 return this.familyName;
528 public void setFamilyName (final String familyName) {
529 this.familyName = familyName;
533 public Integer getFaxAreaCode () {
534 return this.faxAreaCode;
538 public void setFaxAreaCode (final Integer faxAreaCode) {
539 this.faxAreaCode = faxAreaCode;
543 public Country getFaxCountry () {
544 return this.faxCountry;
548 public void setFaxCountry (final Country faxCountry) {
549 this.faxCountry = faxCountry;
553 public Long getFaxId () {
558 public void setFaxId (final Long faxId) {
563 public Long getFaxNumber () {
564 return this.faxNumber;
568 public void setFaxNumber (final Long faxNumber) {
569 this.faxNumber = faxNumber;
573 public String getFirstName () {
574 return this.firstName;
578 public void setFirstName (final String firstName) {
579 this.firstName = firstName;
583 public Gender getGender () {
588 public void setGender (final Gender gender) {
589 this.gender = gender;
593 public Short getHouseNumber () {
594 return this.houseNumber;
598 public void setHouseNumber (final Short houseNumber) {
599 this.houseNumber = houseNumber;
603 public Long getLandLineId () {
604 return this.landLineId;
608 public void setLandLineId (final Long landLineId) {
609 this.landLineId = landLineId;
613 public Integer getPhoneAreaCode () {
614 return this.phoneAreaCode;
618 public void setPhoneAreaCode (final Integer phoneAreaCode) {
619 this.phoneAreaCode = phoneAreaCode;
623 public Country getPhoneCountry () {
624 return this.phoneCountry;
628 public void setPhoneCountry (final Country phoneCountry) {
629 this.phoneCountry = phoneCountry;
633 public Long getPhoneNumber () {
634 return this.phoneNumber;
638 public void setPhoneNumber (final Long phoneNumber) {
639 this.phoneNumber = phoneNumber;
643 public String getStreet () {
648 public void setStreet (final String street) {
649 this.street = street;
653 public Integer getZipCode () {
658 public void setZipCode (final Integer zipCode) {
659 this.zipCode = zipCode;
663 * Post-initialization of this class
666 public void init () {
672 private void clear () {
675 this.setGender(Gender.UNKNOWN);
676 this.setFirstName(null);
677 this.setFamilyName(null);
678 this.setStreet(null);
679 this.setHouseNumber(null);
680 this.setZipCode(null);
682 this.setCountry(null);
685 this.setEmailAddress(null);
686 this.setPhoneCountry(null);
687 this.setPhoneAreaCode(null);
688 this.setPhoneNumber(null);
689 this.setCellphoneCarrier(null);
690 this.setCellphoneNumber(null);
691 this.setFaxCountry(null);
692 this.setFaxAreaCode(null);
693 this.setFaxNumber(null);
696 this.setBirthday(null);
697 this.setComment(null);
701 * Checks whether the given contact is found
703 * @param contact Contact inastance
705 * @return Wether contact has been found
707 private boolean isSameContactFound (final Contact contact) {
708 // Default is not found
709 boolean IsFound = false;
712 Iterator<Contact> iterator = this.allContacts().iterator();
715 while (iterator.hasNext()) {
717 Contact next = iterator.next();
720 if (ContactUtils.isSameContact(contact, next)) {
721 // Yes, then abort loop
732 * Updates all data in contact instance.
734 * @param contact Contact instance
736 private void updateContactData (final Contact contact) {
737 // Contact instance should be valid
738 if (null == contact) {
740 throw new NullPointerException("contact is null"); //NOI18N
741 } else if (contact.getContactId() == null) {
743 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
744 } else if (contact.getContactId() < 1) {
746 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
750 contact.setContactGender(this.getGender());
751 contact.setContactFirstName(this.getFirstName());
752 contact.setContactFamilyName(this.getFamilyName());
753 contact.setContactStreet(this.getStreet());
754 contact.setContactHouseNumber(this.getHouseNumber());
755 contact.setContactZipCode(this.getZipCode());
756 contact.setContactCity(this.getCity());
757 contact.setContactCountry(this.getCountry());
759 // Update contact's cellphone number
760 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
762 // Update contact's land-line number
763 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
765 // Update contact's fax number
766 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());