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;
116 * General contact controller
119 private AddressbookContactWebSessionController contactController;
124 private Long contactId;
129 private Country country;
134 private String emailAddress;
139 private String familyName;
142 * Fax number's area code
144 private Integer faxAreaCode;
147 * Country instance for fax number
149 private Country faxCountry;
159 private Long faxNumber;
164 private String firstName;
169 private Gender gender;
174 private Short houseNumber;
177 * Whether a cellphone entry has been unlinked
179 private boolean isCellphoneUnlinked;
182 * Whether a fax entry has been unlinked
184 private boolean isFaxUnlinked;
187 * Whether a land-line number has been unlinked
189 private boolean isLandLineUnlinked;
192 * Land-line id number
194 private Long landLineId;
197 * Phone number area code
199 private Integer phoneAreaCode;
202 * Country instance for phone number
204 private Country phoneCountry;
209 private Long phoneNumber;
214 private String street;
217 * An event fired when the administrator has updated contact data
221 private Event<AdminUpdatedContactEvent> updatedContactEvent;
226 private Integer zipCode;
229 * Default constructor
231 public JobsAdminContactWebRequestBean () {
232 // Set gender to UNKNOWN
233 this.gender = Gender.UNKNOWN;
237 // Get initial context
238 Context context = new InitialContext();
241 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jratecalc-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
242 } catch (final NamingException e) {
244 throw new FaceletException(e);
249 public String addContact () {
250 // Are all minimum fields set?
251 if (this.getGender() == null) {
253 throw new NullPointerException("gender is null"); //NOI18N
254 } else if (this.getFirstName() == null) {
256 throw new NullPointerException("firstName is null"); //NOI18N
257 } else if (this.getFirstName().isEmpty()) {
259 throw new IllegalStateException("firstName is empty"); //NOI18N
260 } else if (this.getFamilyName() == null) {
262 throw new NullPointerException("familyName is null"); //NOI18N
263 } else if (this.getFamilyName().isEmpty()) {
265 throw new IllegalStateException("familyName is empty"); //NOI18N
268 // Create new contact instance
269 Contact contact = this.createContactInstance();
271 // Default is not same contact
272 if (this.isSameContactFound(contact)) {
273 // Already registered
274 throw new FaceletException(new ContactAlreadyAddedException(contact));
278 Contact updatedContact;
283 updatedContact = this.contactBean.addContact(contact);
284 } catch (final ContactAlreadyAddedException ex) {
286 throw new FaceletException(ex);
290 this.addedContactEvent.fire(new AdminContactAddedEvent(updatedContact));
293 return "admin_list_contact"; //NOI18N
297 public List<Contact> allContacts () {
298 return this.contactController.allContacts();
302 public void copyContactToController (final Contact contact) {
303 // The contact instance must be valid
304 if (null == contact) {
306 throw new NullPointerException("contact is null"); //NOI18N
307 } else if (contact.getContactId() == null) {
309 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
310 } else if (contact.getContactId() < 1) {
312 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
315 // Set all fields: contact
316 this.setContactId(contact.getContactId());
317 this.setBirthday(contact.getContactBirthday());
318 this.setCity(contact.getContactCity());
319 this.setComment(contact.getContactComment());
320 this.setCountry(contact.getContactCountry());
321 this.setEmailAddress(contact.getContactEmailAddress());
322 this.setFamilyName(contact.getContactFamilyName());
323 this.setFirstName(contact.getContactFirstName());
324 this.setGender(contact.getContactGender());
325 this.setHouseNumber(contact.getContactHouseNumber());
326 this.setStreet(contact.getContactStreet());
327 this.setZipCode(contact.getContactZipCode());
329 // ... cellphone data
330 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
331 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
332 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
335 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
336 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
337 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
338 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
341 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
342 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
343 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
344 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
348 public Contact createContactInstance () {
349 // Generate phone number
350 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
351 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
352 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
354 // Create new instance
355 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
357 // Check if contact instance is in helper and valid
358 if (null == contact) {
360 throw new NullPointerException("adminHelper.contact is null"); //NOI18N
361 } else if (contact.getContactId() == null) {
363 throw new NullPointerException("adminHelper.contact.contactId is null"); //NOI18N //NOI18N
364 } else if (contact.getContactId() < 1) {
366 throw new IllegalStateException(MessageFormat.format("adminHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
369 // Update all data in contact
370 this.updateContactData(contact);
372 // Call EJB for updating contact data
373 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
376 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
386 public String editContactData () {
387 // Get contact instance
388 Contact contact = this.adminHelper.getContact();
390 // Check if contact instance is in helper and valid
391 if (null == contact) {
393 throw new NullPointerException("adminHelper.contact is null"); //NOI18N
394 } else if (contact.getContactId() == null) {
396 throw new NullPointerException("adminHelper.contact.contactId is null"); //NOI18N //NOI18N
397 } else if (contact.getContactId() < 1) {
399 throw new IllegalStateException(MessageFormat.format("adminHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
402 // Update all data in contact
403 this.updateContactData(contact);
405 // Call EJB for updating contact data
406 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
409 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
414 // Return to contact list (for now)
415 return "admin_list_contact"; //NOI18N
419 @SuppressWarnings ("ReturnOfDateField")
420 public Date getBirthday () {
421 return this.birthday;
425 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
426 public void setBirthday (final Date birthday) {
427 this.birthday = birthday;
431 public MobileProvider getCellphoneCarrier () {
432 return this.cellphoneCarrier;
436 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
437 this.cellphoneCarrier = cellphoneCarrier;
441 public Long getCellphoneId () {
442 return this.cellphoneId;
446 public void setCellphoneId (final Long cellphoneId) {
447 this.cellphoneId = cellphoneId;
451 public Long getCellphoneNumber () {
452 return this.cellphoneNumber;
456 public void setCellphoneNumber (Long cellphoneNumber) {
457 this.cellphoneNumber = cellphoneNumber;
461 public String getCity () {
466 public void setCity (final String city) {
471 public String getComment () {
476 public void setComment (final String comment) {
477 this.comment = comment;
481 public Long getContactId () {
482 return this.contactId;
486 public void setContactId (final Long contactId) {
487 this.contactId = contactId;
491 public Country getCountry () {
496 public void setCountry (final Country country) {
497 this.country = country;
501 public String getEmailAddress () {
502 return this.emailAddress;
506 public void setEmailAddress (final String emailAddress) {
507 this.emailAddress = emailAddress;
511 public String getFamilyName () {
512 return this.familyName;
516 public void setFamilyName (final String familyName) {
517 this.familyName = familyName;
521 public Integer getFaxAreaCode () {
522 return this.faxAreaCode;
526 public void setFaxAreaCode (final Integer faxAreaCode) {
527 this.faxAreaCode = faxAreaCode;
531 public Country getFaxCountry () {
532 return this.faxCountry;
536 public void setFaxCountry (final Country faxCountry) {
537 this.faxCountry = faxCountry;
541 public Long getFaxId () {
546 public void setFaxId (final Long faxId) {
551 public Long getFaxNumber () {
552 return this.faxNumber;
556 public void setFaxNumber (final Long faxNumber) {
557 this.faxNumber = faxNumber;
561 public String getFirstName () {
562 return this.firstName;
566 public void setFirstName (final String firstName) {
567 this.firstName = firstName;
571 public Gender getGender () {
576 public void setGender (final Gender gender) {
577 this.gender = gender;
581 public Short getHouseNumber () {
582 return this.houseNumber;
586 public void setHouseNumber (final Short houseNumber) {
587 this.houseNumber = houseNumber;
591 public Long getLandLineId () {
592 return this.landLineId;
596 public void setLandLineId (final Long landLineId) {
597 this.landLineId = landLineId;
601 public Integer getPhoneAreaCode () {
602 return this.phoneAreaCode;
606 public void setPhoneAreaCode (final Integer phoneAreaCode) {
607 this.phoneAreaCode = phoneAreaCode;
611 public Country getPhoneCountry () {
612 return this.phoneCountry;
616 public void setPhoneCountry (final Country phoneCountry) {
617 this.phoneCountry = phoneCountry;
621 public Long getPhoneNumber () {
622 return this.phoneNumber;
626 public void setPhoneNumber (final Long phoneNumber) {
627 this.phoneNumber = phoneNumber;
631 public String getStreet () {
636 public void setStreet (final String street) {
637 this.street = street;
641 public Integer getZipCode () {
646 public void setZipCode (final Integer zipCode) {
647 this.zipCode = zipCode;
651 public boolean hasContacts () {
652 return (!this.allContacts().isEmpty());
656 * Post-initialization of this class
659 public void init () {
665 private void clear () {
668 this.setGender(Gender.UNKNOWN);
669 this.setFirstName(null);
670 this.setFamilyName(null);
671 this.setStreet(null);
672 this.setHouseNumber(null);
673 this.setZipCode(null);
675 this.setCountry(null);
678 this.setEmailAddress(null);
679 this.setPhoneCountry(null);
680 this.setPhoneAreaCode(null);
681 this.setPhoneNumber(null);
682 this.setCellphoneCarrier(null);
683 this.setCellphoneNumber(null);
684 this.setFaxCountry(null);
685 this.setFaxAreaCode(null);
686 this.setFaxNumber(null);
689 this.setBirthday(null);
690 this.setComment(null);
694 * Checks whether the given contact is found
696 * @param contact Contact inastance
698 * @return Wether contact has been found
700 private boolean isSameContactFound (final Contact contact) {
701 // Default is not found
702 boolean IsFound = false;
705 Iterator<Contact> iterator = this.allContacts().iterator();
708 while (iterator.hasNext()) {
710 Contact next = iterator.next();
713 if (ContactUtils.isSameContact(contact, next)) {
714 // Yes, then abort loop
725 * Updates all data in contact instance.
727 * @param contact Contact instance
729 private void updateContactData (final Contact contact) {
730 // Contact instance should be valid
731 if (null == contact) {
733 throw new NullPointerException("contact is null"); //NOI18N
734 } else if (contact.getContactId() == null) {
736 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
737 } else if (contact.getContactId() < 1) {
739 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
743 contact.setContactGender(this.getGender());
744 contact.setContactFirstName(this.getFirstName());
745 contact.setContactFamilyName(this.getFamilyName());
746 contact.setContactStreet(this.getStreet());
747 contact.setContactHouseNumber(this.getHouseNumber());
748 contact.setContactZipCode(this.getZipCode());
749 contact.setContactCity(this.getCity());
750 contact.setContactCountry(this.getCountry());
752 // Update contact's cellphone number
753 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
755 // Update contact's land-line number
756 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
758 // Update contact's fax number
759 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());