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.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.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
40 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
41 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
42 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
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 PizzaAdminContactWebRequestBean implements PizzaAdminContactWebRequestController {
57 private static final long serialVersionUID = 542_145_347_916L;
60 * An event fired when the administrator has added a new contact
64 private Event<AdminAddedContactEvent> addedContactEvent;
67 * Admin helper instance
70 private PizzaAdminWebRequestController 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 PizzaAdminContactWebRequestBean () {
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/PizzaService-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) {
255 System.out.println(MessageFormat.format("AdminContactController::copyContactToController(): contact={0} - CALLED!", contact)); //NOI18N
257 // The contact instance must be valid
258 if (null == contact) {
260 throw new NullPointerException("contact is null"); //NOI18N
261 } else if (contact.getContactId() < 1) {
263 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
266 // Set all fields: contact
267 this.setContactId(contact.getContactId());
268 this.setBirthday(contact.getContactBirthday());
269 this.setCity(contact.getContactCity());
270 this.setComment(contact.getContactComment());
271 this.setCountry(contact.getContactCountry());
272 this.setEmailAddress(contact.getContactEmailAddress());
273 this.setFamilyName(contact.getContactFamilyName());
274 this.setFirstName(contact.getContactFirstName());
275 this.setGender(contact.getContactGender());
276 this.setHouseNumber(contact.getContactHouseNumber());
277 this.setStreet(contact.getContactStreet());
278 this.setZipCode(contact.getContactZipCode());
280 // Is the cell phone set?
281 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
282 // ... cellphone data
283 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
284 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
285 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
289 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
291 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
292 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
293 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
294 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
297 // Is the land-line number set?
298 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
300 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
301 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
302 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
303 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
307 System.out.println("AdminContactController::copyContactToController(): EXIT!"); //NOI18N
311 public Date getBirthday () {
312 return this.birthday;
316 public void setBirthday (final Date birthday) {
317 this.birthday = birthday;
321 public MobileProvider getCellphoneCarrier () {
322 return this.cellphoneCarrier;
326 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
327 this.cellphoneCarrier = cellphoneCarrier;
331 public Long getCellphoneId () {
332 return this.cellphoneId;
336 public void setCellphoneId (final Long cellphoneId) {
337 this.cellphoneId = cellphoneId;
341 public Long getCellphoneNumber () {
342 return this.cellphoneNumber;
346 public void setCellphoneNumber (Long cellphoneNumber) {
347 this.cellphoneNumber = cellphoneNumber;
351 public String getCity () {
356 public void setCity (final String city) {
361 public String getComment () {
366 public void setComment (final String comment) {
367 this.comment = comment;
371 public Long getContactId () {
372 return this.contactId;
376 public void setContactId (final Long contactId) {
377 this.contactId = contactId;
381 public Country getCountry () {
386 public void setCountry (final Country country) {
387 this.country = country;
391 public String getEmailAddress () {
392 return this.emailAddress;
396 public void setEmailAddress (final String emailAddress) {
397 this.emailAddress = emailAddress;
401 public String getFamilyName () {
402 return this.familyName;
406 public void setFamilyName (final String familyName) {
407 this.familyName = familyName;
411 public Integer getFaxAreaCode () {
412 return this.faxAreaCode;
416 public void setFaxAreaCode (final Integer faxAreaCode) {
417 this.faxAreaCode = faxAreaCode;
421 public Country getFaxCountry () {
422 return this.faxCountry;
426 public void setFaxCountry (final Country faxCountry) {
427 this.faxCountry = faxCountry;
431 public Long getFaxId () {
436 public void setFaxId (final Long faxId) {
441 public Long getFaxNumber () {
442 return this.faxNumber;
446 public void setFaxNumber (final Long faxNumber) {
447 this.faxNumber = faxNumber;
451 public String getFirstName () {
452 return this.firstName;
456 public void setFirstName (final String firstName) {
457 this.firstName = firstName;
461 public Gender getGender () {
466 public void setGender (final Gender gender) {
467 this.gender = gender;
471 public Short getHouseNumber () {
472 return this.houseNumber;
476 public void setHouseNumber (final Short houseNumber) {
477 this.houseNumber = houseNumber;
481 public Long getLandLineId () {
482 return this.landLineId;
486 public void setLandLineId (final Long landLineId) {
487 this.landLineId = landLineId;
491 public Integer getPhoneAreaCode () {
492 return this.phoneAreaCode;
496 public void setPhoneAreaCode (final Integer phoneAreaCode) {
497 this.phoneAreaCode = phoneAreaCode;
501 public Country getPhoneCountry () {
502 return this.phoneCountry;
506 public void setPhoneCountry (final Country phoneCountry) {
507 this.phoneCountry = phoneCountry;
511 public Long getPhoneNumber () {
512 return this.phoneNumber;
516 public void setPhoneNumber (final Long phoneNumber) {
517 this.phoneNumber = phoneNumber;
521 public String getStreet () {
526 public void setStreet (final String street) {
527 this.street = street;
531 public Integer getZipCode () {
536 public void setZipCode (final Integer zipCode) {
537 this.zipCode = zipCode;
541 * Post-initialization of this class
544 public void init () {
548 * Updates all data in contact instance.
550 * @param contact Contact instance
552 private void updateContactData (final Contact contact) {
553 // Contact instance should be valid
554 if (null == contact) {
556 throw new NullPointerException("contact is null"); //NOI18N
557 } else if (contact.getContactId() == null) {
559 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
560 } else if (contact.getContactId() < 1) {
562 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
566 contact.setContactGender(this.getGender());
567 contact.setContactFirstName(this.getFirstName());
568 contact.setContactFamilyName(this.getFamilyName());
569 contact.setContactStreet(this.getStreet());
570 contact.setContactHouseNumber(this.getHouseNumber());
571 contact.setContactZipCode(this.getZipCode());
572 contact.setContactCity(this.getCity());
573 contact.setContactCountry(this.getCountry());
575 // Update contact's cellphone number
576 ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
578 // Is there a phone number?
579 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
581 System.out.println(MessageFormat.format("updateContactData: phoneId={0}", contact.getContactLandLineNumber().getPhoneId())); //NOI18N
583 // Yes, then update as well
584 contact.getContactLandLineNumber().setPhoneAreaCode(this.getPhoneAreaCode());
585 contact.getContactLandLineNumber().setPhoneNumber(this.getPhoneNumber());
588 // Is there a fax number?
589 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
591 System.out.println(MessageFormat.format("updateContactData: faxId={0}", contact.getContactFaxNumber().getPhoneId())); //NOI18N
593 // Yes, then update as well
594 contact.getContactFaxNumber().setPhoneAreaCode(this.getFaxAreaCode());
595 contact.getContactFaxNumber().setPhoneNumber(this.getFaxNumber());