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.UserContact;
34 import org.mxchange.jcontacts.contact.gender.Gender;
35 import org.mxchange.jcontacts.contact.utils.ContactUtils;
36 import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent;
37 import org.mxchange.jcontacts.events.contact.update.AdminContactUpdatedEvent;
38 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
39 import org.mxchange.jcountry.data.Country;
40 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
41 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
42 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
43 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
44 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
45 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
46 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
47 import org.mxchange.pizzaapplication.beans.helper.PizzaAdminWebRequestController;
50 * Administrative user bean (controller)
52 * @author Roland Haeder<roland@mxchange.org>
54 @Named ("adminContactController")
56 public class PizzaAdminContactWebRequestBean implements PizzaAdminContactWebRequestController {
61 private static final long serialVersionUID = 542_145_347_916L;
64 * An event fired when the administrator has added a new contact
68 private Event<AdminAddedContactEvent> addedContactEvent;
71 * Admin helper instance
74 private PizzaAdminWebRequestController adminHelper;
79 private Date birthday;
82 * Cellphone number's carrier
84 private MobileProvider cellphoneCarrier;
89 private Long cellphoneId;
94 private Long cellphoneNumber;
104 private String comment;
107 * Remote contact bean
109 private final ContactSessionBeanRemote contactBean;
114 private Long contactId;
119 private Country country;
124 private String emailAddress;
129 private String familyName;
132 * Fax number's area code
134 private Integer faxAreaCode;
137 * Country instance for fax number
139 private Country faxCountry;
149 private Long faxNumber;
154 private String firstName;
159 private Gender gender;
164 private Short houseNumber;
167 * Whether a cellphone entry has been unlinked
169 private boolean isCellphoneUnlinked;
172 * Whether a fax entry has been unlinked
174 private boolean isFaxUnlinked;
177 * Whether a land-line number has been unlinked
179 private boolean isLandLineUnlinked;
182 * Land-line id number
184 private Long landLineId;
187 * Phone number area code
189 private Integer phoneAreaCode;
192 * Country instance for phone number
194 private Country phoneCountry;
199 private Long phoneNumber;
204 private String street;
207 * An event fired when the administrator has updated contact data
211 private Event<AdminUpdatedContactEvent> updatedContactEvent;
216 private Integer zipCode;
219 * Default constructor
221 public PizzaAdminContactWebRequestBean () {
222 // Set gender to UNKNOWN
223 this.gender = Gender.UNKNOWN;
227 // Get initial context
228 Context context = new InitialContext();
231 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
232 } catch (final NamingException e) {
234 throw new FaceletException(e);
239 public String changeContactData () {
240 // Get contact instance
241 Contact contact = this.adminHelper.getContact();
243 // Check if contact instance is in helper and valid
244 if (null == contact) {
246 throw new NullPointerException("adminHelper.contact is null"); //NOI18N
247 } else if (contact.getContactId() == null) {
249 throw new NullPointerException("adminHelper.contact.contactId is null"); //NOI18N //NOI18N
250 } else if (contact.getContactId() < 1) {
252 throw new IllegalStateException(MessageFormat.format("adminHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
255 // Update all data in contact
256 this.updateContactData(contact);
258 // Call EJB for updating contact data
259 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
262 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
264 // Return to contact list (for now)
265 return "admin_list_contact"; //NOI18N
269 public void copyContactToController (final Contact contact) {
271 System.out.println(MessageFormat.format("AdminContactController::copyContactToController(): contact={0} - CALLED!", contact)); //NOI18N
273 // The contact instance must be valid
274 if (null == contact) {
276 throw new NullPointerException("contact is null"); //NOI18N
277 } else if (contact.getContactId() == null) {
279 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
280 } else if (contact.getContactId() < 1) {
282 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
285 // Set all fields: contact
286 this.setContactId(contact.getContactId());
287 this.setBirthday(contact.getContactBirthday());
288 this.setCity(contact.getContactCity());
289 this.setComment(contact.getContactComment());
290 this.setCountry(contact.getContactCountry());
291 this.setEmailAddress(contact.getContactEmailAddress());
292 this.setFamilyName(contact.getContactFamilyName());
293 this.setFirstName(contact.getContactFirstName());
294 this.setGender(contact.getContactGender());
295 this.setHouseNumber(contact.getContactHouseNumber());
296 this.setStreet(contact.getContactStreet());
297 this.setZipCode(contact.getContactZipCode());
299 // Is the cell phone set?
300 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
301 // ... cellphone data
302 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
303 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
304 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
308 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
310 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
311 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
312 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
313 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
316 // Is the land-line number set?
317 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
319 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
320 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
321 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
322 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
326 System.out.println("AdminContactController::copyContactToController(): EXIT!"); //NOI18N
330 public Contact createContactInstance () {
332 //this.getLogger().logTrace("createContactInstance: CALLED!");
334 // Generate phone number
335 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
336 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
337 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
339 // Create new instance
340 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
343 contact.setContactBirthday(this.getBirthday());
344 contact.setContactStreet(this.getStreet());
345 contact.setContactHouseNumber(this.getHouseNumber());
346 contact.setContactZipCode(this.getZipCode());
347 contact.setContactCity(this.getCity());
348 contact.setContactCountry(this.getCountry());
349 contact.setContactEmailAddress(this.getEmailAddress());
350 contact.setContactBirthday(this.getBirthday());
351 contact.setContactComment(this.getComment());
354 contact.setContactOwnContact(Boolean.TRUE);
356 // Don't set null or wrong references
357 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
358 // Now the number must be given
359 if (phone.getPhoneAreaCode() == null) {
361 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
362 } else if (phone.getPhoneAreaCode() < 1) {
364 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
365 } else if (phone.getPhoneNumber() == null) {
367 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
368 } else if (phone.getPhoneNumber() < 1) {
370 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
374 contact.setContactLandLineNumber(phone);
377 // Don't set null or wrong references
378 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
379 // Now the number must be given
380 if (fax.getPhoneAreaCode() == null) {
382 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
383 } else if (fax.getPhoneAreaCode() < 1) {
385 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
386 } else if (fax.getPhoneNumber() == null) {
388 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
389 } else if (fax.getPhoneNumber() < 1) {
391 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
395 contact.setContactFaxNumber(fax);
398 // Is the provider set?
399 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
400 // Is the number set?
401 if (cellphone.getPhoneNumber() == null) {
403 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
404 } else if (cellphone.getPhoneNumber() < 1) {
406 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
409 // Set cellphone number
410 contact.setContactCellphoneNumber(cellphone);
414 //this.getLogger().logTrace(MessageFormat.format("createContactInstance: localContact={0} - EXIT!", localContact));
421 public Date getBirthday () {
422 return this.birthday;
426 public void setBirthday (final Date birthday) {
427 this.birthday = birthday;
431 public MobileProvider getCellphoneCarrier () {
432 return this.cellphoneCarrier;
436 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
437 this.cellphoneCarrier = cellphoneCarrier;
441 public Long getCellphoneId () {
442 return this.cellphoneId;
446 public void setCellphoneId (final Long cellphoneId) {
447 this.cellphoneId = cellphoneId;
451 public Long getCellphoneNumber () {
452 return this.cellphoneNumber;
456 public void setCellphoneNumber (Long cellphoneNumber) {
457 this.cellphoneNumber = cellphoneNumber;
461 public String getCity () {
466 public void setCity (final String city) {
471 public String getComment () {
476 public void setComment (final String comment) {
477 this.comment = comment;
481 public Long getContactId () {
482 return this.contactId;
486 public void setContactId (final Long contactId) {
487 this.contactId = contactId;
491 public Country getCountry () {
496 public void setCountry (final Country country) {
497 this.country = country;
501 public String getEmailAddress () {
502 return this.emailAddress;
506 public void setEmailAddress (final String emailAddress) {
507 this.emailAddress = emailAddress;
511 public String getFamilyName () {
512 return this.familyName;
516 public void setFamilyName (final String familyName) {
517 this.familyName = familyName;
521 public Integer getFaxAreaCode () {
522 return this.faxAreaCode;
526 public void setFaxAreaCode (final Integer faxAreaCode) {
527 this.faxAreaCode = faxAreaCode;
531 public Country getFaxCountry () {
532 return this.faxCountry;
536 public void setFaxCountry (final Country faxCountry) {
537 this.faxCountry = faxCountry;
541 public Long getFaxId () {
546 public void setFaxId (final Long faxId) {
551 public Long getFaxNumber () {
552 return this.faxNumber;
556 public void setFaxNumber (final Long faxNumber) {
557 this.faxNumber = faxNumber;
561 public String getFirstName () {
562 return this.firstName;
566 public void setFirstName (final String firstName) {
567 this.firstName = firstName;
571 public Gender getGender () {
576 public void setGender (final Gender gender) {
577 this.gender = gender;
581 public Short getHouseNumber () {
582 return this.houseNumber;
586 public void setHouseNumber (final Short houseNumber) {
587 this.houseNumber = houseNumber;
591 public Long getLandLineId () {
592 return this.landLineId;
596 public void setLandLineId (final Long landLineId) {
597 this.landLineId = landLineId;
601 public Integer getPhoneAreaCode () {
602 return this.phoneAreaCode;
606 public void setPhoneAreaCode (final Integer phoneAreaCode) {
607 this.phoneAreaCode = phoneAreaCode;
611 public Country getPhoneCountry () {
612 return this.phoneCountry;
616 public void setPhoneCountry (final Country phoneCountry) {
617 this.phoneCountry = phoneCountry;
621 public Long getPhoneNumber () {
622 return this.phoneNumber;
626 public void setPhoneNumber (final Long phoneNumber) {
627 this.phoneNumber = phoneNumber;
631 public String getStreet () {
636 public void setStreet (final String street) {
637 this.street = street;
641 public Integer getZipCode () {
646 public void setZipCode (final Integer zipCode) {
647 this.zipCode = zipCode;
651 * Post-initialization of this class
654 public void init () {
658 * Updates all data in contact instance.
660 * @param contact Contact instance
662 private void updateContactData (final Contact contact) {
663 // Contact instance should be valid
664 if (null == contact) {
666 throw new NullPointerException("contact is null"); //NOI18N
667 } else if (contact.getContactId() == null) {
669 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
670 } else if (contact.getContactId() < 1) {
672 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
676 contact.setContactGender(this.getGender());
677 contact.setContactFirstName(this.getFirstName());
678 contact.setContactFamilyName(this.getFamilyName());
679 contact.setContactStreet(this.getStreet());
680 contact.setContactHouseNumber(this.getHouseNumber());
681 contact.setContactZipCode(this.getZipCode());
682 contact.setContactCity(this.getCity());
683 contact.setContactCountry(this.getCountry());
685 // Update contact's cellphone number
686 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
688 // Update contact's land-line number
689 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
691 // Update contact's fax number
692 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());