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