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