]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/contact/phone/JobsContactPhoneWebSessionBean.java
Please cherry-pick:
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / contact / phone / JobsContactPhoneWebSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
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.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.AdminAddedContactEvent;
31 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
32 import org.mxchange.jcontacts.events.fax.unlinked.AdminUnlinkedFaxNumberEvent;
33 import org.mxchange.jcontacts.events.landline.unlinked.AdminUnlinkedLandLineNumberEvent;
34 import org.mxchange.jcontacts.events.mobile.unlinked.AdminUnlinkedMobileNumberEvent;
35 import org.mxchange.jjobs.beans.BaseJobsController;
36 import org.mxchange.jjobs.beans.contact.JobsContactWebSessionController;
37 import org.mxchange.jphone.phonenumbers.DialableNumber;
38 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
39 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
40 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
41 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
42 import org.mxchange.jjobs.beans.helper.JobsWebViewHelperController;
43
44 /**
45  * An administrative contact phone controller (bean)
46  * <p>
47  * @author Roland Haeder<roland@mxchange.org>
48  */
49 @Named ("contactPhoneController")
50 @SessionScoped
51 public class JobsContactPhoneWebSessionBean extends BaseJobsController implements JobsContactPhoneWebSessionController {
52
53         /**
54          * Serial number
55          */
56         private static final long serialVersionUID = 542_145_347_916L;
57
58         /**
59          * Admin helper instance
60          */
61         @Inject
62         private JobsWebViewHelperController beanHelper;
63
64         /**
65          * General contact controller
66          */
67         @Inject
68         private JobsContactWebSessionController 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 JobsContactPhoneWebSessionBean () {
81                 // Init lists/maps
82                 this.contacts = new HashMap<>(10);
83         }
84
85         @Override
86         public void afterAdminAddedContact (@Observes final AdminAddedContactEvent event) {
87                 // The event must be valid
88                 if (null == event) {
89                         // Throw NPE
90                         throw new NullPointerException("event is null"); //NOI18N
91                 } else if (event.getAddedContact() == null) {
92                         // Throw again ...
93                         throw new NullPointerException("event.addedContact is null"); //NOI18N
94                 } else if (event.getAddedContact().getContactId() == null) {
95                         // ... and again
96                         throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
97                 } else if (event.getAddedContact().getContactId() < 1) {
98                         // Not valid
99                         throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N
100                 }
101
102                 // Clear this bean
103                 this.clear();
104         }
105
106         @Override
107         public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
108                 // event should not be null
109                 if (null == event) {
110                         // Throw NPE
111                         throw new NullPointerException("event is null"); //NOI18N
112                 } else if (event.getAddedUser() == null) {
113                         // Throw NPE again
114                         throw new NullPointerException("event.addedUser is null"); //NOI18N
115                 } else if (event.getAddedUser().getUserId() == null) {
116                         // userId is null
117                         throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
118                 } else if (event.getAddedUser().getUserId() < 1) {
119                         // Not avalid id
120                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
121                 }
122
123                 // Clear all data
124                 this.clear();
125         }
126
127         @Override
128         public void afterAdminUnlinkedFaxContactDataEvent (@Observes final AdminUnlinkedFaxNumberEvent event) {
129                 // event should not be null
130                 if (null == event) {
131                         // Throw NPE
132                         throw new NullPointerException("event is null"); //NOI18N
133                 } else if (event.getUnlinkedFaxNumber() == null) {
134                         // Throw NPE again
135                         throw new NullPointerException("event.unlinkedFaxNumber is null"); //NOI18N
136                 } else if (event.getUnlinkedFaxNumber().getPhoneId() == null) {
137                         // userId is null
138                         throw new NullPointerException("event.unlinkedFaxNumber.contactId is null"); //NOI18N
139                 } else if (event.getUnlinkedFaxNumber().getPhoneId() < 1) {
140                         // Not avalid id
141                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedFaxNumber(), event.getUnlinkedFaxNumber().getPhoneId())); //NOI18N
142                 }
143
144                 // Remove it from list
145                 this.contacts.remove(event.getUnlinkedFaxNumber());
146
147                 // Clear all data
148                 this.clear();
149         }
150
151         @Override
152         public void afterAdminUnlinkedLandLineContactDataEvent (@Observes final AdminUnlinkedLandLineNumberEvent event) {
153                 // event should not be null
154                 if (null == event) {
155                         // Throw NPE
156                         throw new NullPointerException("event is null"); //NOI18N
157                 } else if (event.getUnlinkedLandLineNumber() == null) {
158                         // Throw NPE again
159                         throw new NullPointerException("event.unlinkedLandLineNumber is null"); //NOI18N
160                 } else if (event.getUnlinkedLandLineNumber().getPhoneId() == null) {
161                         // userId is null
162                         throw new NullPointerException("event.unlinkedLandLineNumber.contactId is null"); //NOI18N
163                 } else if (event.getUnlinkedLandLineNumber().getPhoneId() < 1) {
164                         // Not avalid id
165                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedLandLineNumber(), event.getUnlinkedLandLineNumber().getPhoneId())); //NOI18N
166                 }
167
168                 // Remove it from list
169                 this.contacts.remove(event.getUnlinkedLandLineNumber());
170
171                 // Clear all data
172                 this.clear();
173         }
174
175         @Override
176         public void afterAdminUnlinkedMobileContactDataEvent (@Observes final AdminUnlinkedMobileNumberEvent event) {
177                 // event should not be null
178                 if (null == event) {
179                         // Throw NPE
180                         throw new NullPointerException("event is null"); //NOI18N
181                 } else if (event.getUnlinkedMobileNumber() == null) {
182                         // Throw NPE again
183                         throw new NullPointerException("event.unlinkedMobileNumber is null"); //NOI18N
184                 } else if (event.getUnlinkedMobileNumber().getPhoneId() == null) {
185                         // userId is null
186                         throw new NullPointerException("event.unlinkedMobileNumber.contactId is null"); //NOI18N
187                 } else if (event.getUnlinkedMobileNumber().getPhoneId() < 1) {
188                         // Not avalid id
189                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedMobileNumber(), event.getUnlinkedMobileNumber().getPhoneId())); //NOI18N
190                 }
191
192                 // Remove it from list
193                 this.contacts.remove(event.getUnlinkedMobileNumber());
194
195                 // Clear all data
196                 this.clear();
197         }
198
199         @Override
200         public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent 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.getUpdatedContact() == null) {
206                         // Throw NPE again
207                         throw new NullPointerException("event.updatedContact is null"); //NOI18N
208                 } else if (event.getUpdatedContact().getContactId() == null) {
209                         // userId is null
210                         throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
211                 } else if (event.getUpdatedContact().getContactId() < 1) {
212                         // Not avalid id
213                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
214                 }
215
216                 // Clear all data
217                 this.clear();
218         }
219
220         @Override
221         public List<Contact> allFaxNumberContacts () {
222                 // Get id
223                 DialableFaxNumber faxNumber = this.beanHelper.getFaxNumber();
224
225                 // Is cache there?
226                 if (this.contacts.containsKey(faxNumber)) {
227                         // Return cached version
228                         return this.contacts.get(faxNumber);
229                 } else {
230                         // Ask bean
231                         List<Contact> list = new LinkedList<>();
232
233                         // "Walk" through all contacts
234                         for (final Contact contact : this.contactController.allContacts()) {
235                                 // Is mobile instance the same?
236                                 if (Objects.equals(contact.getContactFaxNumber(), this.beanHelper.getFaxNumber())) {
237                                         // Found one
238                                         list.add(contact);
239                                 }
240                         }
241
242                         // Store result in cache
243                         this.contacts.put(faxNumber, list);
244
245                         // Return now-cached list
246                         return list;
247                 }
248         }
249
250         @Override
251         public List<Contact> allLandLineNumberContacts () {
252                 // Get id
253                 DialableLandLineNumber landLineNumber = this.beanHelper.getLandLineNumber();
254
255                 // Is cache there?
256                 if (this.contacts.containsKey(landLineNumber)) {
257                         // Return cached version
258                         return this.contacts.get(landLineNumber);
259                 } else {
260                         // Ask bean
261                         List<Contact> list = new LinkedList<>();
262
263                         // "Walk" through all contacts
264                         for (final Contact contact : this.contactController.allContacts()) {
265                                 // Is mobile instance the same?
266                                 if (Objects.equals(contact.getContactLandLineNumber(), this.beanHelper.getLandLineNumber())) {
267                                         // Found one
268                                         list.add(contact);
269                                 }
270                         }
271
272                         // Store result in cache
273                         this.contacts.put(landLineNumber, list);
274
275                         // Return now-cached list
276                         return list;
277                 }
278         }
279
280         @Override
281         public List<Contact> allMobileNumberContacts () {
282                 // Get id
283                 DialableMobileNumber mobileNumber = this.beanHelper.getMobileNumber();
284
285                 // Is cache there?
286                 if (this.contacts.containsKey(mobileNumber)) {
287                         // Return cached version
288                         return this.contacts.get(mobileNumber);
289                 } else {
290                         // Ask bean
291                         List<Contact> list = new LinkedList<>();
292
293                         // "Walk" through all contacts
294                         for (final Contact contact : this.contactController.allContacts()) {
295                                 // Is mobile instance the same?
296                                 if (Objects.equals(contact.getContactMobileNumber(), this.beanHelper.getMobileNumber())) {
297                                         // Found one
298                                         list.add(contact);
299                                 }
300                         }
301
302                         // Store result in cache
303                         this.contacts.put(mobileNumber, list);
304
305                         // Return now-cached list
306                         return list;
307                 }
308         }
309
310         /**
311          * Clears this bean
312          */
313         private void clear () {
314                 // Clear all data
315         }
316
317 }