]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/phone/PizzaPhoneWebRequestBean.java
Updated copyright year
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / phone / PizzaPhoneWebRequestBean.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.phone;
18
19 import fish.payara.cdi.jsr107.impl.NamedCache;
20 import java.text.MessageFormat;
21 import java.util.Iterator;
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.context.RequestScoped;
29 import javax.enterprise.event.Observes;
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.update.ObservableAdminUpdatedContactEvent;
34 import org.mxchange.jcontacts.events.fax.linked.ObservableAdminLinkedFaxNumberEvent;
35 import org.mxchange.jcontacts.events.landline.linked.ObservableAdminLinkedLandLineNumberEvent;
36 import org.mxchange.jcontacts.events.mobile.linked.ObservableAdminLinkedMobileNumberEvent;
37 import org.mxchange.jcontacts.model.contact.Contact;
38 import org.mxchange.jphone.events.fax.deleted.AdminDeletedFaxNumberEvent;
39 import org.mxchange.jphone.events.fax.updated.AdminUpdatedFaxNumberEvent;
40 import org.mxchange.jphone.events.landline.deleted.AdminDeletedLandLineNumberEvent;
41 import org.mxchange.jphone.events.landline.updated.AdminUpdatedLandLineNumberEvent;
42 import org.mxchange.jphone.events.mobile.deleted.AdminDeletedMobileNumberEvent;
43 import org.mxchange.jphone.events.mobile.updated.AdminUpdatedMobileNumberEvent;
44 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
45 import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
46 import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
47 import org.mxchange.jphone.model.phonenumbers.phone.PhoneSessionBeanRemote;
48 import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
49 import org.mxchange.pizzaapplication.beans.BasePizzaBean;
50
51 /**
52  * Regular controller (bean) for phone numbers
53  * <p>
54  * @author Roland Häder<roland@mxchange.org>
55  */
56 @Named ("phoneController")
57 @RequestScoped
58 public class PizzaPhoneWebRequestBean extends BasePizzaBean implements PizzaPhoneWebRequestController {
59
60         /**
61          * Serial number
62          */
63         private static final long serialVersionUID = 491_058_674_675_690_105L;
64
65         /**
66          * All fax numbers
67          */
68         @Inject
69         @NamedCache (cacheName = "faxNumberCache")
70         private Cache<Long, DialableFaxNumber> faxNumberCache;
71
72         /**
73          * All land-line numbers
74          */
75         @Inject
76         @NamedCache (cacheName = "landLineNumberCache")
77         private Cache<Long, DialableLandLineNumber> landLineNumberCache;
78
79         /**
80          * All mobile numbers
81          */
82         @Inject
83         @NamedCache (cacheName = "mobileNumberCache")
84         private Cache<Long, DialableMobileNumber> mobileNumberCache;
85
86         /**
87          * General EJB for phone numbers
88          */
89         @EJB (lookup = "java:global/pizzaservice-ejb/phone!org.mxchange.jphone.model.phonenumbers.phone.PhoneSessionBeanRemote")
90         private PhoneSessionBeanRemote phoneBean;
91
92         /**
93          * Default constructor
94          */
95         public PizzaPhoneWebRequestBean () {
96                 // Call super constructor
97                 super();
98         }
99
100         /**
101          * Observes events being fired when an administrator has added a new
102          * contact.
103          * <p>
104          * @param event Event being fired
105          */
106         public void afterAdminAddedContactEvent (@Observes final ObservableAdminAddedContactEvent event) {
107                 // The event must be valid
108                 if (null == event) {
109                         // Throw NPE
110                         throw new NullPointerException("event is null"); //NOI18N
111                 } else if (event.getAddedContact() == null) {
112                         // Throw again ...
113                         throw new NullPointerException("event.addedContact is null"); //NOI18N
114                 } else if (event.getAddedContact().getContactId() == null) {
115                         // ... and again
116                         throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
117                 } else if (event.getAddedContact().getContactId() < 1) {
118                         // Not valid
119                         throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N
120                 }
121
122                 // Update contact's mobile, land-line and fax number
123                 this.updateContactPhoneNumbers(event.getAddedContact());
124
125                 // Clear this bean
126                 this.clear();
127         }
128
129         /**
130          * Event observer for newly added users by administrator
131          * <p>
132          * @param event Event being fired
133          */
134         public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
135                 // event should not be null
136                 if (null == event) {
137                         // Throw NPE
138                         throw new NullPointerException("event is null"); //NOI18N
139                 } else if (event.getAddedUser() == null) {
140                         // Throw NPE again
141                         throw new NullPointerException("event.addedUser is null"); //NOI18N
142                 } else if (event.getAddedUser().getUserId() == null) {
143                         // userId is null
144                         throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
145                 } else if (event.getAddedUser().getUserId() < 1) {
146                         // Not avalid id
147                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
148                 }
149
150                 // Update contact's mobile, land-line and fax number
151                 this.updateContactPhoneNumbers(event.getAddedUser().getUserContact());
152
153                 // Clear all data
154                 this.clear();
155         }
156
157         /**
158          * Observes events being fired when an administrator has deleted a fax
159          * number
160          * <p>
161          * @param event Event being fired
162          */
163         public void afterAdminDeletedFaxNumberEvent (@Observes final AdminDeletedFaxNumberEvent event) {
164                 // event should not be null
165                 if (null == event) {
166                         // Throw NPE
167                         throw new NullPointerException("event is null"); //NOI18N
168                 } else if (event.getDeletedFaxNumber() == null) {
169                         // Throw NPE again
170                         throw new NullPointerException("event.deletedFaxNumber is null"); //NOI18N
171                 } else if (event.getDeletedFaxNumber().getPhoneId() == null) {
172                         // userId is null
173                         throw new NullPointerException("event.deletedFaxNumber.phoneId is null"); //NOI18N
174                 } else if (event.getDeletedFaxNumber().getPhoneId() < 1) {
175                         // Not avalid id
176                         throw new IllegalArgumentException(MessageFormat.format("phoneId of contact={0} is not valid: {1}", event.getDeletedFaxNumber(), event.getDeletedFaxNumber().getPhoneId())); //NOI18N
177                 }
178
179                 // Update contact's mobile, land-line and fax number
180                 this.allFaxNumbers().remove(event.getDeletedFaxNumber());
181
182                 // Clear all data
183                 this.clear();
184         }
185
186         /**
187          * Observes events being fired when an administrator has deleted a land-line
188          * number
189          * <p>
190          * @param event Event being fired
191          */
192         public void afterAdminDeletedLandLineNumberEvent (@Observes final AdminDeletedLandLineNumberEvent event) {
193                 // event should not be null
194                 if (null == event) {
195                         // Throw NPE
196                         throw new NullPointerException("event is null"); //NOI18N
197                 } else if (event.getDeletedLandLineNumber() == null) {
198                         // Throw NPE again
199                         throw new NullPointerException("event.deletedLandLineNumber is null"); //NOI18N
200                 } else if (event.getDeletedLandLineNumber().getPhoneId() == null) {
201                         // userId is null
202                         throw new NullPointerException("event.deletedLandLineNumber.phoneId is null"); //NOI18N
203                 } else if (event.getDeletedLandLineNumber().getPhoneId() < 1) {
204                         // Not avalid id
205                         throw new IllegalArgumentException(MessageFormat.format("phoneId of contact={0} is not valid: {1}", event.getDeletedLandLineNumber(), event.getDeletedLandLineNumber().getPhoneId())); //NOI18N
206                 }
207
208                 // Update contact's mobile, land-line and fax number
209                 this.allLandLineNumbers().remove(event.getDeletedLandLineNumber());
210
211                 // Clear all data
212                 this.clear();
213         }
214
215         /**
216          * Observes events being fired when an administrator has deleted a mobile
217          * number
218          * <p>
219          * @param event Event being fired
220          */
221         public void afterAdminDeletedMobileNumberEvent (@Observes final AdminDeletedMobileNumberEvent event) {
222                 // event should not be null
223                 if (null == event) {
224                         // Throw NPE
225                         throw new NullPointerException("event is null"); //NOI18N
226                 } else if (event.getDeletedMobileNumber() == null) {
227                         // Throw NPE again
228                         throw new NullPointerException("event.deletedMobileNumber is null"); //NOI18N
229                 } else if (event.getDeletedMobileNumber().getPhoneId() == null) {
230                         // userId is null
231                         throw new NullPointerException("event.deletedMobileNumber.phoneId is null"); //NOI18N
232                 } else if (event.getDeletedMobileNumber().getPhoneId() < 1) {
233                         // Not avalid id
234                         throw new IllegalArgumentException(MessageFormat.format("phoneId of contact={0} is not valid: {1}", event.getDeletedMobileNumber(), event.getDeletedMobileNumber().getPhoneId())); //NOI18N
235                 }
236
237                 // Update contact's mobile, land-line and fax number
238                 this.allMobileNumbers().remove(event.getDeletedMobileNumber());
239
240                 // Clear all data
241                 this.clear();
242         }
243
244         /**
245          * Observes events being fired when an administrator has a linked a fax
246          * number
247          * <p>
248          * @param event Event being fired
249          */
250         public void afterAdminLinkedFaxNumberEvent (@Observes final ObservableAdminLinkedFaxNumberEvent event) {
251                 // Is the event fine?
252                 if (event == null) {
253                         // Throw NPE
254                         throw new NullPointerException("event is null"); //NOI18N
255                 } else if (event.getContact() == null) {
256                         // Throw again ...
257                         throw new NullPointerException("event.contact is null");
258                 } else if (event.getContact().getContactId() == null) {
259                         // Throw again ...
260                         throw new NullPointerException("event.contact.contactId is null");
261                 } else if (event.getContact().getContactId() < 1) {
262                         // Throw again ...
263                         throw new NullPointerException("event.contact.contactId=" + event.getContact().getContactId() + " is invalid");
264                 } else if (event.getContact().getContactFaxNumber() == null) {
265                         // Throw again ...
266                         throw new NullPointerException("event.contact.contactFaxNumber is null");
267                 } else if (event.getContact().getContactFaxNumber().getPhoneId() == null) {
268                         // Throw again ...
269                         throw new NullPointerException("event.contact.contactFaxNumber.phoneId is null");
270                 } else if (event.getContact().getContactFaxNumber().getPhoneId() < 1) {
271                         // Throw again ...
272                         throw new NullPointerException("event.contact.contactFaxNumber.phoneId=" + event.getContact().getContactFaxNumber().getPhoneId() + " is invalid");
273                 } else if (event.getLinkedFaxNumber() == null) {
274                         // Throw again ...
275                         throw new NullPointerException("event.linkedFaxNumer is null");
276                 }
277
278                 // Is the id number in linked number not set?
279                 if (event.getLinkedFaxNumber().getPhoneId() == null) {
280                         // Then it is a new number, so add it from contact as there the id number has been set
281                         this.uniqueAddFaxNumber(event.getContact().getContactFaxNumber());
282                 }
283         }
284
285         /**
286          * Observes events being fired when an administrator has a linked a
287          * land-line number
288          * <p>
289          * @param event Event being fired
290          */
291         public void afterAdminLinkedLandLineNumberEvent (@Observes final ObservableAdminLinkedLandLineNumberEvent event) {
292                 // Is the event fine?
293                 if (event == null) {
294                         // Throw NPE
295                         throw new NullPointerException("event is null"); //NOI18N
296                 } else if (event.getContact() == null) {
297                         // Throw again ...
298                         throw new NullPointerException("event.contact is null");
299                 } else if (event.getContact().getContactId() == null) {
300                         // Throw again ...
301                         throw new NullPointerException("event.contact.contactId is null");
302                 } else if (event.getContact().getContactId() < 1) {
303                         // Throw again ...
304                         throw new NullPointerException("event.contact.contactId=" + event.getContact().getContactId() + " is invalid");
305                 } else if (event.getContact().getContactLandLineNumber() == null) {
306                         // Throw again ...
307                         throw new NullPointerException("event.contact.contactLandLineNumber is null");
308                 } else if (event.getContact().getContactLandLineNumber().getPhoneId() == null) {
309                         // Throw again ...
310                         throw new NullPointerException("event.contact.contactLandLineNumber.phoneId is null");
311                 } else if (event.getContact().getContactLandLineNumber().getPhoneId() < 1) {
312                         // Throw again ...
313                         throw new NullPointerException("event.contact.contactLandLineNumber.phoneId=" + event.getContact().getContactLandLineNumber().getPhoneId() + " is invalid");
314                 } else if (event.getLinkedLandLineNumber() == null) {
315                         // Throw again ...
316                         throw new NullPointerException("event.linkedLandLineNumer is null");
317                 }
318
319                 // Is the id number in linked number not set?
320                 if (event.getLinkedLandLineNumber().getPhoneId() == null) {
321                         // Then it is a new number, so add it from contact as there the id number has been set
322                         this.uniqueAddLandLineNumber(event.getContact().getContactLandLineNumber());
323                 }
324         }
325
326         /**
327          * Observes events being fired when an administrator has a linked a mobile
328          * number
329          * <p>
330          * @param event Event being fired
331          */
332         public void afterAdminLinkedMobileNumberEvent (@Observes final ObservableAdminLinkedMobileNumberEvent event) {
333                 // Is the event fine?
334                 if (event == null) {
335                         // Throw NPE
336                         throw new NullPointerException("event is null"); //NOI18N
337                 } else if (event.getContact() == null) {
338                         // Throw again ...
339                         throw new NullPointerException("event.contact is null");
340                 } else if (event.getContact().getContactId() == null) {
341                         // Throw again ...
342                         throw new NullPointerException("event.contact.contactId is null");
343                 } else if (event.getContact().getContactId() < 1) {
344                         // Throw again ...
345                         throw new NullPointerException("event.contact.contactId=" + event.getContact().getContactId() + " is invalid");
346                 } else if (event.getContact().getContactMobileNumber() == null) {
347                         // Throw again ...
348                         throw new NullPointerException("event.contact.contactMobileNumber is null");
349                 } else if (event.getContact().getContactMobileNumber().getPhoneId() == null) {
350                         // Throw again ...
351                         throw new NullPointerException("event.contact.contactMobileNumber.phoneId is null");
352                 } else if (event.getContact().getContactMobileNumber().getPhoneId() < 1) {
353                         // Throw again ...
354                         throw new NullPointerException("event.contact.contactMobileNumber.phoneId=" + event.getContact().getContactMobileNumber().getPhoneId() + " is invalid");
355                 } else if (event.getLinkedMobileNumber() == null) {
356                         // Throw again ...
357                         throw new NullPointerException("event.linkedMobileNumer is null");
358                 }
359
360                 // Is the id number in linked number not set?
361                 if (event.getLinkedMobileNumber().getPhoneId() == null) {
362                         // Then it is a new number, so add it from contact as there the id number has been set
363                         this.uniqueAddMobileNumber(event.getContact().getContactMobileNumber());
364                 }
365         }
366
367         /**
368          * Observes events being fired when an administrator has updated contact
369          * data.
370          * <p>
371          * @param event Event being fired
372          */
373         public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent event) {
374                 // event should not be null
375                 if (null == event) {
376                         // Throw NPE
377                         throw new NullPointerException("event is null"); //NOI18N
378                 } else if (event.getUpdatedContact() == null) {
379                         // Throw NPE again
380                         throw new NullPointerException("event.updatedContact is null"); //NOI18N
381                 } else if (event.getUpdatedContact().getContactId() == null) {
382                         // userId is null
383                         throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
384                 } else if (event.getUpdatedContact().getContactId() < 1) {
385                         // Not avalid id
386                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
387                 }
388
389                 // Update contact's mobile, land-line and fax number
390                 this.updateContactPhoneNumbers(event.getUpdatedContact());
391
392                 // Clear all data
393                 this.clear();
394         }
395
396         /**
397          * Observes events being fired when an administrator has updated a fax
398          * number.
399          * <p>
400          * @param event Event being fired
401          */
402         public void afterAdminUpdatedFaxNumberEvent (@Observes final AdminUpdatedFaxNumberEvent event) {
403                 // event should not be null
404                 if (null == event) {
405                         // Throw NPE
406                         throw new NullPointerException("event is null"); //NOI18N
407                 } else if (event.getUpdatedFaxNumber() == null) {
408                         // Throw NPE again
409                         throw new NullPointerException("event.updatedFaxNumber is null"); //NOI18N
410                 } else if (event.getUpdatedFaxNumber().getPhoneId() == null) {
411                         // userId is null
412                         throw new NullPointerException("event.updatedFaxNumber.phoneId is null"); //NOI18N
413                 } else if (event.getUpdatedFaxNumber().getPhoneId() < 1) {
414                         // Not avalid id
415                         throw new IllegalArgumentException(MessageFormat.format("phoneId of contact={0} is not valid: {1}", event.getUpdatedFaxNumber(), event.getUpdatedFaxNumber().getPhoneId())); //NOI18N
416                 }
417
418                 // Uniquely add it
419                 this.uniqueAddFaxNumber(event.getUpdatedFaxNumber());
420
421                 // Clear it
422                 this.clear();
423         }
424
425         /**
426          * Observes events being fired when an administrator has updated a land-line
427          * number.
428          * <p>
429          * @param event Event being fired
430          */
431         public void afterAdminUpdatedLandLineNumberEvent (@Observes final AdminUpdatedLandLineNumberEvent event) {
432                 // event should not be null
433                 if (null == event) {
434                         // Throw NPE
435                         throw new NullPointerException("event is null"); //NOI18N
436                 } else if (event.getUpdatedLandLineNumber() == null) {
437                         // Throw NPE again
438                         throw new NullPointerException("event.updatedLandLineNumber is null"); //NOI18N
439                 } else if (event.getUpdatedLandLineNumber().getPhoneId() == null) {
440                         // userId is null
441                         throw new NullPointerException("event.updatedLandLineNumber.phoneId is null"); //NOI18N
442                 } else if (event.getUpdatedLandLineNumber().getPhoneId() < 1) {
443                         // Not avalid id
444                         throw new IllegalArgumentException(MessageFormat.format("phoneId of contact={0} is not valid: {1}", event.getUpdatedLandLineNumber(), event.getUpdatedLandLineNumber().getPhoneId())); //NOI18N
445                 }
446
447                 // Uniquely add it
448                 this.uniqueAddLandLineNumber(event.getUpdatedLandLineNumber());
449
450                 // Clear it
451                 this.clear();
452         }
453
454         /**
455          * Observes events being fired when an administrator has updated a mobile
456          * number.
457          * <p>
458          * @param event Event being fired
459          */
460         public void afterAdminUpdatedMobileNumberEvent (@Observes final AdminUpdatedMobileNumberEvent event) {
461                 // event should not be null
462                 if (null == event) {
463                         // Throw NPE
464                         throw new NullPointerException("event is null"); //NOI18N
465                 } else if (event.getUpdatedMobileNumber() == null) {
466                         // Throw NPE again
467                         throw new NullPointerException("event.updatedMobileNumber is null"); //NOI18N
468                 } else if (event.getUpdatedMobileNumber().getPhoneId() == null) {
469                         // userId is null
470                         throw new NullPointerException("event.updatedMobileNumber.phoneId is null"); //NOI18N
471                 } else if (event.getUpdatedMobileNumber().getPhoneId() < 1) {
472                         // Not avalid id
473                         throw new IllegalArgumentException(MessageFormat.format("phoneId of contact={0} is not valid: {1}", event.getUpdatedMobileNumber(), event.getUpdatedMobileNumber().getPhoneId())); //NOI18N
474                 }
475
476                 // Uniquely add it
477                 this.uniqueAddMobileNumber(event.getUpdatedMobileNumber());
478
479                 // Clear it
480                 this.clear();
481         }
482
483         @Override
484         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
485         public List<DialableFaxNumber> allFaxNumbers () {
486                 // Init list
487                 final List<DialableFaxNumber> list = new LinkedList<>();
488
489                 // Get iterator
490                 final Iterator<Cache.Entry<Long, DialableFaxNumber>> iterator = this.faxNumberCache.iterator();
491
492                 // Loop over all
493                 while (iterator.hasNext()) {
494                         // Get next entry
495                         final Cache.Entry<Long, DialableFaxNumber> next = iterator.next();
496
497                         // Add value to list
498                         list.add(next.getValue());
499                 }
500
501                 // Return it
502                 return list;
503         }
504
505         @Override
506         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
507         public List<DialableLandLineNumber> allLandLineNumbers () {
508                 // Init list
509                 final List<DialableLandLineNumber> list = new LinkedList<>();
510
511                 // Get iterator
512                 final Iterator<Cache.Entry<Long, DialableLandLineNumber>> iterator = this.landLineNumberCache.iterator();
513
514                 // Loop over all
515                 while (iterator.hasNext()) {
516                         // Get next entry
517                         final Cache.Entry<Long, DialableLandLineNumber> next = iterator.next();
518
519                         // Add value to list
520                         list.add(next.getValue());
521                 }
522
523                 // Return it
524                 return list;
525         }
526
527         @Override
528         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
529         public List<DialableMobileNumber> allMobileNumbers () {
530                 // Init list
531                 final List<DialableMobileNumber> list = new LinkedList<>();
532
533                 // Get iterator
534                 final Iterator<Cache.Entry<Long, DialableMobileNumber>> iterator = this.mobileNumberCache.iterator();
535
536                 // Loop over all
537                 while (iterator.hasNext()) {
538                         // Get next entry
539                         final Cache.Entry<Long, DialableMobileNumber> next = iterator.next();
540
541                         // Add value to list
542                         list.add(next.getValue());
543                 }
544
545                 // Return it
546                 return list;
547         }
548
549         /**
550          * Post-construction method
551          */
552         @PostConstruct
553         public void init () {
554                 // Is cache there?
555                 if (!this.faxNumberCache.iterator().hasNext()) {
556                         // Get whole list
557                         final List<DialableFaxNumber> list = this.phoneBean.allFaxNumbers();
558
559                         // Add all
560                         for (final Iterator<DialableFaxNumber> iterator = list.iterator(); iterator.hasNext();) {
561                                 // Get next element
562                                 final DialableFaxNumber next = iterator.next();
563
564                                 // Add it to cache
565                                 this.faxNumberCache.put(next.getPhoneId(), next);
566                         }
567                 }
568
569                 // Is cache there?
570                 if (!this.landLineNumberCache.iterator().hasNext()) {
571                         // Get whole list
572                         final List<DialableLandLineNumber> list = this.phoneBean.allLandLineNumbers();
573
574                         // Add all
575                         for (final Iterator<DialableLandLineNumber> iterator = list.iterator(); iterator.hasNext();) {
576                                 // Get next element
577                                 final DialableLandLineNumber next = iterator.next();
578
579                                 // Add it to cache
580                                 this.landLineNumberCache.put(next.getPhoneId(), next);
581                         }
582                 }
583
584                 // Is cache there?
585                 if (!this.mobileNumberCache.iterator().hasNext()) {
586                         // Get whole list
587                         final List<DialableMobileNumber> list = this.phoneBean.allMobileNumbers();
588
589                         // Add all
590                         for (final Iterator<DialableMobileNumber> iterator = list.iterator(); iterator.hasNext();) {
591                                 // Get next element
592                                 final DialableMobileNumber next = iterator.next();
593
594                                 // Add it to cache
595                                 this.mobileNumberCache.put(next.getPhoneId(), next);
596                         }
597                 }
598         }
599
600         /**
601          * Clears this bean
602          */
603         private void clear () {
604                 // Clear all data
605         }
606
607         /**
608          * Uniquely add given fax number to this bean's list. First remove the old
609          * instance (by id number), then re-add it again.
610          * <p>
611          * @param faxNumber number to add
612          */
613         private void uniqueAddFaxNumber (final DialableFaxNumber faxNumber) {
614                 // Make sure the parameter is valid
615                 if (null == faxNumber) {
616                         // Throw NPE
617                         throw new NullPointerException("faxNumber is null");
618                 } else if (faxNumber.getPhoneId() == null) {
619                         // Throw again ...
620                         throw new NullPointerException("faxNumber.phoneId is null");
621                 } else if (faxNumber.getPhoneId() < 1) {
622                         // Not valid
623                         throw new IllegalArgumentException(MessageFormat.format("faxNumber.phoneId={0} is not valid.", faxNumber.getPhoneId()));
624                 }
625
626                 // First remove it
627                 if (!this.allFaxNumbers().remove(faxNumber)) {
628                         // Did not work, try by id number
629                         for (final DialableFaxNumber fax : this.allFaxNumbers()) {
630                                 // Is id number the same?
631                                 if (Objects.equals(fax.getPhoneId(), faxNumber.getPhoneId())) {
632                                         // Found it
633                                         this.allFaxNumbers().remove(fax);
634                                         break;
635                                 }
636                         }
637                 }
638
639                 // ... then add it
640                 this.allFaxNumbers().add(faxNumber);
641         }
642
643         /**
644          * Uniquely add given land-line number to this bean's list. First remove the
645          * old instance (by id number), then re-add it again.
646          * <p>
647          * @param landLineNumber Land-line number to add
648          */
649         private void uniqueAddLandLineNumber (final DialableLandLineNumber landLineNumber) {
650                 // Make sure the parameter is valid
651                 if (null == landLineNumber) {
652                         // Throw NPE
653                         throw new NullPointerException("landLineNumber is null");
654                 } else if (landLineNumber.getPhoneId() == null) {
655                         // Throw again ...
656                         throw new NullPointerException("landLineNumber.phoneId is null");
657                 } else if (landLineNumber.getPhoneId() < 1) {
658                         // Not valid
659                         throw new IllegalArgumentException(MessageFormat.format("landLineNumber.phoneId={0} is not valid.", landLineNumber.getPhoneId()));
660                 }
661
662                 // First remove it
663                 if (!this.allLandLineNumbers().remove(landLineNumber)) {
664                         // Did not work, try by id number
665                         for (final DialableLandLineNumber landLine : this.allLandLineNumbers()) {
666                                 // Is id number the same?
667                                 if (Objects.equals(landLine.getPhoneId(), landLineNumber.getPhoneId())) {
668                                         // Found it
669                                         this.allLandLineNumbers().remove(landLine);
670                                         break;
671                                 }
672                         }
673                 }
674
675                 // ... then add it
676                 this.allLandLineNumbers().add(landLineNumber);
677         }
678
679         /**
680          * Uniquely add given mobile number to this bean's list. First remove the
681          * old instance (by id number), then re-add it again.
682          * <p>
683          * @param mobileNumber Mobile number to add
684          */
685         private void uniqueAddMobileNumber (final DialableMobileNumber mobileNumber) {
686                 // Make sure the parameter is valid
687                 if (null == mobileNumber) {
688                         // Throw NPE
689                         throw new NullPointerException("mobileNumber is null");
690                 } else if (mobileNumber.getPhoneId() == null) {
691                         // Throw again ...
692                         throw new NullPointerException("mobileNumber.phoneId is null");
693                 } else if (mobileNumber.getPhoneId() < 1) {
694                         // Not valid
695                         throw new IllegalArgumentException(MessageFormat.format("mobileNumber.phoneId={0} is not valid.", mobileNumber.getPhoneId()));
696                 }
697
698                 // First remove it by object
699                 if (!this.allMobileNumbers().remove(mobileNumber)) {
700                         // Did not work, try by id number
701                         for (final DialableMobileNumber cell : this.allMobileNumbers()) {
702                                 // Is id number the same?
703                                 if (Objects.equals(cell.getPhoneId(), mobileNumber.getPhoneId())) {
704                                         // Found it
705                                         this.allMobileNumbers().remove(cell);
706                                         break;
707                                 }
708                         }
709                 }
710
711                 // ... then add it
712                 this.allMobileNumbers().add(mobileNumber);
713         }
714
715         /**
716          * Updates given contact's mobile, land-line and fax number
717          * <p>
718          * @param contact Contact instance
719          */
720         private void updateContactPhoneNumbers (final Contact contact) {
721                 // Parameter must be valid
722                 if (null == contact) {
723                         // Throw NPE
724                         throw new NullPointerException("contact is null");
725                 } else if (contact.getContactId() == null) {
726                         // Throw again
727                         throw new NullPointerException("contact.contactId is null");
728                 } else if (contact.getContactId() < 1) {
729                         // Id number is not valid
730                 }
731
732                 // Is mobile set?
733                 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
734                         // Unique-add it
735                         this.uniqueAddMobileNumber(contact.getContactMobileNumber());
736                 }
737
738                 // Is land-line set?
739                 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
740                         // Unique-add it
741                         this.uniqueAddLandLineNumber(contact.getContactLandLineNumber());
742                 }
743
744                 // Is fax set?
745                 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
746                         // Unique-add it
747                         this.uniqueAddFaxNumber(contact.getContactFaxNumber());
748                 }
749         }
750
751 }