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