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.jjobs.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.jcontacts.contact.Contact;
32 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
33 import org.mxchange.jcontacts.contact.gender.Gender;
34 import org.mxchange.jcontacts.contact.utils.ContactUtils;
35 import org.mxchange.jcountry.data.Country;
36 import org.mxchange.jjobs.beans.helper.JobsAdminWebRequestController;
37 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
38 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
39 import org.mxchange.jusercore.events.user.update.AdminUserDataUpdatedEvent;
42 * A user bean (controller)
44 * @author Roland Haeder<roland@mxchange.org>
46 @Named ("adminContactController")
48 public class JobsAdminContactWebRequestBean implements JobsAdminContactWebRequestController {
53 private static final long serialVersionUID = 542_145_347_916L;
56 * An event fired when the administrator has added a new user
60 private Event<AdminAddedUserEvent> addedUserEvent;
63 * Admin helper instance
66 private JobsAdminWebRequestController adminHelper;
71 private Date birthday;
74 * Cellphone number's carrier
76 private MobileProvider cellphoneCarrier;
81 private Long cellphoneId;
86 private Long cellphoneNumber;
96 private String comment;
101 private final ContactSessionBeanRemote contactBean;
106 private Long contactId;
111 private Country country;
116 private String emailAddress;
121 private String familyName;
124 * Fax number's area code
126 private Integer faxAreaCode;
129 * Country instance for fax number
131 private Country faxCountry;
141 private Long faxNumber;
146 private String firstName;
151 private Gender gender;
156 private Short houseNumber;
159 * Land-line id number
161 private Long landLineId;
164 * Phone number area code
166 private Integer phoneAreaCode;
169 * Country instance for phone number
171 private Country phoneCountry;
176 private Long phoneNumber;
181 private String street;
186 private Integer zipCode;
189 * Default constructor
191 public JobsAdminContactWebRequestBean () {
192 // Set gender to UNKNOWN
193 this.gender = Gender.UNKNOWN;
197 // Get initial context
198 Context context = new InitialContext();
201 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jratecalc-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
202 } catch (final NamingException e) {
204 throw new FaceletException(e);
209 public String changeContactData () {
210 // Get contact instance
211 Contact contact = this.adminHelper.getContact();
213 // Check if contact instance is in helper and valid
214 if (null == contact) {
216 throw new NullPointerException("adminHelper.contact is null"); //NOI18N
217 } else if (contact.getContactId() == null) {
219 throw new NullPointerException("adminHelper.contact.contactId is null"); //NOI18N //NOI18N
220 } else if (contact.getContactId() < 1) {
222 throw new IllegalStateException(MessageFormat.format("adminHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
225 // Update all data in contact
226 this.updateContactData(contact);
228 // Call EJB for updating contact data
229 Contact updatedContact = this.contactBean.updateContactData(contact);
232 this.updateList(updatedContact);
235 this.updatedContactDataEvent.fire(new AdminUserDataUpdatedEvent(updatedContact));
237 // Return to contact list (for now)
238 return "admin_list_contact"; //NOI18N
242 public void copyContactToController (final Contact contact) {
243 // The contact instance must be valid
244 if (null == contact) {
246 throw new NullPointerException("contact is null"); //NOI18N
247 } else if (contact.getContactId() < 1) {
249 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
252 // Set all fields: contact
253 this.setContactId(contact.getContactId());
254 this.setBirthday(contact.getContactBirthday());
255 this.setCity(contact.getContactCity());
256 this.setComment(contact.getContactComment());
257 this.setCountry(contact.getContactCountry());
258 this.setEmailAddress(contact.getContactEmailAddress());
259 this.setFamilyName(contact.getContactFamilyName());
260 this.setFirstName(contact.getContactFirstName());
261 this.setGender(contact.getContactGender());
262 this.setHouseNumber(contact.getContactHouseNumber());
263 this.setStreet(contact.getContactStreet());
264 this.setZipCode(contact.getContactZipCode());
266 // ... cellphone data
267 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
268 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
269 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
272 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
273 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
274 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
275 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
278 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
279 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
280 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
281 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
285 public Date getBirthday () {
286 return this.birthday;
290 public void setBirthday (final Date birthday) {
291 this.birthday = birthday;
295 public MobileProvider getCellphoneCarrier () {
296 return this.cellphoneCarrier;
300 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
301 this.cellphoneCarrier = cellphoneCarrier;
305 public Long getCellphoneId () {
306 return this.cellphoneId;
310 public void setCellphoneId (final Long cellphoneId) {
311 this.cellphoneId = cellphoneId;
315 public Long getCellphoneNumber () {
316 return this.cellphoneNumber;
320 public void setCellphoneNumber (Long cellphoneNumber) {
321 this.cellphoneNumber = cellphoneNumber;
325 public String getCity () {
330 public void setCity (final String city) {
335 public String getComment () {
340 public void setComment (final String comment) {
341 this.comment = comment;
345 public Long getContactId () {
346 return this.contactId;
350 public void setContactId (final Long contactId) {
351 this.contactId = contactId;
355 public Country getCountry () {
360 public void setCountry (final Country country) {
361 this.country = country;
365 public String getEmailAddress () {
366 return this.emailAddress;
370 public void setEmailAddress (final String emailAddress) {
371 this.emailAddress = emailAddress;
375 public String getFamilyName () {
376 return this.familyName;
380 public void setFamilyName (final String familyName) {
381 this.familyName = familyName;
385 public Integer getFaxAreaCode () {
386 return this.faxAreaCode;
390 public void setFaxAreaCode (final Integer faxAreaCode) {
391 this.faxAreaCode = faxAreaCode;
395 public Country getFaxCountry () {
396 return this.faxCountry;
400 public void setFaxCountry (final Country faxCountry) {
401 this.faxCountry = faxCountry;
405 public Long getFaxId () {
410 public void setFaxId (final Long faxId) {
415 public Long getFaxNumber () {
416 return this.faxNumber;
420 public void setFaxNumber (final Long faxNumber) {
421 this.faxNumber = faxNumber;
425 public String getFirstName () {
426 return this.firstName;
430 public void setFirstName (final String firstName) {
431 this.firstName = firstName;
435 public Gender getGender () {
440 public void setGender (final Gender gender) {
441 this.gender = gender;
445 public Short getHouseNumber () {
446 return this.houseNumber;
450 public void setHouseNumber (final Short houseNumber) {
451 this.houseNumber = houseNumber;
455 public Long getLandLineId () {
456 return this.landLineId;
460 public void setLandLineId (final Long landLineId) {
461 this.landLineId = landLineId;
465 public Integer getPhoneAreaCode () {
466 return this.phoneAreaCode;
470 public void setPhoneAreaCode (final Integer phoneAreaCode) {
471 this.phoneAreaCode = phoneAreaCode;
475 public Country getPhoneCountry () {
476 return this.phoneCountry;
480 public void setPhoneCountry (final Country phoneCountry) {
481 this.phoneCountry = phoneCountry;
485 public Long getPhoneNumber () {
486 return this.phoneNumber;
490 public void setPhoneNumber (final Long phoneNumber) {
491 this.phoneNumber = phoneNumber;
495 public String getStreet () {
500 public void setStreet (final String street) {
501 this.street = street;
505 public Integer getZipCode () {
510 public void setZipCode (final Integer zipCode) {
511 this.zipCode = zipCode;
515 * Post-initialization of this class
518 public void init () {
522 * Updates all data in contact instance.
524 * @param contact Contact instance
526 private void updateContactData (final Contact contact) {
527 // Contact instance should be valid
528 if (null == contact) {
530 throw new NullPointerException("contact is null"); //NOI18N
531 } else if (contact.getContactId() == null) {
533 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
534 } else if (contact.getContactId() < 1) {
536 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
540 contact.setContactGender(this.getGender());
541 contact.setContactFirstName(this.getFirstName());
542 contact.setContactFamilyName(this.getFamilyName());
543 contact.setContactStreet(this.getStreet());
544 contact.setContactHouseNumber(this.getHouseNumber());
545 contact.setContactZipCode(this.getZipCode());
546 contact.setContactCity(this.getCity());
547 contact.setContactCountry(this.getCountry());
549 // Update contact's cellphone number
550 ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
552 // Is there a phone number?
553 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
555 System.out.println(MessageFormat.format("updateContactData: phoneId={0}", contact.getContactLandLineNumber().getPhoneId())); //NOI18N
557 // Yes, then update as well
558 contact.getContactLandLineNumber().setPhoneAreaCode(this.getPhoneAreaCode());
559 contact.getContactLandLineNumber().setPhoneNumber(this.getPhoneNumber());
562 // Is there a fax number?
563 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
565 System.out.println(MessageFormat.format("updateContactData: faxId={0}", contact.getContactFaxNumber().getPhoneId())); //NOI18N
567 // Yes, then update as well
568 contact.getContactFaxNumber().setPhoneAreaCode(this.getFaxAreaCode());
569 contact.getContactFaxNumber().setPhoneNumber(this.getFaxNumber());