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