]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/beans/contact/AddressbookAdminContactWebRequestBean.java
Continued with data export with PrimeFaces:
[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                 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
327                 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
328                 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
329
330                 // ... fax data
331                 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
332                 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
333                 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
334                 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
335
336                 // .. land-line data
337                 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
338                 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
339                 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
340                 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
341         }
342
343         @Override
344         public Contact createContactInstance () {
345                 // Generate phone number
346                 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
347                 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
348                 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
349
350                 // Create new instance
351                 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
352
353                 // Check if contact instance is in helper and valid
354                 if (null == contact) {
355                         // Throw NPE
356                         throw new NullPointerException("beanHelper.contact is null"); //NOI18N
357                 } else if (contact.getContactId() == null) {
358                         // Throw NPE again
359                         throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
360                 } else if (contact.getContactId() < 1) {
361                         // Invalid id
362                         throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
363                 }
364
365                 // Update all data in contact
366                 this.updateContactData(contact);
367
368                 // Call EJB for updating contact data
369                 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
370
371                 // Fire event
372                 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
373
374                 // Clear bean
375                 this.clear();
376
377                 // Return it
378                 return contact;
379         }
380
381         @Override
382         public String editContactData () {
383                 // Get contact instance
384                 Contact contact = this.beanHelper.getContact();
385
386                 // Check if contact instance is in helper and valid
387                 if (null == contact) {
388                         // Throw NPE
389                         throw new NullPointerException("beanHelper.contact is null"); //NOI18N
390                 } else if (contact.getContactId() == null) {
391                         // Throw NPE again
392                         throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
393                 } else if (contact.getContactId() < 1) {
394                         // Invalid id
395                         throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
396                 }
397
398                 // Update all data in contact
399                 this.updateContactData(contact);
400
401                 // Call EJB for updating contact data
402                 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
403
404                 // Fire event
405                 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
406
407                 // Clear bean
408                 this.clear();
409
410                 // Return to contact list (for now)
411                 return "admin_list_contact"; //NOI18N
412         }
413
414         @Override
415         public String generateCellphoneNumber (final DialableCellphoneNumber cellphoneNumber) {
416                 // Is it null?
417                 if (null == cellphoneNumber) {
418                         // Return null
419                         return null;
420                 }
421
422                 // Get all data
423                 String number = String.format(
424                            "%s%d%d", //NOI18N
425                            cellphoneNumber.getCellphoneProvider().getProviderCountry().getCountryExternalDialPrefix(),
426                            cellphoneNumber.getCellphoneProvider().getProviderDialPrefix(),
427                            cellphoneNumber.getPhoneNumber()
428            );
429
430                 // Return it
431                 return number;
432         }
433
434         @Override
435         public String generatePhoneNumber (final DialableNumber phoneNumber) {
436                 // Is it null?
437                 if (null == phoneNumber) {
438                         // Return null
439                         return null;
440                 }
441
442                 // Generate it
443                 String number = String.format(
444                            "%s%d%d", //NOI18N
445                            phoneNumber.getPhoneCountry().getCountryExternalDialPrefix(),
446                            phoneNumber.getPhoneAreaCode(),
447                            phoneNumber.getPhoneNumber()
448            );
449
450                 // Return it
451                 return number;
452         }
453
454         @Override
455         @SuppressWarnings ("ReturnOfDateField")
456         public Date getBirthday () {
457                 return this.birthday;
458         }
459
460         @Override
461         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
462         public void setBirthday (final Date birthday) {
463                 this.birthday = birthday;
464         }
465
466         @Override
467         public MobileProvider getCellphoneCarrier () {
468                 return this.cellphoneCarrier;
469         }
470
471         @Override
472         public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
473                 this.cellphoneCarrier = cellphoneCarrier;
474         }
475
476         @Override
477         public Long getCellphoneId () {
478                 return this.cellphoneId;
479         }
480
481         @Override
482         public void setCellphoneId (final Long cellphoneId) {
483                 this.cellphoneId = cellphoneId;
484         }
485
486         @Override
487         public Long getCellphoneNumber () {
488                 return this.cellphoneNumber;
489         }
490
491         @Override
492         public void setCellphoneNumber (Long cellphoneNumber) {
493                 this.cellphoneNumber = cellphoneNumber;
494         }
495
496         @Override
497         public String getCity () {
498                 return this.city;
499         }
500
501         @Override
502         public void setCity (final String city) {
503                 this.city = city;
504         }
505
506         @Override
507         public String getComment () {
508                 return this.comment;
509         }
510
511         @Override
512         public void setComment (final String comment) {
513                 this.comment = comment;
514         }
515
516         @Override
517         public Long getContactId () {
518                 return this.contactId;
519         }
520
521         @Override
522         public void setContactId (final Long contactId) {
523                 this.contactId = contactId;
524         }
525
526         @Override
527         public Country getCountry () {
528                 return this.country;
529         }
530
531         @Override
532         public void setCountry (final Country country) {
533                 this.country = country;
534         }
535
536         @Override
537         public String getEmailAddress () {
538                 return this.emailAddress;
539         }
540
541         @Override
542         public void setEmailAddress (final String emailAddress) {
543                 this.emailAddress = emailAddress;
544         }
545
546         @Override
547         public String getFamilyName () {
548                 return this.familyName;
549         }
550
551         @Override
552         public void setFamilyName (final String familyName) {
553                 this.familyName = familyName;
554         }
555
556         @Override
557         public Integer getFaxAreaCode () {
558                 return this.faxAreaCode;
559         }
560
561         @Override
562         public void setFaxAreaCode (final Integer faxAreaCode) {
563                 this.faxAreaCode = faxAreaCode;
564         }
565
566         @Override
567         public Country getFaxCountry () {
568                 return this.faxCountry;
569         }
570
571         @Override
572         public void setFaxCountry (final Country faxCountry) {
573                 this.faxCountry = faxCountry;
574         }
575
576         @Override
577         public Long getFaxId () {
578                 return this.faxId;
579         }
580
581         @Override
582         public void setFaxId (final Long faxId) {
583                 this.faxId = faxId;
584         }
585
586         @Override
587         public Long getFaxNumber () {
588                 return this.faxNumber;
589         }
590
591         @Override
592         public void setFaxNumber (final Long faxNumber) {
593                 this.faxNumber = faxNumber;
594         }
595
596         @Override
597         public String getFirstName () {
598                 return this.firstName;
599         }
600
601         @Override
602         public void setFirstName (final String firstName) {
603                 this.firstName = firstName;
604         }
605
606         @Override
607         public Gender getGender () {
608                 return this.gender;
609         }
610
611         @Override
612         public void setGender (final Gender gender) {
613                 this.gender = gender;
614         }
615
616         @Override
617         public Short getHouseNumber () {
618                 return this.houseNumber;
619         }
620
621         @Override
622         public void setHouseNumber (final Short houseNumber) {
623                 this.houseNumber = houseNumber;
624         }
625
626         @Override
627         public Long getLandLineId () {
628                 return this.landLineId;
629         }
630
631         @Override
632         public void setLandLineId (final Long landLineId) {
633                 this.landLineId = landLineId;
634         }
635
636         @Override
637         public Integer getPhoneAreaCode () {
638                 return this.phoneAreaCode;
639         }
640
641         @Override
642         public void setPhoneAreaCode (final Integer phoneAreaCode) {
643                 this.phoneAreaCode = phoneAreaCode;
644         }
645
646         @Override
647         public Country getPhoneCountry () {
648                 return this.phoneCountry;
649         }
650
651         @Override
652         public void setPhoneCountry (final Country phoneCountry) {
653                 this.phoneCountry = phoneCountry;
654         }
655
656         @Override
657         public Long getPhoneNumber () {
658                 return this.phoneNumber;
659         }
660
661         @Override
662         public void setPhoneNumber (final Long phoneNumber) {
663                 this.phoneNumber = phoneNumber;
664         }
665
666         @Override
667         public String getStreet () {
668                 return this.street;
669         }
670
671         @Override
672         public void setStreet (final String street) {
673                 this.street = street;
674         }
675
676         @Override
677         public Integer getZipCode () {
678                 return this.zipCode;
679         }
680
681         @Override
682         public void setZipCode (final Integer zipCode) {
683                 this.zipCode = zipCode;
684         }
685
686         /**
687          * Post-initialization of this class
688          */
689         @PostConstruct
690         public void init () {
691         }
692
693         /**
694          * Clears this bean
695          */
696         private void clear () {
697                 // Clear all data
698                 // - personal data
699                 this.setGender(Gender.UNKNOWN);
700                 this.setFirstName(null);
701                 this.setFamilyName(null);
702                 this.setStreet(null);
703                 this.setHouseNumber(null);
704                 this.setZipCode(null);
705                 this.setCity(null);
706                 this.setCountry(null);
707
708                 // - contact data
709                 this.setEmailAddress(null);
710                 this.setPhoneCountry(null);
711                 this.setPhoneAreaCode(null);
712                 this.setPhoneNumber(null);
713                 this.setCellphoneCarrier(null);
714                 this.setCellphoneNumber(null);
715                 this.setFaxCountry(null);
716                 this.setFaxAreaCode(null);
717                 this.setFaxNumber(null);
718
719                 // - other data
720                 this.setBirthday(null);
721                 this.setComment(null);
722         }
723
724         /**
725          * Checks whether the given contact is found
726          * <p>
727          * @param contact Contact inastance
728          *
729          * @return Wether contact has been found
730          */
731         private boolean isSameContactFound (final Contact contact) {
732                 // Default is not found
733                 boolean IsFound = false;
734
735                 // Get iterator
736                 Iterator<Contact> iterator = this.contactController.allContacts().iterator();
737
738                 // Loop through all
739                 while (iterator.hasNext()) {
740                         // Get next contact
741                         Contact next = iterator.next();
742
743                         // Is the same?
744                         if (ContactUtils.isSameContact(contact, next)) {
745                                 // Yes, then abort loop
746                                 IsFound = false;
747                                 break;
748                         }
749                 }
750
751                 // Return status
752                 return IsFound;
753         }
754
755         /**
756          * Updates all data in contact instance.
757          * <p>
758          * @param contact Contact instance
759          */
760         private void updateContactData (final Contact contact) {
761                 // Contact instance should be valid
762                 if (null == contact) {
763                         // Throw NPE
764                         throw new NullPointerException("contact is null"); //NOI18N
765                 } else if (contact.getContactId() == null) {
766                         // Throw NPE again
767                         throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
768                 } else if (contact.getContactId() < 1) {
769                         // Invalid id
770                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
771                 }
772
773                 // Update all fields
774                 contact.setContactGender(this.getGender());
775                 contact.setContactFirstName(this.getFirstName());
776                 contact.setContactFamilyName(this.getFamilyName());
777                 contact.setContactStreet(this.getStreet());
778                 contact.setContactHouseNumber(this.getHouseNumber());
779                 contact.setContactZipCode(this.getZipCode());
780                 contact.setContactCity(this.getCity());
781                 contact.setContactCountry(this.getCountry());
782
783                 // Update contact's cellphone number
784                 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
785
786                 // Update contact's land-line number
787                 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
788
789                 // Update contact's fax number
790                 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
791         }
792
793 }