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