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