]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/beans/contact/AddressbookAdminContactWebRequestBean.java
avoid NPEs, if cellphone, fax or land-line instances are not set
[addressbook-war.git] / src / java / org / mxchange / addressbook / beans / contact / AddressbookAdminContactWebRequestBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.addressbook.beans.contact;
18
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.AddressbookWebRequestController;
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.DialableNumber;
46 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
47 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
48 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
49 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
50 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
51 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
52 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
53
54 /**
55  * An administrative user bean (controller)
56  * <p>
57  * @author Roland Haeder<roland@mxchange.org>
58  */
59 @Named ("adminContactController")
60 @RequestScoped
61 public class AddressbookAdminContactWebRequestBean extends BaseAddressbookController implements AddressbookAdminContactWebRequestController {
62
63         /**
64          * Serial number
65          */
66         private static final long serialVersionUID = 542_145_347_916L;
67
68         /**
69          * An event fired when the administrator has added a new contact
70          */
71         @Inject
72         @Any
73         private Event<AdminAddedContactEvent> addedContactEvent;
74
75         /**
76          * Admin helper instance
77          */
78         @Inject
79         private AddressbookWebRequestController beanHelper;
80
81         /**
82          * Birth day
83          */
84         private Date birthday;
85
86         /**
87          * Cellphone number's carrier
88          */
89         private MobileProvider cellphoneCarrier;
90
91         /**
92          * Cellphone id number
93          */
94         private Long cellphoneId;
95
96         /**
97          * Cellphone number
98          */
99         private Long cellphoneNumber;
100
101         /**
102          * City
103          */
104         private String city;
105
106         /**
107          * Optional comments
108          */
109         private String comment;
110
111         /**
112          * Remote contact bean
113          */
114         private final ContactSessionBeanRemote contactBean;
115
116         /**
117          * General contact controller
118          */
119         @Inject
120         private AddressbookContactWebSessionController contactController;
121
122         /**
123          * Contact id
124          */
125         private Long contactId;
126
127         /**
128          * Country instance
129          */
130         private Country country;
131
132         /**
133          * Email address
134          */
135         private String emailAddress;
136
137         /**
138          * Family name
139          */
140         private String familyName;
141
142         /**
143          * Fax number's area code
144          */
145         private Integer faxAreaCode;
146
147         /**
148          * Country instance for fax number
149          */
150         private Country faxCountry;
151
152         /**
153          * Fax id number
154          */
155         private Long faxId;
156
157         /**
158          * Fax number
159          */
160         private Long faxNumber;
161
162         /**
163          * First name
164          */
165         private String firstName;
166
167         /**
168          * Gender instance
169          */
170         private Gender gender;
171
172         /**
173          * House number
174          */
175         private Short houseNumber;
176
177         /**
178          * Whether a cellphone entry has been unlinked
179          */
180         private boolean isCellphoneUnlinked;
181
182         /**
183          * Whether a fax entry has been unlinked
184          */
185         private boolean isFaxUnlinked;
186
187         /**
188          * Whether a land-line number has been unlinked
189          */
190         private boolean isLandLineUnlinked;
191
192         /**
193          * Land-line id number
194          */
195         private Long landLineId;
196
197         /**
198          * Phone number area code
199          */
200         private Integer phoneAreaCode;
201
202         /**
203          * Country instance for phone number
204          */
205         private Country phoneCountry;
206
207         /**
208          * Phone number
209          */
210         private Long phoneNumber;
211
212         /**
213          * Street
214          */
215         private String street;
216
217         /**
218          * An event fired when the administrator has updated contact data
219          */
220         @Inject
221         @Any
222         private Event<AdminUpdatedContactEvent> updatedContactEvent;
223
224         /**
225          * ZIP code
226          */
227         private Integer zipCode;
228
229         /**
230          * Default constructor
231          */
232         public AddressbookAdminContactWebRequestBean () {
233                 // Try it
234                 try {
235                         // Get initial context
236                         Context context = new InitialContext();
237
238                         // Try to lookup
239                         this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/addressbook-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
240                 } catch (final NamingException e) {
241                         // Throw again
242                         throw new FaceletException(e);
243                 }
244         }
245
246         @Override
247         public String addContact () {
248                 // Are all minimum fields set?
249                 if (this.getGender() == null) {
250                         // Throw NPE
251                         throw new NullPointerException("gender is null"); //NOI18N
252                 } else if (this.getFirstName() == null) {
253                         // Throw NPE
254                         throw new NullPointerException("firstName is null"); //NOI18N
255                 } else if (this.getFirstName().isEmpty()) {
256                         // Empty string
257                         throw new IllegalStateException("firstName is empty"); //NOI18N
258                 } else if (this.getFamilyName() == null) {
259                         // Throw NPE
260                         throw new NullPointerException("familyName is null"); //NOI18N
261                 } else if (this.getFamilyName().isEmpty()) {
262                         // Empty string
263                         throw new IllegalStateException("familyName is empty"); //NOI18N
264                 }
265
266                 // Create new contact instance
267                 Contact contact = this.createContactInstance();
268
269                 // Default is not same contact
270                 if (this.isSameContactFound(contact)) {
271                         // Already registered
272                         throw new FaceletException(new ContactAlreadyAddedException(contact));
273                 }
274
275                 // Init contact
276                 Contact updatedContact;
277
278                 // Try to call EJB
279                 try {
280                         // Call EJB
281                         updatedContact = this.contactBean.addContact(contact);
282                 } catch (final ContactAlreadyAddedException ex) {
283                         // Throw again
284                         throw new FaceletException(ex);
285                 }
286
287                 // Fire event
288                 this.addedContactEvent.fire(new AdminContactAddedEvent(updatedContact));
289
290                 // Clear this bean
291                 this.clear();
292
293                 // Return outcome
294                 return "admin_list_contact"; //NOI18N
295         }
296
297         @Override
298         public void copyContactToController (final Contact contact) {
299                 // The contact instance must be valid
300                 if (null == contact) {
301                         // Throw NPE again
302                         throw new NullPointerException("contact is null"); //NOI18N
303                 } else if (contact.getContactId() == null) {
304                         // Throw NPE again
305                         throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
306                 } else if (contact.getContactId() < 1) {
307                         // Not valid
308                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
309                 }
310
311                 // Set all fields: contact
312                 this.setContactId(contact.getContactId());
313                 this.setBirthday(contact.getContactBirthday());
314                 this.setCity(contact.getContactCity());
315                 this.setComment(contact.getContactComment());
316                 this.setCountry(contact.getContactCountry());
317                 this.setEmailAddress(contact.getContactEmailAddress());
318                 this.setFamilyName(contact.getContactFamilyName());
319                 this.setFirstName(contact.getContactFirstName());
320                 this.setGender(contact.getContactGender());
321                 this.setHouseNumber(contact.getContactHouseNumber());
322                 this.setStreet(contact.getContactStreet());
323                 this.setZipCode(contact.getContactZipCode());
324
325                 // ... cellphone data
326                 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
327                         this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
328                         this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
329                         this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
330                 }
331
332                 // ... fax data
333                 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
334                         this.setFaxId(contact.getContactFaxNumber().getPhoneId());
335                         this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
336                         this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
337                         this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
338                 }
339
340                 // .. land-line data
341                 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
342                         this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
343                         this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
344                         this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
345                         this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
346                 }
347         }
348
349         @Override
350         public Contact createContactInstance () {
351                 // Generate phone number
352                 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
353                 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
354                 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
355
356                 // Create new instance
357                 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
358
359                 // Check if contact instance is in helper and valid
360                 if (null == contact) {
361                         // Throw NPE
362                         throw new NullPointerException("beanHelper.contact is null"); //NOI18N
363                 } else if (contact.getContactId() == null) {
364                         // Throw NPE again
365                         throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
366                 } else if (contact.getContactId() < 1) {
367                         // Invalid id
368                         throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
369                 }
370
371                 // Update all data in contact
372                 this.updateContactData(contact);
373
374                 // Call EJB for updating contact data
375                 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
376
377                 // Fire event
378                 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
379
380                 // Clear bean
381                 this.clear();
382
383                 // Return it
384                 return contact;
385         }
386
387         @Override
388         public String editContactData () {
389                 // Get contact instance
390                 Contact contact = this.beanHelper.getContact();
391
392                 // Check if contact instance is in helper and valid
393                 if (null == contact) {
394                         // Throw NPE
395                         throw new NullPointerException("beanHelper.contact is null"); //NOI18N
396                 } else if (contact.getContactId() == null) {
397                         // Throw NPE again
398                         throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
399                 } else if (contact.getContactId() < 1) {
400                         // Invalid id
401                         throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
402                 }
403
404                 // Update all data in contact
405                 this.updateContactData(contact);
406
407                 // Call EJB for updating contact data
408                 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
409
410                 // Fire event
411                 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
412
413                 // Clear bean
414                 this.clear();
415
416                 // Return to contact list (for now)
417                 return "admin_list_contact"; //NOI18N
418         }
419
420         @Override
421         public String generateCellphoneNumber (final DialableCellphoneNumber cellphoneNumber) {
422                 // Is it null?
423                 if (null == cellphoneNumber) {
424                         // Return null
425                         return null;
426                 }
427
428                 // Get all data
429                 String number = String.format(
430                            "%s%d%d", //NOI18N
431                            cellphoneNumber.getCellphoneProvider().getProviderCountry().getCountryExternalDialPrefix(),
432                            cellphoneNumber.getCellphoneProvider().getProviderDialPrefix(),
433                            cellphoneNumber.getPhoneNumber()
434            );
435
436                 // Return it
437                 return number;
438         }
439
440         @Override
441         public String generatePhoneNumber (final DialableNumber phoneNumber) {
442                 // Is it null?
443                 if (null == phoneNumber) {
444                         // Return null
445                         return null;
446                 }
447
448                 // Generate it
449                 String number = String.format(
450                            "%s%d%d", //NOI18N
451                            phoneNumber.getPhoneCountry().getCountryExternalDialPrefix(),
452                            phoneNumber.getPhoneAreaCode(),
453                            phoneNumber.getPhoneNumber()
454            );
455
456                 // Return it
457                 return number;
458         }
459
460         @Override
461         @SuppressWarnings ("ReturnOfDateField")
462         public Date getBirthday () {
463                 return this.birthday;
464         }
465
466         @Override
467         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
468         public void setBirthday (final Date birthday) {
469                 this.birthday = birthday;
470         }
471
472         @Override
473         public MobileProvider getCellphoneCarrier () {
474                 return this.cellphoneCarrier;
475         }
476
477         @Override
478         public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
479                 this.cellphoneCarrier = cellphoneCarrier;
480         }
481
482         @Override
483         public Long getCellphoneId () {
484                 return this.cellphoneId;
485         }
486
487         @Override
488         public void setCellphoneId (final Long cellphoneId) {
489                 this.cellphoneId = cellphoneId;
490         }
491
492         @Override
493         public Long getCellphoneNumber () {
494                 return this.cellphoneNumber;
495         }
496
497         @Override
498         public void setCellphoneNumber (Long cellphoneNumber) {
499                 this.cellphoneNumber = cellphoneNumber;
500         }
501
502         @Override
503         public String getCity () {
504                 return this.city;
505         }
506
507         @Override
508         public void setCity (final String city) {
509                 this.city = city;
510         }
511
512         @Override
513         public String getComment () {
514                 return this.comment;
515         }
516
517         @Override
518         public void setComment (final String comment) {
519                 this.comment = comment;
520         }
521
522         @Override
523         public Long getContactId () {
524                 return this.contactId;
525         }
526
527         @Override
528         public void setContactId (final Long contactId) {
529                 this.contactId = contactId;
530         }
531
532         @Override
533         public Country getCountry () {
534                 return this.country;
535         }
536
537         @Override
538         public void setCountry (final Country country) {
539                 this.country = country;
540         }
541
542         @Override
543         public String getEmailAddress () {
544                 return this.emailAddress;
545         }
546
547         @Override
548         public void setEmailAddress (final String emailAddress) {
549                 this.emailAddress = emailAddress;
550         }
551
552         @Override
553         public String getFamilyName () {
554                 return this.familyName;
555         }
556
557         @Override
558         public void setFamilyName (final String familyName) {
559                 this.familyName = familyName;
560         }
561
562         @Override
563         public Integer getFaxAreaCode () {
564                 return this.faxAreaCode;
565         }
566
567         @Override
568         public void setFaxAreaCode (final Integer faxAreaCode) {
569                 this.faxAreaCode = faxAreaCode;
570         }
571
572         @Override
573         public Country getFaxCountry () {
574                 return this.faxCountry;
575         }
576
577         @Override
578         public void setFaxCountry (final Country faxCountry) {
579                 this.faxCountry = faxCountry;
580         }
581
582         @Override
583         public Long getFaxId () {
584                 return this.faxId;
585         }
586
587         @Override
588         public void setFaxId (final Long faxId) {
589                 this.faxId = faxId;
590         }
591
592         @Override
593         public Long getFaxNumber () {
594                 return this.faxNumber;
595         }
596
597         @Override
598         public void setFaxNumber (final Long faxNumber) {
599                 this.faxNumber = faxNumber;
600         }
601
602         @Override
603         public String getFirstName () {
604                 return this.firstName;
605         }
606
607         @Override
608         public void setFirstName (final String firstName) {
609                 this.firstName = firstName;
610         }
611
612         @Override
613         public Gender getGender () {
614                 return this.gender;
615         }
616
617         @Override
618         public void setGender (final Gender gender) {
619                 this.gender = gender;
620         }
621
622         @Override
623         public Short getHouseNumber () {
624                 return this.houseNumber;
625         }
626
627         @Override
628         public void setHouseNumber (final Short houseNumber) {
629                 this.houseNumber = houseNumber;
630         }
631
632         @Override
633         public Long getLandLineId () {
634                 return this.landLineId;
635         }
636
637         @Override
638         public void setLandLineId (final Long landLineId) {
639                 this.landLineId = landLineId;
640         }
641
642         @Override
643         public Integer getPhoneAreaCode () {
644                 return this.phoneAreaCode;
645         }
646
647         @Override
648         public void setPhoneAreaCode (final Integer phoneAreaCode) {
649                 this.phoneAreaCode = phoneAreaCode;
650         }
651
652         @Override
653         public Country getPhoneCountry () {
654                 return this.phoneCountry;
655         }
656
657         @Override
658         public void setPhoneCountry (final Country phoneCountry) {
659                 this.phoneCountry = phoneCountry;
660         }
661
662         @Override
663         public Long getPhoneNumber () {
664                 return this.phoneNumber;
665         }
666
667         @Override
668         public void setPhoneNumber (final Long phoneNumber) {
669                 this.phoneNumber = phoneNumber;
670         }
671
672         @Override
673         public String getStreet () {
674                 return this.street;
675         }
676
677         @Override
678         public void setStreet (final String street) {
679                 this.street = street;
680         }
681
682         @Override
683         public Integer getZipCode () {
684                 return this.zipCode;
685         }
686
687         @Override
688         public void setZipCode (final Integer zipCode) {
689                 this.zipCode = zipCode;
690         }
691
692         /**
693          * Post-initialization of this class
694          */
695         @PostConstruct
696         public void init () {
697         }
698
699         /**
700          * Clears this bean
701          */
702         private void clear () {
703                 // Clear all data
704                 // - personal data
705                 this.setGender(Gender.UNKNOWN);
706                 this.setFirstName(null);
707                 this.setFamilyName(null);
708                 this.setStreet(null);
709                 this.setHouseNumber(null);
710                 this.setZipCode(null);
711                 this.setCity(null);
712                 this.setCountry(null);
713
714                 // - contact data
715                 this.setEmailAddress(null);
716                 this.setPhoneCountry(null);
717                 this.setPhoneAreaCode(null);
718                 this.setPhoneNumber(null);
719                 this.setCellphoneCarrier(null);
720                 this.setCellphoneNumber(null);
721                 this.setFaxCountry(null);
722                 this.setFaxAreaCode(null);
723                 this.setFaxNumber(null);
724
725                 // - other data
726                 this.setBirthday(null);
727                 this.setComment(null);
728         }
729
730         /**
731          * Checks whether the given contact is found
732          * <p>
733          * @param contact Contact inastance
734          *
735          * @return Wether contact has been found
736          */
737         private boolean isSameContactFound (final Contact contact) {
738                 // Default is not found
739                 boolean IsFound = false;
740
741                 // Get iterator
742                 Iterator<Contact> iterator = this.contactController.allContacts().iterator();
743
744                 // Loop through all
745                 while (iterator.hasNext()) {
746                         // Get next contact
747                         Contact next = iterator.next();
748
749                         // Is the same?
750                         if (ContactUtils.isSameContact(contact, next)) {
751                                 // Yes, then abort loop
752                                 IsFound = false;
753                                 break;
754                         }
755                 }
756
757                 // Return status
758                 return IsFound;
759         }
760
761         /**
762          * Updates all data in contact instance.
763          * <p>
764          * @param contact Contact instance
765          */
766         private void updateContactData (final Contact contact) {
767                 // Contact instance should be valid
768                 if (null == contact) {
769                         // Throw NPE
770                         throw new NullPointerException("contact is null"); //NOI18N
771                 } else if (contact.getContactId() == null) {
772                         // Throw NPE again
773                         throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
774                 } else if (contact.getContactId() < 1) {
775                         // Invalid id
776                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
777                 }
778
779                 // Update all fields
780                 contact.setContactGender(this.getGender());
781                 contact.setContactFirstName(this.getFirstName());
782                 contact.setContactFamilyName(this.getFamilyName());
783                 contact.setContactStreet(this.getStreet());
784                 contact.setContactHouseNumber(this.getHouseNumber());
785                 contact.setContactZipCode(this.getZipCode());
786                 contact.setContactCity(this.getCity());
787                 contact.setContactCountry(this.getCountry());
788
789                 // Update contact's cellphone number
790                 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
791
792                 // Update contact's land-line number
793                 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
794
795                 // Update contact's fax number
796                 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
797         }
798
799 }