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