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