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