]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/contact/phone/JobsContactPhoneWebSessionBean.java
Continued with phone numbers: (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.annotation.PostConstruct;
26 import javax.enterprise.context.SessionScoped;
27 import javax.enterprise.event.Observes;
28 import javax.faces.view.facelets.FaceletException;
29 import javax.inject.Inject;
30 import javax.inject.Named;
31 import javax.naming.Context;
32 import javax.naming.InitialContext;
33 import javax.naming.NamingException;
34 import org.mxchange.jcontacts.contact.Contact;
35 import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent;
36 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
37 import org.mxchange.jjobs.beans.BaseJobsController;
38 import org.mxchange.jjobs.beans.contact.JobsContactWebSessionController;
39 import org.mxchange.jjobs.beans.phone.JobsAdminPhoneWebRequestController;
40 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
41 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
42 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
43 import org.mxchange.jphone.phonenumbers.phone.AdminPhoneSessionBeanRemote;
44 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
45
46 /**
47  * A general contact bean (controller)
48  * <p>
49  * @author Roland Haeder<roland@mxchange.org>
50  */
51 @Named ("contactPhoneController")
52 @SessionScoped
53 public class JobsContactPhoneWebSessionBean extends BaseJobsController implements JobsContactPhoneWebSessionController {
54
55         /**
56          * Serial number
57          */
58         private static final long serialVersionUID = 542_145_347_916L;
59
60         /**
61          * Remote EJB for phone number (administrative)
62          */
63         private AdminPhoneSessionBeanRemote adminPhoneBean;
64
65         /**
66          * Administrative phone controller
67          */
68         @Inject
69         private JobsAdminPhoneWebRequestController adminPhoneController;
70
71         /**
72          * All cell phone numbers
73          */
74         private final List<DialableCellphoneNumber> cellphoneNumbers;
75
76         /**
77          * All fax numbers
78          */
79         private final List<DialableFaxNumber> faxNumbers;
80
81         /**
82          * All land-line numbers
83          */
84         private final List<DialableLandLineNumber> landLineNumbers;
85
86         /**
87          * General contact controller
88          */
89         @Inject
90         private JobsContactWebSessionController contactController;
91
92         /**
93          * "Cache" for contact lists, mostly only one is assigned. So this cache
94          * shouldn't grow beyond control.
95          */
96         private final Map<Long, List<Contact>> contacts;
97
98         /**
99          * Default constructor
100          */
101         public JobsContactPhoneWebSessionBean () {
102                 // Try it
103                 try {
104                         // Get initial context
105                         Context context = new InitialContext();
106
107                         // Try to lookup the beans
108                         this.adminPhoneBean = (AdminPhoneSessionBeanRemote) context.lookup("java:global/jlandingpage-ejb/adminphone!org.mxchange.jphone.phonenumbers.phone.AdminPhoneSessionBeanRemote"); //NOI18N
109                 } catch (final NamingException e) {
110                         // Throw again
111                         throw new FaceletException(e);
112                 }
113
114                 // Init lists/maps
115                 this.cellphoneNumbers = new LinkedList<>();
116                 this.faxNumbers = new LinkedList<>();
117                 this.landLineNumbers = new LinkedList<>();
118                 this.contacts = new HashMap<>(10);
119         }
120
121         @Override
122         public void afterAdminAddedContact (@Observes final AdminAddedContactEvent event) {
123                 // The event must be valid
124                 if (null == event) {
125                         // Throw NPE
126                         throw new NullPointerException("event is null"); //NOI18N
127                 } else if (event.getAddedContact() == null) {
128                         // Throw again ...
129                         throw new NullPointerException("event.addedContact is null"); //NOI18N
130                 } else if (event.getAddedContact().getContactId() == null) {
131                         // ... and again
132                         throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
133                 } else if (event.getAddedContact().getContactId() < 1) {
134                         // Not valid
135                         throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N
136                 }
137
138                 // Update contact's cellphone, land-line and fax number
139                 this.updateContactPhoneNumbers(event.getAddedContact());
140
141                 // Clear this bean
142                 this.clear();
143         }
144
145         @Override
146         public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
147                 // event should not be null
148                 if (null == event) {
149                         // Throw NPE
150                         throw new NullPointerException("event is null"); //NOI18N
151                 } else if (event.getAddedUser() == null) {
152                         // Throw NPE again
153                         throw new NullPointerException("event.addedUser is null"); //NOI18N
154                 } else if (event.getAddedUser().getUserId() == null) {
155                         // userId is null
156                         throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
157                 } else if (event.getAddedUser().getUserId() < 1) {
158                         // Not avalid id
159                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
160                 }
161
162                 // Update contact's cellphone, land-line and fax number
163                 this.updateContactPhoneNumbers(event.getAddedUser().getUserContact());
164
165                 // Clear all data
166                 this.clear();
167         }
168
169         @Override
170         public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent event) {
171                 // event should not be null
172                 if (null == event) {
173                         // Throw NPE
174                         throw new NullPointerException("event is null"); //NOI18N
175                 } else if (event.getUpdatedContact() == null) {
176                         // Throw NPE again
177                         throw new NullPointerException("event.updatedContact is null"); //NOI18N
178                 } else if (event.getUpdatedContact().getContactId() == null) {
179                         // userId is null
180                         throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
181                 } else if (event.getUpdatedContact().getContactId() < 1) {
182                         // Not avalid id
183                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
184                 }
185
186                 // Update contact's cellphone, land-line and fax number
187                 this.updateContactPhoneNumbers(event.getUpdatedContact());
188
189                 // Clear all data
190                 this.clear();
191         }
192
193         @Override
194         public List<Contact> allCellphoneContacts () {
195                 // Get id
196                 Long phoneId = this.adminPhoneController.getCellPhone().getPhoneId();
197
198                 // Is cache there?
199                 if (this.contacts.containsKey(phoneId)) {
200                         // Return cached version
201                         return this.contacts.get(phoneId);
202                 } else {
203                         // Ask bean
204                         List<Contact> list = new LinkedList<>();
205
206                         // "Walk" through all contacts
207                         for (final Contact contact : this.contactController.allContacts()) {
208                                 // Is cellphone instance the same?
209                                 if (Objects.equals(contact.getContactCellphoneNumber(), this.adminPhoneController.getCellPhone())) {
210                                         // Found one
211                                         list.add(contact);
212                                 }
213                         }
214
215                         // Store result in cache
216                         this.contacts.put(phoneId, list);
217
218                         // Return now-cached list
219                         return list;
220                 }
221         }
222
223         /**
224          * Post-initialization of this class
225          */
226         @PostConstruct
227         public void init () {
228                 // All phone numbers
229                 this.cellphoneNumbers.addAll(this.adminPhoneBean.allCellphoneNumbers());
230         }
231
232         /**
233          * Clears this bean
234          */
235         private void clear () {
236                 // Clear all data
237         }
238
239         /**
240          * Uniquely add given cellphone number to this bean's list. First remove the
241          * old instance (by id number), then re-add it again.
242          * <p>
243          * @param cellphoneNumber Cellphone number to add
244          */
245         private void uniqueAddCellphoneNumber (final DialableCellphoneNumber cellphoneNumber) {
246                 // Make sure the parameter is valid
247                 if (null == cellphoneNumber) {
248                         // Throw NPE
249                         throw new NullPointerException("cellphoneNumber is null");
250                 } else if (cellphoneNumber.getPhoneId() == null) {
251                         // Throw again ...
252                         throw new NullPointerException("cellphoneNumber.phoneId is null");
253                 } else if (cellphoneNumber.getPhoneId() < 1) {
254                         // Not valid
255                         throw new IllegalArgumentException(MessageFormat.format("cellphoneNumber.phoneId={0} is not valid.", cellphoneNumber.getPhoneId()));
256                 }
257
258                 // First remove it by object
259                 if (!this.cellphoneNumbers.remove(cellphoneNumber)) {
260                         // Did not work, try by id number
261                         for (final DialableCellphoneNumber cell : this.cellphoneNumbers) {
262                                 // Is id number the same?
263                                 if (Objects.equals(cell.getPhoneId(), cellphoneNumber.getPhoneId())) {
264                                         // Found it
265                                         this.cellphoneNumbers.remove(cell);
266                                         break;
267                                 }
268                         }
269                 }
270
271                 // ... then add it
272                 this.cellphoneNumbers.add(cellphoneNumber);
273         }
274
275         /**
276          * Uniquely add given fax number to this bean's list. First remove the old
277          * instance (by id number), then re-add it again.
278          * <p>
279          * @param faxNumber number to add
280          */
281         private void uniqueAddFaxNumber (final DialableFaxNumber faxNumber) {
282                 // Make sure the parameter is valid
283                 if (null == faxNumber) {
284                         // Throw NPE
285                         throw new NullPointerException("faxNumber is null");
286                 } else if (faxNumber.getPhoneId() == null) {
287                         // Throw again ...
288                         throw new NullPointerException("faxNumber.phoneId is null");
289                 } else if (faxNumber.getPhoneId() < 1) {
290                         // Not valid
291                         throw new IllegalArgumentException(MessageFormat.format("faxNumber.phoneId={0} is not valid.", faxNumber.getPhoneId()));
292                 }
293
294                 // First remove it
295                 if (!this.faxNumbers.remove(faxNumber)) {
296                         // Did not work, try by id number
297                         for (final DialableFaxNumber fax : this.faxNumbers) {
298                                 // Is id number the same?
299                                 if (Objects.equals(fax.getPhoneId(), faxNumber.getPhoneId())) {
300                                         // Found it
301                                         this.faxNumbers.remove(fax);
302                                         break;
303                                 }
304                         }
305                 }
306
307                 // ... then add it
308                 this.faxNumbers.add(faxNumber);
309         }
310
311         /**
312          * Uniquely add given land-line number to this bean's list. First remove the
313          * old instance (by id number), then re-add it again.
314          * <p>
315          * @param landLineNumber Land-line number to add
316          */
317         private void uniqueAddLandLineNumber (final DialableLandLineNumber landLineNumber) {
318                 // Make sure the parameter is valid
319                 if (null == landLineNumber) {
320                         // Throw NPE
321                         throw new NullPointerException("landLineNumber is null");
322                 } else if (landLineNumber.getPhoneId() == null) {
323                         // Throw again ...
324                         throw new NullPointerException("landLineNumber.phoneId is null");
325                 } else if (landLineNumber.getPhoneId() < 1) {
326                         // Not valid
327                         throw new IllegalArgumentException(MessageFormat.format("landLineNumber.phoneId={0} is not valid.", landLineNumber.getPhoneId()));
328                 }
329
330                 // First remove it
331                 if (!this.landLineNumbers.remove(landLineNumber)) {
332                         // Did not work, try by id number
333                         for (final DialableLandLineNumber landLine : this.landLineNumbers) {
334                                 // Is id number the same?
335                                 if (Objects.equals(landLine.getPhoneId(), landLineNumber.getPhoneId())) {
336                                         // Found it
337                                         this.landLineNumbers.remove(landLine);
338                                         break;
339                                 }
340                         }
341                 }
342
343                 // ... then add it
344                 this.landLineNumbers.add(landLineNumber);
345         }
346
347         /**
348          * Updates given contact's cellphone, land-line and fax number
349          * <p>
350          * @param contact Contact instance
351          */
352         private void updateContactPhoneNumbers (final Contact contact) {
353                 // Parameter must be valid
354                 if (null == contact) {
355                         // Throw NPE
356                         throw new NullPointerException("contact is null");
357                 } else if (contact.getContactId() == null) {
358                         // Throw again
359                         throw new NullPointerException("contact.contactId is null");
360                 } else if (contact.getContactId() < 1) {
361                         // Id number is not valid
362                 }
363
364                 // Is cellphone set?
365                 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
366                         // Unique-add it
367                         this.uniqueAddCellphoneNumber(contact.getContactCellphoneNumber());
368                 }
369
370                 // Is land-line set?
371                 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
372                         // Unique-add it
373                         this.uniqueAddLandLineNumber(contact.getContactLandLineNumber());
374                 }
375
376                 // Is fax set?
377                 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
378                         // Unique-add it
379                         this.uniqueAddFaxNumber(contact.getContactFaxNumber());
380                 }
381         }
382
383 }