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.phone.AdminPhoneSessionBeanRemote;
42 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
45 * A general contact bean (controller)
47 * @author Roland Haeder<roland@mxchange.org>
49 @Named ("contactPhoneController")
51 public class JobsContactPhoneWebSessionBean extends BaseJobsController implements JobsContactPhoneWebSessionController {
56 private static final long serialVersionUID = 542_145_347_916L;
59 * Remote EJB for phone number (administrative)
61 private AdminPhoneSessionBeanRemote adminPhoneBean;
64 * Administrative phone controller
67 private JobsAdminPhoneWebRequestController adminPhoneController;
70 * All cell phone numbers
72 private final List<DialableCellphoneNumber> cellphoneNumbers;
75 * General contact controller
78 private JobsContactWebSessionController contactController;
81 * "Cache" for contact lists, mostly only one is assigned. So this cache
82 * shouldn't grow beyond control.
84 private final Map<Long, List<Contact>> contacts;
89 public JobsContactPhoneWebSessionBean () {
92 // Get initial context
93 Context context = new InitialContext();
95 // Try to lookup the beans
96 this.adminPhoneBean = (AdminPhoneSessionBeanRemote) context.lookup("java:global/jlandingpage-ejb/adminphone!org.mxchange.jphone.phonenumbers.phone.AdminPhoneSessionBeanRemote"); //NOI18N
97 } catch (final NamingException e) {
99 throw new FaceletException(e);
103 this.cellphoneNumbers = new LinkedList<>();
104 this.contacts = new HashMap<>(10);
108 public void afterAdminAddedContact (@Observes final AdminAddedContactEvent event) {
109 // The event must be valid
112 throw new NullPointerException("event is null"); //NOI18N
113 } else if (event.getAddedContact() == null) {
115 throw new NullPointerException("event.addedContact is null"); //NOI18N
116 } else if (event.getAddedContact().getContactId() == null) {
118 throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
119 } else if (event.getAddedContact().getContactId() < 1) {
121 throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N
129 public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
130 // event should not be null
133 throw new NullPointerException("event is null"); //NOI18N
134 } else if (event.getAddedUser() == null) {
136 throw new NullPointerException("event.addedUser is null"); //NOI18N
137 } else if (event.getAddedUser().getUserId() == null) {
139 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
140 } else if (event.getAddedUser().getUserId() < 1) {
142 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
150 public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent event) {
151 // event should not be null
154 throw new NullPointerException("event is null"); //NOI18N
155 } else if (event.getUpdatedContact() == null) {
157 throw new NullPointerException("event.updatedContact is null"); //NOI18N
158 } else if (event.getUpdatedContact().getContactId() == null) {
160 throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
161 } else if (event.getUpdatedContact().getContactId() < 1) {
163 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
168 public List<Contact> allCellphoneContacts () {
170 Long phoneId = this.adminPhoneController.getCellPhone().getPhoneId();
173 if (this.contacts.containsKey(phoneId)) {
174 // Return cached version
175 return this.contacts.get(phoneId);
178 List<Contact> list = new LinkedList<>();
180 // "Walk" through all contacts
181 for (final Contact contact : this.contactController.allContacts()) {
182 // Is cellphone instance the same?
183 if (Objects.equals(contact.getContactCellphoneNumber(), this.adminPhoneController.getCellPhone())) {
189 // Store result in cache
190 this.contacts.put(phoneId, list);
192 // Return now-cached list
198 * Post-initialization of this class
201 public void init () {
203 this.cellphoneNumbers.addAll(this.adminPhoneBean.allCellphoneNumbers());
209 private void clear () {