]> 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 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                 // Init lists/maps
82                 this.contacts = new HashMap<>(10);
83         }
84
85         /**
86          * Observes events being fired when an administrator has added a new
87          * contact.
88          * <p>
89          * @param event Event being fired
90          */
91         public void afterAdminAddedContactEvent (@Observes final ObservableAdminAddedContactEvent event) {
92                 // The event must be valid
93                 if (null == event) {
94                         // Throw NPE
95                         throw new NullPointerException("event is null"); //NOI18N
96                 } else if (event.getAddedContact() == null) {
97                         // Throw again ...
98                         throw new NullPointerException("event.addedContact is null"); //NOI18N
99                 } else if (event.getAddedContact().getContactId() == null) {
100                         // ... and again
101                         throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
102                 } else if (event.getAddedContact().getContactId() < 1) {
103                         // Not valid
104                         throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N
105                 }
106
107                 // Clear this bean
108                 this.clear();
109         }
110
111         /**
112          * Event observer for newly added users by administrator
113          * <p>
114          * @param event Event being fired
115          */
116         public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
117                 // event should not be null
118                 if (null == event) {
119                         // Throw NPE
120                         throw new NullPointerException("event is null"); //NOI18N
121                 } else if (event.getAddedUser() == null) {
122                         // Throw NPE again
123                         throw new NullPointerException("event.addedUser is null"); //NOI18N
124                 } else if (event.getAddedUser().getUserId() == null) {
125                         // userId is null
126                         throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
127                 } else if (event.getAddedUser().getUserId() < 1) {
128                         // Not avalid id
129                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
130                 }
131
132                 // Clear all data
133                 this.clear();
134         }
135
136         /**
137          * Event observer for unlinked fax contact by administrators
138          * <p>
139          * @param event Unlinked fax contact event
140          */
141         public void afterAdminUnlinkedFaxContactDataEvent (@Observes final ObservableAdminUnlinkedFaxNumberEvent event) {
142                 // event should not be null
143                 if (null == event) {
144                         // Throw NPE
145                         throw new NullPointerException("event is null"); //NOI18N
146                 } else if (event.getUnlinkedFaxNumber() == null) {
147                         // Throw NPE again
148                         throw new NullPointerException("event.unlinkedFaxNumber is null"); //NOI18N
149                 } else if (event.getUnlinkedFaxNumber().getPhoneId() == null) {
150                         // userId is null
151                         throw new NullPointerException("event.unlinkedFaxNumber.contactId is null"); //NOI18N
152                 } else if (event.getUnlinkedFaxNumber().getPhoneId() < 1) {
153                         // Not avalid id
154                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedFaxNumber(), event.getUnlinkedFaxNumber().getPhoneId())); //NOI18N
155                 }
156
157                 // Remove it from list
158                 this.contacts.remove(event.getUnlinkedFaxNumber());
159
160                 // Clear all data
161                 this.clear();
162         }
163
164         /**
165          * Event observer for unlinked land-line contact by administrators
166          * <p>
167          * @param event Unlinked land-line contact event
168          */
169         public void afterAdminUnlinkedLandLineContactDataEvent (@Observes final ObservableAdminUnlinkedLandLineNumberEvent event) {
170                 // event should not be null
171                 if (null == event) {
172                         // Throw NPE
173                         throw new NullPointerException("event is null"); //NOI18N
174                 } else if (event.getUnlinkedLandLineNumber() == null) {
175                         // Throw NPE again
176                         throw new NullPointerException("event.unlinkedLandLineNumber is null"); //NOI18N
177                 } else if (event.getUnlinkedLandLineNumber().getPhoneId() == null) {
178                         // userId is null
179                         throw new NullPointerException("event.unlinkedLandLineNumber.contactId is null"); //NOI18N
180                 } else if (event.getUnlinkedLandLineNumber().getPhoneId() < 1) {
181                         // Not avalid id
182                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedLandLineNumber(), event.getUnlinkedLandLineNumber().getPhoneId())); //NOI18N
183                 }
184
185                 // Remove it from list
186                 this.contacts.remove(event.getUnlinkedLandLineNumber());
187
188                 // Clear all data
189                 this.clear();
190         }
191
192         /**
193          * Event observer for unlinked mobile contact by administrators
194          * <p>
195          * @param event Unlinked mobile contact event
196          */
197         public void afterAdminUnlinkedMobileContactDataEvent (@Observes final ObservableAdminUnlinkedMobileNumberEvent event) {
198                 // event should not be null
199                 if (null == event) {
200                         // Throw NPE
201                         throw new NullPointerException("event is null"); //NOI18N
202                 } else if (event.getUnlinkedMobileNumber() == null) {
203                         // Throw NPE again
204                         throw new NullPointerException("event.unlinkedMobileNumber is null"); //NOI18N
205                 } else if (event.getUnlinkedMobileNumber().getPhoneId() == null) {
206                         // userId is null
207                         throw new NullPointerException("event.unlinkedMobileNumber.contactId is null"); //NOI18N
208                 } else if (event.getUnlinkedMobileNumber().getPhoneId() < 1) {
209                         // Not avalid id
210                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedMobileNumber(), event.getUnlinkedMobileNumber().getPhoneId())); //NOI18N
211                 }
212
213                 // Remove it from list
214                 this.contacts.remove(event.getUnlinkedMobileNumber());
215
216                 // Clear all data
217                 this.clear();
218         }
219
220         /**
221          * Event observer for updated contact data by administrators
222          * <p>
223          * @param event Updated contact data event
224          */
225         public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent event) {
226                 // event should not be null
227                 if (null == event) {
228                         // Throw NPE
229                         throw new NullPointerException("event is null"); //NOI18N
230                 } else if (event.getUpdatedContact() == null) {
231                         // Throw NPE again
232                         throw new NullPointerException("event.updatedContact is null"); //NOI18N
233                 } else if (event.getUpdatedContact().getContactId() == null) {
234                         // userId is null
235                         throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
236                 } else if (event.getUpdatedContact().getContactId() < 1) {
237                         // Not avalid id
238                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
239                 }
240
241                 // Clear all data
242                 this.clear();
243         }
244
245         @Override
246         public List<Contact> allFaxNumberContacts () {
247                 // Get id
248                 DialableFaxNumber faxNumber = this.beanHelper.getFaxNumber();
249
250                 // Is cache there?
251                 if (this.contacts.containsKey(faxNumber)) {
252                         // Return cached version
253                         return this.contacts.get(faxNumber);
254                 } else {
255                         // Ask bean
256                         List<Contact> list = new LinkedList<>();
257
258                         // "Walk" through all contacts
259                         for (final Contact contact : this.contactController.allContacts()) {
260                                 // Is mobile instance the same?
261                                 if (Objects.equals(contact.getContactFaxNumber(), this.beanHelper.getFaxNumber())) {
262                                         // Found one
263                                         list.add(contact);
264                                 }
265                         }
266
267                         // Store result in cache
268                         this.contacts.put(faxNumber, list);
269
270                         // Return now-cached list
271                         return list;
272                 }
273         }
274
275         @Override
276         public List<Contact> allLandLineNumberContacts () {
277                 // Get id
278                 DialableLandLineNumber landLineNumber = this.beanHelper.getLandLineNumber();
279
280                 // Is cache there?
281                 if (this.contacts.containsKey(landLineNumber)) {
282                         // Return cached version
283                         return this.contacts.get(landLineNumber);
284                 } else {
285                         // Ask bean
286                         List<Contact> list = new LinkedList<>();
287
288                         // "Walk" through all contacts
289                         for (final Contact contact : this.contactController.allContacts()) {
290                                 // Is mobile instance the same?
291                                 if (Objects.equals(contact.getContactLandLineNumber(), this.beanHelper.getLandLineNumber())) {
292                                         // Found one
293                                         list.add(contact);
294                                 }
295                         }
296
297                         // Store result in cache
298                         this.contacts.put(landLineNumber, list);
299
300                         // Return now-cached list
301                         return list;
302                 }
303         }
304
305         @Override
306         public List<Contact> allMobileNumberContacts () {
307                 // Get id
308                 DialableMobileNumber mobileNumber = this.beanHelper.getMobileNumber();
309
310                 // Is cache there?
311                 if (this.contacts.containsKey(mobileNumber)) {
312                         // Return cached version
313                         return this.contacts.get(mobileNumber);
314                 } else {
315                         // Ask bean
316                         List<Contact> list = new LinkedList<>();
317
318                         // "Walk" through all contacts
319                         for (final Contact contact : this.contactController.allContacts()) {
320                                 // Is mobile instance the same?
321                                 if (Objects.equals(contact.getContactMobileNumber(), this.beanHelper.getMobileNumber())) {
322                                         // Found one
323                                         list.add(contact);
324                                 }
325                         }
326
327                         // Store result in cache
328                         this.contacts.put(mobileNumber, list);
329
330                         // Return now-cached list
331                         return list;
332                 }
333         }
334
335         /**
336          * Clears this bean
337          */
338         private void clear () {
339                 // Clear all data
340         }
341
342 }