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.gender.Gender;
34 import org.mxchange.jcontacts.contact.utils.ContactUtils;
35 import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent;
36 import org.mxchange.jcontacts.events.contact.update.AdminContactUpdatedEvent;
37 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
38 import org.mxchange.jcountry.data.Country;
39 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
40 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
41 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
42 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
43 import org.mxchange.pizzaapplication.beans.helper.PizzaAdminWebRequestController;
46 * An administrative user bean (controller)
48 * @author Roland Haeder<roland@mxchange.org>
50 @Named ("adminContactController")
52 public class PizzaAdminContactWebRequestBean implements PizzaAdminContactWebRequestController {
57 private static final long serialVersionUID = 542_145_347_916L;
60 * An event fired when the administrator has added a new contact
64 private Event<AdminAddedContactEvent> addedContactEvent;
67 * Admin helper instance
70 private PizzaAdminWebRequestController adminHelper;
75 private Date birthday;
78 * Cellphone number's carrier
80 private MobileProvider cellphoneCarrier;
85 private Long cellphoneId;
90 private Long cellphoneNumber;
100 private String comment;
103 * Remote contact bean
105 private final ContactSessionBeanRemote contactBean;
110 private Long contactId;
115 private Country country;
120 private String emailAddress;
125 private String familyName;
128 * Fax number's area code
130 private Integer faxAreaCode;
133 * Country instance for fax number
135 private Country faxCountry;
145 private Long faxNumber;
150 private String firstName;
155 private Gender gender;
160 private Short houseNumber;
163 * Whether a cellphone entry has been unlinked
165 private boolean isCellphoneUnlinked;
168 * Whether a fax entry has been unlinked
170 private boolean isFaxUnlinked;
173 * Whether a land-line number has been unlinked
175 private boolean isLandLineUnlinked;
178 * Land-line id number
180 private Long landLineId;
183 * Phone number area code
185 private Integer phoneAreaCode;
188 * Country instance for phone number
190 private Country phoneCountry;
195 private Long phoneNumber;
200 private String street;
203 * An event fired when the administrator has updated contact data
207 private Event<AdminUpdatedContactEvent> updatedContactEvent;
212 private Integer zipCode;
215 * Default constructor
217 public PizzaAdminContactWebRequestBean () {
218 // Set gender to UNKNOWN
219 this.gender = Gender.UNKNOWN;
223 // Get initial context
224 Context context = new InitialContext();
227 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
228 } catch (final NamingException e) {
230 throw new FaceletException(e);
235 public String changeContactData () {
236 // Get contact instance
237 Contact contact = this.adminHelper.getContact();
239 // Check if contact instance is in helper and valid
240 if (null == contact) {
242 throw new NullPointerException("adminHelper.contact is null"); //NOI18N
243 } else if (contact.getContactId() == null) {
245 throw new NullPointerException("adminHelper.contact.contactId is null"); //NOI18N //NOI18N
246 } else if (contact.getContactId() < 1) {
248 throw new IllegalStateException(MessageFormat.format("adminHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
251 // Update all data in contact
252 this.updateContactData(contact);
254 // Call EJB for updating contact data
255 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
258 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
260 // Return to contact list (for now)
261 return "admin_list_contact"; //NOI18N
265 public void copyContactToController (final Contact contact) {
267 System.out.println(MessageFormat.format("AdminContactController::copyContactToController(): contact={0} - CALLED!", contact)); //NOI18N
269 // The contact instance must be valid
270 if (null == contact) {
272 throw new NullPointerException("contact is null"); //NOI18N
273 } else if (contact.getContactId() == null) {
275 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
276 } else if (contact.getContactId() < 1) {
278 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
281 // Set all fields: contact
282 this.setContactId(contact.getContactId());
283 this.setBirthday(contact.getContactBirthday());
284 this.setCity(contact.getContactCity());
285 this.setComment(contact.getContactComment());
286 this.setCountry(contact.getContactCountry());
287 this.setEmailAddress(contact.getContactEmailAddress());
288 this.setFamilyName(contact.getContactFamilyName());
289 this.setFirstName(contact.getContactFirstName());
290 this.setGender(contact.getContactGender());
291 this.setHouseNumber(contact.getContactHouseNumber());
292 this.setStreet(contact.getContactStreet());
293 this.setZipCode(contact.getContactZipCode());
295 // Is the cell phone set?
296 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
297 // ... cellphone data
298 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
299 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
300 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
304 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
306 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
307 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
308 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
309 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
312 // Is the land-line number set?
313 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
315 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
316 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
317 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
318 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
322 System.out.println("AdminContactController::copyContactToController(): EXIT!"); //NOI18N
326 public Date getBirthday () {
327 return this.birthday;
331 public void setBirthday (final Date birthday) {
332 this.birthday = birthday;
336 public MobileProvider getCellphoneCarrier () {
337 return this.cellphoneCarrier;
341 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
342 this.cellphoneCarrier = cellphoneCarrier;
346 public Long getCellphoneId () {
347 return this.cellphoneId;
351 public void setCellphoneId (final Long cellphoneId) {
352 this.cellphoneId = cellphoneId;
356 public Long getCellphoneNumber () {
357 return this.cellphoneNumber;
361 public void setCellphoneNumber (Long cellphoneNumber) {
362 this.cellphoneNumber = cellphoneNumber;
366 public String getCity () {
371 public void setCity (final String city) {
376 public String getComment () {
381 public void setComment (final String comment) {
382 this.comment = comment;
386 public Long getContactId () {
387 return this.contactId;
391 public void setContactId (final Long contactId) {
392 this.contactId = contactId;
396 public Country getCountry () {
401 public void setCountry (final Country country) {
402 this.country = country;
406 public String getEmailAddress () {
407 return this.emailAddress;
411 public void setEmailAddress (final String emailAddress) {
412 this.emailAddress = emailAddress;
416 public String getFamilyName () {
417 return this.familyName;
421 public void setFamilyName (final String familyName) {
422 this.familyName = familyName;
426 public Integer getFaxAreaCode () {
427 return this.faxAreaCode;
431 public void setFaxAreaCode (final Integer faxAreaCode) {
432 this.faxAreaCode = faxAreaCode;
436 public Country getFaxCountry () {
437 return this.faxCountry;
441 public void setFaxCountry (final Country faxCountry) {
442 this.faxCountry = faxCountry;
446 public Long getFaxId () {
451 public void setFaxId (final Long faxId) {
456 public Long getFaxNumber () {
457 return this.faxNumber;
461 public void setFaxNumber (final Long faxNumber) {
462 this.faxNumber = faxNumber;
466 public String getFirstName () {
467 return this.firstName;
471 public void setFirstName (final String firstName) {
472 this.firstName = firstName;
476 public Gender getGender () {
481 public void setGender (final Gender gender) {
482 this.gender = gender;
486 public Short getHouseNumber () {
487 return this.houseNumber;
491 public void setHouseNumber (final Short houseNumber) {
492 this.houseNumber = houseNumber;
496 public Long getLandLineId () {
497 return this.landLineId;
501 public void setLandLineId (final Long landLineId) {
502 this.landLineId = landLineId;
506 public Integer getPhoneAreaCode () {
507 return this.phoneAreaCode;
511 public void setPhoneAreaCode (final Integer phoneAreaCode) {
512 this.phoneAreaCode = phoneAreaCode;
516 public Country getPhoneCountry () {
517 return this.phoneCountry;
521 public void setPhoneCountry (final Country phoneCountry) {
522 this.phoneCountry = phoneCountry;
526 public Long getPhoneNumber () {
527 return this.phoneNumber;
531 public void setPhoneNumber (final Long phoneNumber) {
532 this.phoneNumber = phoneNumber;
536 public String getStreet () {
541 public void setStreet (final String street) {
542 this.street = street;
546 public Integer getZipCode () {
551 public void setZipCode (final Integer zipCode) {
552 this.zipCode = zipCode;
556 * Post-initialization of this class
559 public void init () {
563 * Updates all data in contact instance.
565 * @param contact Contact instance
567 private void updateContactData (final Contact contact) {
568 // Contact instance should be valid
569 if (null == contact) {
571 throw new NullPointerException("contact is null"); //NOI18N
572 } else if (contact.getContactId() == null) {
574 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
575 } else if (contact.getContactId() < 1) {
577 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
581 contact.setContactGender(this.getGender());
582 contact.setContactFirstName(this.getFirstName());
583 contact.setContactFamilyName(this.getFamilyName());
584 contact.setContactStreet(this.getStreet());
585 contact.setContactHouseNumber(this.getHouseNumber());
586 contact.setContactZipCode(this.getZipCode());
587 contact.setContactCity(this.getCity());
588 contact.setContactCountry(this.getCountry());
590 // Update contact's cellphone number
591 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
593 // Update contact's land-line number
594 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
596 // Update contact's fax number
597 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());