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.addressbook.beans.contact;
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import java.util.List;
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.addressbook.beans.helper.AddressbookAdminWebRequestController;
33 import org.mxchange.jcontacts.contact.Contact;
34 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
35 import org.mxchange.jcontacts.contact.gender.Gender;
36 import org.mxchange.jcontacts.contact.utils.ContactUtils;
37 import org.mxchange.jcontacts.events.contact.update.AdminContactUpdatedEvent;
38 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
39 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
40 import org.mxchange.jcountry.data.Country;
41 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
42 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
45 * An administrative user bean (controller)
47 * @author Roland Haeder<roland@mxchange.org>
49 @Named ("adminContactController")
51 public class AddressbookAdminContactWebRequestBean implements AddressbookAdminContactWebRequestController {
56 private static final long serialVersionUID = 542_145_347_916L;
59 * An event fired when the administrator has added a new user
63 private Event<AdminAddedUserEvent> addedUserEvent;
66 * Admin helper instance
69 private AddressbookAdminWebRequestController adminHelper;
74 private Date birthday;
77 * Cellphone number's carrier
79 private MobileProvider cellphoneCarrier;
84 private Long cellphoneId;
89 private Long cellphoneNumber;
99 private String comment;
102 * Remote contact bean
104 private final ContactSessionBeanRemote contactBean;
109 private Long contactId;
114 private Country country;
119 private String emailAddress;
124 private String familyName;
127 * Fax number's area code
129 private Integer faxAreaCode;
132 * Country instance for fax number
134 private Country faxCountry;
144 private Long faxNumber;
149 private String firstName;
154 private Gender gender;
159 private Short houseNumber;
162 * Whether a cellphone entry has been unlinked
164 private boolean isCellphoneUnlinked;
167 * Whether a fax entry has been unlinked
169 private boolean isFaxUnlinked;
172 * Whether a land-line number has been unlinked
174 private boolean isLandLineUnlinked;
177 * Land-line id number
179 private Long landLineId;
182 * Phone number area code
184 private Integer phoneAreaCode;
187 * Country instance for phone number
189 private Country phoneCountry;
194 private Long phoneNumber;
199 private String street;
202 * An event fired when the administrator has updated contact data
206 private Event<AdminUpdatedContactEvent> updatedContactEvent;
211 private Integer zipCode;
214 * Default constructor
216 public AddressbookAdminContactWebRequestBean () {
217 // Set gender to UNKNOWN
218 this.gender = Gender.UNKNOWN;
222 // Get initial context
223 Context context = new InitialContext();
226 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jratecalc-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
227 } catch (final NamingException e) {
229 throw new FaceletException(e);
234 public String addContact () {
235 // Create new contact instance
236 Contact contact = this.createContactInstance();
238 // Default is not same contact
239 if (this.isSameContactFound(contact)) {
240 // Already registered
241 throw new FaceletException(new ContactAlreadyAddedException(contact));
245 Contact updatedContact;
250 updatedContact = this.contactBean.addContact(contact);
251 } catch (final ContactAlreadyAddedException ex) {
253 throw new FaceletException(ex);
257 this.addedContactEvent.fire(new AdminContactAddedEvent(updatedContact));
263 return "admin_list_contact"; //NOI18N
267 public List<Contact> allContacts () {
268 return this.contactController.allContacts();
272 public void copyContactToController (final Contact contact) {
273 // The contact instance must be valid
274 if (null == contact) {
276 throw new NullPointerException("contact is null"); //NOI18N
277 } else if (contact.getContactId() == null) {
279 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
280 } else if (contact.getContactId() < 1) {
282 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
285 // Set all fields: contact
286 this.setContactId(contact.getContactId());
287 this.setBirthday(contact.getContactBirthday());
288 this.setCity(contact.getContactCity());
289 this.setComment(contact.getContactComment());
290 this.setCountry(contact.getContactCountry());
291 this.setEmailAddress(contact.getContactEmailAddress());
292 this.setFamilyName(contact.getContactFamilyName());
293 this.setFirstName(contact.getContactFirstName());
294 this.setGender(contact.getContactGender());
295 this.setHouseNumber(contact.getContactHouseNumber());
296 this.setStreet(contact.getContactStreet());
297 this.setZipCode(contact.getContactZipCode());
299 // ... cellphone data
300 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
301 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
302 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
305 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
306 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
307 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
308 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
311 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
312 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
313 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
314 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
318 public String editContactData () {
319 // Get contact instance
320 Contact contact = this.adminHelper.getContact();
322 // Check if contact instance is in helper and valid
323 if (null == contact) {
325 throw new NullPointerException("adminHelper.contact is null"); //NOI18N
326 } else if (contact.getContactId() == null) {
328 throw new NullPointerException("adminHelper.contact.contactId is null"); //NOI18N //NOI18N
329 } else if (contact.getContactId() < 1) {
331 throw new IllegalStateException(MessageFormat.format("adminHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
334 // Update all data in contact
335 this.updateContactData(contact);
337 // Call EJB for updating contact data
338 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
341 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
346 // Return to contact list (for now)
347 return "admin_list_contact"; //NOI18N
351 @SuppressWarnings ("ReturnOfDateField")
352 public Date getBirthday () {
353 return this.birthday;
357 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
358 public void setBirthday (final Date birthday) {
359 this.birthday = birthday;
363 public MobileProvider getCellphoneCarrier () {
364 return this.cellphoneCarrier;
368 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
369 this.cellphoneCarrier = cellphoneCarrier;
373 public Long getCellphoneId () {
374 return this.cellphoneId;
378 public void setCellphoneId (final Long cellphoneId) {
379 this.cellphoneId = cellphoneId;
383 public Long getCellphoneNumber () {
384 return this.cellphoneNumber;
388 public void setCellphoneNumber (Long cellphoneNumber) {
389 this.cellphoneNumber = cellphoneNumber;
393 public String getCity () {
398 public void setCity (final String city) {
403 public String getComment () {
408 public void setComment (final String comment) {
409 this.comment = comment;
413 public Long getContactId () {
414 return this.contactId;
418 public void setContactId (final Long contactId) {
419 this.contactId = contactId;
423 public Country getCountry () {
428 public void setCountry (final Country country) {
429 this.country = country;
433 public String getEmailAddress () {
434 return this.emailAddress;
438 public void setEmailAddress (final String emailAddress) {
439 this.emailAddress = emailAddress;
443 public String getFamilyName () {
444 return this.familyName;
448 public void setFamilyName (final String familyName) {
449 this.familyName = familyName;
453 public Integer getFaxAreaCode () {
454 return this.faxAreaCode;
458 public void setFaxAreaCode (final Integer faxAreaCode) {
459 this.faxAreaCode = faxAreaCode;
463 public Country getFaxCountry () {
464 return this.faxCountry;
468 public void setFaxCountry (final Country faxCountry) {
469 this.faxCountry = faxCountry;
473 public Long getFaxId () {
478 public void setFaxId (final Long faxId) {
483 public Long getFaxNumber () {
484 return this.faxNumber;
488 public void setFaxNumber (final Long faxNumber) {
489 this.faxNumber = faxNumber;
493 public String getFirstName () {
494 return this.firstName;
498 public void setFirstName (final String firstName) {
499 this.firstName = firstName;
503 public Gender getGender () {
508 public void setGender (final Gender gender) {
509 this.gender = gender;
513 public Short getHouseNumber () {
514 return this.houseNumber;
518 public void setHouseNumber (final Short houseNumber) {
519 this.houseNumber = houseNumber;
523 public Long getLandLineId () {
524 return this.landLineId;
528 public void setLandLineId (final Long landLineId) {
529 this.landLineId = landLineId;
533 public Integer getPhoneAreaCode () {
534 return this.phoneAreaCode;
538 public void setPhoneAreaCode (final Integer phoneAreaCode) {
539 this.phoneAreaCode = phoneAreaCode;
543 public Country getPhoneCountry () {
544 return this.phoneCountry;
548 public void setPhoneCountry (final Country phoneCountry) {
549 this.phoneCountry = phoneCountry;
553 public Long getPhoneNumber () {
554 return this.phoneNumber;
558 public void setPhoneNumber (final Long phoneNumber) {
559 this.phoneNumber = phoneNumber;
563 public String getStreet () {
568 public void setStreet (final String street) {
569 this.street = street;
573 public Integer getZipCode () {
578 public void setZipCode (final Integer zipCode) {
579 this.zipCode = zipCode;
583 * Post-initialization of this class
586 public void init () {
590 * Updates all data in contact instance.
592 * @param contact Contact instance
594 private void updateContactData (final Contact contact) {
595 // Contact instance should be valid
596 if (null == contact) {
598 throw new NullPointerException("contact is null"); //NOI18N
599 } else if (contact.getContactId() == null) {
601 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
602 } else if (contact.getContactId() < 1) {
604 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
608 contact.setContactGender(this.getGender());
609 contact.setContactFirstName(this.getFirstName());
610 contact.setContactFamilyName(this.getFamilyName());
611 contact.setContactStreet(this.getStreet());
612 contact.setContactHouseNumber(this.getHouseNumber());
613 contact.setContactZipCode(this.getZipCode());
614 contact.setContactCity(this.getCity());
615 contact.setContactCountry(this.getCountry());
617 // Update contact's cellphone number
618 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
620 // Update contact's land-line number
621 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
623 // Update contact's fax number
624 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());