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