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.jcontacts.events.contact.update.AdminContactUpdatedEvent;
36 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
37 import org.mxchange.jcountry.data.Country;
38 import org.mxchange.jjobs.beans.helper.JobsAdminWebRequestController;
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 JobsAdminContactWebRequestBean implements JobsAdminContactWebRequestController {
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 JobsAdminWebRequestController 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 JobsAdminContactWebRequestBean () {
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.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
244 // Return to contact list (for now)
245 return "admin_list_contact"; //NOI18N
249 public void copyContactToController (final Contact contact) {
250 // The contact instance must be valid
251 if (null == contact) {
253 throw new NullPointerException("contact is null"); //NOI18N
254 } else if (contact.getContactId() < 1) {
256 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
259 // Set all fields: contact
260 this.setContactId(contact.getContactId());
261 this.setBirthday(contact.getContactBirthday());
262 this.setCity(contact.getContactCity());
263 this.setComment(contact.getContactComment());
264 this.setCountry(contact.getContactCountry());
265 this.setEmailAddress(contact.getContactEmailAddress());
266 this.setFamilyName(contact.getContactFamilyName());
267 this.setFirstName(contact.getContactFirstName());
268 this.setGender(contact.getContactGender());
269 this.setHouseNumber(contact.getContactHouseNumber());
270 this.setStreet(contact.getContactStreet());
271 this.setZipCode(contact.getContactZipCode());
273 // ... cellphone data
274 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
275 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
276 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
279 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
280 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
281 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
282 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
285 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
286 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
287 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
288 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
292 public Date getBirthday () {
293 return this.birthday;
297 public void setBirthday (final Date birthday) {
298 this.birthday = birthday;
302 public MobileProvider getCellphoneCarrier () {
303 return this.cellphoneCarrier;
307 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
308 this.cellphoneCarrier = cellphoneCarrier;
312 public Long getCellphoneId () {
313 return this.cellphoneId;
317 public void setCellphoneId (final Long cellphoneId) {
318 this.cellphoneId = cellphoneId;
322 public Long getCellphoneNumber () {
323 return this.cellphoneNumber;
327 public void setCellphoneNumber (Long cellphoneNumber) {
328 this.cellphoneNumber = cellphoneNumber;
332 public String getCity () {
337 public void setCity (final String city) {
342 public String getComment () {
347 public void setComment (final String comment) {
348 this.comment = comment;
352 public Long getContactId () {
353 return this.contactId;
357 public void setContactId (final Long contactId) {
358 this.contactId = contactId;
362 public Country getCountry () {
367 public void setCountry (final Country country) {
368 this.country = country;
372 public String getEmailAddress () {
373 return this.emailAddress;
377 public void setEmailAddress (final String emailAddress) {
378 this.emailAddress = emailAddress;
382 public String getFamilyName () {
383 return this.familyName;
387 public void setFamilyName (final String familyName) {
388 this.familyName = familyName;
392 public Integer getFaxAreaCode () {
393 return this.faxAreaCode;
397 public void setFaxAreaCode (final Integer faxAreaCode) {
398 this.faxAreaCode = faxAreaCode;
402 public Country getFaxCountry () {
403 return this.faxCountry;
407 public void setFaxCountry (final Country faxCountry) {
408 this.faxCountry = faxCountry;
412 public Long getFaxId () {
417 public void setFaxId (final Long faxId) {
422 public Long getFaxNumber () {
423 return this.faxNumber;
427 public void setFaxNumber (final Long faxNumber) {
428 this.faxNumber = faxNumber;
432 public String getFirstName () {
433 return this.firstName;
437 public void setFirstName (final String firstName) {
438 this.firstName = firstName;
442 public Gender getGender () {
447 public void setGender (final Gender gender) {
448 this.gender = gender;
452 public Short getHouseNumber () {
453 return this.houseNumber;
457 public void setHouseNumber (final Short houseNumber) {
458 this.houseNumber = houseNumber;
462 public Long getLandLineId () {
463 return this.landLineId;
467 public void setLandLineId (final Long landLineId) {
468 this.landLineId = landLineId;
472 public Integer getPhoneAreaCode () {
473 return this.phoneAreaCode;
477 public void setPhoneAreaCode (final Integer phoneAreaCode) {
478 this.phoneAreaCode = phoneAreaCode;
482 public Country getPhoneCountry () {
483 return this.phoneCountry;
487 public void setPhoneCountry (final Country phoneCountry) {
488 this.phoneCountry = phoneCountry;
492 public Long getPhoneNumber () {
493 return this.phoneNumber;
497 public void setPhoneNumber (final Long phoneNumber) {
498 this.phoneNumber = phoneNumber;
502 public String getStreet () {
507 public void setStreet (final String street) {
508 this.street = street;
512 public Integer getZipCode () {
517 public void setZipCode (final Integer zipCode) {
518 this.zipCode = zipCode;
522 * Post-initialization of this class
525 public void init () {
529 * Updates all data in contact instance.
531 * @param contact Contact instance
533 private void updateContactData (final Contact contact) {
534 // Contact instance should be valid
535 if (null == contact) {
537 throw new NullPointerException("contact is null"); //NOI18N
538 } else if (contact.getContactId() == null) {
540 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
541 } else if (contact.getContactId() < 1) {
543 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
547 contact.setContactGender(this.getGender());
548 contact.setContactFirstName(this.getFirstName());
549 contact.setContactFamilyName(this.getFamilyName());
550 contact.setContactStreet(this.getStreet());
551 contact.setContactHouseNumber(this.getHouseNumber());
552 contact.setContactZipCode(this.getZipCode());
553 contact.setContactCity(this.getCity());
554 contact.setContactCountry(this.getCountry());
556 // Update contact's cellphone number
557 ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
559 // Is there a phone number?
560 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
562 System.out.println(MessageFormat.format("updateContactData: phoneId={0}", contact.getContactLandLineNumber().getPhoneId())); //NOI18N
564 // Yes, then update as well
565 contact.getContactLandLineNumber().setPhoneAreaCode(this.getPhoneAreaCode());
566 contact.getContactLandLineNumber().setPhoneNumber(this.getPhoneNumber());
569 // Is there a fax number?
570 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
572 System.out.println(MessageFormat.format("updateContactData: faxId={0}", contact.getContactFaxNumber().getPhoneId())); //NOI18N
574 // Yes, then update as well
575 contact.getContactFaxNumber().setPhoneAreaCode(this.getFaxAreaCode());
576 contact.getContactFaxNumber().setPhoneNumber(this.getFaxNumber());