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.phone.PhoneSessionBeanRemote;
41 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
44 * A general contact phone bean (controller)
46 * @author Roland Haeder<roland@mxchange.org>
48 @Named ("contactPhoneController")
50 public class JobsContactPhoneWebSessionBean extends BaseJobsController implements JobsContactPhoneWebSessionController {
55 private static final long serialVersionUID = 542_145_347_916L;
58 * Administrative phone controller
61 private JobsAdminPhoneWebRequestController adminPhoneController;
64 * General contact controller
67 private JobsContactWebSessionController contactController;
70 * "Cache" for contact lists, mostly only one is assigned. So this cache
71 * shouldn't grow beyond control.
73 private final Map<Long, List<Contact>> contacts;
76 * Remote EJB for phone number (administrative)
78 private PhoneSessionBeanRemote phoneBean;
83 public JobsContactPhoneWebSessionBean () {
86 // Get initial context
87 Context context = new InitialContext();
89 // Try to lookup the beans
90 this.phoneBean = (PhoneSessionBeanRemote) context.lookup("java:global/jlandingpage-ejb/phone!org.mxchange.jphone.phonenumbers.phone.PhoneSessionBeanRemote"); //NOI18N
91 } catch (final NamingException e) {
93 throw new FaceletException(e);
97 this.contacts = new HashMap<>(10);
101 public void afterAdminAddedContact (@Observes final AdminAddedContactEvent event) {
102 // The event must be valid
105 throw new NullPointerException("event is null"); //NOI18N
106 } else if (event.getAddedContact() == null) {
108 throw new NullPointerException("event.addedContact is null"); //NOI18N
109 } else if (event.getAddedContact().getContactId() == null) {
111 throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
112 } else if (event.getAddedContact().getContactId() < 1) {
114 throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N
122 public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
123 // event should not be null
126 throw new NullPointerException("event is null"); //NOI18N
127 } else if (event.getAddedUser() == null) {
129 throw new NullPointerException("event.addedUser is null"); //NOI18N
130 } else if (event.getAddedUser().getUserId() == null) {
132 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
133 } else if (event.getAddedUser().getUserId() < 1) {
135 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
143 public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent event) {
144 // event should not be null
147 throw new NullPointerException("event is null"); //NOI18N
148 } else if (event.getUpdatedContact() == null) {
150 throw new NullPointerException("event.updatedContact is null"); //NOI18N
151 } else if (event.getUpdatedContact().getContactId() == null) {
153 throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
154 } else if (event.getUpdatedContact().getContactId() < 1) {
156 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
164 public List<Contact> allCellphoneContacts () {
166 Long phoneId = this.adminPhoneController.getCellPhone().getPhoneId();
169 if (this.contacts.containsKey(phoneId)) {
170 // Return cached version
171 return this.contacts.get(phoneId);
174 List<Contact> list = new LinkedList<>();
176 // "Walk" through all contacts
177 for (final Contact contact : this.contactController.allContacts()) {
178 // Is cellphone instance the same?
179 if (Objects.equals(contact.getContactCellphoneNumber(), this.adminPhoneController.getCellPhone())) {
185 // Store result in cache
186 this.contacts.put(phoneId, list);
188 // Return now-cached list
194 * Post-initialization of this class
197 public void init () {
203 private void clear () {