]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/contact/phone/PizzaContactPhoneWebSessionBean.java
Please cherry-pick:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / contact / phone / PizzaContactPhoneWebSessionBean.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
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 java.text.MessageFormat;
20 import java.util.HashMap;
21 import java.util.LinkedList;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Objects;
25 import javax.enterprise.context.SessionScoped;
26 import javax.enterprise.event.Observes;
27 import javax.inject.Inject;
28 import javax.inject.Named;
29 import org.mxchange.jcontacts.contact.Contact;
30 import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent;
31 import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent;
32 import org.mxchange.jcontacts.events.fax.unlinked.ObservableAdminUnlinkedFaxNumberEvent;
33 import org.mxchange.jcontacts.events.landline.unlinked.ObservableAdminUnlinkedLandLineNumberEvent;
34 import org.mxchange.jcontacts.events.mobile.unlinked.ObservableAdminUnlinkedMobileNumberEvent;
35 import org.mxchange.jphone.phonenumbers.DialableNumber;
36 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
37 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
38 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
39 import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
40 import org.mxchange.pizzaapplication.beans.BasePizzaController;
41 import org.mxchange.pizzaapplication.beans.contact.PizzaContactWebSessionController;
42 import org.mxchange.pizzaapplication.beans.helper.PizzaWebRequestHelperController;
43
44 /**
45  * A general contact bean (controller)
46  * <p>
47  * @author Roland Häder<roland@mxchange.org>
48  */
49 @Named ("contactPhoneController")
50 @SessionScoped
51 public class PizzaContactPhoneWebSessionBean extends BasePizzaController implements PizzaContactPhoneWebSessionController {
52
53         /**
54          * Serial number
55          */
56         private static final long serialVersionUID = 542_145_347_916L;
57
58         /**
59          * Bean helper
60          */
61         @Inject
62         private PizzaWebRequestHelperController beanHelper;
63
64         /**
65          * General contact controller
66          */
67         @Inject
68         private PizzaContactWebSessionController contactController;
69
70         /**
71          * "Cache" for contact's mobile, land-line and fax numbers. Currently one
72          * per each type is supported. Maybe later this will change into a OneToMany
73          * relationship (one contact, many numbers).
74          */
75         private final Map<DialableNumber, List<Contact>> contacts;
76
77         /**
78          * Default constructor
79          */
80         public PizzaContactPhoneWebSessionBean () {
81                 // Call super constructor
82                 super();
83
84                 // Init lists/maps
85                 this.contacts = new HashMap<>(10);
86         }
87
88         /**
89          * Observes events being fired when an administrator has added a new
90          * contact.
91          * <p>
92          * @param event Event being fired
93          */
94         public void afterAdminAddedContactEvent (@Observes final ObservableAdminAddedContactEvent event) {
95                 // The event must be valid
96                 if (null == event) {
97                         // Throw NPE
98                         throw new NullPointerException("event is null"); //NOI18N
99                 } else if (event.getAddedContact() == null) {
100                         // Throw again ...
101                         throw new NullPointerException("event.addedContact is null"); //NOI18N
102                 } else if (event.getAddedContact().getContactId() == null) {
103                         // ... and again
104                         throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
105                 } else if (event.getAddedContact().getContactId() < 1) {
106                         // Not valid
107                         throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N
108                 }
109
110                 // Clear this bean
111                 this.clear();
112         }
113
114         /**
115          * Event observer for newly added users by administrator
116          * <p>
117          * @param event Event being fired
118          */
119         public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
120                 // event should not be null
121                 if (null == event) {
122                         // Throw NPE
123                         throw new NullPointerException("event is null"); //NOI18N
124                 } else if (event.getAddedUser() == null) {
125                         // Throw NPE again
126                         throw new NullPointerException("event.addedUser is null"); //NOI18N
127                 } else if (event.getAddedUser().getUserId() == null) {
128                         // userId is null
129                         throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
130                 } else if (event.getAddedUser().getUserId() < 1) {
131                         // Not avalid id
132                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
133                 }
134
135                 // Clear all data
136                 this.clear();
137         }
138
139         /**
140          * Event observer for unlinked fax contact by administrators
141          * <p>
142          * @param event Unlinked fax contact event
143          */
144         public void afterAdminUnlinkedFaxContactDataEvent (@Observes final ObservableAdminUnlinkedFaxNumberEvent event) {
145                 // event should not be null
146                 if (null == event) {
147                         // Throw NPE
148                         throw new NullPointerException("event is null"); //NOI18N
149                 } else if (event.getUnlinkedFaxNumber() == null) {
150                         // Throw NPE again
151                         throw new NullPointerException("event.unlinkedFaxNumber is null"); //NOI18N
152                 } else if (event.getUnlinkedFaxNumber().getPhoneId() == null) {
153                         // userId is null
154                         throw new NullPointerException("event.unlinkedFaxNumber.contactId is null"); //NOI18N
155                 } else if (event.getUnlinkedFaxNumber().getPhoneId() < 1) {
156                         // Not avalid id
157                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedFaxNumber(), event.getUnlinkedFaxNumber().getPhoneId())); //NOI18N
158                 }
159
160                 // Remove it from list
161                 this.contacts.remove(event.getUnlinkedFaxNumber());
162
163                 // Clear all data
164                 this.clear();
165         }
166
167         /**
168          * Event observer for unlinked land-line contact by administrators
169          * <p>
170          * @param event Unlinked land-line contact event
171          */
172         public void afterAdminUnlinkedLandLineContactDataEvent (@Observes final ObservableAdminUnlinkedLandLineNumberEvent event) {
173                 // event should not be null
174                 if (null == event) {
175                         // Throw NPE
176                         throw new NullPointerException("event is null"); //NOI18N
177                 } else if (event.getUnlinkedLandLineNumber() == null) {
178                         // Throw NPE again
179                         throw new NullPointerException("event.unlinkedLandLineNumber is null"); //NOI18N
180                 } else if (event.getUnlinkedLandLineNumber().getPhoneId() == null) {
181                         // userId is null
182                         throw new NullPointerException("event.unlinkedLandLineNumber.contactId is null"); //NOI18N
183                 } else if (event.getUnlinkedLandLineNumber().getPhoneId() < 1) {
184                         // Not avalid id
185                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedLandLineNumber(), event.getUnlinkedLandLineNumber().getPhoneId())); //NOI18N
186                 }
187
188                 // Remove it from list
189                 this.contacts.remove(event.getUnlinkedLandLineNumber());
190
191                 // Clear all data
192                 this.clear();
193         }
194
195         /**
196          * Event observer for unlinked mobile contact by administrators
197          * <p>
198          * @param event Unlinked mobile contact event
199          */
200         public void afterAdminUnlinkedMobileContactDataEvent (@Observes final ObservableAdminUnlinkedMobileNumberEvent event) {
201                 // event should not be null
202                 if (null == event) {
203                         // Throw NPE
204                         throw new NullPointerException("event is null"); //NOI18N
205                 } else if (event.getUnlinkedMobileNumber() == null) {
206                         // Throw NPE again
207                         throw new NullPointerException("event.unlinkedMobileNumber is null"); //NOI18N
208                 } else if (event.getUnlinkedMobileNumber().getPhoneId() == null) {
209                         // userId is null
210                         throw new NullPointerException("event.unlinkedMobileNumber.contactId is null"); //NOI18N
211                 } else if (event.getUnlinkedMobileNumber().getPhoneId() < 1) {
212                         // Not avalid id
213                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedMobileNumber(), event.getUnlinkedMobileNumber().getPhoneId())); //NOI18N
214                 }
215
216                 // Remove it from list
217                 this.contacts.remove(event.getUnlinkedMobileNumber());
218
219                 // Clear all data
220                 this.clear();
221         }
222
223         /**
224          * Event observer for updated contact data by administrators
225          * <p>
226          * @param event Updated contact data event
227          */
228         public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent event) {
229                 // event should not be null
230                 if (null == event) {
231                         // Throw NPE
232                         throw new NullPointerException("event is null"); //NOI18N
233                 } else if (event.getUpdatedContact() == null) {
234                         // Throw NPE again
235                         throw new NullPointerException("event.updatedContact is null"); //NOI18N
236                 } else if (event.getUpdatedContact().getContactId() == null) {
237                         // userId is null
238                         throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
239                 } else if (event.getUpdatedContact().getContactId() < 1) {
240                         // Not avalid id
241                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
242                 }
243
244                 // Clear all data
245                 this.clear();
246         }
247
248         @Override
249         public List<Contact> allFaxNumberContacts () {
250                 // Get id
251                 DialableFaxNumber faxNumber = this.beanHelper.getFaxNumber();
252
253                 // Is cache there?
254                 if (this.contacts.containsKey(faxNumber)) {
255                         // Return cached version
256                         return this.contacts.get(faxNumber);
257                 } else {
258                         // Ask bean
259                         List<Contact> list = new LinkedList<>();
260
261                         // "Walk" through all contacts
262                         for (final Contact contact : this.contactController.allContacts()) {
263                                 // Is mobile instance the same?
264                                 if (Objects.equals(contact.getContactFaxNumber(), this.beanHelper.getFaxNumber())) {
265                                         // Found one
266                                         list.add(contact);
267                                 }
268                         }
269
270                         // Store result in cache
271                         this.contacts.put(faxNumber, list);
272
273                         // Return now-cached list
274                         return list;
275                 }
276         }
277
278         @Override
279         public List<Contact> allLandLineNumberContacts () {
280                 // Get id
281                 DialableLandLineNumber landLineNumber = this.beanHelper.getLandLineNumber();
282
283                 // Is cache there?
284                 if (this.contacts.containsKey(landLineNumber)) {
285                         // Return cached version
286                         return this.contacts.get(landLineNumber);
287                 } else {
288                         // Ask bean
289                         List<Contact> list = new LinkedList<>();
290
291                         // "Walk" through all contacts
292                         for (final Contact contact : this.contactController.allContacts()) {
293                                 // Is mobile instance the same?
294                                 if (Objects.equals(contact.getContactLandLineNumber(), this.beanHelper.getLandLineNumber())) {
295                                         // Found one
296                                         list.add(contact);
297                                 }
298                         }
299
300                         // Store result in cache
301                         this.contacts.put(landLineNumber, list);
302
303                         // Return now-cached list
304                         return list;
305                 }
306         }
307
308         @Override
309         public List<Contact> allMobileNumberContacts () {
310                 // Get id
311                 DialableMobileNumber mobileNumber = this.beanHelper.getMobileNumber();
312
313                 // Is cache there?
314                 if (this.contacts.containsKey(mobileNumber)) {
315                         // Return cached version
316                         return this.contacts.get(mobileNumber);
317                 } else {
318                         // Ask bean
319                         List<Contact> list = new LinkedList<>();
320
321                         // "Walk" through all contacts
322                         for (final Contact contact : this.contactController.allContacts()) {
323                                 // Is mobile instance the same?
324                                 if (Objects.equals(contact.getContactMobileNumber(), this.beanHelper.getMobileNumber())) {
325                                         // Found one
326                                         list.add(contact);
327                                 }
328                         }
329
330                         // Store result in cache
331                         this.contacts.put(mobileNumber, list);
332
333                         // Return now-cached list
334                         return list;
335                 }
336         }
337
338         /**
339          * Clears this bean
340          */
341         private void clear () {
342                 // @TODO Clear all data
343         }
344
345 }