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