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 javax.annotation.PostConstruct;
22 import javax.enterprise.context.RequestScoped;
23 import javax.enterprise.event.Event;
24 import javax.enterprise.inject.Any;
25 import javax.faces.view.facelets.FaceletException;
26 import javax.inject.Inject;
27 import javax.inject.Named;
28 import javax.naming.Context;
29 import javax.naming.InitialContext;
30 import javax.naming.NamingException;
31 import org.mxchange.addressbook.beans.helper.AddressbookAdminWebRequestController;
32 import org.mxchange.jcontacts.contact.Contact;
33 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
34 import org.mxchange.jcontacts.contact.gender.Gender;
35 import org.mxchange.jcontacts.contact.utils.ContactUtils;
36 import org.mxchange.jcontacts.events.contact.update.AdminContactUpdatedEvent;
37 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
38 import org.mxchange.jcountry.data.Country;
39 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
40 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
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 changeContactData () {
235 // Get contact instance
236 Contact contact = this.adminHelper.getContact();
238 // Check if contact instance is in helper and valid
239 if (null == contact) {
241 throw new NullPointerException("adminHelper.contact is null"); //NOI18N
242 } else if (contact.getContactId() == null) {
244 throw new NullPointerException("adminHelper.contact.contactId is null"); //NOI18N //NOI18N
245 } else if (contact.getContactId() < 1) {
247 throw new IllegalStateException(MessageFormat.format("adminHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
250 // Update all data in contact
251 this.updateContactData(contact);
253 // Call EJB for updating contact data
254 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
257 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
259 // Return to contact list (for now)
260 return "admin_list_contact"; //NOI18N
264 public void copyContactToController (final Contact contact) {
265 // The contact instance must be valid
266 if (null == contact) {
268 throw new NullPointerException("contact is null"); //NOI18N
269 } else if (contact.getContactId() == null) {
271 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
272 } else if (contact.getContactId() < 1) {
274 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
277 // Set all fields: contact
278 this.setContactId(contact.getContactId());
279 this.setBirthday(contact.getContactBirthday());
280 this.setCity(contact.getContactCity());
281 this.setComment(contact.getContactComment());
282 this.setCountry(contact.getContactCountry());
283 this.setEmailAddress(contact.getContactEmailAddress());
284 this.setFamilyName(contact.getContactFamilyName());
285 this.setFirstName(contact.getContactFirstName());
286 this.setGender(contact.getContactGender());
287 this.setHouseNumber(contact.getContactHouseNumber());
288 this.setStreet(contact.getContactStreet());
289 this.setZipCode(contact.getContactZipCode());
291 // ... cellphone data
292 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
293 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
294 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
297 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
298 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
299 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
300 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
303 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
304 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
305 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
306 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
310 public Date getBirthday () {
311 return this.birthday;
315 public void setBirthday (final Date birthday) {
316 this.birthday = birthday;
320 public MobileProvider getCellphoneCarrier () {
321 return this.cellphoneCarrier;
325 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
326 this.cellphoneCarrier = cellphoneCarrier;
330 public Long getCellphoneId () {
331 return this.cellphoneId;
335 public void setCellphoneId (final Long cellphoneId) {
336 this.cellphoneId = cellphoneId;
340 public Long getCellphoneNumber () {
341 return this.cellphoneNumber;
345 public void setCellphoneNumber (Long cellphoneNumber) {
346 this.cellphoneNumber = cellphoneNumber;
350 public String getCity () {
355 public void setCity (final String city) {
360 public String getComment () {
365 public void setComment (final String comment) {
366 this.comment = comment;
370 public Long getContactId () {
371 return this.contactId;
375 public void setContactId (final Long contactId) {
376 this.contactId = contactId;
380 public Country getCountry () {
385 public void setCountry (final Country country) {
386 this.country = country;
390 public String getEmailAddress () {
391 return this.emailAddress;
395 public void setEmailAddress (final String emailAddress) {
396 this.emailAddress = emailAddress;
400 public String getFamilyName () {
401 return this.familyName;
405 public void setFamilyName (final String familyName) {
406 this.familyName = familyName;
410 public Integer getFaxAreaCode () {
411 return this.faxAreaCode;
415 public void setFaxAreaCode (final Integer faxAreaCode) {
416 this.faxAreaCode = faxAreaCode;
420 public Country getFaxCountry () {
421 return this.faxCountry;
425 public void setFaxCountry (final Country faxCountry) {
426 this.faxCountry = faxCountry;
430 public Long getFaxId () {
435 public void setFaxId (final Long faxId) {
440 public Long getFaxNumber () {
441 return this.faxNumber;
445 public void setFaxNumber (final Long faxNumber) {
446 this.faxNumber = faxNumber;
450 public String getFirstName () {
451 return this.firstName;
455 public void setFirstName (final String firstName) {
456 this.firstName = firstName;
460 public Gender getGender () {
465 public void setGender (final Gender gender) {
466 this.gender = gender;
470 public Short getHouseNumber () {
471 return this.houseNumber;
475 public void setHouseNumber (final Short houseNumber) {
476 this.houseNumber = houseNumber;
480 public Long getLandLineId () {
481 return this.landLineId;
485 public void setLandLineId (final Long landLineId) {
486 this.landLineId = landLineId;
490 public Integer getPhoneAreaCode () {
491 return this.phoneAreaCode;
495 public void setPhoneAreaCode (final Integer phoneAreaCode) {
496 this.phoneAreaCode = phoneAreaCode;
500 public Country getPhoneCountry () {
501 return this.phoneCountry;
505 public void setPhoneCountry (final Country phoneCountry) {
506 this.phoneCountry = phoneCountry;
510 public Long getPhoneNumber () {
511 return this.phoneNumber;
515 public void setPhoneNumber (final Long phoneNumber) {
516 this.phoneNumber = phoneNumber;
520 public String getStreet () {
525 public void setStreet (final String street) {
526 this.street = street;
530 public Integer getZipCode () {
535 public void setZipCode (final Integer zipCode) {
536 this.zipCode = zipCode;
540 * Post-initialization of this class
543 public void init () {
547 * Updates all data in contact instance.
549 * @param contact Contact instance
551 private void updateContactData (final Contact contact) {
552 // Contact instance should be valid
553 if (null == contact) {
555 throw new NullPointerException("contact is null"); //NOI18N
556 } else if (contact.getContactId() == null) {
558 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
559 } else if (contact.getContactId() < 1) {
561 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
565 contact.setContactGender(this.getGender());
566 contact.setContactFirstName(this.getFirstName());
567 contact.setContactFamilyName(this.getFamilyName());
568 contact.setContactStreet(this.getStreet());
569 contact.setContactHouseNumber(this.getHouseNumber());
570 contact.setContactZipCode(this.getZipCode());
571 contact.setContactCity(this.getCity());
572 contact.setContactCountry(this.getCountry());
574 // Update contact's cellphone number
575 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
577 // Update contact's land-line number
578 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
580 // Update contact's fax number
581 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());