2 * Copyright (C) 2016 Cho-Time GmbH
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 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.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 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 () {
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/jratecalc-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
242 } catch (final NamingException e) {
244 throw new FaceletException(e);
249 public String addContact () {
250 // Are all minimum fields set?
251 if (this.getGender() == null) {
253 throw new NullPointerException("gender is null"); //NOI18N
254 } else if (this.getFirstName() == null) {
256 throw new NullPointerException("firstName is null"); //NOI18N
257 } else if (this.getFirstName().isEmpty()) {
259 throw new IllegalStateException("firstName is empty"); //NOI18N
260 } else if (this.getFamilyName() == null) {
262 throw new NullPointerException("familyName is null"); //NOI18N
263 } else if (this.getFamilyName().isEmpty()) {
265 throw new IllegalStateException("familyName is empty"); //NOI18N
268 // Create new contact instance
269 Contact contact = this.createContactInstance();
271 // Default is not same contact
272 if (this.isSameContactFound(contact)) {
273 // Already registered
274 throw new FaceletException(new ContactAlreadyAddedException(contact));
278 Contact updatedContact;
283 updatedContact = this.contactBean.addContact(contact);
284 } catch (final ContactAlreadyAddedException ex) {
286 throw new FaceletException(ex);
290 this.addedContactEvent.fire(new AdminContactAddedEvent(updatedContact));
293 return "admin_list_contact"; //NOI18N
297 public List<Contact> allContacts () {
298 return this.contactController.allContacts();
302 public void copyContactToController (final Contact contact) {
303 // The contact instance must be valid
304 if (null == contact) {
306 throw new NullPointerException("contact is null"); //NOI18N
307 } else if (contact.getContactId() == null) {
309 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
310 } else if (contact.getContactId() < 1) {
312 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
315 // Set all fields: contact
316 this.setContactId(contact.getContactId());
317 this.setBirthday(contact.getContactBirthday());
318 this.setCity(contact.getContactCity());
319 this.setComment(contact.getContactComment());
320 this.setCountry(contact.getContactCountry());
321 this.setEmailAddress(contact.getContactEmailAddress());
322 this.setFamilyName(contact.getContactFamilyName());
323 this.setFirstName(contact.getContactFirstName());
324 this.setGender(contact.getContactGender());
325 this.setHouseNumber(contact.getContactHouseNumber());
326 this.setStreet(contact.getContactStreet());
327 this.setZipCode(contact.getContactZipCode());
329 // ... cellphone data
330 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
331 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
332 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
335 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
336 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
337 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
338 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
341 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
342 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
343 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
344 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
348 public String editContactData () {
349 // Get contact instance
350 Contact contact = this.adminHelper.getContact();
352 // Is the land-line number set?
353 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
355 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
356 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
357 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
358 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
362 //* NOISY-DEBUG: */ System.out.println("AdminContactController::copyContactToController(): EXIT!"); //NOI18N
366 public Contact createContactInstance () {
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());
375 // Check if contact instance is in helper and valid
376 if (null == contact) {
378 throw new NullPointerException("adminHelper.contact is null"); //NOI18N
379 } else if (contact.getContactId() == null) {
381 throw new NullPointerException("adminHelper.contact.contactId is null"); //NOI18N //NOI18N
382 } else if (contact.getContactId() < 1) {
384 throw new IllegalStateException(MessageFormat.format("adminHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
387 // Update all data in contact
388 this.updateContactData(contact);
390 // Call EJB for updating contact data
391 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
394 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
404 @SuppressWarnings ("ReturnOfDateField")
405 public Date getBirthday () {
406 return this.birthday;
410 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
411 public void setBirthday (final Date birthday) {
412 this.birthday = birthday;
416 public MobileProvider getCellphoneCarrier () {
417 return this.cellphoneCarrier;
421 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
422 this.cellphoneCarrier = cellphoneCarrier;
426 public Long getCellphoneId () {
427 return this.cellphoneId;
431 public void setCellphoneId (final Long cellphoneId) {
432 this.cellphoneId = cellphoneId;
436 public Long getCellphoneNumber () {
437 return this.cellphoneNumber;
441 public void setCellphoneNumber (Long cellphoneNumber) {
442 this.cellphoneNumber = cellphoneNumber;
446 public String getCity () {
451 public void setCity (final String city) {
456 public String getComment () {
461 public void setComment (final String comment) {
462 this.comment = comment;
466 public Long getContactId () {
467 return this.contactId;
471 public void setContactId (final Long contactId) {
472 this.contactId = contactId;
476 public Country getCountry () {
481 public void setCountry (final Country country) {
482 this.country = country;
486 public String getEmailAddress () {
487 return this.emailAddress;
491 public void setEmailAddress (final String emailAddress) {
492 this.emailAddress = emailAddress;
496 public String getFamilyName () {
497 return this.familyName;
501 public void setFamilyName (final String familyName) {
502 this.familyName = familyName;
506 public Integer getFaxAreaCode () {
507 return this.faxAreaCode;
511 public void setFaxAreaCode (final Integer faxAreaCode) {
512 this.faxAreaCode = faxAreaCode;
516 public Country getFaxCountry () {
517 return this.faxCountry;
521 public void setFaxCountry (final Country faxCountry) {
522 this.faxCountry = faxCountry;
526 public Long getFaxId () {
531 public void setFaxId (final Long faxId) {
536 public Long getFaxNumber () {
537 return this.faxNumber;
541 public void setFaxNumber (final Long faxNumber) {
542 this.faxNumber = faxNumber;
546 public String getFirstName () {
547 return this.firstName;
551 public void setFirstName (final String firstName) {
552 this.firstName = firstName;
556 public Gender getGender () {
561 public void setGender (final Gender gender) {
562 this.gender = gender;
566 public Short getHouseNumber () {
567 return this.houseNumber;
571 public void setHouseNumber (final Short houseNumber) {
572 this.houseNumber = houseNumber;
576 public Long getLandLineId () {
577 return this.landLineId;
581 public void setLandLineId (final Long landLineId) {
582 this.landLineId = landLineId;
586 public Integer getPhoneAreaCode () {
587 return this.phoneAreaCode;
591 public void setPhoneAreaCode (final Integer phoneAreaCode) {
592 this.phoneAreaCode = phoneAreaCode;
596 public Country getPhoneCountry () {
597 return this.phoneCountry;
601 public void setPhoneCountry (final Country phoneCountry) {
602 this.phoneCountry = phoneCountry;
606 public Long getPhoneNumber () {
607 return this.phoneNumber;
611 public void setPhoneNumber (final Long phoneNumber) {
612 this.phoneNumber = phoneNumber;
616 public String getStreet () {
621 public void setStreet (final String street) {
622 this.street = street;
626 public Integer getZipCode () {
631 public void setZipCode (final Integer zipCode) {
632 this.zipCode = zipCode;
636 public boolean hasContacts () {
637 return (!this.allContacts().isEmpty());
641 * Post-initialization of this class
644 public void init () {
650 private void clear () {
653 this.setGender(Gender.UNKNOWN);
654 this.setFirstName(null);
655 this.setFamilyName(null);
656 this.setStreet(null);
657 this.setHouseNumber(null);
658 this.setZipCode(null);
660 this.setCountry(null);
663 this.setEmailAddress(null);
664 this.setPhoneCountry(null);
665 this.setPhoneAreaCode(null);
666 this.setPhoneNumber(null);
667 this.setCellphoneCarrier(null);
668 this.setCellphoneNumber(null);
669 this.setFaxCountry(null);
670 this.setFaxAreaCode(null);
671 this.setFaxNumber(null);
674 this.setBirthday(null);
675 this.setComment(null);
679 * Checks whether the given contact is found
681 * @param contact Contact inastance
683 * @return Wether contact has been found
685 private boolean isSameContactFound (final Contact contact) {
686 // Default is not found
687 boolean IsFound = false;
690 Iterator<Contact> iterator = this.allContacts().iterator();
693 while (iterator.hasNext()) {
695 Contact next = iterator.next();
698 if (ContactUtils.isSameContact(contact, next)) {
699 // Yes, then abort loop
710 * Updates all data in contact instance.
712 * @param contact Contact instance
714 private void updateContactData (final Contact contact) {
715 // Contact instance should be valid
716 if (null == contact) {
718 throw new NullPointerException("contact is null"); //NOI18N
719 } else if (contact.getContactId() == null) {
721 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
722 } else if (contact.getContactId() < 1) {
724 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
728 contact.setContactGender(this.getGender());
729 contact.setContactFirstName(this.getFirstName());
730 contact.setContactFamilyName(this.getFamilyName());
731 contact.setContactStreet(this.getStreet());
732 contact.setContactHouseNumber(this.getHouseNumber());
733 contact.setContactZipCode(this.getZipCode());
734 contact.setContactCity(this.getCity());
735 contact.setContactCountry(this.getCountry());
737 // Update contact's cellphone number
738 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
740 // Update contact's land-line number
741 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
743 // Update contact's fax number
744 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());