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