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.jcountry.data.Country;
37 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
38 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
39 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
40 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
41 import org.mxchange.jusercore.events.user.update.AdminUserDataUpdatedEvent;
44 * A user bean (controller)
46 * @author Roland Haeder<roland@mxchange.org>
48 @Named ("adminContactController")
50 public class AddressbookAdminContactWebRequestBean implements AddressbookAdminContactWebRequestController {
55 private static final long serialVersionUID = 542_145_347_916L;
58 * An event fired when the administrator has added a new user
62 private Event<AdminAddedUserEvent> addedUserEvent;
65 * Admin helper instance
68 private AddressbookAdminWebRequestController adminHelper;
73 private Date birthday;
76 * Cellphone number's carrier
78 private MobileProvider cellphoneCarrier;
83 private Long cellphoneId;
88 private Long cellphoneNumber;
98 private String comment;
101 * Remote contact bean
103 private final ContactSessionBeanRemote contactBean;
108 private Long contactId;
113 private Country country;
118 private String emailAddress;
123 private String familyName;
126 * Fax number's area code
128 private Integer faxAreaCode;
131 * Country instance for fax number
133 private Country faxCountry;
143 private Long faxNumber;
148 private String firstName;
153 private Gender gender;
158 private Short houseNumber;
161 * Land-line id number
163 private Long landLineId;
166 * Phone number area code
168 private Integer phoneAreaCode;
171 * Country instance for phone number
173 private Country phoneCountry;
178 private Long phoneNumber;
183 private String street;
188 private Integer zipCode;
191 * Default constructor
193 public AddressbookAdminContactWebRequestBean () {
194 // Set gender to UNKNOWN
195 this.gender = Gender.UNKNOWN;
199 // Get initial context
200 Context context = new InitialContext();
203 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jratecalc-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
204 } catch (final NamingException e) {
206 throw new FaceletException(e);
211 public String changeContactData () {
212 // Get contact instance
213 Contact contact = this.adminHelper.getContact();
215 // Check if contact instance is in helper and valid
216 if (null == contact) {
218 throw new NullPointerException("adminHelper.contact is null"); //NOI18N
219 } else if (contact.getContactId() == null) {
221 throw new NullPointerException("adminHelper.contact.contactId is null"); //NOI18N //NOI18N
222 } else if (contact.getContactId() < 1) {
224 throw new IllegalStateException(MessageFormat.format("adminHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
227 // Update all data in contact
228 this.updateContactData(contact);
230 // Call EJB for updating contact data
231 Contact updatedContact = this.contactBean.updateContactData(contact);
234 this.updateList(updatedContact);
237 this.updatedContactDataEvent.fire(new AdminUserDataUpdatedEvent(updatedContact));
239 // Return to contact list (for now)
240 return "admin_list_contact"; //NOI18N
244 public void copyContactToController (final Contact contact) {
245 // The contact instance must be valid
246 if (null == contact) {
248 throw new NullPointerException("contact is null"); //NOI18N
249 } else if (contact.getContactId() < 1) {
251 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
254 // Set all fields: contact
255 this.setContactId(contact.getContactId());
256 this.setBirthday(contact.getContactBirthday());
257 this.setCity(contact.getContactCity());
258 this.setComment(contact.getContactComment());
259 this.setCountry(contact.getContactCountry());
260 this.setEmailAddress(contact.getContactEmailAddress());
261 this.setFamilyName(contact.getContactFamilyName());
262 this.setFirstName(contact.getContactFirstName());
263 this.setGender(contact.getContactGender());
264 this.setHouseNumber(contact.getContactHouseNumber());
265 this.setStreet(contact.getContactStreet());
266 this.setZipCode(contact.getContactZipCode());
268 // ... cellphone data
269 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
270 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
271 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
274 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
275 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
276 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
277 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
280 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
281 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
282 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
283 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
287 public Date getBirthday () {
288 return this.birthday;
292 public void setBirthday (final Date birthday) {
293 this.birthday = birthday;
297 public MobileProvider getCellphoneCarrier () {
298 return this.cellphoneCarrier;
302 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
303 this.cellphoneCarrier = cellphoneCarrier;
307 public Long getCellphoneId () {
308 return this.cellphoneId;
312 public void setCellphoneId (final Long cellphoneId) {
313 this.cellphoneId = cellphoneId;
317 public Long getCellphoneNumber () {
318 return this.cellphoneNumber;
322 public void setCellphoneNumber (Long cellphoneNumber) {
323 this.cellphoneNumber = cellphoneNumber;
327 public String getCity () {
332 public void setCity (final String city) {
337 public String getComment () {
342 public void setComment (final String comment) {
343 this.comment = comment;
347 public Long getContactId () {
348 return this.contactId;
352 public void setContactId (final Long contactId) {
353 this.contactId = contactId;
357 public Country getCountry () {
362 public void setCountry (final Country country) {
363 this.country = country;
367 public String getEmailAddress () {
368 return this.emailAddress;
372 public void setEmailAddress (final String emailAddress) {
373 this.emailAddress = emailAddress;
377 public String getFamilyName () {
378 return this.familyName;
382 public void setFamilyName (final String familyName) {
383 this.familyName = familyName;
387 public Integer getFaxAreaCode () {
388 return this.faxAreaCode;
392 public void setFaxAreaCode (final Integer faxAreaCode) {
393 this.faxAreaCode = faxAreaCode;
397 public Country getFaxCountry () {
398 return this.faxCountry;
402 public void setFaxCountry (final Country faxCountry) {
403 this.faxCountry = faxCountry;
407 public Long getFaxId () {
412 public void setFaxId (final Long faxId) {
417 public Long getFaxNumber () {
418 return this.faxNumber;
422 public void setFaxNumber (final Long faxNumber) {
423 this.faxNumber = faxNumber;
427 public String getFirstName () {
428 return this.firstName;
432 public void setFirstName (final String firstName) {
433 this.firstName = firstName;
437 public Gender getGender () {
442 public void setGender (final Gender gender) {
443 this.gender = gender;
447 public Short getHouseNumber () {
448 return this.houseNumber;
452 public void setHouseNumber (final Short houseNumber) {
453 this.houseNumber = houseNumber;
457 public Long getLandLineId () {
458 return this.landLineId;
462 public void setLandLineId (final Long landLineId) {
463 this.landLineId = landLineId;
467 public Integer getPhoneAreaCode () {
468 return this.phoneAreaCode;
472 public void setPhoneAreaCode (final Integer phoneAreaCode) {
473 this.phoneAreaCode = phoneAreaCode;
477 public Country getPhoneCountry () {
478 return this.phoneCountry;
482 public void setPhoneCountry (final Country phoneCountry) {
483 this.phoneCountry = phoneCountry;
487 public Long getPhoneNumber () {
488 return this.phoneNumber;
492 public void setPhoneNumber (final Long phoneNumber) {
493 this.phoneNumber = phoneNumber;
497 public String getStreet () {
502 public void setStreet (final String street) {
503 this.street = street;
507 public Integer getZipCode () {
512 public void setZipCode (final Integer zipCode) {
513 this.zipCode = zipCode;
517 * Post-initialization of this class
520 public void init () {
524 * Updates all data in contact instance.
526 * @param contact Contact instance
528 private void updateContactData (final Contact contact) {
529 // Contact instance should be valid
530 if (null == contact) {
532 throw new NullPointerException("contact is null"); //NOI18N
533 } else if (contact.getContactId() == null) {
535 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
536 } else if (contact.getContactId() < 1) {
538 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
542 contact.setContactGender(this.getGender());
543 contact.setContactFirstName(this.getFirstName());
544 contact.setContactFamilyName(this.getFamilyName());
545 contact.setContactStreet(this.getStreet());
546 contact.setContactHouseNumber(this.getHouseNumber());
547 contact.setContactZipCode(this.getZipCode());
548 contact.setContactCity(this.getCity());
549 contact.setContactCountry(this.getCountry());
551 // Update contact's cellphone number
552 ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
554 // Is there a phone number?
555 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
557 System.out.println(MessageFormat.format("updateContactData: phoneId={0}", contact.getContactLandLineNumber().getPhoneId())); //NOI18N
559 // Yes, then update as well
560 contact.getContactLandLineNumber().setPhoneAreaCode(this.getPhoneAreaCode());
561 contact.getContactLandLineNumber().setPhoneNumber(this.getPhoneNumber());
564 // Is there a fax number?
565 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
567 System.out.println(MessageFormat.format("updateContactData: faxId={0}", contact.getContactFaxNumber().getPhoneId())); //NOI18N
569 // Yes, then update as well
570 contact.getContactFaxNumber().setPhoneAreaCode(this.getFaxAreaCode());
571 contact.getContactFaxNumber().setPhoneNumber(this.getFaxNumber());