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