2 * Copyright (C) 2016 Roland Häder
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.pizzaapplication.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.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.ObservableAdminAddedContactEvent;
31 import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent;
32 import org.mxchange.jcontacts.events.fax.unlinked.ObservableAdminUnlinkedFaxNumberEvent;
33 import org.mxchange.jcontacts.events.landline.unlinked.ObservableAdminUnlinkedLandLineNumberEvent;
34 import org.mxchange.jcontacts.events.mobile.unlinked.ObservableAdminUnlinkedMobileNumberEvent;
35 import org.mxchange.jphone.phonenumbers.DialableNumber;
36 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
37 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
38 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
39 import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
40 import org.mxchange.pizzaapplication.beans.BasePizzaController;
41 import org.mxchange.pizzaapplication.beans.contact.PizzaContactWebSessionController;
42 import org.mxchange.pizzaapplication.beans.helper.PizzaWebRequestHelperController;
45 * A general contact bean (controller)
47 * @author Roland Häder<roland@mxchange.org>
49 @Named ("contactPhoneController")
51 public class PizzaContactPhoneWebSessionBean extends BasePizzaController implements PizzaContactPhoneWebSessionController {
56 private static final long serialVersionUID = 542_145_347_916L;
62 private PizzaWebRequestHelperController beanHelper;
65 * General contact controller
68 private PizzaContactWebSessionController contactController;
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).
75 private final Map<DialableNumber, List<Contact>> contacts;
80 public PizzaContactPhoneWebSessionBean () {
82 this.contacts = new HashMap<>(10);
86 * Observes events being fired when an administrator has added a new
89 * @param event Event being fired
91 public void afterAdminAddedContactEvent (@Observes final ObservableAdminAddedContactEvent event) {
92 // The event must be valid
95 throw new NullPointerException("event is null"); //NOI18N
96 } else if (event.getAddedContact() == null) {
98 throw new NullPointerException("event.addedContact is null"); //NOI18N
99 } else if (event.getAddedContact().getContactId() == null) {
101 throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
102 } else if (event.getAddedContact().getContactId() < 1) {
104 throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N
112 * Event observer for newly added users by administrator
114 * @param event Event being fired
116 public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
117 // event should not be null
120 throw new NullPointerException("event is null"); //NOI18N
121 } else if (event.getAddedUser() == null) {
123 throw new NullPointerException("event.addedUser is null"); //NOI18N
124 } else if (event.getAddedUser().getUserId() == null) {
126 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
127 } else if (event.getAddedUser().getUserId() < 1) {
129 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
137 * Event observer for unlinked fax contact by administrators
139 * @param event Unlinked fax contact event
141 public void afterAdminUnlinkedFaxContactDataEvent (@Observes final ObservableAdminUnlinkedFaxNumberEvent event) {
142 // event should not be null
145 throw new NullPointerException("event is null"); //NOI18N
146 } else if (event.getUnlinkedFaxNumber() == null) {
148 throw new NullPointerException("event.unlinkedFaxNumber is null"); //NOI18N
149 } else if (event.getUnlinkedFaxNumber().getPhoneId() == null) {
151 throw new NullPointerException("event.unlinkedFaxNumber.contactId is null"); //NOI18N
152 } else if (event.getUnlinkedFaxNumber().getPhoneId() < 1) {
154 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedFaxNumber(), event.getUnlinkedFaxNumber().getPhoneId())); //NOI18N
157 // Remove it from list
158 this.contacts.remove(event.getUnlinkedFaxNumber());
165 * Event observer for unlinked land-line contact by administrators
167 * @param event Unlinked land-line contact event
169 public void afterAdminUnlinkedLandLineContactDataEvent (@Observes final ObservableAdminUnlinkedLandLineNumberEvent event) {
170 // event should not be null
173 throw new NullPointerException("event is null"); //NOI18N
174 } else if (event.getUnlinkedLandLineNumber() == null) {
176 throw new NullPointerException("event.unlinkedLandLineNumber is null"); //NOI18N
177 } else if (event.getUnlinkedLandLineNumber().getPhoneId() == null) {
179 throw new NullPointerException("event.unlinkedLandLineNumber.contactId is null"); //NOI18N
180 } else if (event.getUnlinkedLandLineNumber().getPhoneId() < 1) {
182 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedLandLineNumber(), event.getUnlinkedLandLineNumber().getPhoneId())); //NOI18N
185 // Remove it from list
186 this.contacts.remove(event.getUnlinkedLandLineNumber());
193 * Event observer for unlinked mobile contact by administrators
195 * @param event Unlinked mobile contact event
197 public void afterAdminUnlinkedMobileContactDataEvent (@Observes final ObservableAdminUnlinkedMobileNumberEvent event) {
198 // event should not be null
201 throw new NullPointerException("event is null"); //NOI18N
202 } else if (event.getUnlinkedMobileNumber() == null) {
204 throw new NullPointerException("event.unlinkedMobileNumber is null"); //NOI18N
205 } else if (event.getUnlinkedMobileNumber().getPhoneId() == null) {
207 throw new NullPointerException("event.unlinkedMobileNumber.contactId is null"); //NOI18N
208 } else if (event.getUnlinkedMobileNumber().getPhoneId() < 1) {
210 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedMobileNumber(), event.getUnlinkedMobileNumber().getPhoneId())); //NOI18N
213 // Remove it from list
214 this.contacts.remove(event.getUnlinkedMobileNumber());
221 * Event observer for updated contact data by administrators
223 * @param event Updated contact data event
225 public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent event) {
226 // event should not be null
229 throw new NullPointerException("event is null"); //NOI18N
230 } else if (event.getUpdatedContact() == null) {
232 throw new NullPointerException("event.updatedContact is null"); //NOI18N
233 } else if (event.getUpdatedContact().getContactId() == null) {
235 throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
236 } else if (event.getUpdatedContact().getContactId() < 1) {
238 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
246 public List<Contact> allFaxNumberContacts () {
248 DialableFaxNumber faxNumber = this.beanHelper.getFaxNumber();
251 if (this.contacts.containsKey(faxNumber)) {
252 // Return cached version
253 return this.contacts.get(faxNumber);
256 List<Contact> list = new LinkedList<>();
258 // "Walk" through all contacts
259 for (final Contact contact : this.contactController.allContacts()) {
260 // Is mobile instance the same?
261 if (Objects.equals(contact.getContactFaxNumber(), this.beanHelper.getFaxNumber())) {
267 // Store result in cache
268 this.contacts.put(faxNumber, list);
270 // Return now-cached list
276 public List<Contact> allLandLineNumberContacts () {
278 DialableLandLineNumber landLineNumber = this.beanHelper.getLandLineNumber();
281 if (this.contacts.containsKey(landLineNumber)) {
282 // Return cached version
283 return this.contacts.get(landLineNumber);
286 List<Contact> list = new LinkedList<>();
288 // "Walk" through all contacts
289 for (final Contact contact : this.contactController.allContacts()) {
290 // Is mobile instance the same?
291 if (Objects.equals(contact.getContactLandLineNumber(), this.beanHelper.getLandLineNumber())) {
297 // Store result in cache
298 this.contacts.put(landLineNumber, list);
300 // Return now-cached list
306 public List<Contact> allMobileNumberContacts () {
308 DialableMobileNumber mobileNumber = this.beanHelper.getMobileNumber();
311 if (this.contacts.containsKey(mobileNumber)) {
312 // Return cached version
313 return this.contacts.get(mobileNumber);
316 List<Contact> list = new LinkedList<>();
318 // "Walk" through all contacts
319 for (final Contact contact : this.contactController.allContacts()) {
320 // Is mobile instance the same?
321 if (Objects.equals(contact.getContactMobileNumber(), this.beanHelper.getMobileNumber())) {
327 // Store result in cache
328 this.contacts.put(mobileNumber, list);
330 // Return now-cached list
338 private void clear () {