]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/beans/contact/AddressbookAdminContactWebRequestBean.java
added title (e.g. Dr., Prof. etc)
[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          * Title
219          */
220         private String title;
221
222         /**
223          * An event fired when the administrator has updated contact data
224          */
225         @Inject
226         @Any
227         private Event<AdminUpdatedContactEvent> updatedContactEvent;
228
229         /**
230          * ZIP code
231          */
232         private Integer zipCode;
233
234         /**
235          * Default constructor
236          */
237         public AddressbookAdminContactWebRequestBean () {
238                 // Try it
239                 try {
240                         // Get initial context
241                         Context context = new InitialContext();
242
243                         // Try to lookup
244                         this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/addressbook-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
245                 } catch (final NamingException e) {
246                         // Throw again
247                         throw new FaceletException(e);
248                 }
249         }
250
251         @Override
252         public String addContact () {
253                 // Are all minimum fields set?
254                 if (this.getGender() == null) {
255                         // Throw NPE
256                         throw new NullPointerException("gender is null"); //NOI18N
257                 } else if (this.getFirstName() == null) {
258                         // Throw NPE
259                         throw new NullPointerException("firstName is null"); //NOI18N
260                 } else if (this.getFirstName().isEmpty()) {
261                         // Empty string
262                         throw new IllegalStateException("firstName is empty"); //NOI18N
263                 } else if (this.getFamilyName() == null) {
264                         // Throw NPE
265                         throw new NullPointerException("familyName is null"); //NOI18N
266                 } else if (this.getFamilyName().isEmpty()) {
267                         // Empty string
268                         throw new IllegalStateException("familyName is empty"); //NOI18N
269                 }
270
271                 // Create new contact instance
272                 Contact contact = this.createContactInstance();
273
274                 // Default is not same contact
275                 if (this.isSameContactFound(contact)) {
276                         // Already registered
277                         throw new FaceletException(new ContactAlreadyAddedException(contact));
278                 }
279
280                 // Init contact
281                 Contact updatedContact;
282
283                 // Try to call EJB
284                 try {
285                         // Call EJB
286                         updatedContact = this.contactBean.addContact(contact);
287                 } catch (final ContactAlreadyAddedException ex) {
288                         // Throw again
289                         throw new FaceletException(ex);
290                 }
291
292                 // Fire event
293                 this.addedContactEvent.fire(new AdminContactAddedEvent(updatedContact));
294
295                 // Clear this bean
296                 this.clear();
297
298                 // Return outcome
299                 return "admin_list_contact"; //NOI18N
300         }
301
302         @Override
303         public void copyContactToController (final Contact contact) {
304                 // The contact instance must be valid
305                 if (null == contact) {
306                         // Throw NPE again
307                         throw new NullPointerException("contact is null"); //NOI18N
308                 } else if (contact.getContactId() == null) {
309                         // Throw NPE again
310                         throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
311                 } else if (contact.getContactId() < 1) {
312                         // Not valid
313                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
314                 }
315
316                 // Set all fields: contact
317                 this.setContactId(contact.getContactId());
318                 this.setTitle(contact.getContactTitle());
319                 this.setBirthday(contact.getContactBirthday());
320                 this.setCity(contact.getContactCity());
321                 this.setComment(contact.getContactComment());
322                 this.setCountry(contact.getContactCountry());
323                 this.setEmailAddress(contact.getContactEmailAddress());
324                 this.setFamilyName(contact.getContactFamilyName());
325                 this.setFirstName(contact.getContactFirstName());
326                 this.setGender(contact.getContactGender());
327                 this.setHouseNumber(contact.getContactHouseNumber());
328                 this.setStreet(contact.getContactStreet());
329                 this.setZipCode(contact.getContactZipCode());
330
331                 // ... cellphone data
332                 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
333                         this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
334                         this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
335                         this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
336                 }
337
338                 // ... fax data
339                 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
340                         this.setFaxId(contact.getContactFaxNumber().getPhoneId());
341                         this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
342                         this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
343                         this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
344                 }
345
346                 // .. land-line data
347                 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
348                         this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
349                         this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
350                         this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
351                         this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
352                 }
353         }
354
355         @Override
356         public Contact createContactInstance () {
357                 // Generate phone number
358                 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
359                 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
360                 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
361
362                 // Create new instance
363                 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
364
365                 // Check if contact instance is in helper and valid
366                 if (null == contact) {
367                         // Throw NPE
368                         throw new NullPointerException("beanHelper.contact is null"); //NOI18N
369                 } else if (contact.getContactId() == null) {
370                         // Throw NPE again
371                         throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
372                 } else if (contact.getContactId() < 1) {
373                         // Invalid id
374                         throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
375                 }
376
377                 // Update all data in contact
378                 this.updateContactData(contact);
379
380                 // Call EJB for updating contact data
381                 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
382
383                 // Fire event
384                 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
385
386                 // Clear bean
387                 this.clear();
388
389                 // Return it
390                 return contact;
391         }
392
393         @Override
394         public String editContactData () {
395                 // Get contact instance
396                 Contact contact = this.beanHelper.getContact();
397
398                 // Check if contact instance is in helper and valid
399                 if (null == contact) {
400                         // Throw NPE
401                         throw new NullPointerException("beanHelper.contact is null"); //NOI18N
402                 } else if (contact.getContactId() == null) {
403                         // Throw NPE again
404                         throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
405                 } else if (contact.getContactId() < 1) {
406                         // Invalid id
407                         throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
408                 }
409
410                 // Update all data in contact
411                 this.updateContactData(contact);
412
413                 // Call EJB for updating contact data
414                 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
415
416                 // Fire event
417                 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
418
419                 // Clear bean
420                 this.clear();
421
422                 // Return to contact list (for now)
423                 return "admin_list_contact"; //NOI18N
424         }
425
426         @Override
427         public String generateCellphoneNumber (final DialableCellphoneNumber cellphoneNumber) {
428                 // Is it null?
429                 if (null == cellphoneNumber) {
430                         // Return null
431                         return null;
432                 }
433
434                 // Get all data
435                 String number = String.format(
436                            "%s%d%d", //NOI18N
437                            cellphoneNumber.getCellphoneProvider().getProviderCountry().getCountryExternalDialPrefix(),
438                            cellphoneNumber.getCellphoneProvider().getProviderDialPrefix(),
439                            cellphoneNumber.getPhoneNumber()
440            );
441
442                 // Return it
443                 return number;
444         }
445
446         @Override
447         public String generatePhoneNumber (final DialableNumber phoneNumber) {
448                 // Is it null?
449                 if (null == phoneNumber) {
450                         // Return null
451                         return null;
452                 }
453
454                 // Generate it
455                 String number = String.format(
456                            "%s%d%d", //NOI18N
457                            phoneNumber.getPhoneCountry().getCountryExternalDialPrefix(),
458                            phoneNumber.getPhoneAreaCode(),
459                            phoneNumber.getPhoneNumber()
460            );
461
462                 // Return it
463                 return number;
464         }
465
466         @Override
467         @SuppressWarnings ("ReturnOfDateField")
468         public Date getBirthday () {
469                 return this.birthday;
470         }
471
472         @Override
473         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
474         public void setBirthday (final Date birthday) {
475                 this.birthday = birthday;
476         }
477
478         @Override
479         public MobileProvider getCellphoneCarrier () {
480                 return this.cellphoneCarrier;
481         }
482
483         @Override
484         public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
485                 this.cellphoneCarrier = cellphoneCarrier;
486         }
487
488         @Override
489         public Long getCellphoneId () {
490                 return this.cellphoneId;
491         }
492
493         @Override
494         public void setCellphoneId (final Long cellphoneId) {
495                 this.cellphoneId = cellphoneId;
496         }
497
498         @Override
499         public Long getCellphoneNumber () {
500                 return this.cellphoneNumber;
501         }
502
503         @Override
504         public void setCellphoneNumber (Long cellphoneNumber) {
505                 this.cellphoneNumber = cellphoneNumber;
506         }
507
508         @Override
509         public String getCity () {
510                 return this.city;
511         }
512
513         @Override
514         public void setCity (final String city) {
515                 this.city = city;
516         }
517
518         @Override
519         public String getComment () {
520                 return this.comment;
521         }
522
523         @Override
524         public void setComment (final String comment) {
525                 this.comment = comment;
526         }
527
528         @Override
529         public Long getContactId () {
530                 return this.contactId;
531         }
532
533         @Override
534         public void setContactId (final Long contactId) {
535                 this.contactId = contactId;
536         }
537
538         @Override
539         public Country getCountry () {
540                 return this.country;
541         }
542
543         @Override
544         public void setCountry (final Country country) {
545                 this.country = country;
546         }
547
548         @Override
549         public String getEmailAddress () {
550                 return this.emailAddress;
551         }
552
553         @Override
554         public void setEmailAddress (final String emailAddress) {
555                 this.emailAddress = emailAddress;
556         }
557
558         @Override
559         public String getFamilyName () {
560                 return this.familyName;
561         }
562
563         @Override
564         public void setFamilyName (final String familyName) {
565                 this.familyName = familyName;
566         }
567
568         @Override
569         public Integer getFaxAreaCode () {
570                 return this.faxAreaCode;
571         }
572
573         @Override
574         public void setFaxAreaCode (final Integer faxAreaCode) {
575                 this.faxAreaCode = faxAreaCode;
576         }
577
578         @Override
579         public Country getFaxCountry () {
580                 return this.faxCountry;
581         }
582
583         @Override
584         public void setFaxCountry (final Country faxCountry) {
585                 this.faxCountry = faxCountry;
586         }
587
588         @Override
589         public Long getFaxId () {
590                 return this.faxId;
591         }
592
593         @Override
594         public void setFaxId (final Long faxId) {
595                 this.faxId = faxId;
596         }
597
598         @Override
599         public Long getFaxNumber () {
600                 return this.faxNumber;
601         }
602
603         @Override
604         public void setFaxNumber (final Long faxNumber) {
605                 this.faxNumber = faxNumber;
606         }
607
608         @Override
609         public String getFirstName () {
610                 return this.firstName;
611         }
612
613         @Override
614         public void setFirstName (final String firstName) {
615                 this.firstName = firstName;
616         }
617
618         @Override
619         public Gender getGender () {
620                 return this.gender;
621         }
622
623         @Override
624         public void setGender (final Gender gender) {
625                 this.gender = gender;
626         }
627
628         @Override
629         public Short getHouseNumber () {
630                 return this.houseNumber;
631         }
632
633         @Override
634         public void setHouseNumber (final Short houseNumber) {
635                 this.houseNumber = houseNumber;
636         }
637
638         @Override
639         public Long getLandLineId () {
640                 return this.landLineId;
641         }
642
643         @Override
644         public void setLandLineId (final Long landLineId) {
645                 this.landLineId = landLineId;
646         }
647
648         @Override
649         public Integer getPhoneAreaCode () {
650                 return this.phoneAreaCode;
651         }
652
653         @Override
654         public void setPhoneAreaCode (final Integer phoneAreaCode) {
655                 this.phoneAreaCode = phoneAreaCode;
656         }
657
658         @Override
659         public Country getPhoneCountry () {
660                 return this.phoneCountry;
661         }
662
663         @Override
664         public void setPhoneCountry (final Country phoneCountry) {
665                 this.phoneCountry = phoneCountry;
666         }
667
668         @Override
669         public Long getPhoneNumber () {
670                 return this.phoneNumber;
671         }
672
673         @Override
674         public void setPhoneNumber (final Long phoneNumber) {
675                 this.phoneNumber = phoneNumber;
676         }
677
678         @Override
679         public String getStreet () {
680                 return this.street;
681         }
682
683         @Override
684         public void setStreet (final String street) {
685                 this.street = street;
686         }
687
688         @Override
689         public String getTitle () {
690                 return this.title;
691         }
692
693         @Override
694         public void setTitle (final String title) {
695                 this.title = title;
696         }
697
698         @Override
699         public Integer getZipCode () {
700                 return this.zipCode;
701         }
702
703         @Override
704         public void setZipCode (final Integer zipCode) {
705                 this.zipCode = zipCode;
706         }
707
708         /**
709          * Post-initialization of this class
710          */
711         @PostConstruct
712         public void init () {
713         }
714
715         /**
716          * Clears this bean
717          */
718         private void clear () {
719                 // Clear all data
720                 // - personal data
721                 this.setGender(Gender.UNKNOWN);
722                 this.setTitle(null);
723                 this.setFirstName(null);
724                 this.setFamilyName(null);
725                 this.setStreet(null);
726                 this.setHouseNumber(null);
727                 this.setZipCode(null);
728                 this.setCity(null);
729                 this.setCountry(null);
730
731                 // - contact data
732                 this.setEmailAddress(null);
733                 this.setPhoneCountry(null);
734                 this.setPhoneAreaCode(null);
735                 this.setPhoneNumber(null);
736                 this.setCellphoneCarrier(null);
737                 this.setCellphoneNumber(null);
738                 this.setFaxCountry(null);
739                 this.setFaxAreaCode(null);
740                 this.setFaxNumber(null);
741
742                 // - other data
743                 this.setBirthday(null);
744                 this.setComment(null);
745         }
746
747         /**
748          * Checks whether the given contact is found
749          * <p>
750          * @param contact Contact inastance
751          *
752          * @return Wether contact has been found
753          */
754         private boolean isSameContactFound (final Contact contact) {
755                 // Default is not found
756                 boolean IsFound = false;
757
758                 // Get iterator
759                 Iterator<Contact> iterator = this.contactController.allContacts().iterator();
760
761                 // Loop through all
762                 while (iterator.hasNext()) {
763                         // Get next contact
764                         Contact next = iterator.next();
765
766                         // Is the same?
767                         if (ContactUtils.isSameContact(contact, next)) {
768                                 // Yes, then abort loop
769                                 IsFound = false;
770                                 break;
771                         }
772                 }
773
774                 // Return status
775                 return IsFound;
776         }
777
778         /**
779          * Updates all data in contact instance.
780          * <p>
781          * @param contact Contact instance
782          */
783         private void updateContactData (final Contact contact) {
784                 // Contact instance should be valid
785                 if (null == contact) {
786                         // Throw NPE
787                         throw new NullPointerException("contact is null"); //NOI18N
788                 } else if (contact.getContactId() == null) {
789                         // Throw NPE again
790                         throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
791                 } else if (contact.getContactId() < 1) {
792                         // Invalid id
793                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
794                 }
795
796                 // Update all fields
797                 contact.setContactGender(this.getGender());
798                 contact.setContactTitle(this.getTitle());
799                 contact.setContactFirstName(this.getFirstName());
800                 contact.setContactFamilyName(this.getFamilyName());
801                 contact.setContactStreet(this.getStreet());
802                 contact.setContactHouseNumber(this.getHouseNumber());
803                 contact.setContactZipCode(this.getZipCode());
804                 contact.setContactCity(this.getCity());
805                 contact.setContactCountry(this.getCountry());
806
807                 // Update contact's cellphone number
808                 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
809
810                 // Update contact's land-line number
811                 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
812
813                 // Update contact's fax number
814                 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
815         }
816
817 }