]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/contact/phone/PizzaContactPhoneWebRequestBean.java
Updated copyright year
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / contact / phone / PizzaContactPhoneWebRequestBean.java
1 /*
2  * Copyright (C) 2016 - 2020 Free Software Foundation
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.phone;
18
19 import fish.payara.cdi.jsr107.impl.NamedCache;
20 import java.text.MessageFormat;
21 import java.util.LinkedList;
22 import java.util.List;
23 import java.util.Objects;
24 import javax.cache.Cache;
25 import javax.enterprise.context.RequestScoped;
26 import javax.enterprise.event.Observes;
27 import javax.inject.Inject;
28 import javax.inject.Named;
29 import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent;
30 import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent;
31 import org.mxchange.jcontacts.events.fax.unlinked.ObservableAdminUnlinkedFaxNumberEvent;
32 import org.mxchange.jcontacts.events.landline.unlinked.ObservableAdminUnlinkedLandLineNumberEvent;
33 import org.mxchange.jcontacts.events.mobile.unlinked.ObservableAdminUnlinkedMobileNumberEvent;
34 import org.mxchange.jcontacts.model.contact.Contact;
35 import org.mxchange.jphone.events.fax.created.ObservableCreatedFaxNumberEvent;
36 import org.mxchange.jphone.events.landline.created.ObservableCreatedLandLineNumberEvent;
37 import org.mxchange.jphone.events.mobile.created.ObservableCreatedMobileNumberEvent;
38 import org.mxchange.jphone.model.phonenumbers.DialableNumber;
39 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
40 import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
41 import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
42 import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
43 import org.mxchange.pizzaapplication.beans.BasePizzaBean;
44 import org.mxchange.pizzaapplication.beans.contact.PizzaContactWebRequestController;
45
46 /**
47  * A general contact bean (controller)
48  * <p>
49  * @author Roland Häder<roland@mxchange.org>
50  */
51 @Named ("contactPhoneController")
52 @RequestScoped
53 public class PizzaContactPhoneWebRequestBean extends BasePizzaBean implements PizzaContactPhoneWebRequestController {
54
55         /**
56          * Serial number
57          */
58         private static final long serialVersionUID = 542_145_347_916L;
59
60         /**
61          * General contact controller
62          */
63         @Inject
64         private PizzaContactWebRequestController contactController;
65
66         /**
67          * "Cache" for contact's mobile, land-line and fax numbers. Currently one
68          * per each type is supported. Maybe later this will change into a OneToMany
69          * relationship (one contact, many numbers).
70          */
71         @Inject
72         @NamedCache (cacheName = "contactsPhoneCache")
73         private Cache<DialableNumber, List<Contact>> contactsPhoneCache;
74
75         /**
76          * fax number
77          */
78         private DialableFaxNumber faxNumber;
79
80         /**
81          * land-line number
82          */
83         private DialableLandLineNumber landLineNumber;
84
85         /**
86          * Chosen mobile number
87          */
88         private DialableMobileNumber mobileNumber;
89
90         /**
91          * Default constructor
92          */
93         public PizzaContactPhoneWebRequestBean () {
94                 // Call super constructor
95                 super();
96         }
97
98         /**
99          * Observes events being fired when an administrator has added a new
100          * contact.
101          * <p>
102          * @param event Event being fired
103          */
104         public void afterAdminAddedContactEvent (@Observes final ObservableAdminAddedContactEvent event) {
105                 // The event must be valid
106                 if (null == event) {
107                         // Throw NPE
108                         throw new NullPointerException("event is null"); //NOI18N
109                 } else if (event.getAddedContact() == null) {
110                         // Throw again ...
111                         throw new NullPointerException("event.addedContact is null"); //NOI18N
112                 } else if (event.getAddedContact().getContactId() == null) {
113                         // ... and again
114                         throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
115                 } else if (event.getAddedContact().getContactId() < 1) {
116                         // Not valid
117                         throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N
118                 }
119
120                 // Clear this bean
121                 this.clear();
122         }
123
124         /**
125          * Event observer for newly added users by administrator
126          * <p>
127          * @param event Event being fired
128          */
129         public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
130                 // event should not be null
131                 if (null == event) {
132                         // Throw NPE
133                         throw new NullPointerException("event is null"); //NOI18N
134                 } else if (event.getAddedUser() == null) {
135                         // Throw NPE again
136                         throw new NullPointerException("event.addedUser is null"); //NOI18N
137                 } else if (event.getAddedUser().getUserId() == null) {
138                         // userId is null
139                         throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
140                 } else if (event.getAddedUser().getUserId() < 1) {
141                         // Not avalid id
142                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
143                 }
144
145                 // Clear all data
146                 this.clear();
147         }
148
149         /**
150          * Event observer for unlinked fax contact by administrators
151          * <p>
152          * @param event Unlinked fax contact event
153          */
154         public void afterAdminUnlinkedFaxContactDataEvent (@Observes final ObservableAdminUnlinkedFaxNumberEvent event) {
155                 // event should not be null
156                 if (null == event) {
157                         // Throw NPE
158                         throw new NullPointerException("event is null"); //NOI18N
159                 } else if (event.getUnlinkedFaxNumber() == null) {
160                         // Throw NPE again
161                         throw new NullPointerException("event.unlinkedFaxNumber is null"); //NOI18N
162                 } else if (event.getUnlinkedFaxNumber().getPhoneId() == null) {
163                         // userId is null
164                         throw new NullPointerException("event.unlinkedFaxNumber.contactId is null"); //NOI18N
165                 } else if (event.getUnlinkedFaxNumber().getPhoneId() < 1) {
166                         // Not avalid id
167                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedFaxNumber(), event.getUnlinkedFaxNumber().getPhoneId())); //NOI18N
168                 }
169
170                 // Remove it from list
171                 this.contactsPhoneCache.remove(event.getUnlinkedFaxNumber());
172
173                 // Clear all data
174                 this.clear();
175         }
176
177         /**
178          * Event observer for unlinked land-line contact by administrators
179          * <p>
180          * @param event Unlinked land-line contact event
181          */
182         public void afterAdminUnlinkedLandLineContactDataEvent (@Observes final ObservableAdminUnlinkedLandLineNumberEvent event) {
183                 // event should not be null
184                 if (null == event) {
185                         // Throw NPE
186                         throw new NullPointerException("event is null"); //NOI18N
187                 } else if (event.getUnlinkedLandLineNumber() == null) {
188                         // Throw NPE again
189                         throw new NullPointerException("event.unlinkedLandLineNumber is null"); //NOI18N
190                 } else if (event.getUnlinkedLandLineNumber().getPhoneId() == null) {
191                         // userId is null
192                         throw new NullPointerException("event.unlinkedLandLineNumber.contactId is null"); //NOI18N
193                 } else if (event.getUnlinkedLandLineNumber().getPhoneId() < 1) {
194                         // Not avalid id
195                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedLandLineNumber(), event.getUnlinkedLandLineNumber().getPhoneId())); //NOI18N
196                 }
197
198                 // Remove it from list
199                 this.contactsPhoneCache.remove(event.getUnlinkedLandLineNumber());
200
201                 // Clear all data
202                 this.clear();
203         }
204
205         /**
206          * Event observer for unlinked mobile contact by administrators
207          * <p>
208          * @param event Unlinked mobile contact event
209          */
210         public void afterAdminUnlinkedMobileContactDataEvent (@Observes final ObservableAdminUnlinkedMobileNumberEvent event) {
211                 // event should not be null
212                 if (null == event) {
213                         // Throw NPE
214                         throw new NullPointerException("event is null"); //NOI18N
215                 } else if (event.getUnlinkedMobileNumber() == null) {
216                         // Throw NPE again
217                         throw new NullPointerException("event.unlinkedMobileNumber is null"); //NOI18N
218                 } else if (event.getUnlinkedMobileNumber().getPhoneId() == null) {
219                         // userId is null
220                         throw new NullPointerException("event.unlinkedMobileNumber.contactId is null"); //NOI18N
221                 } else if (event.getUnlinkedMobileNumber().getPhoneId() < 1) {
222                         // Not avalid id
223                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedMobileNumber(), event.getUnlinkedMobileNumber().getPhoneId())); //NOI18N
224                 }
225
226                 // Remove it from list
227                 this.contactsPhoneCache.remove(event.getUnlinkedMobileNumber());
228
229                 // Clear all data
230                 this.clear();
231         }
232
233         /**
234          * Event observer for updated contact data by administrators
235          * <p>
236          * @param event Updated contact data event
237          */
238         public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent event) {
239                 // event should not be null
240                 if (null == event) {
241                         // Throw NPE
242                         throw new NullPointerException("event is null"); //NOI18N
243                 } else if (event.getUpdatedContact() == null) {
244                         // Throw NPE again
245                         throw new NullPointerException("event.updatedContact is null"); //NOI18N
246                 } else if (event.getUpdatedContact().getContactId() == null) {
247                         // userId is null
248                         throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
249                 } else if (event.getUpdatedContact().getContactId() < 1) {
250                         // Not avalid id
251                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
252                 }
253
254                 // Clear all data
255                 this.clear();
256         }
257
258         /**
259          * Observes events being fired when a bean helper has successfully created a
260          * fax number instance.
261          * <p>
262          * @param event Event being fired
263          */
264         public void afterCreatedFaxNumberEvent (@Observes final ObservableCreatedFaxNumberEvent event) {
265                 // The event instance must be valid
266                 if (null == event) {
267                         // Throw NPE
268                         throw new NullPointerException("event is null"); //NOI18N
269                 } else if (event.getFaxNumber() == null) {
270                         // Throw NPE again
271                         throw new NullPointerException("event.faxNumber is null"); //NOI18N
272                 } else if (event.getFaxNumber().getPhoneId() == null) {
273                         // Throw NPE yet again
274                         throw new NullPointerException("event.faxNumber.phoneId is null"); //NOI18N
275                 } else if (event.getFaxNumber().getPhoneId() < 1) {
276                         // Throw NPE yet again
277                         throw new NullPointerException(MessageFormat.format("event.faxNumber.phoneId={0} is invalid", event.getFaxNumber().getPhoneId())); //NOI18N
278                 }
279
280                 // Set it here
281                 this.setFaxNumber(event.getFaxNumber());
282         }
283
284         /**
285          * Observes events being fired when a bean helper has successfully created a
286          * land-line number instance.
287          * <p>
288          * @param event Event being fired
289          */
290         public void afterCreatedLandLineNumberEvent (@Observes final ObservableCreatedLandLineNumberEvent event) {
291                 // The event instance must be valid
292                 if (null == event) {
293                         // Throw NPE
294                         throw new NullPointerException("event is null"); //NOI18N
295                 } else if (event.getLandLineNumber() == null) {
296                         // Throw NPE again
297                         throw new NullPointerException("event.landLineNumber is null"); //NOI18N
298                 } else if (event.getLandLineNumber().getPhoneId() == null) {
299                         // Throw NPE yet again
300                         throw new NullPointerException("event.landLineNumber.phoneId is null"); //NOI18N
301                 } else if (event.getLandLineNumber().getPhoneId() < 1) {
302                         // Throw NPE yet again
303                         throw new NullPointerException(MessageFormat.format("event.landLineNumber.phoneId={0} is invalid", event.getLandLineNumber().getPhoneId())); //NOI18N
304                 }
305
306                 // Set it here
307                 this.setLandLineNumber(event.getLandLineNumber());
308         }
309
310         /**
311          * Observes events being fired when a bean helper has successfully created a
312          * mobile number instance.
313          * <p>
314          * @param event Event being fired
315          */
316         public void afterCreatedMobileNumberEvent (@Observes final ObservableCreatedMobileNumberEvent event) {
317                 // The event instance must be valid
318                 if (null == event) {
319                         // Throw NPE
320                         throw new NullPointerException("event is null"); //NOI18N
321                 } else if (event.getMobileNumber() == null) {
322                         // Throw NPE again
323                         throw new NullPointerException("event.mobileNumber is null"); //NOI18N
324                 } else if (event.getMobileNumber().getPhoneId() == null) {
325                         // Throw NPE yet again
326                         throw new NullPointerException("event.mobileNumber.phoneId is null"); //NOI18N
327                 } else if (event.getMobileNumber().getPhoneId() < 1) {
328                         // Throw NPE yet again
329                         throw new NullPointerException(MessageFormat.format("event.mobileNumber.phoneId={0} is invalid", event.getMobileNumber().getPhoneId())); //NOI18N
330                 }
331
332                 // Set it here
333                 this.setMobileNumber(event.getMobileNumber());
334         }
335
336         /**
337          * Getter for all contacts having current fax number linked
338          * <p>
339          * @return List of all linked contacts
340          */
341         public List<Contact> allCurrentFaxNumberContacts () {
342                 // Get id
343                 final DialableFaxNumber number = this.getFaxNumber();
344
345                 // Is cache there?
346                 if (this.contactsPhoneCache.containsKey(number)) {
347                         // Return cached version
348                         return this.contactsPhoneCache.get(number);
349                 } else {
350                         // Ask bean
351                         final List<Contact> list = new LinkedList<>();
352
353                         // "Walk" through all contacts
354                         for (final Contact contact : this.contactController.allContacts()) {
355                                 // Is mobile instance the same?
356                                 if (Objects.equals(contact.getContactFaxNumber(), number)) {
357                                         // Found one
358                                         list.add(contact);
359                                 }
360                         }
361
362                         // Store result in cache
363                         this.contactsPhoneCache.put(number, list);
364
365                         // Return now-cached list
366                         return list;
367                 }
368         }
369
370         /**
371          * Getter for all contacts having current land-line number linked
372          * <p>
373          * @return List of all linked contacts
374          */
375         public List<Contact> allCurrentLandLineNumberContacts () {
376                 // Get id
377                 final DialableLandLineNumber number = this.getLandLineNumber();
378
379                 // Is cache there?
380                 if (this.contactsPhoneCache.containsKey(number)) {
381                         // Return cached version
382                         return this.contactsPhoneCache.get(number);
383                 } else {
384                         // Ask bean
385                         final List<Contact> list = new LinkedList<>();
386
387                         // "Walk" through all contacts
388                         for (final Contact contact : this.contactController.allContacts()) {
389                                 // Is mobile instance the same?
390                                 if (Objects.equals(contact.getContactLandLineNumber(), number)) {
391                                         // Found one
392                                         list.add(contact);
393                                 }
394                         }
395
396                         // Store result in cache
397                         this.contactsPhoneCache.put(number, list);
398
399                         // Return now-cached list
400                         return list;
401                 }
402         }
403
404         /**
405          * Getter for all contacts having current mobile number linked
406          * <p>
407          * @return List of all linked contacts
408          */
409         public List<Contact> allCurrentMobileNumberContacts () {
410                 // Get id
411                 final DialableMobileNumber number = this.getMobileNumber();
412
413                 // Is cache there?
414                 if (this.contactsPhoneCache.containsKey(number)) {
415                         // Return cached version
416                         return this.contactsPhoneCache.get(number);
417                 } else {
418                         // Ask bean
419                         final List<Contact> list = new LinkedList<>();
420
421                         // "Walk" through all contacts
422                         for (final Contact contact : this.contactController.allContacts()) {
423                                 // Is mobile instance the same?
424                                 if (Objects.equals(contact.getContactMobileNumber(), number)) {
425                                         // Found one
426                                         list.add(contact);
427                                 }
428                         }
429
430                         // Store result in cache
431                         this.contactsPhoneCache.put(number, list);
432
433                         // Return now-cached list
434                         return list;
435                 }
436         }
437
438         /**
439          * Getter for chosen fax number
440          * <p>
441          * @return fax number
442          */
443         public DialableFaxNumber getFaxNumber () {
444                 return this.faxNumber;
445         }
446
447         /**
448          * Setter for chosen fax number
449          * <p>
450          * @param faxNumber fax number
451          */
452         public void setFaxNumber (final DialableFaxNumber faxNumber) {
453                 this.faxNumber = faxNumber;
454         }
455
456         /**
457          * Getter for chosen land-line number
458          * <p>
459          * @return land-line number
460          */
461         public DialableLandLineNumber getLandLineNumber () {
462                 return this.landLineNumber;
463         }
464
465         /**
466          * Setter for chosen land-line number
467          * <p>
468          * @param landLineNumber land-line number
469          */
470         public void setLandLineNumber (final DialableLandLineNumber landLineNumber) {
471                 this.landLineNumber = landLineNumber;
472         }
473
474         /**
475          * Getter for chosen mobile number
476          * <p>
477          * @return mobile number
478          */
479         public DialableMobileNumber getMobileNumber () {
480                 return this.mobileNumber;
481         }
482
483         /**
484          * Setter for chosen mobile number
485          * <p>
486          * @param mobileNumber mobile number
487          */
488         public void setMobileNumber (final DialableMobileNumber mobileNumber) {
489                 this.mobileNumber = mobileNumber;
490         }
491
492         /**
493          * Clears this bean
494          */
495         private void clear () {
496                 // Clear all data
497         }
498
499 }