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 java.util.List;
23 import javax.annotation.PostConstruct;
24 import javax.enterprise.context.RequestScoped;
25 import javax.enterprise.event.Event;
26 import javax.enterprise.inject.Any;
27 import javax.faces.view.facelets.FaceletException;
28 import javax.inject.Inject;
29 import javax.inject.Named;
30 import javax.naming.Context;
31 import javax.naming.InitialContext;
32 import javax.naming.NamingException;
33 import org.mxchange.jcontacts.contact.Contact;
34 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
35 import org.mxchange.jcontacts.contact.UserContact;
36 import org.mxchange.jcontacts.contact.gender.Gender;
37 import org.mxchange.jcontacts.contact.utils.ContactUtils;
38 import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent;
39 import org.mxchange.jcontacts.events.contact.add.AdminContactAddedEvent;
40 import org.mxchange.jcontacts.events.contact.update.AdminContactUpdatedEvent;
41 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
42 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
43 import org.mxchange.jcountry.data.Country;
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.helper.PizzaAdminWebRequestController;
54 * Administrative user bean (controller)
56 * @author Roland Haeder<roland@mxchange.org>
58 @Named ("adminContactController")
60 public class PizzaAdminContactWebRequestBean implements PizzaAdminContactWebRequestController {
65 private static final long serialVersionUID = 542_145_347_916L;
68 * An event fired when the administrator has added a new contact
72 private Event<AdminAddedContactEvent> addedContactEvent;
75 * Admin helper instance
78 private PizzaAdminWebRequestController adminHelper;
83 private Date birthday;
86 * Cellphone number's carrier
88 private MobileProvider cellphoneCarrier;
93 private Long cellphoneId;
98 private Long cellphoneNumber;
108 private String comment;
111 * Remote contact bean
113 private final ContactSessionBeanRemote contactBean;
116 * General contact controller
119 private PizzaContactWebSessionController contactController;
124 private Long contactId;
129 private Country country;
134 private String emailAddress;
139 private String familyName;
142 * Fax number's area code
144 private Integer faxAreaCode;
147 * Country instance for fax number
149 private Country faxCountry;
159 private Long faxNumber;
164 private String firstName;
169 private Gender gender;
174 private Short houseNumber;
177 * Whether a cellphone entry has been unlinked
179 private boolean isCellphoneUnlinked;
182 * Whether a fax entry has been unlinked
184 private boolean isFaxUnlinked;
187 * Whether a land-line number has been unlinked
189 private boolean isLandLineUnlinked;
192 * Land-line id number
194 private Long landLineId;
197 * Phone number area code
199 private Integer phoneAreaCode;
202 * Country instance for phone number
204 private Country phoneCountry;
209 private Long phoneNumber;
214 private String street;
217 * An event fired when the administrator has updated contact data
221 private Event<AdminUpdatedContactEvent> updatedContactEvent;
226 private Integer zipCode;
229 * Default constructor
231 public PizzaAdminContactWebRequestBean () {
232 // Set gender to UNKNOWN
233 this.gender = Gender.UNKNOWN;
237 // Get initial context
238 Context context = new InitialContext();
241 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
242 } catch (final NamingException e) {
244 throw new FaceletException(e);
249 public String addContact () {
250 // Create new contact instance
251 Contact contact = this.createContactInstance();
253 // Default is not same contact
254 if (this.isSameContactFound(contact)) {
255 // Already registered
256 throw new FaceletException(new ContactAlreadyAddedException(contact));
260 Contact updatedContact;
265 updatedContact = this.contactBean.addContact(contact);
266 } catch (final ContactAlreadyAddedException ex) {
268 throw new FaceletException(ex);
272 this.addedContactEvent.fire(new AdminContactAddedEvent(updatedContact));
278 return "admin_list_contact"; //NOI18N
282 public List<Contact> allContacts () {
283 return this.contactController.allContacts();
287 public void copyContactToController (final Contact contact) {
289 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("AdminContactController::copyContactToController(): contact={0} - CALLED!", contact)); //NOI18N
291 // The contact instance must be valid
292 if (null == contact) {
294 throw new NullPointerException("contact is null"); //NOI18N
295 } else if (contact.getContactId() == null) {
297 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
298 } else if (contact.getContactId() < 1) {
300 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
303 // Set all fields: contact
304 this.setContactId(contact.getContactId());
305 this.setBirthday(contact.getContactBirthday());
306 this.setCity(contact.getContactCity());
307 this.setComment(contact.getContactComment());
308 this.setCountry(contact.getContactCountry());
309 this.setEmailAddress(contact.getContactEmailAddress());
310 this.setFamilyName(contact.getContactFamilyName());
311 this.setFirstName(contact.getContactFirstName());
312 this.setGender(contact.getContactGender());
313 this.setHouseNumber(contact.getContactHouseNumber());
314 this.setStreet(contact.getContactStreet());
315 this.setZipCode(contact.getContactZipCode());
317 // Is the cell phone set?
318 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
319 // ... cellphone data
320 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
321 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
322 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
326 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
328 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
329 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
330 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
331 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
334 // Is the land-line number set?
335 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
337 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
338 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
339 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
340 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
344 //* NOISY-DEBUG: */ System.out.println("AdminContactController::copyContactToController(): EXIT!"); //NOI18N
348 public Contact createContactInstance () {
349 // Are all minimum fields set?
350 if (this.getGender() == null) {
352 throw new NullPointerException("gender is null"); //NOI18N
353 } else if (this.getFirstName() == null) {
355 throw new NullPointerException("firstName is null"); //NOI18N
356 } else if (this.getFirstName().isEmpty()) {
358 throw new IllegalStateException("firstName is empty"); //NOI18N
359 } else if (this.getFamilyName() == null) {
361 throw new NullPointerException("familyName is null"); //NOI18N
362 } else if (this.getFamilyName().isEmpty()) {
364 throw new IllegalStateException("familyName is empty"); //NOI18N
367 // Generate phone number
368 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
369 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
370 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
372 // Create new instance
373 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
376 contact.setContactBirthday(this.getBirthday());
377 contact.setContactStreet(this.getStreet());
378 contact.setContactHouseNumber(this.getHouseNumber());
379 contact.setContactZipCode(this.getZipCode());
380 contact.setContactCity(this.getCity());
381 contact.setContactCountry(this.getCountry());
382 contact.setContactEmailAddress(this.getEmailAddress());
383 contact.setContactBirthday(this.getBirthday());
384 contact.setContactComment(this.getComment());
387 contact.setContactOwnContact(Boolean.TRUE);
389 // Don't set null or wrong references
390 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
391 // Now the number must be given
392 if (phone.getPhoneAreaCode() == null) {
394 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
395 } else if (phone.getPhoneAreaCode() < 1) {
397 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
398 } else if (phone.getPhoneNumber() == null) {
400 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
401 } else if (phone.getPhoneNumber() < 1) {
403 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
407 contact.setContactLandLineNumber(phone);
410 // Don't set null or wrong references
411 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
412 // Now the number must be given
413 if (fax.getPhoneAreaCode() == null) {
415 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
416 } else if (fax.getPhoneAreaCode() < 1) {
418 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
419 } else if (fax.getPhoneNumber() == null) {
421 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
422 } else if (fax.getPhoneNumber() < 1) {
424 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
428 contact.setContactFaxNumber(fax);
431 // Is the provider set?
432 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
433 // Is the number set?
434 if (cellphone.getPhoneNumber() == null) {
436 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
437 } else if (cellphone.getPhoneNumber() < 1) {
439 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
442 // Set cellphone number
443 contact.setContactCellphoneNumber(cellphone);
451 public String editContactData () {
452 // Get contact instance
453 Contact contact = this.adminHelper.getContact();
455 // Check if contact instance is in helper and valid
456 if (null == contact) {
458 throw new NullPointerException("adminHelper.contact is null"); //NOI18N
459 } else if (contact.getContactId() == null) {
461 throw new NullPointerException("adminHelper.contact.contactId is null"); //NOI18N //NOI18N
462 } else if (contact.getContactId() < 1) {
464 throw new IllegalStateException(MessageFormat.format("adminHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
467 // Update all data in contact
468 this.updateContactData(contact);
470 // Call EJB for updating contact data
471 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
474 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
479 // Return to contact list (for now)
480 return "admin_list_contact"; //NOI18N
484 @SuppressWarnings ("ReturnOfDateField")
485 public Date getBirthday () {
486 return this.birthday;
490 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
491 public void setBirthday (final Date birthday) {
492 this.birthday = birthday;
496 public MobileProvider getCellphoneCarrier () {
497 return this.cellphoneCarrier;
501 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
502 this.cellphoneCarrier = cellphoneCarrier;
506 public Long getCellphoneId () {
507 return this.cellphoneId;
511 public void setCellphoneId (final Long cellphoneId) {
512 this.cellphoneId = cellphoneId;
516 public Long getCellphoneNumber () {
517 return this.cellphoneNumber;
521 public void setCellphoneNumber (Long cellphoneNumber) {
522 this.cellphoneNumber = cellphoneNumber;
526 public String getCity () {
531 public void setCity (final String city) {
536 public String getComment () {
541 public void setComment (final String comment) {
542 this.comment = comment;
546 public Long getContactId () {
547 return this.contactId;
551 public void setContactId (final Long contactId) {
552 this.contactId = contactId;
556 public Country getCountry () {
561 public void setCountry (final Country country) {
562 this.country = country;
566 public String getEmailAddress () {
567 return this.emailAddress;
571 public void setEmailAddress (final String emailAddress) {
572 this.emailAddress = emailAddress;
576 public String getFamilyName () {
577 return this.familyName;
581 public void setFamilyName (final String familyName) {
582 this.familyName = familyName;
586 public Integer getFaxAreaCode () {
587 return this.faxAreaCode;
591 public void setFaxAreaCode (final Integer faxAreaCode) {
592 this.faxAreaCode = faxAreaCode;
596 public Country getFaxCountry () {
597 return this.faxCountry;
601 public void setFaxCountry (final Country faxCountry) {
602 this.faxCountry = faxCountry;
606 public Long getFaxId () {
611 public void setFaxId (final Long faxId) {
616 public Long getFaxNumber () {
617 return this.faxNumber;
621 public void setFaxNumber (final Long faxNumber) {
622 this.faxNumber = faxNumber;
626 public String getFirstName () {
627 return this.firstName;
631 public void setFirstName (final String firstName) {
632 this.firstName = firstName;
636 public Gender getGender () {
641 public void setGender (final Gender gender) {
642 this.gender = gender;
646 public Short getHouseNumber () {
647 return this.houseNumber;
651 public void setHouseNumber (final Short houseNumber) {
652 this.houseNumber = houseNumber;
656 public Long getLandLineId () {
657 return this.landLineId;
661 public void setLandLineId (final Long landLineId) {
662 this.landLineId = landLineId;
666 public Integer getPhoneAreaCode () {
667 return this.phoneAreaCode;
671 public void setPhoneAreaCode (final Integer phoneAreaCode) {
672 this.phoneAreaCode = phoneAreaCode;
676 public Country getPhoneCountry () {
677 return this.phoneCountry;
681 public void setPhoneCountry (final Country phoneCountry) {
682 this.phoneCountry = phoneCountry;
686 public Long getPhoneNumber () {
687 return this.phoneNumber;
691 public void setPhoneNumber (final Long phoneNumber) {
692 this.phoneNumber = phoneNumber;
696 public String getStreet () {
701 public void setStreet (final String street) {
702 this.street = street;
706 public Integer getZipCode () {
711 public void setZipCode (final Integer zipCode) {
712 this.zipCode = zipCode;
716 public boolean hasContacts () {
717 return (!this.allContacts().isEmpty());
721 * Post-initialization of this class
724 public void init () {
730 private void clear () {
733 this.setGender(Gender.UNKNOWN);
734 this.setFirstName(null);
735 this.setFamilyName(null);
736 this.setStreet(null);
737 this.setHouseNumber(null);
738 this.setZipCode(null);
740 this.setCountry(null);
743 this.setEmailAddress(null);
744 this.setPhoneCountry(null);
745 this.setPhoneAreaCode(null);
746 this.setPhoneNumber(null);
747 this.setCellphoneCarrier(null);
748 this.setCellphoneNumber(null);
749 this.setFaxCountry(null);
750 this.setFaxAreaCode(null);
751 this.setFaxNumber(null);
754 this.setBirthday(null);
755 this.setComment(null);
759 * Checks whether the given contact is found
761 * @param contact Contact inastance
763 * @return Wether contact has been found
765 private boolean isSameContactFound (final Contact contact) {
766 // Default is not found
767 boolean IsFound = false;
770 Iterator<Contact> iterator = this.allContacts().iterator();
773 while (iterator.hasNext()) {
775 Contact next = iterator.next();
778 if (ContactUtils.isSameContact(contact, next)) {
779 // Yes, then abort loop
790 * Updates all data in contact instance.
792 * @param contact Contact instance
794 private void updateContactData (final Contact contact) {
795 // Contact instance should be valid
796 if (null == contact) {
798 throw new NullPointerException("contact is null"); //NOI18N
799 } else if (contact.getContactId() == null) {
801 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
802 } else if (contact.getContactId() < 1) {
804 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
808 contact.setContactGender(this.getGender());
809 contact.setContactFirstName(this.getFirstName());
810 contact.setContactFamilyName(this.getFamilyName());
811 contact.setContactStreet(this.getStreet());
812 contact.setContactHouseNumber(this.getHouseNumber());
813 contact.setContactZipCode(this.getZipCode());
814 contact.setContactCity(this.getCity());
815 contact.setContactCountry(this.getCountry());
817 // Update contact's cellphone number
818 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
820 // Update contact's land-line number
821 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
823 // Update contact's fax number
824 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());