]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/contact/phone/JobsContactPhoneWebSessionBean.java
Continued with cell phone: (please cherry-pick)
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / contact / phone / JobsContactPhoneWebSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jjobs.beans.contact.phone;
18
19 import java.text.MessageFormat;
20 import java.util.HashMap;
21 import java.util.LinkedList;
22 import java.util.List;
23 import java.util.Map;
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;
42
43 /**
44  * A general contact phone bean (controller)
45  * <p>
46  * @author Roland Haeder<roland@mxchange.org>
47  */
48 @Named ("contactPhoneController")
49 @SessionScoped
50 public class JobsContactPhoneWebSessionBean extends BaseJobsController implements JobsContactPhoneWebSessionController {
51
52         /**
53          * Serial number
54          */
55         private static final long serialVersionUID = 542_145_347_916L;
56
57         /**
58          * Administrative phone controller
59          */
60         @Inject
61         private JobsAdminPhoneWebRequestController adminPhoneController;
62
63         /**
64          * General contact controller
65          */
66         @Inject
67         private JobsContactWebSessionController contactController;
68
69         /**
70          * "Cache" for contact lists, mostly only one is assigned. So this cache
71          * shouldn't grow beyond control.
72          */
73         private final Map<Long, List<Contact>> contacts;
74
75         /**
76          * Remote EJB for phone number (administrative)
77          */
78         private PhoneSessionBeanRemote phoneBean;
79
80         /**
81          * Default constructor
82          */
83         public JobsContactPhoneWebSessionBean () {
84                 // Try it
85                 try {
86                         // Get initial context
87                         Context context = new InitialContext();
88
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) {
92                         // Throw again
93                         throw new FaceletException(e);
94                 }
95
96                 // Init lists/maps
97                 this.contacts = new HashMap<>(10);
98         }
99
100         @Override
101         public void afterAdminAddedContact (@Observes final AdminAddedContactEvent event) {
102                 // The event must be valid
103                 if (null == event) {
104                         // Throw NPE
105                         throw new NullPointerException("event is null"); //NOI18N
106                 } else if (event.getAddedContact() == null) {
107                         // Throw again ...
108                         throw new NullPointerException("event.addedContact is null"); //NOI18N
109                 } else if (event.getAddedContact().getContactId() == null) {
110                         // ... and again
111                         throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
112                 } else if (event.getAddedContact().getContactId() < 1) {
113                         // Not valid
114                         throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N
115                 }
116
117                 // Clear this bean
118                 this.clear();
119         }
120
121         @Override
122         public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
123                 // event should not be null
124                 if (null == event) {
125                         // Throw NPE
126                         throw new NullPointerException("event is null"); //NOI18N
127                 } else if (event.getAddedUser() == null) {
128                         // Throw NPE again
129                         throw new NullPointerException("event.addedUser is null"); //NOI18N
130                 } else if (event.getAddedUser().getUserId() == null) {
131                         // userId is null
132                         throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
133                 } else if (event.getAddedUser().getUserId() < 1) {
134                         // Not avalid id
135                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
136                 }
137
138                 // Clear all data
139                 this.clear();
140         }
141
142         @Override
143         public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent event) {
144                 // event should not be null
145                 if (null == event) {
146                         // Throw NPE
147                         throw new NullPointerException("event is null"); //NOI18N
148                 } else if (event.getUpdatedContact() == null) {
149                         // Throw NPE again
150                         throw new NullPointerException("event.updatedContact is null"); //NOI18N
151                 } else if (event.getUpdatedContact().getContactId() == null) {
152                         // userId is null
153                         throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
154                 } else if (event.getUpdatedContact().getContactId() < 1) {
155                         // Not avalid id
156                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
157                 }
158
159                 // Clear all data
160                 this.clear();
161         }
162
163         @Override
164         public List<Contact> allCellphoneContacts () {
165                 // Get id
166                 Long phoneId = this.adminPhoneController.getCellPhone().getPhoneId();
167
168                 // Is cache there?
169                 if (this.contacts.containsKey(phoneId)) {
170                         // Return cached version
171                         return this.contacts.get(phoneId);
172                 } else {
173                         // Ask bean
174                         List<Contact> list = new LinkedList<>();
175
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())) {
180                                         // Found one
181                                         list.add(contact);
182                                 }
183                         }
184
185                         // Store result in cache
186                         this.contacts.put(phoneId, list);
187
188                         // Return now-cached list
189                         return list;
190                 }
191         }
192
193         /**
194          * Post-initialization of this class
195          */
196         @PostConstruct
197         public void init () {
198         }
199
200         /**
201          * Clears this bean
202          */
203         private void clear () {
204                 // Clear all data
205         }
206
207 }