]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/contact/PizzaAdminContactWebRequestBean.java
Continued with customer:
[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.UserContact;
34 import org.mxchange.jcontacts.contact.gender.Gender;
35 import org.mxchange.jcontacts.contact.utils.ContactUtils;
36 import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent;
37 import org.mxchange.jcontacts.events.contact.update.AdminContactUpdatedEvent;
38 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
39 import org.mxchange.jcountry.data.Country;
40 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
41 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
42 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
43 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
44 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
45 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
46 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
47 import org.mxchange.pizzaapplication.beans.helper.PizzaAdminWebRequestController;
48
49 /**
50  * Administrative user bean (controller)
51  * <p>
52  * @author Roland Haeder<roland@mxchange.org>
53  */
54 @Named ("adminContactController")
55 @RequestScoped
56 public class PizzaAdminContactWebRequestBean implements PizzaAdminContactWebRequestController {
57
58         /**
59          * Serial number
60          */
61         private static final long serialVersionUID = 542_145_347_916L;
62
63         /**
64          * An event fired when the administrator has added a new contact
65          */
66         @Inject
67         @Any
68         private Event<AdminAddedContactEvent> addedContactEvent;
69
70         /**
71          * Admin helper instance
72          */
73         @Inject
74         private PizzaAdminWebRequestController adminHelper;
75
76         /**
77          * Birth day
78          */
79         private Date birthday;
80
81         /**
82          * Cellphone number's carrier
83          */
84         private MobileProvider cellphoneCarrier;
85
86         /**
87          * Cellphone id number
88          */
89         private Long cellphoneId;
90
91         /**
92          * Cellphone number
93          */
94         private Long cellphoneNumber;
95
96         /**
97          * City
98          */
99         private String city;
100
101         /**
102          * Optional comments
103          */
104         private String comment;
105
106         /**
107          * Remote contact bean
108          */
109         private final ContactSessionBeanRemote contactBean;
110
111         /**
112          * Contact id
113          */
114         private Long contactId;
115
116         /**
117          * Country instance
118          */
119         private Country country;
120
121         /**
122          * Email address
123          */
124         private String emailAddress;
125
126         /**
127          * Family name
128          */
129         private String familyName;
130
131         /**
132          * Fax number's area code
133          */
134         private Integer faxAreaCode;
135
136         /**
137          * Country instance for fax number
138          */
139         private Country faxCountry;
140
141         /**
142          * Fax id number
143          */
144         private Long faxId;
145
146         /**
147          * Fax number
148          */
149         private Long faxNumber;
150
151         /**
152          * First name
153          */
154         private String firstName;
155
156         /**
157          * Gender instance
158          */
159         private Gender gender;
160
161         /**
162          * House number
163          */
164         private Short houseNumber;
165
166         /**
167          * Whether a cellphone entry has been unlinked
168          */
169         private boolean isCellphoneUnlinked;
170
171         /**
172          * Whether a fax entry has been unlinked
173          */
174         private boolean isFaxUnlinked;
175
176         /**
177          * Whether a land-line number has been unlinked
178          */
179         private boolean isLandLineUnlinked;
180
181         /**
182          * Land-line id number
183          */
184         private Long landLineId;
185
186         /**
187          * Phone number area code
188          */
189         private Integer phoneAreaCode;
190
191         /**
192          * Country instance for phone number
193          */
194         private Country phoneCountry;
195
196         /**
197          * Phone number
198          */
199         private Long phoneNumber;
200
201         /**
202          * Street
203          */
204         private String street;
205
206         /**
207          * An event fired when the administrator has updated contact data
208          */
209         @Inject
210         @Any
211         private Event<AdminUpdatedContactEvent> updatedContactEvent;
212
213         /**
214          * ZIP code
215          */
216         private Integer zipCode;
217
218         /**
219          * Default constructor
220          */
221         public PizzaAdminContactWebRequestBean () {
222                 // Set gender to UNKNOWN
223                 this.gender = Gender.UNKNOWN;
224
225                 // Try it
226                 try {
227                         // Get initial context
228                         Context context = new InitialContext();
229
230                         // Try to lookup
231                         this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
232                 } catch (final NamingException e) {
233                         // Throw again
234                         throw new FaceletException(e);
235                 }
236         }
237
238         @Override
239         public String changeContactData () {
240                 // Get contact instance
241                 Contact contact = this.adminHelper.getContact();
242
243                 // Check if contact instance is in helper and valid
244                 if (null == contact) {
245                         // Throw NPE
246                         throw new NullPointerException("adminHelper.contact is null"); //NOI18N
247                 } else if (contact.getContactId() == null) {
248                         // Throw NPE again
249                         throw new NullPointerException("adminHelper.contact.contactId is null"); //NOI18N //NOI18N
250                 } else if (contact.getContactId() < 1) {
251                         // Invalid id
252                         throw new IllegalStateException(MessageFormat.format("adminHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
253                 }
254
255                 // Update all data in contact
256                 this.updateContactData(contact);
257
258                 // Call EJB for updating contact data
259                 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
260
261                 // Fire event
262                 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
263
264                 // Return to contact list (for now)
265                 return "admin_list_contact"; //NOI18N
266         }
267
268         @Override
269         public void copyContactToController (final Contact contact) {
270                 // Log message
271                 System.out.println(MessageFormat.format("AdminContactController::copyContactToController(): contact={0} - CALLED!", contact)); //NOI18N
272
273                 // The contact instance must be valid
274                 if (null == contact) {
275                         // Throw NPE again
276                         throw new NullPointerException("contact is null"); //NOI18N
277                 } else if (contact.getContactId() == null) {
278                         // Throw NPE again
279                         throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
280                 } else if (contact.getContactId() < 1) {
281                         // Not valid
282                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
283                 }
284
285                 // Set all fields: contact
286                 this.setContactId(contact.getContactId());
287                 this.setBirthday(contact.getContactBirthday());
288                 this.setCity(contact.getContactCity());
289                 this.setComment(contact.getContactComment());
290                 this.setCountry(contact.getContactCountry());
291                 this.setEmailAddress(contact.getContactEmailAddress());
292                 this.setFamilyName(contact.getContactFamilyName());
293                 this.setFirstName(contact.getContactFirstName());
294                 this.setGender(contact.getContactGender());
295                 this.setHouseNumber(contact.getContactHouseNumber());
296                 this.setStreet(contact.getContactStreet());
297                 this.setZipCode(contact.getContactZipCode());
298
299                 // Is the cell phone set?
300                 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
301                         // ... cellphone data
302                         this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
303                         this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
304                         this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
305                 }
306
307                 // Is the fax set?
308                 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
309                         // ... fax data
310                         this.setFaxId(contact.getContactFaxNumber().getPhoneId());
311                         this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
312                         this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
313                         this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
314                 }
315
316                 // Is the land-line number set?
317                 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
318                         // .. land-line data
319                         this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
320                         this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
321                         this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
322                         this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
323                 }
324
325                 // Log message
326                 System.out.println("AdminContactController::copyContactToController(): EXIT!"); //NOI18N
327         }
328
329         @Override
330         public Contact createContactInstance () {
331                 // User message
332                 //this.getLogger().logTrace("createContactInstance: CALLED!");
333
334                 // Generate phone number
335                 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
336                 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
337                 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
338
339                 // Create new instance
340                 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
341
342                 // Add all others
343                 contact.setContactBirthday(this.getBirthday());
344                 contact.setContactStreet(this.getStreet());
345                 contact.setContactHouseNumber(this.getHouseNumber());
346                 contact.setContactZipCode(this.getZipCode());
347                 contact.setContactCity(this.getCity());
348                 contact.setContactCountry(this.getCountry());
349                 contact.setContactEmailAddress(this.getEmailAddress());
350                 contact.setContactBirthday(this.getBirthday());
351                 contact.setContactComment(this.getComment());
352
353                 // Set ownContact
354                 contact.setContactOwnContact(Boolean.TRUE);
355
356                 // Don't set null or wrong references
357                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
358                         // Now the number must be given
359                         if (phone.getPhoneAreaCode() == null) {
360                                 // Is null
361                                 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
362                         } else if (phone.getPhoneAreaCode() < 1) {
363                                 // Abort here
364                                 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
365                         } else if (phone.getPhoneNumber() == null) {
366                                 // Is null
367                                 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
368                         } else if (phone.getPhoneNumber() < 1) {
369                                 // Abort here
370                                 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
371                         }
372
373                         // Set phone number
374                         contact.setContactLandLineNumber(phone);
375                 }
376
377                 // Don't set null or wrong references
378                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
379                         // Now the number must be given
380                         if (fax.getPhoneAreaCode() == null) {
381                                 // Is null
382                                 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
383                         } else if (fax.getPhoneAreaCode() < 1) {
384                                 // Abort here
385                                 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
386                         } else if (fax.getPhoneNumber() == null) {
387                                 // Is null
388                                 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
389                         } else if (fax.getPhoneNumber() < 1) {
390                                 // Abort here
391                                 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
392                         }
393
394                         // Set fax number
395                         contact.setContactFaxNumber(fax);
396                 }
397
398                 // Is the provider set?
399                 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
400                         // Is the number set?
401                         if (cellphone.getPhoneNumber() == null) {
402                                 // Is null
403                                 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
404                         } else if (cellphone.getPhoneNumber() < 1) {
405                                 // Abort here
406                                 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
407                         }
408
409                         // Set cellphone number
410                         contact.setContactCellphoneNumber(cellphone);
411                 }
412
413                 // Trace message
414                 //this.getLogger().logTrace(MessageFormat.format("createContactInstance: localContact={0} - EXIT!", localContact));
415
416                 // Return it
417                 return contact;
418         }
419
420         @Override
421         public Date getBirthday () {
422                 return this.birthday;
423         }
424
425         @Override
426         public void setBirthday (final Date birthday) {
427                 this.birthday = birthday;
428         }
429
430         @Override
431         public MobileProvider getCellphoneCarrier () {
432                 return this.cellphoneCarrier;
433         }
434
435         @Override
436         public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
437                 this.cellphoneCarrier = cellphoneCarrier;
438         }
439
440         @Override
441         public Long getCellphoneId () {
442                 return this.cellphoneId;
443         }
444
445         @Override
446         public void setCellphoneId (final Long cellphoneId) {
447                 this.cellphoneId = cellphoneId;
448         }
449
450         @Override
451         public Long getCellphoneNumber () {
452                 return this.cellphoneNumber;
453         }
454
455         @Override
456         public void setCellphoneNumber (Long cellphoneNumber) {
457                 this.cellphoneNumber = cellphoneNumber;
458         }
459
460         @Override
461         public String getCity () {
462                 return this.city;
463         }
464
465         @Override
466         public void setCity (final String city) {
467                 this.city = city;
468         }
469
470         @Override
471         public String getComment () {
472                 return this.comment;
473         }
474
475         @Override
476         public void setComment (final String comment) {
477                 this.comment = comment;
478         }
479
480         @Override
481         public Long getContactId () {
482                 return this.contactId;
483         }
484
485         @Override
486         public void setContactId (final Long contactId) {
487                 this.contactId = contactId;
488         }
489
490         @Override
491         public Country getCountry () {
492                 return this.country;
493         }
494
495         @Override
496         public void setCountry (final Country country) {
497                 this.country = country;
498         }
499
500         @Override
501         public String getEmailAddress () {
502                 return this.emailAddress;
503         }
504
505         @Override
506         public void setEmailAddress (final String emailAddress) {
507                 this.emailAddress = emailAddress;
508         }
509
510         @Override
511         public String getFamilyName () {
512                 return this.familyName;
513         }
514
515         @Override
516         public void setFamilyName (final String familyName) {
517                 this.familyName = familyName;
518         }
519
520         @Override
521         public Integer getFaxAreaCode () {
522                 return this.faxAreaCode;
523         }
524
525         @Override
526         public void setFaxAreaCode (final Integer faxAreaCode) {
527                 this.faxAreaCode = faxAreaCode;
528         }
529
530         @Override
531         public Country getFaxCountry () {
532                 return this.faxCountry;
533         }
534
535         @Override
536         public void setFaxCountry (final Country faxCountry) {
537                 this.faxCountry = faxCountry;
538         }
539
540         @Override
541         public Long getFaxId () {
542                 return this.faxId;
543         }
544
545         @Override
546         public void setFaxId (final Long faxId) {
547                 this.faxId = faxId;
548         }
549
550         @Override
551         public Long getFaxNumber () {
552                 return this.faxNumber;
553         }
554
555         @Override
556         public void setFaxNumber (final Long faxNumber) {
557                 this.faxNumber = faxNumber;
558         }
559
560         @Override
561         public String getFirstName () {
562                 return this.firstName;
563         }
564
565         @Override
566         public void setFirstName (final String firstName) {
567                 this.firstName = firstName;
568         }
569
570         @Override
571         public Gender getGender () {
572                 return this.gender;
573         }
574
575         @Override
576         public void setGender (final Gender gender) {
577                 this.gender = gender;
578         }
579
580         @Override
581         public Short getHouseNumber () {
582                 return this.houseNumber;
583         }
584
585         @Override
586         public void setHouseNumber (final Short houseNumber) {
587                 this.houseNumber = houseNumber;
588         }
589
590         @Override
591         public Long getLandLineId () {
592                 return this.landLineId;
593         }
594
595         @Override
596         public void setLandLineId (final Long landLineId) {
597                 this.landLineId = landLineId;
598         }
599
600         @Override
601         public Integer getPhoneAreaCode () {
602                 return this.phoneAreaCode;
603         }
604
605         @Override
606         public void setPhoneAreaCode (final Integer phoneAreaCode) {
607                 this.phoneAreaCode = phoneAreaCode;
608         }
609
610         @Override
611         public Country getPhoneCountry () {
612                 return this.phoneCountry;
613         }
614
615         @Override
616         public void setPhoneCountry (final Country phoneCountry) {
617                 this.phoneCountry = phoneCountry;
618         }
619
620         @Override
621         public Long getPhoneNumber () {
622                 return this.phoneNumber;
623         }
624
625         @Override
626         public void setPhoneNumber (final Long phoneNumber) {
627                 this.phoneNumber = phoneNumber;
628         }
629
630         @Override
631         public String getStreet () {
632                 return this.street;
633         }
634
635         @Override
636         public void setStreet (final String street) {
637                 this.street = street;
638         }
639
640         @Override
641         public Integer getZipCode () {
642                 return this.zipCode;
643         }
644
645         @Override
646         public void setZipCode (final Integer zipCode) {
647                 this.zipCode = zipCode;
648         }
649
650         /**
651          * Post-initialization of this class
652          */
653         @PostConstruct
654         public void init () {
655         }
656
657         /**
658          * Updates all data in contact instance.
659          * <p>
660          * @param contact Contact instance
661          */
662         private void updateContactData (final Contact contact) {
663                 // Contact instance should be valid
664                 if (null == contact) {
665                         // Throw NPE
666                         throw new NullPointerException("contact is null"); //NOI18N
667                 } else if (contact.getContactId() == null) {
668                         // Throw NPE again
669                         throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
670                 } else if (contact.getContactId() < 1) {
671                         // Invalid id
672                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
673                 }
674
675                 // Update all fields
676                 contact.setContactGender(this.getGender());
677                 contact.setContactFirstName(this.getFirstName());
678                 contact.setContactFamilyName(this.getFamilyName());
679                 contact.setContactStreet(this.getStreet());
680                 contact.setContactHouseNumber(this.getHouseNumber());
681                 contact.setContactZipCode(this.getZipCode());
682                 contact.setContactCity(this.getCity());
683                 contact.setContactCountry(this.getCountry());
684
685                 // Update contact's cellphone number
686                 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
687
688                 // Update contact's land-line number
689                 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
690
691                 // Update contact's fax number
692                 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
693         }
694
695 }