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.add.AdminAddedContactEvent;
37 import org.mxchange.jcontacts.events.contact.update.AdminContactUpdatedEvent;
38 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
39 import org.mxchange.jcountry.data.Country;
40 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
41 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
42 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
43 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
44 import org.mxchange.jusercore.events.user.update.AdminUserDataUpdatedEvent;
45 import org.mxchange.pizzaapplication.beans.helper.PizzaAdminWebRequestController;
48 * A user bean (controller)
50 * @author Roland Haeder<roland@mxchange.org>
52 @Named ("adminContactController")
54 public class AddressbookAdminContactWebRequestBean implements AddressbookAdminContactWebRequestController {
59 private static final long serialVersionUID = 542_145_347_916L;
62 * An event fired when the administrator has added a new user
66 private Event<AdminAddedUserEvent> addedUserEvent;
69 * Admin helper instance
72 private AddressbookAdminWebRequestController adminHelper;
77 private Date birthday;
80 * Cellphone number's carrier
82 private MobileProvider cellphoneCarrier;
87 private Long cellphoneId;
92 private Long cellphoneNumber;
102 private String comment;
105 * Remote contact bean
107 private final ContactSessionBeanRemote contactBean;
112 private Long contactId;
117 private Country country;
122 private String emailAddress;
127 private String familyName;
130 * Fax number's area code
132 private Integer faxAreaCode;
135 * Country instance for fax number
137 private Country faxCountry;
147 private Long faxNumber;
152 private String firstName;
157 private Gender gender;
162 private Short houseNumber;
165 * Land-line id number
167 private Long landLineId;
170 * Phone number area code
172 private Integer phoneAreaCode;
175 * Country instance for phone number
177 private Country phoneCountry;
182 private Long phoneNumber;
187 private String street;
190 * An event fired when the administrator has updated contact data
194 private Event<AdminUpdatedContactEvent> updatedContactEvent;
199 private Integer zipCode;
202 * Default constructor
204 public AddressbookAdminContactWebRequestBean () {
205 // Set gender to UNKNOWN
206 this.gender = Gender.UNKNOWN;
210 // Get initial context
211 Context context = new InitialContext();
214 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jratecalc-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
215 } catch (final NamingException e) {
217 throw new FaceletException(e);
222 public String changeContactData () {
223 // Get contact instance
224 Contact contact = this.adminHelper.getContact();
226 // Check if contact instance is in helper and valid
227 if (null == contact) {
229 throw new NullPointerException("adminHelper.contact is null"); //NOI18N
230 } else if (contact.getContactId() == null) {
232 throw new NullPointerException("adminHelper.contact.contactId is null"); //NOI18N //NOI18N
233 } else if (contact.getContactId() < 1) {
235 throw new IllegalStateException(MessageFormat.format("adminHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
238 // Update all data in contact
239 this.updateContactData(contact);
241 // Call EJB for updating contact data
242 Contact updatedContact = this.contactBean.updateContactPersonalData(contact);
245 this.updateList(updatedContact);
248 this.updatedContactDataEvent.fire(new AdminContactUpdatedEvent(updatedContact));
250 // Return to contact list (for now)
251 return "admin_list_contact"; //NOI18N
255 public void copyContactToController (final Contact contact) {
256 // The contact instance must be valid
257 if (null == contact) {
259 throw new NullPointerException("contact is null"); //NOI18N
260 } else if (contact.getContactId() < 1) {
262 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
265 // Set all fields: contact
266 this.setContactId(contact.getContactId());
267 this.setBirthday(contact.getContactBirthday());
268 this.setCity(contact.getContactCity());
269 this.setComment(contact.getContactComment());
270 this.setCountry(contact.getContactCountry());
271 this.setEmailAddress(contact.getContactEmailAddress());
272 this.setFamilyName(contact.getContactFamilyName());
273 this.setFirstName(contact.getContactFirstName());
274 this.setGender(contact.getContactGender());
275 this.setHouseNumber(contact.getContactHouseNumber());
276 this.setStreet(contact.getContactStreet());
277 this.setZipCode(contact.getContactZipCode());
279 // ... cellphone data
280 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
281 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
282 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
285 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
286 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
287 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
288 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
291 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
292 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
293 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
294 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
298 public Date getBirthday () {
299 return this.birthday;
303 public void setBirthday (final Date birthday) {
304 this.birthday = birthday;
308 public MobileProvider getCellphoneCarrier () {
309 return this.cellphoneCarrier;
313 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
314 this.cellphoneCarrier = cellphoneCarrier;
318 public Long getCellphoneId () {
319 return this.cellphoneId;
323 public void setCellphoneId (final Long cellphoneId) {
324 this.cellphoneId = cellphoneId;
328 public Long getCellphoneNumber () {
329 return this.cellphoneNumber;
333 public void setCellphoneNumber (Long cellphoneNumber) {
334 this.cellphoneNumber = cellphoneNumber;
338 public String getCity () {
343 public void setCity (final String city) {
348 public String getComment () {
353 public void setComment (final String comment) {
354 this.comment = comment;
358 public Long getContactId () {
359 return this.contactId;
363 public void setContactId (final Long contactId) {
364 this.contactId = contactId;
368 public Country getCountry () {
373 public void setCountry (final Country country) {
374 this.country = country;
378 public String getEmailAddress () {
379 return this.emailAddress;
383 public void setEmailAddress (final String emailAddress) {
384 this.emailAddress = emailAddress;
388 public String getFamilyName () {
389 return this.familyName;
393 public void setFamilyName (final String familyName) {
394 this.familyName = familyName;
398 public Integer getFaxAreaCode () {
399 return this.faxAreaCode;
403 public void setFaxAreaCode (final Integer faxAreaCode) {
404 this.faxAreaCode = faxAreaCode;
408 public Country getFaxCountry () {
409 return this.faxCountry;
413 public void setFaxCountry (final Country faxCountry) {
414 this.faxCountry = faxCountry;
418 public Long getFaxId () {
423 public void setFaxId (final Long faxId) {
428 public Long getFaxNumber () {
429 return this.faxNumber;
433 public void setFaxNumber (final Long faxNumber) {
434 this.faxNumber = faxNumber;
438 public String getFirstName () {
439 return this.firstName;
443 public void setFirstName (final String firstName) {
444 this.firstName = firstName;
448 public Gender getGender () {
453 public void setGender (final Gender gender) {
454 this.gender = gender;
458 public Short getHouseNumber () {
459 return this.houseNumber;
463 public void setHouseNumber (final Short houseNumber) {
464 this.houseNumber = houseNumber;
468 public Long getLandLineId () {
469 return this.landLineId;
473 public void setLandLineId (final Long landLineId) {
474 this.landLineId = landLineId;
478 public Integer getPhoneAreaCode () {
479 return this.phoneAreaCode;
483 public void setPhoneAreaCode (final Integer phoneAreaCode) {
484 this.phoneAreaCode = phoneAreaCode;
488 public Country getPhoneCountry () {
489 return this.phoneCountry;
493 public void setPhoneCountry (final Country phoneCountry) {
494 this.phoneCountry = phoneCountry;
498 public Long getPhoneNumber () {
499 return this.phoneNumber;
503 public void setPhoneNumber (final Long phoneNumber) {
504 this.phoneNumber = phoneNumber;
508 public String getStreet () {
513 public void setStreet (final String street) {
514 this.street = street;
518 public Integer getZipCode () {
523 public void setZipCode (final Integer zipCode) {
524 this.zipCode = zipCode;
528 * Post-initialization of this class
531 public void init () {
535 * Updates all data in contact instance.
537 * @param contact Contact instance
539 private void updateContactData (final Contact contact) {
540 // Contact instance should be valid
541 if (null == contact) {
543 throw new NullPointerException("contact is null"); //NOI18N
544 } else if (contact.getContactId() == null) {
546 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
547 } else if (contact.getContactId() < 1) {
549 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
553 contact.setContactGender(this.getGender());
554 contact.setContactFirstName(this.getFirstName());
555 contact.setContactFamilyName(this.getFamilyName());
556 contact.setContactStreet(this.getStreet());
557 contact.setContactHouseNumber(this.getHouseNumber());
558 contact.setContactZipCode(this.getZipCode());
559 contact.setContactCity(this.getCity());
560 contact.setContactCountry(this.getCountry());
562 // Update contact's cellphone number
563 ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
565 // Is there a phone number?
566 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
568 System.out.println(MessageFormat.format("updateContactData: phoneId={0}", contact.getContactLandLineNumber().getPhoneId())); //NOI18N
570 // Yes, then update as well
571 contact.getContactLandLineNumber().setPhoneAreaCode(this.getPhoneAreaCode());
572 contact.getContactLandLineNumber().setPhoneNumber(this.getPhoneNumber());
575 // Is there a fax number?
576 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
578 System.out.println(MessageFormat.format("updateContactData: faxId={0}", contact.getContactFaxNumber().getPhoneId())); //NOI18N
580 // Yes, then update as well
581 contact.getContactFaxNumber().setPhoneAreaCode(this.getFaxAreaCode());
582 contact.getContactFaxNumber().setPhoneNumber(this.getFaxNumber());