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