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 javax.enterprise.context.RequestScoped;
21 import javax.enterprise.event.Event;
22 import javax.enterprise.event.Observes;
23 import javax.enterprise.inject.Any;
24 import javax.faces.view.facelets.FaceletException;
25 import javax.inject.Inject;
26 import javax.inject.Named;
27 import javax.naming.Context;
28 import javax.naming.InitialContext;
29 import javax.naming.NamingException;
30 import org.mxchange.jcontacts.contact.Contact;
31 import org.mxchange.jcontacts.events.cellphone.unlinked.AdminCellphoneNumberUnlinkedEvent;
32 import org.mxchange.jcontacts.events.cellphone.unlinked.AdminUnlinkedCellphoneNumberEvent;
33 import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent;
34 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
35 import org.mxchange.jcontacts.phone.AdminContactsPhoneSessionBeanRemote;
36 import org.mxchange.jjobs.beans.BaseJobsController;
37 import org.mxchange.jjobs.beans.helper.JobsWebRequestController;
38 import org.mxchange.jphone.exceptions.CellphoneNotLinkedException;
39 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
42 * An administrative contact phone controller (bean)
44 * @author Roland Haeder<roland@mxchange.org>
46 @Named ("adminContactPhoneController")
48 public class JobsAdminContactPhoneWebRequestBean extends BaseJobsController implements JobsAdminContactPhoneWebRequestController {
53 private static final long serialVersionUID = 542_145_347_916L;
56 * Administrative EJB for phone number
58 private AdminContactsPhoneSessionBeanRemote adminPhoneBean;
61 * Admin helper instance
64 private JobsWebRequestController beanHelper;
67 * Event being fired when admin unlinks cellphone from contact
71 private Event<AdminUnlinkedCellphoneNumberEvent> cellphoneDataUnlinkedEvent;
76 public JobsAdminContactPhoneWebRequestBean () {
79 // Get initial context
80 Context context = new InitialContext();
82 // Try to lookup the beans
83 this.adminPhoneBean = (AdminContactsPhoneSessionBeanRemote) context.lookup("java:global/jlandingpage-ejb/adminContactPhone!org.mxchange.jcontacts.phone.AdminContactsPhoneSessionBeanRemote"); //NOI18N
84 } catch (final NamingException e) {
86 throw new FaceletException(e);
91 public void afterAdminAddedContact (@Observes final AdminAddedContactEvent event) {
92 // The event must be valid
95 throw new NullPointerException("event is null"); //NOI18N
96 } else if (event.getAddedContact() == null) {
98 throw new NullPointerException("event.addedContact is null"); //NOI18N
99 } else if (event.getAddedContact().getContactId() == null) {
101 throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
102 } else if (event.getAddedContact().getContactId() < 1) {
104 throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N
112 public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
113 // event should not be null
116 throw new NullPointerException("event is null"); //NOI18N
117 } else if (event.getAddedUser() == null) {
119 throw new NullPointerException("event.addedUser is null"); //NOI18N
120 } else if (event.getAddedUser().getUserId() == null) {
122 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
123 } else if (event.getAddedUser().getUserId() < 1) {
125 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
133 public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent event) {
134 // event should not be null
137 throw new NullPointerException("event is null"); //NOI18N
138 } else if (event.getUpdatedContact() == null) {
140 throw new NullPointerException("event.updatedContact is null"); //NOI18N
141 } else if (event.getUpdatedContact().getContactId() == null) {
143 throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
144 } else if (event.getUpdatedContact().getContactId() < 1) {
146 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
154 public String unlinkCellphoneContactData () {
156 if (this.beanHelper.getCellPhoneNumber() == null) {
157 // Not set, throw NPE
158 throw new NullPointerException("this.beanHelper.cellphoneNumber is null"); //NOI18N
159 } else if (this.beanHelper.getCellPhoneNumber().getPhoneId() == null) {
161 throw new NullPointerException("this.beanHelper.cellphoneNumber.phoneId is null"); //NOI18N
162 } else if (this.beanHelper.getCellPhoneNumber().getPhoneId() < 1) {
164 throw new IllegalArgumentException(MessageFormat.format("this.beanHelper.cellphoneNumber.phoneId={0} is not valid", this.beanHelper.getCellPhoneNumber().getPhoneId())); //NOI18N
165 } else if (this.beanHelper.getCellPhoneNumber().getCellphoneProvider() == null) {
167 throw new NullPointerException("this.beanHelper.cellphoneNumber.cellphoneProvider is null"); //NOI18N
168 } else if (this.beanHelper.getCellPhoneNumber().getCellphoneProvider().getProviderId() == null) {
170 throw new NullPointerException("this.beanHelper.cellphoneNumber.cellphoneProvider.providerId is null"); //NOI18N
171 } else if (this.beanHelper.getCellPhoneNumber().getCellphoneProvider().getProviderId() < 1) {
173 throw new IllegalArgumentException(MessageFormat.format("this.beanHelper.cellphoneNumber.cellphoneProvider.providerId={0} is not valid.", this.beanHelper.getCellPhoneNumber().getCellphoneProvider().getProviderId())); //NOI18N
174 } else if (this.beanHelper.getCellPhoneNumber().getPhoneNumber() == null) {
176 throw new NullPointerException("this.beanHelper.cellphoneNumber.phoneNumber is null"); //NOI18N
177 } else if (this.beanHelper.getCellPhoneNumber().getPhoneNumber() < 1) {
178 // Throw it again ...
179 throw new NullPointerException(MessageFormat.format("this.beanHelper.cellphoneNumber.phoneNumber={0} is not valid.", this.beanHelper.getCellPhoneNumber().getPhoneNumber())); //NOI18N
180 } else if (this.beanHelper.getContact() == null) {
181 // ... and throw again
182 throw new NullPointerException("this.beanHelper.contact is null"); //NOI18N
183 } else if (this.beanHelper.getContact().getContactId() == null) {
185 throw new NullPointerException("this.beanHelper.contact.contactId is null"); //NOI18N
186 } else if (this.beanHelper.getContact().getContactId() < 1) {
188 throw new IllegalArgumentException(MessageFormat.format("this.beanHelper.contact.contactId={0} is invalid.", this.beanHelper.getContact().getContactId())); //NOI18N
191 // Init contact instance
192 Contact updatedContact;
195 // Unlink it and return contact without cell phone instance
196 updatedContact = this.adminPhoneBean.unlinkCellphoneDataFromContact(this.beanHelper.getContact(), this.beanHelper.getCellPhoneNumber());
197 } catch (final CellphoneNotLinkedException ex) {
199 this.showFacesMessage("form_unlink_contact_cellphone:cellphoneId", ex); //NOI18N
204 this.cellphoneDataUnlinkedEvent.fire(new AdminCellphoneNumberUnlinkedEvent(updatedContact, this.beanHelper.getCellPhoneNumber()));
207 return "admin_show_contact?faces-redirect=true&includeViewParams=true"; //NOI18N
213 private void clear () {