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.phone;
19 import java.text.MessageFormat;
20 import java.util.LinkedList;
21 import java.util.List;
22 import java.util.Objects;
23 import javax.annotation.PostConstruct;
24 import javax.enterprise.context.ApplicationScoped;
25 import javax.enterprise.event.Observes;
26 import javax.faces.view.facelets.FaceletException;
27 import javax.inject.Named;
28 import javax.naming.Context;
29 import javax.naming.InitialContext;
30 import javax.naming.NamingException;
31 import org.mxchange.jcontacts.contact.Contact;
32 import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent;
33 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
34 import org.mxchange.jjobs.beans.BaseJobsController;
35 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
36 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
37 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
38 import org.mxchange.jphone.phonenumbers.phone.PhoneSessionBeanRemote;
39 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
42 * Regular controller (bean) for phone numbers
44 * @author Roland Haeder<roland@mxchange.org>
46 @Named ("phoneController")
48 public class JobsPhoneWebApplicationBean extends BaseJobsController implements JobsPhoneWebApplicationController {
53 private static final long serialVersionUID = 491_058_674_675_690_105L;
56 * All cell phone numbers
58 private final List<DialableCellphoneNumber> cellphoneNumbers;
63 private final List<DialableFaxNumber> faxNumbers;
66 * All land-line numbers
68 private final List<DialableLandLineNumber> landLineNumbers;
71 * General EJB for phone numbers
73 private PhoneSessionBeanRemote phoneBean;
78 public JobsPhoneWebApplicationBean () {
81 // Get initial context
82 Context context = new InitialContext();
84 // Try to lookup the beans
85 this.phoneBean = (PhoneSessionBeanRemote) context.lookup("java:global/jlandingpage-ejb/phone!org.mxchange.jphone.phonenumbers.phone.PhoneSessionBeanRemote"); //NOI18N
86 } catch (final NamingException e) {
88 throw new FaceletException(e);
92 this.cellphoneNumbers = new LinkedList<>();
93 this.faxNumbers = new LinkedList<>();
94 this.landLineNumbers = new LinkedList<>();
98 public void afterAdminAddedContact (@Observes final AdminAddedContactEvent event) {
99 // The event must be valid
102 throw new NullPointerException("event is null"); //NOI18N
103 } else if (event.getAddedContact() == null) {
105 throw new NullPointerException("event.addedContact is null"); //NOI18N
106 } else if (event.getAddedContact().getContactId() == null) {
108 throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
109 } else if (event.getAddedContact().getContactId() < 1) {
111 throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N
114 // Update contact's cellphone, land-line and fax number
115 this.updateContactPhoneNumbers(event.getAddedContact());
122 public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
123 // event should not be null
126 throw new NullPointerException("event is null"); //NOI18N
127 } else if (event.getAddedUser() == null) {
129 throw new NullPointerException("event.addedUser is null"); //NOI18N
130 } else if (event.getAddedUser().getUserId() == null) {
132 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
133 } else if (event.getAddedUser().getUserId() < 1) {
135 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
138 // Update contact's cellphone, land-line and fax number
139 this.updateContactPhoneNumbers(event.getAddedUser().getUserContact());
146 public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent event) {
147 // event should not be null
150 throw new NullPointerException("event is null"); //NOI18N
151 } else if (event.getUpdatedContact() == null) {
153 throw new NullPointerException("event.updatedContact is null"); //NOI18N
154 } else if (event.getUpdatedContact().getContactId() == null) {
156 throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
157 } else if (event.getUpdatedContact().getContactId() < 1) {
159 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
162 // Update contact's cellphone, land-line and fax number
163 this.updateContactPhoneNumbers(event.getUpdatedContact());
170 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
171 public List<DialableCellphoneNumber> allCellphoneNumbers () {
172 return this.cellphoneNumbers;
176 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
177 public List<DialableFaxNumber> allFaxNumbers () {
178 return this.faxNumbers;
182 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
183 public List<DialableLandLineNumber> allLandLineNumbers () {
184 return this.landLineNumbers;
188 * Post-construction method
191 public void init () {
193 this.cellphoneNumbers.addAll(this.phoneBean.allCellphoneNumbers());
194 this.faxNumbers.addAll(this.phoneBean.allFaxNumbers());
195 this.landLineNumbers.addAll(this.phoneBean.allLandLineNumbers());
201 private void clear () {
206 * Uniquely add given cellphone number to this bean's list. First remove the
207 * old instance (by id number), then re-add it again.
209 * @param cellphoneNumber Cellphone number to add
211 private void uniqueAddCellphoneNumber (final DialableCellphoneNumber cellphoneNumber) {
212 // Make sure the parameter is valid
213 if (null == cellphoneNumber) {
215 throw new NullPointerException("cellphoneNumber is null");
216 } else if (cellphoneNumber.getPhoneId() == null) {
218 throw new NullPointerException("cellphoneNumber.phoneId is null");
219 } else if (cellphoneNumber.getPhoneId() < 1) {
221 throw new IllegalArgumentException(MessageFormat.format("cellphoneNumber.phoneId={0} is not valid.", cellphoneNumber.getPhoneId()));
224 // First remove it by object
225 if (!this.cellphoneNumbers.remove(cellphoneNumber)) {
226 // Did not work, try by id number
227 for (final DialableCellphoneNumber cell : this.cellphoneNumbers) {
228 // Is id number the same?
229 if (Objects.equals(cell.getPhoneId(), cellphoneNumber.getPhoneId())) {
231 this.cellphoneNumbers.remove(cell);
238 this.cellphoneNumbers.add(cellphoneNumber);
242 * Uniquely add given fax number to this bean's list. First remove the old
243 * instance (by id number), then re-add it again.
245 * @param faxNumber number to add
247 private void uniqueAddFaxNumber (final DialableFaxNumber faxNumber) {
248 // Make sure the parameter is valid
249 if (null == faxNumber) {
251 throw new NullPointerException("faxNumber is null");
252 } else if (faxNumber.getPhoneId() == null) {
254 throw new NullPointerException("faxNumber.phoneId is null");
255 } else if (faxNumber.getPhoneId() < 1) {
257 throw new IllegalArgumentException(MessageFormat.format("faxNumber.phoneId={0} is not valid.", faxNumber.getPhoneId()));
261 if (!this.faxNumbers.remove(faxNumber)) {
262 // Did not work, try by id number
263 for (final DialableFaxNumber fax : this.faxNumbers) {
264 // Is id number the same?
265 if (Objects.equals(fax.getPhoneId(), faxNumber.getPhoneId())) {
267 this.faxNumbers.remove(fax);
274 this.faxNumbers.add(faxNumber);
278 * Uniquely add given land-line number to this bean's list. First remove the
279 * old instance (by id number), then re-add it again.
281 * @param landLineNumber Land-line number to add
283 private void uniqueAddLandLineNumber (final DialableLandLineNumber landLineNumber) {
284 // Make sure the parameter is valid
285 if (null == landLineNumber) {
287 throw new NullPointerException("landLineNumber is null");
288 } else if (landLineNumber.getPhoneId() == null) {
290 throw new NullPointerException("landLineNumber.phoneId is null");
291 } else if (landLineNumber.getPhoneId() < 1) {
293 throw new IllegalArgumentException(MessageFormat.format("landLineNumber.phoneId={0} is not valid.", landLineNumber.getPhoneId()));
297 if (!this.landLineNumbers.remove(landLineNumber)) {
298 // Did not work, try by id number
299 for (final DialableLandLineNumber landLine : this.landLineNumbers) {
300 // Is id number the same?
301 if (Objects.equals(landLine.getPhoneId(), landLineNumber.getPhoneId())) {
303 this.landLineNumbers.remove(landLine);
310 this.landLineNumbers.add(landLineNumber);
314 * Updates given contact's cellphone, land-line and fax number
316 * @param contact Contact instance
318 private void updateContactPhoneNumbers (final Contact contact) {
319 // Parameter must be valid
320 if (null == contact) {
322 throw new NullPointerException("contact is null");
323 } else if (contact.getContactId() == null) {
325 throw new NullPointerException("contact.contactId is null");
326 } else if (contact.getContactId() < 1) {
327 // Id number is not valid
331 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
333 this.uniqueAddCellphoneNumber(contact.getContactCellphoneNumber());
337 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
339 this.uniqueAddLandLineNumber(contact.getContactLandLineNumber());
343 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
345 this.uniqueAddFaxNumber(contact.getContactFaxNumber());