]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/contact/list/JobsContactListWebViewBean.java
a5d65ee1e0cc88b5a156348436a6a1141721ae1d
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / contact / list / JobsContactListWebViewBean.java
1 /*
2  * Copyright (C) 2016 - 2022 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.jjobs.beans.contact.list;
18
19 import fish.payara.cdi.jsr107.impl.NamedCache;
20 import java.text.MessageFormat;
21 import java.util.Comparator;
22 import java.util.LinkedList;
23 import java.util.List;
24 import java.util.Objects;
25 import javax.annotation.PostConstruct;
26 import javax.cache.Cache;
27 import javax.ejb.EJB;
28 import javax.enterprise.event.Observes;
29 import javax.faces.view.ViewScoped;
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent;
33 import org.mxchange.jcontacts.events.contact.deleted.ObservableAdminDeletedContactEvent;
34 import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent;
35 import org.mxchange.jcontacts.events.contact.update.ObservableUpdatedContactEvent;
36 import org.mxchange.jcontacts.events.fax.unlinked.ObservableAdminUnlinkedFaxNumberEvent;
37 import org.mxchange.jcontacts.events.landline.unlinked.ObservableAdminUnlinkedLandLineNumberEvent;
38 import org.mxchange.jcontacts.events.mobile.unlinked.ObservableAdminUnlinkedMobileNumberEvent;
39 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
40 import org.mxchange.jcontacts.model.contact.Contact;
41 import org.mxchange.jcontacts.model.contact.ContactSessionBeanRemote;
42 import org.mxchange.jcontacts.model.contact.Contacts;
43 import org.mxchange.jjobs.beans.BaseJobsBean;
44 import org.mxchange.jphone.model.phonenumbers.DialableNumber;
45 import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
46 import org.mxchange.juserlogincore.events.confirmation.ObservableUserConfirmedAccountEvent;
47 import org.mxchange.juserlogincore.events.registration.ObservableUserRegisteredEvent;
48
49 /**
50  * A regular contact list bean (controller)
51  * <p>
52  * @author Roland Häder<roland@mxchange.org>
53  */
54 @Named ("contactListController")
55 @ViewScoped
56 public class JobsContactListWebViewBean extends BaseJobsBean implements JobsContactListWebViewController {
57
58         /**
59          * Serial number
60          */
61         private static final long serialVersionUID = 542_145_347_917L;
62
63         /**
64          * All contacts
65          */
66         private final List<Contact> allContacts;
67
68         /**
69          * Administrative contact EJB
70          */
71         @EJB (lookup = "java:global/jjobs-ejb/contact!org.mxchange.jcontacts.model.contact.ContactSessionBeanRemote")
72         private ContactSessionBeanRemote contactBean;
73
74         /**
75          * Contact list
76          */
77         @Inject
78         @NamedCache (cacheName = "contactCache")
79         private transient Cache<Long, Contact> contactCache;
80
81         /**
82          * A list of filtered contacts
83          */
84         private List<Contact> filteredContacts;
85
86         /**
87          * Current selected contact
88          */
89         private Contact selectedContact;
90
91         /**
92          * Default constructor
93          */
94         public JobsContactListWebViewBean () {
95                 // Call super constructor
96                 super();
97
98                 // Init list
99                 this.allContacts = new LinkedList<>();
100         }
101
102         /**
103          * Observes events being fired when an administrator has added a new
104          * contact.
105          * <p>
106          * @param event Event being fired
107          */
108         public void afterAdminAddedContactEvent (@Observes final ObservableAdminAddedContactEvent event) {
109                 // The event must be valid
110                 if (null == event) {
111                         // Throw NPE
112                         throw new NullPointerException("event is null"); //NOI18N
113                 } else if (event.getAddedContact() == null) {
114                         // Throw again ...
115                         throw new NullPointerException("event.addedContact is null"); //NOI18N
116                 } else if (event.getAddedContact().getContactId() == null) {
117                         // ... and again
118                         throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
119                 } else if (event.getAddedContact().getContactId() < 1) {
120                         // Not valid
121                         throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N
122                 }
123
124                 // Call other method
125                 this.uniqueAddContact(event.getAddedContact());
126         }
127
128         /**
129          * Event observer for when an administrator has deleted a contact
130          * <p>
131          * @param event Event being fired
132          */
133         public void afterAdminDeletedContactEvent (@Observes final ObservableAdminDeletedContactEvent event) {
134                 // Validate parameter
135                 if (null == event) {
136                         // Throw NPE
137                         throw new NullPointerException("event is null"); //NOI18N
138                 } else if (event.getDeletedContact() == null) {
139                         // Throw NPE again
140                         throw new NullPointerException("event.deletedContact is null"); //NOI18N
141                 } else if (event.getDeletedContact().getContactId() == null) {
142                         // Throw NPE again
143                         throw new NullPointerException("event.deletedContact.contactId is null"); //NOI18N
144                 } else if (event.getDeletedContact().getContactId() < 1) {
145                         // Throw IAE
146                         throw new IllegalArgumentException(MessageFormat.format("event.deletedContact.contactId={0} is not valid.", event.getDeletedContact().getContactId())); //NOI18N
147                 }
148
149                 // Delete from cache and list
150                 this.contactCache.remove(event.getDeletedContact().getContactId());
151                 this.getAllContacts().remove(event.getDeletedContact());
152         }
153
154         /**
155          * Event observer for unlinked fax contact by administrators
156          * <p>
157          * @param event Unlinked fax contact event
158          */
159         public void afterAdminUnlinkedFaxContactDataEvent (@Observes final ObservableAdminUnlinkedFaxNumberEvent event) {
160                 // Event and contained entity instance should not be null
161                 if (null == event) {
162                         // Throw NPE
163                         throw new NullPointerException("event is null"); //NOI18N
164                 } else if (event.getUnlinkedFaxNumber() == null) {
165                         // Throw NPE again
166                         throw new NullPointerException("event.unlinkedFaxNumber is null"); //NOI18N
167                 } else if (event.getUnlinkedFaxNumber().getPhoneId() == null) {
168                         // userId is null
169                         throw new NullPointerException("event.unlinkedFaxNumber.contactId is null"); //NOI18N
170                 } else if (event.getUnlinkedFaxNumber().getPhoneId() < 1) {
171                         // Not avalid id
172                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedFaxNumber(), event.getUnlinkedFaxNumber().getPhoneId())); //NOI18N
173                 }
174
175                 // Remove it from any entry
176                 for (final Cache.Entry<Long, Contact> entry : this.contactCache) {
177                         // Get contact instance from it
178                         final Contact contact = entry.getValue();
179
180                         // Is the number matching?
181                         if (Objects.equals(event.getUnlinkedFaxNumber(), contact.getContactFaxNumber())) {
182                                 // Yes, then unset it
183                                 contact.setContactFaxNumber(null);
184                         }
185                 }
186         }
187
188         /**
189          * Event observer for unlinked land-line contact by administrators
190          * <p>
191          * @param event Unlinked land-line contact event
192          */
193         public void afterAdminUnlinkedLandLineContactDataEvent (@Observes final ObservableAdminUnlinkedLandLineNumberEvent event) {
194                 // Event and contained entity instance should not be null
195                 if (null == event) {
196                         // Throw NPE
197                         throw new NullPointerException("event is null"); //NOI18N
198                 } else if (event.getUnlinkedLandLineNumber() == null) {
199                         // Throw NPE again
200                         throw new NullPointerException("event.unlinkedLandLineNumber is null"); //NOI18N
201                 } else if (event.getUnlinkedLandLineNumber().getPhoneId() == null) {
202                         // userId is null
203                         throw new NullPointerException("event.unlinkedLandLineNumber.contactId is null"); //NOI18N
204                 } else if (event.getUnlinkedLandLineNumber().getPhoneId() < 1) {
205                         // Not avalid id
206                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedLandLineNumber(), event.getUnlinkedLandLineNumber().getPhoneId())); //NOI18N
207                 }
208
209                 // Remove it from any entry
210                 for (final Cache.Entry<Long, Contact> entry : this.contactCache) {
211                         // Get contact instance from it
212                         final Contact contact = entry.getValue();
213
214                         // Is the number matching?
215                         if (Objects.equals(event.getUnlinkedLandLineNumber(), contact.getContactLandLineNumber())) {
216                                 // Yes, then unset it
217                                 contact.setContactLandLineNumber(null);
218                         }
219                 }
220         }
221
222         /**
223          * Event observer for unlinked mobile contact by administrators
224          * <p>
225          * @param event Unlinked mobile contact event
226          */
227         public void afterAdminUnlinkedMobileContactDataEvent (@Observes final ObservableAdminUnlinkedMobileNumberEvent event) {
228                 // Event and contained entity instance should not be null
229                 if (null == event) {
230                         // Throw NPE
231                         throw new NullPointerException("event is null"); //NOI18N
232                 } else if (event.getUnlinkedMobileNumber() == null) {
233                         // Throw NPE again
234                         throw new NullPointerException("event.unlinkedMobileNumber is null"); //NOI18N
235                 } else if (event.getUnlinkedMobileNumber().getMobileId() == null) {
236                         // userId is null
237                         throw new NullPointerException("event.unlinkedMobileNumber.contactId is null"); //NOI18N
238                 } else if (event.getUnlinkedMobileNumber().getMobileId() < 1) {
239                         // Not avalid id
240                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedMobileNumber(), event.getUnlinkedMobileNumber().getMobileId())); //NOI18N
241                 }
242
243                 // Remove it from any entry
244                 for (final Cache.Entry<Long, Contact> entry : this.contactCache) {
245                         // Get contact instance from it
246                         final Contact contact = entry.getValue();
247
248                         // Is the number matching?
249                         if (Objects.equals(event.getUnlinkedMobileNumber(), contact.getContactMobileNumber())) {
250                                 // Yes, then unset it
251                                 contact.setContactMobileNumber(null);
252                         }
253                 }
254         }
255
256         /**
257          * Event observer for updated contact data by administrators
258          * <p>
259          * @param event Updated contact data event
260          */
261         public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent event) {
262                 // Event and contained entity instance should not be null
263                 if (null == event) {
264                         // Throw NPE
265                         throw new NullPointerException("event is null"); //NOI18N
266                 } else if (event.getUpdatedContact() == null) {
267                         // Throw NPE again
268                         throw new NullPointerException("event.updatedContact is null"); //NOI18N
269                 } else if (event.getUpdatedContact().getContactId() == null) {
270                         // userId is null
271                         throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
272                 } else if (event.getUpdatedContact().getContactId() < 1) {
273                         // Not avalid id
274                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
275                 }
276
277                 // Add contact instance only once
278                 this.uniqueAddContact(event.getUpdatedContact());
279         }
280
281         /**
282          * Event observer for updated contact data by the user
283          * <p>
284          * @param event Updated contact data event
285          */
286         public void afterUpdatedContactDataEvent (@Observes final ObservableUpdatedContactEvent event) {
287                 // Event and contained entity instance should not be null
288                 if (null == event) {
289                         // Throw NPE
290                         throw new NullPointerException("event is null"); //NOI18N
291                 } else if (event.getUpdatedContact() == null) {
292                         // Throw NPE again
293                         throw new NullPointerException("event.updatedContact is null"); //NOI18N
294                 } else if (event.getUpdatedContact().getContactId() == null) {
295                         // userId is null
296                         throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
297                 } else if (event.getUpdatedContact().getContactId() < 1) {
298                         // Not avalid id
299                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
300                 }
301
302                 // Add contact instance only once
303                 this.uniqueAddContact(event.getUpdatedContact());
304         }
305
306         /**
307          * Event observer when user confirmed account.
308          * <p>
309          * @param event Event being fired
310          */
311         public void afterUserConfirmedAccount (@Observes final ObservableUserConfirmedAccountEvent event) {
312                 // Event and contained entity instance should not be null
313                 if (null == event) {
314                         // Throw NPE
315                         throw new NullPointerException("event is null"); //NOI18N
316                 } else if (event.getConfirmedUser() == null) {
317                         // Throw NPE again
318                         throw new NullPointerException("event.confirmedUser is null"); //NOI18N
319                 } else if (event.getConfirmedUser().getUserId() == null) {
320                         // userId is null
321                         throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
322                 } else if (event.getConfirmedUser().getUserId() < 1) {
323                         // Not avalid id
324                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
325                 }
326
327                 // Add contact instance only once
328                 this.uniqueAddContact(event.getConfirmedUser().getUserContact());
329         }
330
331         /**
332          * Event observer for new user registrations
333          * <p>
334          * @param event User registration event
335          */
336         public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) {
337                 // Event and contained entity instance should not be null
338                 if (null == event) {
339                         // Throw NPE
340                         throw new NullPointerException("event is null"); //NOI18N
341                 } else if (event.getRegisteredUser() == null) {
342                         // Throw NPE again
343                         throw new NullPointerException("event.registeredUser is null"); //NOI18N
344                 } else if (event.getRegisteredUser().getUserId() == null) {
345                         // userId is null
346                         throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
347                 } else if (event.getRegisteredUser().getUserId() < 1) {
348                         // Not avalid id
349                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
350                 }
351
352                 // Get user instance
353                 final Contact registeredContact = event.getRegisteredUser().getUserContact();
354
355                 // Add contact instance only once
356                 this.uniqueAddContact(registeredContact);
357         }
358
359         @Override
360         public Contact findContactById (final Long contactId) throws ContactNotFoundException {
361                 // Validate parameter
362                 if (null == contactId) {
363                         // Throw NPE
364                         throw new NullPointerException("contactId is null"); //NOI18N
365                 } else if (contactId < 1) {
366                         // Throw IAE
367                         throw new IllegalArgumentException(MessageFormat.format("contactId={0} is invalid", contactId)); //NOI18N
368                 } else if (!this.contactCache.containsKey(contactId)) {
369                         // Not found
370                         throw new ContactNotFoundException(contactId);
371                 }
372
373                 // Get it from cache
374                 final Contact contact = this.contactCache.get(contactId);
375
376                 // Return it
377                 return contact;
378         }
379
380         /**
381          * Returns a text representation of given mobile number or null if not set.
382          * <p>
383          * @param mobileNumber Mobile number
384          * <p>
385          * @return Text representation or null
386          */
387         public String generateMobileNumber (final DialableMobileNumber mobileNumber) {
388                 // Is it null?
389                 if (null == mobileNumber) {
390                         // Return null
391                         return null;
392                 }
393
394                 // Get all data
395                 final String number = String.format(
396                                          "%s%d%d", //NOI18N
397                                          mobileNumber.getMobileProvider().getProviderCountry().getCountryExternalDialPrefix(),
398                                          mobileNumber.getMobileProvider().getProviderDialPrefix(),
399                                          mobileNumber.getMobileNumber()
400                          );
401
402                 // Return it
403                 return number;
404         }
405
406         /**
407          * Returns a text representation of given land-line or fax number or null if
408          * not set.
409          * <p>
410          * @param phoneNumber Land-line or fax number
411          * <p>
412          * @return Text representation or null
413          */
414         public String generatePhoneNumber (final DialableNumber phoneNumber) {
415                 // Is it null?
416                 if (null == phoneNumber) {
417                         // Return null
418                         return null;
419                 }
420
421                 // Generate it
422                 final String number = String.format(
423                                          "%s%d%d", //NOI18N
424                                          phoneNumber.getPhoneCountry().getCountryExternalDialPrefix(),
425                                          phoneNumber.getPhoneAreaCode(),
426                                          phoneNumber.getPhoneNumber()
427                          );
428
429                 // Return it
430                 return number;
431         }
432
433         @Override
434         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
435         public List<Contact> getAllContacts () {
436                 return this.allContacts;
437         }
438
439         /**
440          * Getter for filtered contacts
441          * <p>
442          * @return
443          */
444         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
445         public List<Contact> getFilteredContacts () {
446                 return this.filteredContacts;
447         }
448
449         /**
450          * Setter for filtered contacts
451          * <p>
452          * @param filteredContacts Filtered contacts list
453          */
454         @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
455         public void setFilteredContacts (final List<Contact> filteredContacts) {
456                 this.filteredContacts = filteredContacts;
457         }
458
459         /**
460          * Getter for selected contact
461          * <p>
462          * @return Selected Contact instance
463          */
464         public Contact getSelectedContact () {
465                 return this.selectedContact;
466         }
467
468         /**
469          * Setter for selected contact
470          * <p>
471          * @param selectedContact
472          */
473         public void setSelectedContact (final Contact selectedContact) {
474                 this.selectedContact = selectedContact;
475         }
476
477         /**
478          * Post-construction method
479          */
480         @PostConstruct
481         public void initializeList () {
482                 // Is cache there?
483                 if (!this.contactCache.iterator().hasNext()) {
484                         // Add all
485                         for (final Contact contact : this.contactBean.fetchAllContacts()) {
486                                 // Add it to cache
487                                 this.contactCache.put(contact.getContactId(), contact);
488                         }
489                 }
490
491                 // Is cache there and list is not full?
492                 if ((this.getAllContacts().isEmpty()) && (this.contactCache.iterator().hasNext())) {
493                         // Build up list
494                         for (final Cache.Entry<Long, Contact> currentEntry : this.contactCache) {
495                                 // Add to list
496                                 this.getAllContacts().add(currentEntry.getValue());
497                         }
498
499                         // Sort list
500                         this.getAllContacts().sort(new Comparator<Contact>() {
501                                 @Override
502                                 public int compare (final Contact contact1, final Contact contact2) {
503                                         return contact1.getContactId() > contact2.getContactId() ? 1 : contact1.getContactId() < contact2.getContactId() ? -1 : 0;
504                                 }
505                         });
506
507                         // Set full list
508                         this.setFilteredContacts(this.getAllContacts());
509                 }
510         }
511
512         @Override
513         public boolean isContactFound (final Contact contact) {
514                 // Default is not found
515                 boolean IsFound = false;
516
517                 // Loop through all
518                 for (final Contact currentContact : this.getAllContacts()) {
519                         // Is the same?
520                         if (Contacts.isSameContact(contact, currentContact)) {
521                                 // Yes, then abort loop
522                                 IsFound = false;
523                                 break;
524                         }
525                 }
526
527                 // Return status
528                 return IsFound;
529         }
530
531         /**
532          * Removes given contact from all lists
533          * <p>
534          * @param contact Contact instance to remove
535          */
536         private void removeContact (final Contact contact) {
537                 // Remove from general list
538                 this.contactCache.remove(contact.getContactId());
539         }
540
541         /**
542          * Adds unique instance to contact list. First any existing instance is
543          * being removed, then the new instance is added.
544          * <p>
545          * @param contact Contact instance to add uniquely
546          */
547         private void uniqueAddContact (final Contact contact) {
548                 // "Walk" through all entries
549                 for (final Cache.Entry<Long, Contact> currentEntry : this.contactCache) {
550                         // Is id number the same?
551                         if (Objects.equals(contact.getContactId(), currentEntry.getKey())) {
552                                 // Found entry, so remove it and abort
553                                 this.removeContact(currentEntry.getValue());
554                                 break;
555                         }
556                 }
557
558                 // Add contact to list
559                 this.contactCache.put(contact.getContactId(), contact);
560         }
561
562 }