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 java.util.Iterator;
22 import javax.annotation.PostConstruct;
23 import javax.enterprise.context.RequestScoped;
24 import javax.enterprise.event.Event;
25 import javax.enterprise.inject.Any;
26 import javax.faces.view.facelets.FaceletException;
27 import javax.inject.Inject;
28 import javax.inject.Named;
29 import javax.naming.Context;
30 import javax.naming.InitialContext;
31 import javax.naming.NamingException;
32 import org.mxchange.jcontacts.contact.Contact;
33 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
34 import org.mxchange.jcontacts.contact.UserContact;
35 import org.mxchange.jcontacts.contact.gender.Gender;
36 import org.mxchange.jcontacts.contact.utils.ContactUtils;
37 import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent;
38 import org.mxchange.jcontacts.events.contact.add.AdminContactAddedEvent;
39 import org.mxchange.jcontacts.events.contact.update.AdminContactUpdatedEvent;
40 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
41 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
42 import org.mxchange.jcountry.data.Country;
43 import org.mxchange.jphone.phonenumbers.DialableNumber;
44 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
45 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
46 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
47 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
48 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
49 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
50 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
51 import org.mxchange.pizzaapplication.beans.BasePizzaController;
52 import org.mxchange.pizzaapplication.beans.helper.PizzaWebRequestController;
55 * Administrative user bean (controller)
57 * @author Roland Haeder<roland@mxchange.org>
59 @Named ("adminContactController")
61 public class PizzaAdminContactWebRequestBean extends BasePizzaController implements PizzaAdminContactWebRequestController {
66 private static final long serialVersionUID = 542_145_347_916L;
69 * An event fired when the administrator has added a new contact
73 private Event<AdminAddedContactEvent> addedContactEvent;
76 * Admin helper instance
79 private PizzaWebRequestController beanHelper;
84 private Date birthday;
87 * Cellphone number's carrier
89 private MobileProvider cellphoneCarrier;
94 private Long cellphoneId;
99 private Long cellphoneNumber;
109 private String comment;
112 * Remote contact bean
114 private final ContactSessionBeanRemote contactBean;
117 * General contact controller
120 private PizzaContactWebSessionController contactController;
125 private Long contactId;
130 private Country country;
135 private String emailAddress;
140 private String familyName;
143 * Fax number's area code
145 private Integer faxAreaCode;
148 * Country instance for fax number
150 private Country faxCountry;
160 private Long faxNumber;
165 private String firstName;
170 private Gender gender;
175 private Short houseNumber;
178 * Whether a cellphone entry has been unlinked
180 private boolean isCellphoneUnlinked;
183 * Whether a fax entry has been unlinked
185 private boolean isFaxUnlinked;
188 * Whether a land-line number has been unlinked
190 private boolean isLandLineUnlinked;
193 * Land-line id number
195 private Long landLineId;
198 * Phone number area code
200 private Integer phoneAreaCode;
203 * Country instance for phone number
205 private Country phoneCountry;
210 private Long phoneNumber;
215 private String street;
220 private String title;
223 * An event fired when the administrator has updated contact data
227 private Event<AdminUpdatedContactEvent> updatedContactEvent;
232 private Integer zipCode;
235 * Default constructor
237 public PizzaAdminContactWebRequestBean () {
240 // Get initial context
241 Context context = new InitialContext();
244 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
245 } catch (final NamingException e) {
247 throw new FaceletException(e);
252 public String addContact () {
253 // Create new contact instance
254 Contact contact = this.createContactInstance();
256 // Default is not same contact
257 if (this.isSameContactFound(contact)) {
258 // Already registered
259 throw new FaceletException(new ContactAlreadyAddedException(contact));
263 Contact updatedContact;
268 updatedContact = this.contactBean.addContact(contact);
269 } catch (final ContactAlreadyAddedException ex) {
271 throw new FaceletException(ex);
275 this.addedContactEvent.fire(new AdminContactAddedEvent(updatedContact));
281 return "admin_list_contact"; //NOI18N
285 public void copyContactToController (final Contact contact) {
287 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("AdminContactController::copyContactToController(): contact={0} - CALLED!", contact)); //NOI18N
289 // The contact instance must be valid
290 if (null == contact) {
292 throw new NullPointerException("contact is null"); //NOI18N
293 } else if (contact.getContactId() == null) {
295 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
296 } else if (contact.getContactId() < 1) {
298 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
301 // Set all fields: contact
302 this.setContactId(contact.getContactId());
303 this.setTitle(contact.getContactTitle());
304 this.setBirthday(contact.getContactBirthday());
305 this.setCity(contact.getContactCity());
306 this.setComment(contact.getContactComment());
307 this.setCountry(contact.getContactCountry());
308 this.setEmailAddress(contact.getContactEmailAddress());
309 this.setFamilyName(contact.getContactFamilyName());
310 this.setFirstName(contact.getContactFirstName());
311 this.setGender(contact.getContactGender());
312 this.setHouseNumber(contact.getContactHouseNumber());
313 this.setStreet(contact.getContactStreet());
314 this.setZipCode(contact.getContactZipCode());
316 // Is the cell phone set?
317 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
318 // ... cellphone data
319 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
320 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
321 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
325 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
327 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
328 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
329 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
330 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
333 // Is the land-line number set?
334 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
336 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
337 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
338 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
339 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
343 //* NOISY-DEBUG: */ System.out.println("AdminContactController::copyContactToController(): EXIT!"); //NOI18N
347 public Contact createContactInstance () {
348 // Are all minimum fields set?
349 if (this.getGender() == null) {
351 throw new NullPointerException("gender is null"); //NOI18N
352 } else if (this.getFirstName() == null) {
354 throw new NullPointerException("firstName is null"); //NOI18N
355 } else if (this.getFirstName().isEmpty()) {
357 throw new IllegalStateException("firstName is empty"); //NOI18N
358 } else if (this.getFamilyName() == null) {
360 throw new NullPointerException("familyName is null"); //NOI18N
361 } else if (this.getFamilyName().isEmpty()) {
363 throw new IllegalStateException("familyName is empty"); //NOI18N
366 // Generate phone number
367 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
368 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
369 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
371 // Create new instance
372 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
375 contact.setContactBirthday(this.getBirthday());
376 contact.setContactStreet(this.getStreet());
377 contact.setContactHouseNumber(this.getHouseNumber());
378 contact.setContactZipCode(this.getZipCode());
379 contact.setContactCity(this.getCity());
380 contact.setContactCountry(this.getCountry());
381 contact.setContactEmailAddress(this.getEmailAddress());
382 contact.setContactBirthday(this.getBirthday());
383 contact.setContactComment(this.getComment());
386 contact.setContactOwnContact(Boolean.TRUE);
388 // Don't set null or wrong references
389 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
390 // Now the number must be given
391 if (phone.getPhoneAreaCode() == null) {
393 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
394 } else if (phone.getPhoneAreaCode() < 1) {
396 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
397 } else if (phone.getPhoneNumber() == null) {
399 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
400 } else if (phone.getPhoneNumber() < 1) {
402 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
406 contact.setContactLandLineNumber(phone);
409 // Don't set null or wrong references
410 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
411 // Now the number must be given
412 if (fax.getPhoneAreaCode() == null) {
414 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
415 } else if (fax.getPhoneAreaCode() < 1) {
417 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
418 } else if (fax.getPhoneNumber() == null) {
420 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
421 } else if (fax.getPhoneNumber() < 1) {
423 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
427 contact.setContactFaxNumber(fax);
430 // Is the provider set?
431 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
432 // Is the number set?
433 if (cellphone.getPhoneNumber() == null) {
435 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
436 } else if (cellphone.getPhoneNumber() < 1) {
438 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
441 // Set cellphone number
442 contact.setContactCellphoneNumber(cellphone);
450 public String editContactData () {
451 // Get contact instance
452 Contact contact = this.beanHelper.getContact();
454 // Check if contact instance is in helper and valid
455 if (null == contact) {
457 throw new NullPointerException("beanHelper.contact is null"); //NOI18N
458 } else if (contact.getContactId() == null) {
460 throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
461 } else if (contact.getContactId() < 1) {
463 throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
466 // Update all data in contact
467 this.updateContactData(contact);
469 // Call EJB for updating contact data
470 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
473 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
478 // Return to contact list (for now)
479 return "admin_list_contact"; //NOI18N
483 public String generateCellphoneNumber (final DialableCellphoneNumber cellphoneNumber) {
485 if (null == cellphoneNumber) {
491 String number = String.format(
493 cellphoneNumber.getCellphoneProvider().getProviderCountry().getCountryExternalDialPrefix(),
494 cellphoneNumber.getCellphoneProvider().getProviderDialPrefix(),
495 cellphoneNumber.getPhoneNumber()
503 public String generatePhoneNumber (final DialableNumber phoneNumber) {
505 if (null == phoneNumber) {
511 String number = String.format(
513 phoneNumber.getPhoneCountry().getCountryExternalDialPrefix(),
514 phoneNumber.getPhoneAreaCode(),
515 phoneNumber.getPhoneNumber()
523 @SuppressWarnings ("ReturnOfDateField")
524 public Date getBirthday () {
525 return this.birthday;
529 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
530 public void setBirthday (final Date birthday) {
531 this.birthday = birthday;
535 public MobileProvider getCellphoneCarrier () {
536 return this.cellphoneCarrier;
540 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
541 this.cellphoneCarrier = cellphoneCarrier;
545 public Long getCellphoneId () {
546 return this.cellphoneId;
550 public void setCellphoneId (final Long cellphoneId) {
551 this.cellphoneId = cellphoneId;
555 public Long getCellphoneNumber () {
556 return this.cellphoneNumber;
560 public void setCellphoneNumber (Long cellphoneNumber) {
561 this.cellphoneNumber = cellphoneNumber;
565 public String getCity () {
570 public void setCity (final String city) {
575 public String getComment () {
580 public void setComment (final String comment) {
581 this.comment = comment;
585 public Long getContactId () {
586 return this.contactId;
590 public void setContactId (final Long contactId) {
591 this.contactId = contactId;
595 public Country getCountry () {
600 public void setCountry (final Country country) {
601 this.country = country;
605 public String getEmailAddress () {
606 return this.emailAddress;
610 public void setEmailAddress (final String emailAddress) {
611 this.emailAddress = emailAddress;
615 public String getFamilyName () {
616 return this.familyName;
620 public void setFamilyName (final String familyName) {
621 this.familyName = familyName;
625 public Integer getFaxAreaCode () {
626 return this.faxAreaCode;
630 public void setFaxAreaCode (final Integer faxAreaCode) {
631 this.faxAreaCode = faxAreaCode;
635 public Country getFaxCountry () {
636 return this.faxCountry;
640 public void setFaxCountry (final Country faxCountry) {
641 this.faxCountry = faxCountry;
645 public Long getFaxId () {
650 public void setFaxId (final Long faxId) {
655 public Long getFaxNumber () {
656 return this.faxNumber;
660 public void setFaxNumber (final Long faxNumber) {
661 this.faxNumber = faxNumber;
665 public String getFirstName () {
666 return this.firstName;
670 public void setFirstName (final String firstName) {
671 this.firstName = firstName;
675 public Gender getGender () {
680 public void setGender (final Gender gender) {
681 this.gender = gender;
685 public Short getHouseNumber () {
686 return this.houseNumber;
690 public void setHouseNumber (final Short houseNumber) {
691 this.houseNumber = houseNumber;
695 public Long getLandLineId () {
696 return this.landLineId;
700 public void setLandLineId (final Long landLineId) {
701 this.landLineId = landLineId;
705 public Integer getPhoneAreaCode () {
706 return this.phoneAreaCode;
710 public void setPhoneAreaCode (final Integer phoneAreaCode) {
711 this.phoneAreaCode = phoneAreaCode;
715 public Country getPhoneCountry () {
716 return this.phoneCountry;
720 public void setPhoneCountry (final Country phoneCountry) {
721 this.phoneCountry = phoneCountry;
725 public Long getPhoneNumber () {
726 return this.phoneNumber;
730 public void setPhoneNumber (final Long phoneNumber) {
731 this.phoneNumber = phoneNumber;
735 public String getStreet () {
740 public void setStreet (final String street) {
741 this.street = street;
745 public String getTitle () {
750 public void setTitle (final String title) {
755 public Integer getZipCode () {
760 public void setZipCode (final Integer zipCode) {
761 this.zipCode = zipCode;
765 * Post-initialization of this class
768 public void init () {
774 private void clear () {
777 this.setGender(Gender.UNKNOWN);
779 this.setFirstName(null);
780 this.setFamilyName(null);
781 this.setStreet(null);
782 this.setHouseNumber(null);
783 this.setZipCode(null);
785 this.setCountry(null);
788 this.setEmailAddress(null);
789 this.setPhoneCountry(null);
790 this.setPhoneAreaCode(null);
791 this.setPhoneNumber(null);
792 this.setCellphoneCarrier(null);
793 this.setCellphoneNumber(null);
794 this.setFaxCountry(null);
795 this.setFaxAreaCode(null);
796 this.setFaxNumber(null);
799 this.setBirthday(null);
800 this.setComment(null);
804 * Checks whether the given contact is found
806 * @param contact Contact inastance
808 * @return Wether contact has been found
810 private boolean isSameContactFound (final Contact contact) {
811 // Default is not found
812 boolean IsFound = false;
815 Iterator<Contact> iterator = this.contactController.allContacts().iterator();
818 while (iterator.hasNext()) {
820 Contact next = iterator.next();
823 if (ContactUtils.isSameContact(contact, next)) {
824 // Yes, then abort loop
835 * Updates all data in contact instance.
837 * @param contact Contact instance
839 private void updateContactData (final Contact contact) {
840 // Contact instance should be valid
841 if (null == contact) {
843 throw new NullPointerException("contact is null"); //NOI18N
844 } else if (contact.getContactId() == null) {
846 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
847 } else if (contact.getContactId() < 1) {
849 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
853 contact.setContactGender(this.getGender());
854 contact.setContactTitle(this.getTitle());
855 contact.setContactFirstName(this.getFirstName());
856 contact.setContactFamilyName(this.getFamilyName());
857 contact.setContactStreet(this.getStreet());
858 contact.setContactHouseNumber(this.getHouseNumber());
859 contact.setContactZipCode(this.getZipCode());
860 contact.setContactCity(this.getCity());
861 contact.setContactCountry(this.getCountry());
863 // Update contact's cellphone number
864 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
866 // Update contact's land-line number
867 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
869 // Update contact's fax number
870 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());