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.add.AdminAddedContactEvent;
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.jjobs.beans.helper.JobsAdminWebRequestController;
40 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
41 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
42 import org.mxchange.jusercore.events.user.update.AdminUserDataUpdatedEvent;
43 import org.mxchange.pizzaapplication.beans.helper.PizzaAdminWebRequestController;
46 * A user bean (controller)
48 * @author Roland Haeder<roland@mxchange.org>
50 @Named ("adminContactController")
52 public class JobsAdminContactWebRequestBean implements JobsAdminContactWebRequestController {
57 private static final long serialVersionUID = 542_145_347_916L;
60 * An event fired when the administrator has added a new user
64 private Event<AdminAddedUserEvent> addedUserEvent;
67 * Admin helper instance
70 private JobsAdminWebRequestController adminHelper;
75 private Date birthday;
78 * Cellphone number's carrier
80 private MobileProvider cellphoneCarrier;
85 private Long cellphoneId;
90 private Long cellphoneNumber;
100 private String comment;
103 * Remote contact bean
105 private final ContactSessionBeanRemote contactBean;
110 private Long contactId;
115 private Country country;
120 private String emailAddress;
125 private String familyName;
128 * Fax number's area code
130 private Integer faxAreaCode;
133 * Country instance for fax number
135 private Country faxCountry;
145 private Long faxNumber;
150 private String firstName;
155 private Gender gender;
160 private Short houseNumber;
163 * Land-line id number
165 private Long landLineId;
168 * Phone number area code
170 private Integer phoneAreaCode;
173 * Country instance for phone number
175 private Country phoneCountry;
180 private Long phoneNumber;
185 private String street;
188 * An event fired when the administrator has updated contact data
192 private Event<AdminUpdatedContactEvent> updatedContactEvent;
197 private Integer zipCode;
200 * Default constructor
202 public JobsAdminContactWebRequestBean () {
203 // Set gender to UNKNOWN
204 this.gender = Gender.UNKNOWN;
208 // Get initial context
209 Context context = new InitialContext();
212 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jratecalc-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
213 } catch (final NamingException e) {
215 throw new FaceletException(e);
220 public String changeContactData () {
221 // Get contact instance
222 Contact contact = this.adminHelper.getContact();
224 // Check if contact instance is in helper and valid
225 if (null == contact) {
227 throw new NullPointerException("adminHelper.contact is null"); //NOI18N
228 } else if (contact.getContactId() == null) {
230 throw new NullPointerException("adminHelper.contact.contactId is null"); //NOI18N //NOI18N
231 } else if (contact.getContactId() < 1) {
233 throw new IllegalStateException(MessageFormat.format("adminHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
236 // Update all data in contact
237 this.updateContactData(contact);
239 // Call EJB for updating contact data
240 Contact updatedContact = this.contactBean.updateContactPersonalData(contact);
243 this.updateList(updatedContact);
246 this.updatedContactDataEvent.fire(new AdminContactUpdatedEvent(updatedContact));
248 // Return to contact list (for now)
249 return "admin_list_contact"; //NOI18N
253 public void copyContactToController (final Contact contact) {
254 // The contact instance must be valid
255 if (null == contact) {
257 throw new NullPointerException("contact is null"); //NOI18N
258 } else if (contact.getContactId() < 1) {
260 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
263 // Set all fields: contact
264 this.setContactId(contact.getContactId());
265 this.setBirthday(contact.getContactBirthday());
266 this.setCity(contact.getContactCity());
267 this.setComment(contact.getContactComment());
268 this.setCountry(contact.getContactCountry());
269 this.setEmailAddress(contact.getContactEmailAddress());
270 this.setFamilyName(contact.getContactFamilyName());
271 this.setFirstName(contact.getContactFirstName());
272 this.setGender(contact.getContactGender());
273 this.setHouseNumber(contact.getContactHouseNumber());
274 this.setStreet(contact.getContactStreet());
275 this.setZipCode(contact.getContactZipCode());
277 // ... cellphone data
278 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
279 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
280 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
283 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
284 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
285 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
286 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
289 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
290 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
291 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
292 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
296 public Date getBirthday () {
297 return this.birthday;
301 public void setBirthday (final Date birthday) {
302 this.birthday = birthday;
306 public MobileProvider getCellphoneCarrier () {
307 return this.cellphoneCarrier;
311 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
312 this.cellphoneCarrier = cellphoneCarrier;
316 public Long getCellphoneId () {
317 return this.cellphoneId;
321 public void setCellphoneId (final Long cellphoneId) {
322 this.cellphoneId = cellphoneId;
326 public Long getCellphoneNumber () {
327 return this.cellphoneNumber;
331 public void setCellphoneNumber (Long cellphoneNumber) {
332 this.cellphoneNumber = cellphoneNumber;
336 public String getCity () {
341 public void setCity (final String city) {
346 public String getComment () {
351 public void setComment (final String comment) {
352 this.comment = comment;
356 public Long getContactId () {
357 return this.contactId;
361 public void setContactId (final Long contactId) {
362 this.contactId = contactId;
366 public Country getCountry () {
371 public void setCountry (final Country country) {
372 this.country = country;
376 public String getEmailAddress () {
377 return this.emailAddress;
381 public void setEmailAddress (final String emailAddress) {
382 this.emailAddress = emailAddress;
386 public String getFamilyName () {
387 return this.familyName;
391 public void setFamilyName (final String familyName) {
392 this.familyName = familyName;
396 public Integer getFaxAreaCode () {
397 return this.faxAreaCode;
401 public void setFaxAreaCode (final Integer faxAreaCode) {
402 this.faxAreaCode = faxAreaCode;
406 public Country getFaxCountry () {
407 return this.faxCountry;
411 public void setFaxCountry (final Country faxCountry) {
412 this.faxCountry = faxCountry;
416 public Long getFaxId () {
421 public void setFaxId (final Long faxId) {
426 public Long getFaxNumber () {
427 return this.faxNumber;
431 public void setFaxNumber (final Long faxNumber) {
432 this.faxNumber = faxNumber;
436 public String getFirstName () {
437 return this.firstName;
441 public void setFirstName (final String firstName) {
442 this.firstName = firstName;
446 public Gender getGender () {
451 public void setGender (final Gender gender) {
452 this.gender = gender;
456 public Short getHouseNumber () {
457 return this.houseNumber;
461 public void setHouseNumber (final Short houseNumber) {
462 this.houseNumber = houseNumber;
466 public Long getLandLineId () {
467 return this.landLineId;
471 public void setLandLineId (final Long landLineId) {
472 this.landLineId = landLineId;
476 public Integer getPhoneAreaCode () {
477 return this.phoneAreaCode;
481 public void setPhoneAreaCode (final Integer phoneAreaCode) {
482 this.phoneAreaCode = phoneAreaCode;
486 public Country getPhoneCountry () {
487 return this.phoneCountry;
491 public void setPhoneCountry (final Country phoneCountry) {
492 this.phoneCountry = phoneCountry;
496 public Long getPhoneNumber () {
497 return this.phoneNumber;
501 public void setPhoneNumber (final Long phoneNumber) {
502 this.phoneNumber = phoneNumber;
506 public String getStreet () {
511 public void setStreet (final String street) {
512 this.street = street;
516 public Integer getZipCode () {
521 public void setZipCode (final Integer zipCode) {
522 this.zipCode = zipCode;
526 * Post-initialization of this class
529 public void init () {
533 * Updates all data in contact instance.
535 * @param contact Contact instance
537 private void updateContactData (final Contact contact) {
538 // Contact instance should be valid
539 if (null == contact) {
541 throw new NullPointerException("contact is null"); //NOI18N
542 } else if (contact.getContactId() == null) {
544 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
545 } else if (contact.getContactId() < 1) {
547 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
551 contact.setContactGender(this.getGender());
552 contact.setContactFirstName(this.getFirstName());
553 contact.setContactFamilyName(this.getFamilyName());
554 contact.setContactStreet(this.getStreet());
555 contact.setContactHouseNumber(this.getHouseNumber());
556 contact.setContactZipCode(this.getZipCode());
557 contact.setContactCity(this.getCity());
558 contact.setContactCountry(this.getCountry());
560 // Update contact's cellphone number
561 ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
563 // Is there a phone number?
564 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
566 System.out.println(MessageFormat.format("updateContactData: phoneId={0}", contact.getContactLandLineNumber().getPhoneId())); //NOI18N
568 // Yes, then update as well
569 contact.getContactLandLineNumber().setPhoneAreaCode(this.getPhoneAreaCode());
570 contact.getContactLandLineNumber().setPhoneNumber(this.getPhoneNumber());
573 // Is there a fax number?
574 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
576 System.out.println(MessageFormat.format("updateContactData: faxId={0}", contact.getContactFaxNumber().getPhoneId())); //NOI18N
578 // Yes, then update as well
579 contact.getContactFaxNumber().setPhoneAreaCode(this.getFaxAreaCode());
580 contact.getContactFaxNumber().setPhoneNumber(this.getFaxNumber());