2 * Copyright (C) 2016 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.jjobs.beans.contact.phone;
19 import java.text.MessageFormat;
20 import java.util.HashMap;
21 import java.util.LinkedList;
22 import java.util.List;
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;
47 * A general contact bean (controller)
49 * @author Roland Haeder<roland@mxchange.org>
51 @Named ("contactPhoneController")
53 public class JobsContactPhoneWebSessionBean extends BaseJobsController implements JobsContactPhoneWebSessionController {
58 private static final long serialVersionUID = 542_145_347_916L;
61 * Remote EJB for phone number (administrative)
63 private AdminPhoneSessionBeanRemote adminPhoneBean;
66 * Administrative phone controller
69 private JobsAdminPhoneWebRequestController adminPhoneController;
72 * All cell phone numbers
74 private final List<DialableCellphoneNumber> cellphoneNumbers;
79 private final List<DialableFaxNumber> faxNumbers;
82 * All land-line numbers
84 private final List<DialableLandLineNumber> landLineNumbers;
87 * General contact controller
90 private JobsContactWebSessionController contactController;
93 * "Cache" for contact lists, mostly only one is assigned. So this cache
94 * shouldn't grow beyond control.
96 private final Map<Long, List<Contact>> contacts;
101 public JobsContactPhoneWebSessionBean () {
104 // Get initial context
105 Context context = new InitialContext();
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) {
111 throw new FaceletException(e);
115 this.cellphoneNumbers = new LinkedList<>();
116 this.faxNumbers = new LinkedList<>();
117 this.landLineNumbers = new LinkedList<>();
118 this.contacts = new HashMap<>(10);
122 public void afterAdminAddedContact (@Observes final AdminAddedContactEvent event) {
123 // The event must be valid
126 throw new NullPointerException("event is null"); //NOI18N
127 } else if (event.getAddedContact() == null) {
129 throw new NullPointerException("event.addedContact is null"); //NOI18N
130 } else if (event.getAddedContact().getContactId() == null) {
132 throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
133 } else if (event.getAddedContact().getContactId() < 1) {
135 throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N
138 // Update contact's cellphone, land-line and fax number
139 this.updateContactPhoneNumbers(event.getAddedContact());
146 public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
147 // event should not be null
150 throw new NullPointerException("event is null"); //NOI18N
151 } else if (event.getAddedUser() == null) {
153 throw new NullPointerException("event.addedUser is null"); //NOI18N
154 } else if (event.getAddedUser().getUserId() == null) {
156 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
157 } else if (event.getAddedUser().getUserId() < 1) {
159 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
162 // Update contact's cellphone, land-line and fax number
163 this.updateContactPhoneNumbers(event.getAddedUser().getUserContact());
170 public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent event) {
171 // event should not be null
174 throw new NullPointerException("event is null"); //NOI18N
175 } else if (event.getUpdatedContact() == null) {
177 throw new NullPointerException("event.updatedContact is null"); //NOI18N
178 } else if (event.getUpdatedContact().getContactId() == null) {
180 throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
181 } else if (event.getUpdatedContact().getContactId() < 1) {
183 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
186 // Update contact's cellphone, land-line and fax number
187 this.updateContactPhoneNumbers(event.getUpdatedContact());
194 public List<Contact> allCellphoneContacts () {
196 Long phoneId = this.adminPhoneController.getCellPhone().getPhoneId();
199 if (this.contacts.containsKey(phoneId)) {
200 // Return cached version
201 return this.contacts.get(phoneId);
204 List<Contact> list = new LinkedList<>();
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())) {
215 // Store result in cache
216 this.contacts.put(phoneId, list);
218 // Return now-cached list
224 * Post-initialization of this class
227 public void init () {
229 this.cellphoneNumbers.addAll(this.adminPhoneBean.allCellphoneNumbers());
235 private void clear () {
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.
243 * @param cellphoneNumber Cellphone number to add
245 private void uniqueAddCellphoneNumber (final DialableCellphoneNumber cellphoneNumber) {
246 // Make sure the parameter is valid
247 if (null == cellphoneNumber) {
249 throw new NullPointerException("cellphoneNumber is null");
250 } else if (cellphoneNumber.getPhoneId() == null) {
252 throw new NullPointerException("cellphoneNumber.phoneId is null");
253 } else if (cellphoneNumber.getPhoneId() < 1) {
255 throw new IllegalArgumentException(MessageFormat.format("cellphoneNumber.phoneId={0} is not valid.", cellphoneNumber.getPhoneId()));
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())) {
265 this.cellphoneNumbers.remove(cell);
272 this.cellphoneNumbers.add(cellphoneNumber);
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.
279 * @param faxNumber number to add
281 private void uniqueAddFaxNumber (final DialableFaxNumber faxNumber) {
282 // Make sure the parameter is valid
283 if (null == faxNumber) {
285 throw new NullPointerException("faxNumber is null");
286 } else if (faxNumber.getPhoneId() == null) {
288 throw new NullPointerException("faxNumber.phoneId is null");
289 } else if (faxNumber.getPhoneId() < 1) {
291 throw new IllegalArgumentException(MessageFormat.format("faxNumber.phoneId={0} is not valid.", faxNumber.getPhoneId()));
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())) {
301 this.faxNumbers.remove(fax);
308 this.faxNumbers.add(faxNumber);
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.
315 * @param landLineNumber Land-line number to add
317 private void uniqueAddLandLineNumber (final DialableLandLineNumber landLineNumber) {
318 // Make sure the parameter is valid
319 if (null == landLineNumber) {
321 throw new NullPointerException("landLineNumber is null");
322 } else if (landLineNumber.getPhoneId() == null) {
324 throw new NullPointerException("landLineNumber.phoneId is null");
325 } else if (landLineNumber.getPhoneId() < 1) {
327 throw new IllegalArgumentException(MessageFormat.format("landLineNumber.phoneId={0} is not valid.", landLineNumber.getPhoneId()));
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())) {
337 this.landLineNumbers.remove(landLine);
344 this.landLineNumbers.add(landLineNumber);
348 * Updates given contact's cellphone, land-line and fax number
350 * @param contact Contact instance
352 private void updateContactPhoneNumbers (final Contact contact) {
353 // Parameter must be valid
354 if (null == contact) {
356 throw new NullPointerException("contact is null");
357 } else if (contact.getContactId() == null) {
359 throw new NullPointerException("contact.contactId is null");
360 } else if (contact.getContactId() < 1) {
361 // Id number is not valid
365 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
367 this.uniqueAddCellphoneNumber(contact.getContactCellphoneNumber());
371 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
373 this.uniqueAddLandLineNumber(contact.getContactLandLineNumber());
377 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
379 this.uniqueAddFaxNumber(contact.getContactFaxNumber());