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