]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/contact/PizzaAdminContactWebRequestBean.java
Continued with rewriting:
[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  * An administrative 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          * Whether a cellphone entry has been unlinked
164          */
165         private boolean isCellphoneUnlinked;
166
167         /**
168          * Whether a fax entry has been unlinked
169          */
170         private boolean isFaxUnlinked;
171
172         /**
173          * Whether a land-line number has been unlinked
174          */
175         private boolean isLandLineUnlinked;
176
177         /**
178          * Land-line id number
179          */
180         private Long landLineId;
181
182         /**
183          * Phone number area code
184          */
185         private Integer phoneAreaCode;
186
187         /**
188          * Country instance for phone number
189          */
190         private Country phoneCountry;
191
192         /**
193          * Phone number
194          */
195         private Long phoneNumber;
196
197         /**
198          * Street
199          */
200         private String street;
201
202         /**
203          * An event fired when the administrator has updated contact data
204          */
205         @Inject
206         @Any
207         private Event<AdminUpdatedContactEvent> updatedContactEvent;
208
209         /**
210          * ZIP code
211          */
212         private Integer zipCode;
213
214         /**
215          * Default constructor
216          */
217         public PizzaAdminContactWebRequestBean () {
218                 // Set gender to UNKNOWN
219                 this.gender = Gender.UNKNOWN;
220
221                 // Try it
222                 try {
223                         // Get initial context
224                         Context context = new InitialContext();
225
226                         // Try to lookup
227                         this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
228                 } catch (final NamingException e) {
229                         // Throw again
230                         throw new FaceletException(e);
231                 }
232         }
233
234         @Override
235         public String changeContactData () {
236                 // Get contact instance
237                 Contact contact = this.adminHelper.getContact();
238
239                 // Check if contact instance is in helper and valid
240                 if (null == contact) {
241                         // Throw NPE
242                         throw new NullPointerException("adminHelper.contact is null"); //NOI18N
243                 } else if (contact.getContactId() == null) {
244                         // Throw NPE again
245                         throw new NullPointerException("adminHelper.contact.contactId is null"); //NOI18N //NOI18N
246                 } else if (contact.getContactId() < 1) {
247                         // Invalid id
248                         throw new IllegalStateException(MessageFormat.format("adminHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
249                 }
250
251                 // Update all data in contact
252                 this.updateContactData(contact);
253
254                 // Call EJB for updating contact data
255                 Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
256
257                 // Fire event
258                 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
259
260                 // Return to contact list (for now)
261                 return "admin_list_contact"; //NOI18N
262         }
263
264         @Override
265         public void copyContactToController (final Contact contact) {
266                 // Log message
267                 System.out.println(MessageFormat.format("AdminContactController::copyContactToController(): contact={0} - CALLED!", contact)); //NOI18N
268
269                 // The contact instance must be valid
270                 if (null == contact) {
271                         // Throw NPE again
272                         throw new NullPointerException("contact is null"); //NOI18N
273                 } else if (contact.getContactId() == null) {
274                         // Throw NPE again
275                         throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
276                 } else if (contact.getContactId() < 1) {
277                         // Not valid
278                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
279                 }
280
281                 // Set all fields: contact
282                 this.setContactId(contact.getContactId());
283                 this.setBirthday(contact.getContactBirthday());
284                 this.setCity(contact.getContactCity());
285                 this.setComment(contact.getContactComment());
286                 this.setCountry(contact.getContactCountry());
287                 this.setEmailAddress(contact.getContactEmailAddress());
288                 this.setFamilyName(contact.getContactFamilyName());
289                 this.setFirstName(contact.getContactFirstName());
290                 this.setGender(contact.getContactGender());
291                 this.setHouseNumber(contact.getContactHouseNumber());
292                 this.setStreet(contact.getContactStreet());
293                 this.setZipCode(contact.getContactZipCode());
294
295                 // Is the cell phone set?
296                 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
297                         // ... cellphone data
298                         this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId());
299                         this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider());
300                         this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber());
301                 }
302
303                 // Is the fax set?
304                 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
305                         // ... fax data
306                         this.setFaxId(contact.getContactFaxNumber().getPhoneId());
307                         this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
308                         this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
309                         this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
310                 }
311
312                 // Is the land-line number set?
313                 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
314                         // .. land-line data
315                         this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
316                         this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
317                         this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
318                         this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
319                 }
320
321                 // Log message
322                 System.out.println("AdminContactController::copyContactToController(): EXIT!"); //NOI18N
323         }
324
325         @Override
326         public Date getBirthday () {
327                 return this.birthday;
328         }
329
330         @Override
331         public void setBirthday (final Date birthday) {
332                 this.birthday = birthday;
333         }
334
335         @Override
336         public MobileProvider getCellphoneCarrier () {
337                 return this.cellphoneCarrier;
338         }
339
340         @Override
341         public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
342                 this.cellphoneCarrier = cellphoneCarrier;
343         }
344
345         @Override
346         public Long getCellphoneId () {
347                 return this.cellphoneId;
348         }
349
350         @Override
351         public void setCellphoneId (final Long cellphoneId) {
352                 this.cellphoneId = cellphoneId;
353         }
354
355         @Override
356         public Long getCellphoneNumber () {
357                 return this.cellphoneNumber;
358         }
359
360         @Override
361         public void setCellphoneNumber (Long cellphoneNumber) {
362                 this.cellphoneNumber = cellphoneNumber;
363         }
364
365         @Override
366         public String getCity () {
367                 return this.city;
368         }
369
370         @Override
371         public void setCity (final String city) {
372                 this.city = city;
373         }
374
375         @Override
376         public String getComment () {
377                 return this.comment;
378         }
379
380         @Override
381         public void setComment (final String comment) {
382                 this.comment = comment;
383         }
384
385         @Override
386         public Long getContactId () {
387                 return this.contactId;
388         }
389
390         @Override
391         public void setContactId (final Long contactId) {
392                 this.contactId = contactId;
393         }
394
395         @Override
396         public Country getCountry () {
397                 return this.country;
398         }
399
400         @Override
401         public void setCountry (final Country country) {
402                 this.country = country;
403         }
404
405         @Override
406         public String getEmailAddress () {
407                 return this.emailAddress;
408         }
409
410         @Override
411         public void setEmailAddress (final String emailAddress) {
412                 this.emailAddress = emailAddress;
413         }
414
415         @Override
416         public String getFamilyName () {
417                 return this.familyName;
418         }
419
420         @Override
421         public void setFamilyName (final String familyName) {
422                 this.familyName = familyName;
423         }
424
425         @Override
426         public Integer getFaxAreaCode () {
427                 return this.faxAreaCode;
428         }
429
430         @Override
431         public void setFaxAreaCode (final Integer faxAreaCode) {
432                 this.faxAreaCode = faxAreaCode;
433         }
434
435         @Override
436         public Country getFaxCountry () {
437                 return this.faxCountry;
438         }
439
440         @Override
441         public void setFaxCountry (final Country faxCountry) {
442                 this.faxCountry = faxCountry;
443         }
444
445         @Override
446         public Long getFaxId () {
447                 return this.faxId;
448         }
449
450         @Override
451         public void setFaxId (final Long faxId) {
452                 this.faxId = faxId;
453         }
454
455         @Override
456         public Long getFaxNumber () {
457                 return this.faxNumber;
458         }
459
460         @Override
461         public void setFaxNumber (final Long faxNumber) {
462                 this.faxNumber = faxNumber;
463         }
464
465         @Override
466         public String getFirstName () {
467                 return this.firstName;
468         }
469
470         @Override
471         public void setFirstName (final String firstName) {
472                 this.firstName = firstName;
473         }
474
475         @Override
476         public Gender getGender () {
477                 return this.gender;
478         }
479
480         @Override
481         public void setGender (final Gender gender) {
482                 this.gender = gender;
483         }
484
485         @Override
486         public Short getHouseNumber () {
487                 return this.houseNumber;
488         }
489
490         @Override
491         public void setHouseNumber (final Short houseNumber) {
492                 this.houseNumber = houseNumber;
493         }
494
495         @Override
496         public Long getLandLineId () {
497                 return this.landLineId;
498         }
499
500         @Override
501         public void setLandLineId (final Long landLineId) {
502                 this.landLineId = landLineId;
503         }
504
505         @Override
506         public Integer getPhoneAreaCode () {
507                 return this.phoneAreaCode;
508         }
509
510         @Override
511         public void setPhoneAreaCode (final Integer phoneAreaCode) {
512                 this.phoneAreaCode = phoneAreaCode;
513         }
514
515         @Override
516         public Country getPhoneCountry () {
517                 return this.phoneCountry;
518         }
519
520         @Override
521         public void setPhoneCountry (final Country phoneCountry) {
522                 this.phoneCountry = phoneCountry;
523         }
524
525         @Override
526         public Long getPhoneNumber () {
527                 return this.phoneNumber;
528         }
529
530         @Override
531         public void setPhoneNumber (final Long phoneNumber) {
532                 this.phoneNumber = phoneNumber;
533         }
534
535         @Override
536         public String getStreet () {
537                 return this.street;
538         }
539
540         @Override
541         public void setStreet (final String street) {
542                 this.street = street;
543         }
544
545         @Override
546         public Integer getZipCode () {
547                 return this.zipCode;
548         }
549
550         @Override
551         public void setZipCode (final Integer zipCode) {
552                 this.zipCode = zipCode;
553         }
554
555         /**
556          * Post-initialization of this class
557          */
558         @PostConstruct
559         public void init () {
560         }
561
562         /**
563          * Updates all data in contact instance.
564          * <p>
565          * @param contact Contact instance
566          */
567         private void updateContactData (final Contact contact) {
568                 // Contact instance should be valid
569                 if (null == contact) {
570                         // Throw NPE
571                         throw new NullPointerException("contact is null"); //NOI18N
572                 } else if (contact.getContactId() == null) {
573                         // Throw NPE again
574                         throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
575                 } else if (contact.getContactId() < 1) {
576                         // Invalid id
577                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
578                 }
579
580                 // Update all fields
581                 contact.setContactGender(this.getGender());
582                 contact.setContactFirstName(this.getFirstName());
583                 contact.setContactFamilyName(this.getFamilyName());
584                 contact.setContactStreet(this.getStreet());
585                 contact.setContactHouseNumber(this.getHouseNumber());
586                 contact.setContactZipCode(this.getZipCode());
587                 contact.setContactCity(this.getCity());
588                 contact.setContactCountry(this.getCountry());
589
590                 // Update contact's cellphone number
591                 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
592
593                 // Update contact's land-line number
594                 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
595
596                 // Update contact's fax number
597                 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
598         }
599
600 }