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.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.AdminAddedContactEvent;
31 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
32 import org.mxchange.jcontacts.events.fax.unlinked.AdminUnlinkedFaxNumberEvent;
33 import org.mxchange.jcontacts.events.landline.unlinked.AdminUnlinkedLandLineNumberEvent;
34 import org.mxchange.jcontacts.events.mobile.unlinked.AdminUnlinkedMobileNumberEvent;
35 import org.mxchange.jjobs.beans.BaseJobsController;
36 import org.mxchange.jjobs.beans.contact.JobsContactWebSessionController;
37 import org.mxchange.jjobs.beans.helper.JobsWebViewHelperController;
38 import org.mxchange.jphone.phonenumbers.DialableNumber;
39 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
40 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
41 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
42 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
45 * An administrative contact phone controller (bean)
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;
62 private JobsWebViewHelperController beanHelper;
65 * General contact controller
68 private JobsContactWebSessionController 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 JobsContactPhoneWebSessionBean () {
82 this.contacts = new HashMap<>(10);
86 public void afterAdminAddedContact (@Observes final AdminAddedContactEvent event) {
87 // The event must be valid
90 throw new NullPointerException("event is null"); //NOI18N
91 } else if (event.getAddedContact() == null) {
93 throw new NullPointerException("event.addedContact is null"); //NOI18N
94 } else if (event.getAddedContact().getContactId() == null) {
96 throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
97 } else if (event.getAddedContact().getContactId() < 1) {
99 throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N
107 public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
108 // event should not be null
111 throw new NullPointerException("event is null"); //NOI18N
112 } else if (event.getAddedUser() == null) {
114 throw new NullPointerException("event.addedUser is null"); //NOI18N
115 } else if (event.getAddedUser().getUserId() == null) {
117 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
118 } else if (event.getAddedUser().getUserId() < 1) {
120 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
128 public void afterAdminUnlinkedFaxContactDataEvent (@Observes final AdminUnlinkedFaxNumberEvent event) {
129 // event should not be null
132 throw new NullPointerException("event is null"); //NOI18N
133 } else if (event.getUnlinkedFaxNumber() == null) {
135 throw new NullPointerException("event.unlinkedFaxNumber is null"); //NOI18N
136 } else if (event.getUnlinkedFaxNumber().getPhoneId() == null) {
138 throw new NullPointerException("event.unlinkedFaxNumber.contactId is null"); //NOI18N
139 } else if (event.getUnlinkedFaxNumber().getPhoneId() < 1) {
141 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedFaxNumber(), event.getUnlinkedFaxNumber().getPhoneId())); //NOI18N
144 // Remove it from list
145 this.contacts.remove(event.getUnlinkedFaxNumber());
152 public void afterAdminUnlinkedLandLineContactDataEvent (@Observes final AdminUnlinkedLandLineNumberEvent event) {
153 // event should not be null
156 throw new NullPointerException("event is null"); //NOI18N
157 } else if (event.getUnlinkedLandLineNumber() == null) {
159 throw new NullPointerException("event.unlinkedLandLineNumber is null"); //NOI18N
160 } else if (event.getUnlinkedLandLineNumber().getPhoneId() == null) {
162 throw new NullPointerException("event.unlinkedLandLineNumber.contactId is null"); //NOI18N
163 } else if (event.getUnlinkedLandLineNumber().getPhoneId() < 1) {
165 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedLandLineNumber(), event.getUnlinkedLandLineNumber().getPhoneId())); //NOI18N
168 // Remove it from list
169 this.contacts.remove(event.getUnlinkedLandLineNumber());
176 public void afterAdminUnlinkedMobileContactDataEvent (@Observes final AdminUnlinkedMobileNumberEvent event) {
177 // event should not be null
180 throw new NullPointerException("event is null"); //NOI18N
181 } else if (event.getUnlinkedMobileNumber() == null) {
183 throw new NullPointerException("event.unlinkedMobileNumber is null"); //NOI18N
184 } else if (event.getUnlinkedMobileNumber().getPhoneId() == null) {
186 throw new NullPointerException("event.unlinkedMobileNumber.contactId is null"); //NOI18N
187 } else if (event.getUnlinkedMobileNumber().getPhoneId() < 1) {
189 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedMobileNumber(), event.getUnlinkedMobileNumber().getPhoneId())); //NOI18N
192 // Remove it from list
193 this.contacts.remove(event.getUnlinkedMobileNumber());
200 public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent event) {
201 // event should not be null
204 throw new NullPointerException("event is null"); //NOI18N
205 } else if (event.getUpdatedContact() == null) {
207 throw new NullPointerException("event.updatedContact is null"); //NOI18N
208 } else if (event.getUpdatedContact().getContactId() == null) {
210 throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
211 } else if (event.getUpdatedContact().getContactId() < 1) {
213 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
221 public List<Contact> allFaxNumberContacts () {
223 DialableFaxNumber faxNumber = this.beanHelper.getFaxNumber();
226 if (this.contacts.containsKey(faxNumber)) {
227 // Return cached version
228 return this.contacts.get(faxNumber);
231 List<Contact> list = new LinkedList<>();
233 // "Walk" through all contacts
234 for (final Contact contact : this.contactController.allContacts()) {
235 // Is mobile instance the same?
236 if (Objects.equals(contact.getContactFaxNumber(), this.beanHelper.getFaxNumber())) {
242 // Store result in cache
243 this.contacts.put(faxNumber, list);
245 // Return now-cached list
251 public List<Contact> allLandLineNumberContacts () {
253 DialableLandLineNumber landLineNumber = this.beanHelper.getLandLineNumber();
256 if (this.contacts.containsKey(landLineNumber)) {
257 // Return cached version
258 return this.contacts.get(landLineNumber);
261 List<Contact> list = new LinkedList<>();
263 // "Walk" through all contacts
264 for (final Contact contact : this.contactController.allContacts()) {
265 // Is mobile instance the same?
266 if (Objects.equals(contact.getContactLandLineNumber(), this.beanHelper.getLandLineNumber())) {
272 // Store result in cache
273 this.contacts.put(landLineNumber, list);
275 // Return now-cached list
281 public List<Contact> allMobileNumberContacts () {
283 DialableMobileNumber mobileNumber = this.beanHelper.getMobileNumber();
286 if (this.contacts.containsKey(mobileNumber)) {
287 // Return cached version
288 return this.contacts.get(mobileNumber);
291 List<Contact> list = new LinkedList<>();
293 // "Walk" through all contacts
294 for (final Contact contact : this.contactController.allContacts()) {
295 // Is mobile instance the same?
296 if (Objects.equals(contact.getContactMobileNumber(), this.beanHelper.getMobileNumber())) {
302 // Store result in cache
303 this.contacts.put(mobileNumber, list);
305 // Return now-cached list
313 private void clear () {