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.addressbook.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.addressbook.beans.BaseAddressbookController;
33 import org.mxchange.addressbook.beans.helper.AddressbookAdminWebRequestController;
34 import org.mxchange.jcontacts.contact.Contact;
35 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
36 import org.mxchange.jcontacts.contact.UserContact;
37 import org.mxchange.jcontacts.contact.gender.Gender;
38 import org.mxchange.jcontacts.contact.utils.ContactUtils;
39 import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent;
40 import org.mxchange.jcontacts.events.contact.add.AdminContactAddedEvent;
41 import org.mxchange.jcontacts.events.contact.update.AdminContactUpdatedEvent;
42 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
43 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
44 import org.mxchange.jcountry.data.Country;
45 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
46 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
47 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
48 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
49 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
50 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
51 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
54 * An administrative user bean (controller)
56 * @author Roland Haeder<roland@mxchange.org>
58 @Named ("adminContactController")
60 public class AddressbookAdminContactWebRequestBean extends BaseAddressbookController implements AddressbookAdminContactWebRequestController {
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 AddressbookAdminWebRequestController 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 AddressbookContactWebSessionController 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 AddressbookAdminContactWebRequestBean () {
234 // Get initial context
235 Context context = new InitialContext();
238 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/addressbook-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
239 } catch (final NamingException e) {
241 throw new FaceletException(e);
246 public String addContact () {
247 // Are all minimum fields set?
248 if (this.getGender() == null) {
250 throw new NullPointerException("gender is null"); //NOI18N
251 } else if (this.getFirstName() == null) {
253 throw new NullPointerException("firstName is null"); //NOI18N
254 } else if (this.getFirstName().isEmpty()) {
256 throw new IllegalStateException("firstName is empty"); //NOI18N
257 } else if (this.getFamilyName() == null) {
259 throw new NullPointerException("familyName is null"); //NOI18N
260 } else if (this.getFamilyName().isEmpty()) {
262 throw new IllegalStateException("familyName is empty"); //NOI18N
265 // Create new contact instance
266 Contact contact = this.createContactInstance();
268 // Default is not same contact
269 if (this.isSameContactFound(contact)) {
270 // Already registered
271 throw new FaceletException(new ContactAlreadyAddedException(contact));
275 Contact updatedContact;
280 updatedContact = this.contactBean.addContact(contact);
281 } catch (final ContactAlreadyAddedException ex) {
283 throw new FaceletException(ex);
287 this.addedContactEvent.fire(new AdminContactAddedEvent(updatedContact));
293 return "admin_list_contact"; //NOI18N
297 public void copyContactToController (final Contact contact) {
298 // The contact instance must be valid
299 if (null == contact) {
301 throw new NullPointerException("contact is null"); //NOI18N
302 } else if (contact.getContactId() == null) {
304 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
305 } else if (contact.getContactId() < 1) {
307 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
310 // Set all fields: contact
311 this.setContactId(contact.getContactId());
312 this.setBirthday(contact.getContactBirthday());
313 this.setCity(contact.getContactCity());
314 this.setComment(contact.getContactComment());
315 this.setCountry(contact.getContactCountry());
316 this.setEmailAddress(contact.getContactEmailAddress());
317 this.setFamilyName(contact.getContactFamilyName());
318 this.setFirstName(contact.getContactFirstName());
319 this.setGender(contact.getContactGender());
320 this.setHouseNumber(contact.getContactHouseNumber());
321 this.setStreet(contact.getContactStreet());
322 this.setZipCode(contact.getContactZipCode());
324 // ... cellphone data
325 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
326 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
327 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
330 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
331 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
332 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
333 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
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 public Contact createContactInstance () {
344 // Generate phone number
345 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
346 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
347 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
349 // Create new instance
350 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
352 // Check if contact instance is in helper and valid
353 if (null == contact) {
355 throw new NullPointerException("adminHelper.contact is null"); //NOI18N
356 } else if (contact.getContactId() == null) {
358 throw new NullPointerException("adminHelper.contact.contactId is null"); //NOI18N //NOI18N
359 } else if (contact.getContactId() < 1) {
361 throw new IllegalStateException(MessageFormat.format("adminHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
364 // Update all data in contact
365 this.updateContactData(contact);
367 // Call EJB for updating contact data
368 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
371 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
381 public String editContactData () {
382 // Get contact instance
383 Contact contact = this.adminHelper.getContact();
385 // Check if contact instance is in helper and valid
386 if (null == contact) {
388 throw new NullPointerException("adminHelper.contact is null"); //NOI18N
389 } else if (contact.getContactId() == null) {
391 throw new NullPointerException("adminHelper.contact.contactId is null"); //NOI18N //NOI18N
392 } else if (contact.getContactId() < 1) {
394 throw new IllegalStateException(MessageFormat.format("adminHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
397 // Update all data in contact
398 this.updateContactData(contact);
400 // Call EJB for updating contact data
401 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
404 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
409 // Return to contact list (for now)
410 return "admin_list_contact"; //NOI18N
414 @SuppressWarnings ("ReturnOfDateField")
415 public Date getBirthday () {
416 return this.birthday;
420 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
421 public void setBirthday (final Date birthday) {
422 this.birthday = birthday;
426 public MobileProvider getCellphoneCarrier () {
427 return this.cellphoneCarrier;
431 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
432 this.cellphoneCarrier = cellphoneCarrier;
436 public Long getCellphoneId () {
437 return this.cellphoneId;
441 public void setCellphoneId (final Long cellphoneId) {
442 this.cellphoneId = cellphoneId;
446 public Long getCellphoneNumber () {
447 return this.cellphoneNumber;
451 public void setCellphoneNumber (Long cellphoneNumber) {
452 this.cellphoneNumber = cellphoneNumber;
456 public String getCity () {
461 public void setCity (final String city) {
466 public String getComment () {
471 public void setComment (final String comment) {
472 this.comment = comment;
476 public Long getContactId () {
477 return this.contactId;
481 public void setContactId (final Long contactId) {
482 this.contactId = contactId;
486 public Country getCountry () {
491 public void setCountry (final Country country) {
492 this.country = country;
496 public String getEmailAddress () {
497 return this.emailAddress;
501 public void setEmailAddress (final String emailAddress) {
502 this.emailAddress = emailAddress;
506 public String getFamilyName () {
507 return this.familyName;
511 public void setFamilyName (final String familyName) {
512 this.familyName = familyName;
516 public Integer getFaxAreaCode () {
517 return this.faxAreaCode;
521 public void setFaxAreaCode (final Integer faxAreaCode) {
522 this.faxAreaCode = faxAreaCode;
526 public Country getFaxCountry () {
527 return this.faxCountry;
531 public void setFaxCountry (final Country faxCountry) {
532 this.faxCountry = faxCountry;
536 public Long getFaxId () {
541 public void setFaxId (final Long faxId) {
546 public Long getFaxNumber () {
547 return this.faxNumber;
551 public void setFaxNumber (final Long faxNumber) {
552 this.faxNumber = faxNumber;
556 public String getFirstName () {
557 return this.firstName;
561 public void setFirstName (final String firstName) {
562 this.firstName = firstName;
566 public Gender getGender () {
571 public void setGender (final Gender gender) {
572 this.gender = gender;
576 public Short getHouseNumber () {
577 return this.houseNumber;
581 public void setHouseNumber (final Short houseNumber) {
582 this.houseNumber = houseNumber;
586 public Long getLandLineId () {
587 return this.landLineId;
591 public void setLandLineId (final Long landLineId) {
592 this.landLineId = landLineId;
596 public Integer getPhoneAreaCode () {
597 return this.phoneAreaCode;
601 public void setPhoneAreaCode (final Integer phoneAreaCode) {
602 this.phoneAreaCode = phoneAreaCode;
606 public Country getPhoneCountry () {
607 return this.phoneCountry;
611 public void setPhoneCountry (final Country phoneCountry) {
612 this.phoneCountry = phoneCountry;
616 public Long getPhoneNumber () {
617 return this.phoneNumber;
621 public void setPhoneNumber (final Long phoneNumber) {
622 this.phoneNumber = phoneNumber;
626 public String getStreet () {
631 public void setStreet (final String street) {
632 this.street = street;
636 public Integer getZipCode () {
641 public void setZipCode (final Integer zipCode) {
642 this.zipCode = zipCode;
646 * Post-initialization of this class
649 public void init () {
655 private void clear () {
658 this.setGender(Gender.UNKNOWN);
659 this.setFirstName(null);
660 this.setFamilyName(null);
661 this.setStreet(null);
662 this.setHouseNumber(null);
663 this.setZipCode(null);
665 this.setCountry(null);
668 this.setEmailAddress(null);
669 this.setPhoneCountry(null);
670 this.setPhoneAreaCode(null);
671 this.setPhoneNumber(null);
672 this.setCellphoneCarrier(null);
673 this.setCellphoneNumber(null);
674 this.setFaxCountry(null);
675 this.setFaxAreaCode(null);
676 this.setFaxNumber(null);
679 this.setBirthday(null);
680 this.setComment(null);
684 * Checks whether the given contact is found
686 * @param contact Contact inastance
688 * @return Wether contact has been found
690 private boolean isSameContactFound (final Contact contact) {
691 // Default is not found
692 boolean IsFound = false;
695 Iterator<Contact> iterator = this.contactController.allContacts().iterator();
698 while (iterator.hasNext()) {
700 Contact next = iterator.next();
703 if (ContactUtils.isSameContact(contact, next)) {
704 // Yes, then abort loop
715 * Updates all data in contact instance.
717 * @param contact Contact instance
719 private void updateContactData (final Contact contact) {
720 // Contact instance should be valid
721 if (null == contact) {
723 throw new NullPointerException("contact is null"); //NOI18N
724 } else if (contact.getContactId() == null) {
726 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
727 } else if (contact.getContactId() < 1) {
729 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
733 contact.setContactGender(this.getGender());
734 contact.setContactFirstName(this.getFirstName());
735 contact.setContactFamilyName(this.getFamilyName());
736 contact.setContactStreet(this.getStreet());
737 contact.setContactHouseNumber(this.getHouseNumber());
738 contact.setContactZipCode(this.getZipCode());
739 contact.setContactCity(this.getCity());
740 contact.setContactCountry(this.getCountry());
742 // Update contact's cellphone number
743 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
745 // Update contact's land-line number
746 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
748 // Update contact's fax number
749 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());