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.pizzaapplication.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.AdminAddedContactEvent;
36 import org.mxchange.jcountry.data.Country;
37 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
38 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
39 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
40 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
41 import org.mxchange.jusercore.events.user.update.AdminUserDataUpdatedEvent;
42 import org.mxchange.pizzaapplication.beans.helper.PizzaAdminWebRequestController;
45 * A user bean (controller)
47 * @author Roland Haeder<roland@mxchange.org>
49 @Named ("adminContactController")
51 public class PizzaAdminContactWebRequestBean implements PizzaAdminContactWebRequestController {
56 private static final long serialVersionUID = 542_145_347_916L;
59 * An event fired when the administrator has added a new contact
63 private Event<AdminAddedContactEvent> addedContactEvent;
66 * Admin helper instance
69 private PizzaAdminWebRequestController 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;
189 private Integer zipCode;
192 * Default constructor
194 public PizzaAdminContactWebRequestBean () {
195 // Set gender to UNKNOWN
196 this.gender = Gender.UNKNOWN;
200 // Get initial context
201 Context context = new InitialContext();
204 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
205 } catch (final NamingException e) {
207 throw new FaceletException(e);
212 public String changeContactData () {
213 // Get contact instance
214 Contact contact = this.adminHelper.getContact();
216 // Check if contact instance is in helper and valid
217 if (null == contact) {
219 throw new NullPointerException("adminHelper.contact is null"); //NOI18N
220 } else if (contact.getContactId() == null) {
222 throw new NullPointerException("adminHelper.contact.contactId is null"); //NOI18N //NOI18N
223 } else if (contact.getContactId() < 1) {
225 throw new IllegalStateException(MessageFormat.format("adminHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
228 // Update all data in contact
229 this.updateContactData(contact);
231 // Call EJB for updating contact data
232 Contact updatedContact = this.contactBean.updateContactData(contact);
235 this.updateList(updatedContact);
238 this.updatedContactDataEvent.fire(new AdminUserDataUpdatedEvent(updatedContact));
240 // Return to contact list (for now)
241 return "admin_list_contact"; //NOI18N
245 public void copyContactToController (final Contact contact) {
247 System.out.println(MessageFormat.format("AdminContactController::copyContactToController(): contact={0} - CALLED!", contact)); //NOI18N
249 // The contact instance must be valid
250 if (null == contact) {
252 throw new NullPointerException("contact is null"); //NOI18N
253 } else if (contact.getContactId() < 1) {
255 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
258 // Set all fields: contact
259 this.setContactId(contact.getContactId());
260 this.setBirthday(contact.getContactBirthday());
261 this.setCity(contact.getContactCity());
262 this.setComment(contact.getContactComment());
263 this.setCountry(contact.getContactCountry());
264 this.setEmailAddress(contact.getContactEmailAddress());
265 this.setFamilyName(contact.getContactFamilyName());
266 this.setFirstName(contact.getContactFirstName());
267 this.setGender(contact.getContactGender());
268 this.setHouseNumber(contact.getContactHouseNumber());
269 this.setStreet(contact.getContactStreet());
270 this.setZipCode(contact.getContactZipCode());
272 // Is the cell phone set?
273 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
274 // ... cellphone data
275 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
276 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
277 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
281 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
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 // Is the land-line number set?
290 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
292 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
293 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
294 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
295 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
299 System.out.println("AdminContactController::copyContactToController(): EXIT!"); //NOI18N
303 public Date getBirthday () {
304 return this.birthday;
308 public void setBirthday (final Date birthday) {
309 this.birthday = birthday;
313 public MobileProvider getCellphoneCarrier () {
314 return this.cellphoneCarrier;
318 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
319 this.cellphoneCarrier = cellphoneCarrier;
323 public Long getCellphoneId () {
324 return this.cellphoneId;
328 public void setCellphoneId (final Long cellphoneId) {
329 this.cellphoneId = cellphoneId;
333 public Long getCellphoneNumber () {
334 return this.cellphoneNumber;
338 public void setCellphoneNumber (Long cellphoneNumber) {
339 this.cellphoneNumber = cellphoneNumber;
343 public String getCity () {
348 public void setCity (final String city) {
353 public String getComment () {
358 public void setComment (final String comment) {
359 this.comment = comment;
363 public Long getContactId () {
364 return this.contactId;
368 public void setContactId (final Long contactId) {
369 this.contactId = contactId;
373 public Country getCountry () {
378 public void setCountry (final Country country) {
379 this.country = country;
383 public String getEmailAddress () {
384 return this.emailAddress;
388 public void setEmailAddress (final String emailAddress) {
389 this.emailAddress = emailAddress;
393 public String getFamilyName () {
394 return this.familyName;
398 public void setFamilyName (final String familyName) {
399 this.familyName = familyName;
403 public Integer getFaxAreaCode () {
404 return this.faxAreaCode;
408 public void setFaxAreaCode (final Integer faxAreaCode) {
409 this.faxAreaCode = faxAreaCode;
413 public Country getFaxCountry () {
414 return this.faxCountry;
418 public void setFaxCountry (final Country faxCountry) {
419 this.faxCountry = faxCountry;
423 public Long getFaxId () {
428 public void setFaxId (final Long faxId) {
433 public Long getFaxNumber () {
434 return this.faxNumber;
438 public void setFaxNumber (final Long faxNumber) {
439 this.faxNumber = faxNumber;
443 public String getFirstName () {
444 return this.firstName;
448 public void setFirstName (final String firstName) {
449 this.firstName = firstName;
453 public Gender getGender () {
458 public void setGender (final Gender gender) {
459 this.gender = gender;
463 public Short getHouseNumber () {
464 return this.houseNumber;
468 public void setHouseNumber (final Short houseNumber) {
469 this.houseNumber = houseNumber;
473 public Long getLandLineId () {
474 return this.landLineId;
478 public void setLandLineId (final Long landLineId) {
479 this.landLineId = landLineId;
483 public Integer getPhoneAreaCode () {
484 return this.phoneAreaCode;
488 public void setPhoneAreaCode (final Integer phoneAreaCode) {
489 this.phoneAreaCode = phoneAreaCode;
493 public Country getPhoneCountry () {
494 return this.phoneCountry;
498 public void setPhoneCountry (final Country phoneCountry) {
499 this.phoneCountry = phoneCountry;
503 public Long getPhoneNumber () {
504 return this.phoneNumber;
508 public void setPhoneNumber (final Long phoneNumber) {
509 this.phoneNumber = phoneNumber;
513 public String getStreet () {
518 public void setStreet (final String street) {
519 this.street = street;
523 public Integer getZipCode () {
528 public void setZipCode (final Integer zipCode) {
529 this.zipCode = zipCode;
533 * Post-initialization of this class
536 public void init () {
540 * Updates all data in contact instance.
542 * @param contact Contact instance
544 private void updateContactData (final Contact contact) {
545 // Contact instance should be valid
546 if (null == contact) {
548 throw new NullPointerException("contact is null"); //NOI18N
549 } else if (contact.getContactId() == null) {
551 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
552 } else if (contact.getContactId() < 1) {
554 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
558 contact.setContactGender(this.getGender());
559 contact.setContactFirstName(this.getFirstName());
560 contact.setContactFamilyName(this.getFamilyName());
561 contact.setContactStreet(this.getStreet());
562 contact.setContactHouseNumber(this.getHouseNumber());
563 contact.setContactZipCode(this.getZipCode());
564 contact.setContactCity(this.getCity());
565 contact.setContactCountry(this.getCountry());
567 // Update contact's cellphone number
568 ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
570 // Is there a phone number?
571 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
573 System.out.println(MessageFormat.format("updateContactData: phoneId={0}", contact.getContactLandLineNumber().getPhoneId())); //NOI18N
575 // Yes, then update as well
576 contact.getContactLandLineNumber().setPhoneAreaCode(this.getPhoneAreaCode());
577 contact.getContactLandLineNumber().setPhoneNumber(this.getPhoneNumber());
580 // Is there a fax number?
581 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
583 System.out.println(MessageFormat.format("updateContactData: faxId={0}", contact.getContactFaxNumber().getPhoneId())); //NOI18N
585 // Yes, then update as well
586 contact.getContactFaxNumber().setPhoneAreaCode(this.getFaxAreaCode());
587 contact.getContactFaxNumber().setPhoneNumber(this.getFaxNumber());