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 * A 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 * Land-line id number
164 private Long landLineId;
167 * Phone number area code
169 private Integer phoneAreaCode;
172 * Country instance for phone number
174 private Country phoneCountry;
179 private Long phoneNumber;
184 private String street;
187 * An event fired when the administrator has updated contact data
191 private Event<AdminUpdatedContactEvent> updatedContactEvent;
196 private Integer zipCode;
199 * Default constructor
201 public AddressbookAdminContactWebRequestBean () {
202 // Set gender to UNKNOWN
203 this.gender = Gender.UNKNOWN;
207 // Get initial context
208 Context context = new InitialContext();
211 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jratecalc-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
212 } catch (final NamingException e) {
214 throw new FaceletException(e);
219 public String changeContactData () {
220 // Get contact instance
221 Contact contact = this.adminHelper.getContact();
223 // Check if contact instance is in helper and valid
224 if (null == contact) {
226 throw new NullPointerException("adminHelper.contact is null"); //NOI18N
227 } else if (contact.getContactId() == null) {
229 throw new NullPointerException("adminHelper.contact.contactId is null"); //NOI18N //NOI18N
230 } else if (contact.getContactId() < 1) {
232 throw new IllegalStateException(MessageFormat.format("adminHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
235 // Update all data in contact
236 this.updateContactData(contact);
238 // Call EJB for updating contact data
239 Contact updatedContact = this.contactBean.updateContactPersonalData(contact);
242 this.updateList(updatedContact);
245 this.updatedContactDataEvent.fire(new AdminContactUpdatedEvent(updatedContact));
247 // Return to contact list (for now)
248 return "admin_list_contact"; //NOI18N
252 public void copyContactToController (final Contact contact) {
253 // The contact instance must be valid
254 if (null == contact) {
256 throw new NullPointerException("contact is null"); //NOI18N
257 } else if (contact.getContactId() < 1) {
259 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
262 // Set all fields: contact
263 this.setContactId(contact.getContactId());
264 this.setBirthday(contact.getContactBirthday());
265 this.setCity(contact.getContactCity());
266 this.setComment(contact.getContactComment());
267 this.setCountry(contact.getContactCountry());
268 this.setEmailAddress(contact.getContactEmailAddress());
269 this.setFamilyName(contact.getContactFamilyName());
270 this.setFirstName(contact.getContactFirstName());
271 this.setGender(contact.getContactGender());
272 this.setHouseNumber(contact.getContactHouseNumber());
273 this.setStreet(contact.getContactStreet());
274 this.setZipCode(contact.getContactZipCode());
276 // ... cellphone data
277 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
278 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
279 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
282 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
283 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
284 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
285 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
288 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
289 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
290 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
291 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
295 public Date getBirthday () {
296 return this.birthday;
300 public void setBirthday (final Date birthday) {
301 this.birthday = birthday;
305 public MobileProvider getCellphoneCarrier () {
306 return this.cellphoneCarrier;
310 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
311 this.cellphoneCarrier = cellphoneCarrier;
315 public Long getCellphoneId () {
316 return this.cellphoneId;
320 public void setCellphoneId (final Long cellphoneId) {
321 this.cellphoneId = cellphoneId;
325 public Long getCellphoneNumber () {
326 return this.cellphoneNumber;
330 public void setCellphoneNumber (Long cellphoneNumber) {
331 this.cellphoneNumber = cellphoneNumber;
335 public String getCity () {
340 public void setCity (final String city) {
345 public String getComment () {
350 public void setComment (final String comment) {
351 this.comment = comment;
355 public Long getContactId () {
356 return this.contactId;
360 public void setContactId (final Long contactId) {
361 this.contactId = contactId;
365 public Country getCountry () {
370 public void setCountry (final Country country) {
371 this.country = country;
375 public String getEmailAddress () {
376 return this.emailAddress;
380 public void setEmailAddress (final String emailAddress) {
381 this.emailAddress = emailAddress;
385 public String getFamilyName () {
386 return this.familyName;
390 public void setFamilyName (final String familyName) {
391 this.familyName = familyName;
395 public Integer getFaxAreaCode () {
396 return this.faxAreaCode;
400 public void setFaxAreaCode (final Integer faxAreaCode) {
401 this.faxAreaCode = faxAreaCode;
405 public Country getFaxCountry () {
406 return this.faxCountry;
410 public void setFaxCountry (final Country faxCountry) {
411 this.faxCountry = faxCountry;
415 public Long getFaxId () {
420 public void setFaxId (final Long faxId) {
425 public Long getFaxNumber () {
426 return this.faxNumber;
430 public void setFaxNumber (final Long faxNumber) {
431 this.faxNumber = faxNumber;
435 public String getFirstName () {
436 return this.firstName;
440 public void setFirstName (final String firstName) {
441 this.firstName = firstName;
445 public Gender getGender () {
450 public void setGender (final Gender gender) {
451 this.gender = gender;
455 public Short getHouseNumber () {
456 return this.houseNumber;
460 public void setHouseNumber (final Short houseNumber) {
461 this.houseNumber = houseNumber;
465 public Long getLandLineId () {
466 return this.landLineId;
470 public void setLandLineId (final Long landLineId) {
471 this.landLineId = landLineId;
475 public Integer getPhoneAreaCode () {
476 return this.phoneAreaCode;
480 public void setPhoneAreaCode (final Integer phoneAreaCode) {
481 this.phoneAreaCode = phoneAreaCode;
485 public Country getPhoneCountry () {
486 return this.phoneCountry;
490 public void setPhoneCountry (final Country phoneCountry) {
491 this.phoneCountry = phoneCountry;
495 public Long getPhoneNumber () {
496 return this.phoneNumber;
500 public void setPhoneNumber (final Long phoneNumber) {
501 this.phoneNumber = phoneNumber;
505 public String getStreet () {
510 public void setStreet (final String street) {
511 this.street = street;
515 public Integer getZipCode () {
520 public void setZipCode (final Integer zipCode) {
521 this.zipCode = zipCode;
525 * Post-initialization of this class
528 public void init () {
532 * Updates all data in contact instance.
534 * @param contact Contact instance
536 private void updateContactData (final Contact contact) {
537 // Contact instance should be valid
538 if (null == contact) {
540 throw new NullPointerException("contact is null"); //NOI18N
541 } else if (contact.getContactId() == null) {
543 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
544 } else if (contact.getContactId() < 1) {
546 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
550 contact.setContactGender(this.getGender());
551 contact.setContactFirstName(this.getFirstName());
552 contact.setContactFamilyName(this.getFamilyName());
553 contact.setContactStreet(this.getStreet());
554 contact.setContactHouseNumber(this.getHouseNumber());
555 contact.setContactZipCode(this.getZipCode());
556 contact.setContactCity(this.getCity());
557 contact.setContactCountry(this.getCountry());
559 // Update contact's cellphone number
560 ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
562 // Is there a phone number?
563 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
565 System.out.println(MessageFormat.format("updateContactData: phoneId={0}", contact.getContactLandLineNumber().getPhoneId())); //NOI18N
567 // Yes, then update as well
568 contact.getContactLandLineNumber().setPhoneAreaCode(this.getPhoneAreaCode());
569 contact.getContactLandLineNumber().setPhoneNumber(this.getPhoneNumber());
572 // Is there a fax number?
573 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
575 System.out.println(MessageFormat.format("updateContactData: faxId={0}", contact.getContactFaxNumber().getPhoneId())); //NOI18N
577 // Yes, then update as well
578 contact.getContactFaxNumber().setPhoneAreaCode(this.getFaxAreaCode());
579 contact.getContactFaxNumber().setPhoneNumber(this.getFaxNumber());