]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/contact/JobsAdminContactWebRequestBean.java
7b0a40ba29b03169d69eab861ccb430d422a4464
[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  * An administrative 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          * 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 JobsAdminContactWebRequestBean () {
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 changeContactData () {
235                 // Get contact instance
236                 Contact contact = this.adminHelper.getContact();
237
238                 // Check if contact instance is in helper and valid
239                 if (null == contact) {
240                         // Throw NPE
241                         throw new NullPointerException("adminHelper.contact is null"); //NOI18N
242                 } else if (contact.getContactId() == null) {
243                         // Throw NPE again
244                         throw new NullPointerException("adminHelper.contact.contactId is null"); //NOI18N //NOI18N
245                 } else if (contact.getContactId() < 1) {
246                         // Invalid id
247                         throw new IllegalStateException(MessageFormat.format("adminHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
248                 }
249
250                 // Update all data in contact
251                 this.updateContactData(contact);
252
253                 // Call EJB for updating contact data
254                 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
255
256                 // Fire event
257                 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
258
259                 // Return to contact list (for now)
260                 return "admin_list_contact"; //NOI18N
261         }
262
263         @Override
264         public void copyContactToController (final Contact contact) {
265                 // The contact instance must be valid
266                 if (null == contact) {
267                         // Throw NPE again
268                         throw new NullPointerException("contact is null"); //NOI18N
269                 } else if (contact.getContactId() == null) {
270                         // Throw NPE again
271                         throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
272                 } else if (contact.getContactId() < 1) {
273                         // Not valid
274                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
275                 }
276
277                 // Set all fields: contact
278                 this.setContactId(contact.getContactId());
279                 this.setBirthday(contact.getContactBirthday());
280                 this.setCity(contact.getContactCity());
281                 this.setComment(contact.getContactComment());
282                 this.setCountry(contact.getContactCountry());
283                 this.setEmailAddress(contact.getContactEmailAddress());
284                 this.setFamilyName(contact.getContactFamilyName());
285                 this.setFirstName(contact.getContactFirstName());
286                 this.setGender(contact.getContactGender());
287                 this.setHouseNumber(contact.getContactHouseNumber());
288                 this.setStreet(contact.getContactStreet());
289                 this.setZipCode(contact.getContactZipCode());
290
291                 // ... cellphone data
292                 this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
293                 this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
294                 this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
295
296                 // ... fax data
297                 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
298                 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
299                 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
300                 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
301
302                 // .. land-line data
303                 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
304                 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
305                 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
306                 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
307         }
308
309         @Override
310         public Date getBirthday () {
311                 return this.birthday;
312         }
313
314         @Override
315         public void setBirthday (final Date birthday) {
316                 this.birthday = birthday;
317         }
318
319         @Override
320         public MobileProvider getCellphoneCarrier () {
321                 return this.cellphoneCarrier;
322         }
323
324         @Override
325         public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
326                 this.cellphoneCarrier = cellphoneCarrier;
327         }
328
329         @Override
330         public Long getCellphoneId () {
331                 return this.cellphoneId;
332         }
333
334         @Override
335         public void setCellphoneId (final Long cellphoneId) {
336                 this.cellphoneId = cellphoneId;
337         }
338
339         @Override
340         public Long getCellphoneNumber () {
341                 return this.cellphoneNumber;
342         }
343
344         @Override
345         public void setCellphoneNumber (Long cellphoneNumber) {
346                 this.cellphoneNumber = cellphoneNumber;
347         }
348
349         @Override
350         public String getCity () {
351                 return this.city;
352         }
353
354         @Override
355         public void setCity (final String city) {
356                 this.city = city;
357         }
358
359         @Override
360         public String getComment () {
361                 return this.comment;
362         }
363
364         @Override
365         public void setComment (final String comment) {
366                 this.comment = comment;
367         }
368
369         @Override
370         public Long getContactId () {
371                 return this.contactId;
372         }
373
374         @Override
375         public void setContactId (final Long contactId) {
376                 this.contactId = contactId;
377         }
378
379         @Override
380         public Country getCountry () {
381                 return this.country;
382         }
383
384         @Override
385         public void setCountry (final Country country) {
386                 this.country = country;
387         }
388
389         @Override
390         public String getEmailAddress () {
391                 return this.emailAddress;
392         }
393
394         @Override
395         public void setEmailAddress (final String emailAddress) {
396                 this.emailAddress = emailAddress;
397         }
398
399         @Override
400         public String getFamilyName () {
401                 return this.familyName;
402         }
403
404         @Override
405         public void setFamilyName (final String familyName) {
406                 this.familyName = familyName;
407         }
408
409         @Override
410         public Integer getFaxAreaCode () {
411                 return this.faxAreaCode;
412         }
413
414         @Override
415         public void setFaxAreaCode (final Integer faxAreaCode) {
416                 this.faxAreaCode = faxAreaCode;
417         }
418
419         @Override
420         public Country getFaxCountry () {
421                 return this.faxCountry;
422         }
423
424         @Override
425         public void setFaxCountry (final Country faxCountry) {
426                 this.faxCountry = faxCountry;
427         }
428
429         @Override
430         public Long getFaxId () {
431                 return this.faxId;
432         }
433
434         @Override
435         public void setFaxId (final Long faxId) {
436                 this.faxId = faxId;
437         }
438
439         @Override
440         public Long getFaxNumber () {
441                 return this.faxNumber;
442         }
443
444         @Override
445         public void setFaxNumber (final Long faxNumber) {
446                 this.faxNumber = faxNumber;
447         }
448
449         @Override
450         public String getFirstName () {
451                 return this.firstName;
452         }
453
454         @Override
455         public void setFirstName (final String firstName) {
456                 this.firstName = firstName;
457         }
458
459         @Override
460         public Gender getGender () {
461                 return this.gender;
462         }
463
464         @Override
465         public void setGender (final Gender gender) {
466                 this.gender = gender;
467         }
468
469         @Override
470         public Short getHouseNumber () {
471                 return this.houseNumber;
472         }
473
474         @Override
475         public void setHouseNumber (final Short houseNumber) {
476                 this.houseNumber = houseNumber;
477         }
478
479         @Override
480         public Long getLandLineId () {
481                 return this.landLineId;
482         }
483
484         @Override
485         public void setLandLineId (final Long landLineId) {
486                 this.landLineId = landLineId;
487         }
488
489         @Override
490         public Integer getPhoneAreaCode () {
491                 return this.phoneAreaCode;
492         }
493
494         @Override
495         public void setPhoneAreaCode (final Integer phoneAreaCode) {
496                 this.phoneAreaCode = phoneAreaCode;
497         }
498
499         @Override
500         public Country getPhoneCountry () {
501                 return this.phoneCountry;
502         }
503
504         @Override
505         public void setPhoneCountry (final Country phoneCountry) {
506                 this.phoneCountry = phoneCountry;
507         }
508
509         @Override
510         public Long getPhoneNumber () {
511                 return this.phoneNumber;
512         }
513
514         @Override
515         public void setPhoneNumber (final Long phoneNumber) {
516                 this.phoneNumber = phoneNumber;
517         }
518
519         @Override
520         public String getStreet () {
521                 return this.street;
522         }
523
524         @Override
525         public void setStreet (final String street) {
526                 this.street = street;
527         }
528
529         @Override
530         public Integer getZipCode () {
531                 return this.zipCode;
532         }
533
534         @Override
535         public void setZipCode (final Integer zipCode) {
536                 this.zipCode = zipCode;
537         }
538
539         /**
540          * Post-initialization of this class
541          */
542         @PostConstruct
543         public void init () {
544         }
545
546         /**
547          * Updates all data in contact instance.
548          * <p>
549          * @param contact Contact instance
550          */
551         private void updateContactData (final Contact contact) {
552                 // Contact instance should be valid
553                 if (null == contact) {
554                         // Throw NPE
555                         throw new NullPointerException("contact is null"); //NOI18N
556                 } else if (contact.getContactId() == null) {
557                         // Throw NPE again
558                         throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
559                 } else if (contact.getContactId() < 1) {
560                         // Invalid id
561                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
562                 }
563
564                 // Update all fields
565                 contact.setContactGender(this.getGender());
566                 contact.setContactFirstName(this.getFirstName());
567                 contact.setContactFamilyName(this.getFamilyName());
568                 contact.setContactStreet(this.getStreet());
569                 contact.setContactHouseNumber(this.getHouseNumber());
570                 contact.setContactZipCode(this.getZipCode());
571                 contact.setContactCity(this.getCity());
572                 contact.setContactCountry(this.getCountry());
573
574                 // Update contact's cellphone number
575                 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
576
577                 // Update contact's land-line number
578                 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
579
580                 // Update contact's fax number
581                 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
582         }
583
584 }