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