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