2 * Copyright (C) 2016 Cho-Time GmbH
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 de.chotime.landingpage.beans.BaseLandingController;
20 import de.chotime.landingpage.beans.contact.LandingContactWebSessionController;
21 import de.chotime.landingpage.beans.helper.LandingWebRequestController;
22 import java.text.MessageFormat;
23 import java.util.HashMap;
24 import java.util.LinkedList;
25 import java.util.List;
27 import java.util.Objects;
28 import javax.enterprise.context.SessionScoped;
29 import javax.enterprise.event.Observes;
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 import org.mxchange.jcontacts.contact.Contact;
33 import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent;
34 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
35 import org.mxchange.jcontacts.events.mobile.unlinked.AdminUnlinkedMobileNumberEvent;
36 import org.mxchange.jphone.phonenumbers.DialableNumber;
37 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
38 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
41 * An administrative contact phone controller (bean)
43 * @author Roland Haeder<rhaeder@cho-time.de>
45 @Named ("contactPhoneController")
47 public class JobsContactPhoneWebSessionBean extends BaseLandingController implements JobsContactPhoneWebSessionController {
52 private static final long serialVersionUID = 542_145_347_916L;
55 * Admin helper instance
58 private LandingWebRequestController beanHelper;
61 * General contact controller
64 private LandingContactWebSessionController contactController;
67 * "Cache" for contact's mobile, land-line and fax numbers. Currently one
68 * one per each type is supported. Maybe later this will change into a
69 * OneToMany relationship (one contact, many numbers).
71 private final Map<DialableNumber, List<Contact>> contacts;
76 public JobsContactPhoneWebSessionBean () {
78 this.contacts = new HashMap<>(10);
82 public void afterAdminAddedContact (@Observes final AdminAddedContactEvent event) {
83 // The event must be valid
86 throw new NullPointerException("event is null"); //NOI18N
87 } else if (event.getAddedContact() == null) {
89 throw new NullPointerException("event.addedContact is null"); //NOI18N
90 } else if (event.getAddedContact().getContactId() == null) {
92 throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
93 } else if (event.getAddedContact().getContactId() < 1) {
95 throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N
103 public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
104 // event should not be null
107 throw new NullPointerException("event is null"); //NOI18N
108 } else if (event.getAddedUser() == null) {
110 throw new NullPointerException("event.addedUser is null"); //NOI18N
111 } else if (event.getAddedUser().getUserId() == null) {
113 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
114 } else if (event.getAddedUser().getUserId() < 1) {
116 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
124 public void afterAdminUnlinkedCellphoneContactDataEvent (@Observes final AdminUnlinkedMobileNumberEvent event) {
125 // event should not be null
128 throw new NullPointerException("event is null"); //NOI18N
129 } else if (event.getUnlinkedCellphoneNumber() == null) {
131 throw new NullPointerException("event.unlinkedCellphoneNumber is null"); //NOI18N
132 } else if (event.getUnlinkedCellphoneNumber().getPhoneId() == null) {
134 throw new NullPointerException("event.unlinkedCellphoneNumber.contactId is null"); //NOI18N
135 } else if (event.getUnlinkedCellphoneNumber().getPhoneId() < 1) {
137 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedCellphoneNumber(), event.getUnlinkedCellphoneNumber().getPhoneId())); //NOI18N
140 // Remove it from list
141 this.contacts.remove(event.getUnlinkedCellphoneNumber());
148 public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent event) {
149 // event should not be null
152 throw new NullPointerException("event is null"); //NOI18N
153 } else if (event.getUpdatedContact() == null) {
155 throw new NullPointerException("event.updatedContact is null"); //NOI18N
156 } else if (event.getUpdatedContact().getContactId() == null) {
158 throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
159 } else if (event.getUpdatedContact().getContactId() < 1) {
161 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
169 public List<Contact> allMobileNumberContacts () {
171 DialableMobileNumber mobileNumber = this.beanHelper.getMobileNumber();
174 if (this.contacts.containsKey(mobileNumber)) {
175 // Return cached version
176 return this.contacts.get(mobileNumber);
179 List<Contact> list = new LinkedList<>();
181 // "Walk" through all contacts
182 for (final Contact contact : this.contactController.allContacts()) {
183 // Is mobile instance the same?
184 if (Objects.equals(contact.getContactMobileNumber(), this.beanHelper.getMobileNumber())) {
190 // Store result in cache
191 this.contacts.put(mobileNumber, list);
193 // Return now-cached list
201 private void clear () {