]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/contact/phone/JobsAdminContactPhoneWebRequestBean.java
removed double //NOI18N (please cherry-pick)
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / contact / phone / JobsAdminContactPhoneWebRequestBean.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 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;
40
41 /**
42  * An administrative contact phone controller (bean)
43  * <p>
44  * @author Roland Haeder<roland@mxchange.org>
45  */
46 @Named ("adminContactPhoneController")
47 @RequestScoped
48 public class JobsAdminContactPhoneWebRequestBean extends BaseJobsController implements JobsAdminContactPhoneWebRequestController {
49
50         /**
51          * Serial number
52          */
53         private static final long serialVersionUID = 542_145_347_916L;
54
55         /**
56          * Administrative EJB for phone number
57          */
58         private AdminContactsPhoneSessionBeanRemote adminPhoneBean;
59
60         /**
61          * Admin helper instance
62          */
63         @Inject
64         private JobsWebRequestController beanHelper;
65
66         /**
67          * Event being fired when admin unlinks cellphone from contact
68          */
69         @Inject
70         @Any
71         private Event<AdminUnlinkedCellphoneNumberEvent> cellphoneDataUnlinkedEvent;
72
73         /**
74          * Default constructor
75          */
76         public JobsAdminContactPhoneWebRequestBean () {
77                 // Try it
78                 try {
79                         // Get initial context
80                         Context context = new InitialContext();
81
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) {
85                         // Throw again
86                         throw new FaceletException(e);
87                 }
88         }
89
90         @Override
91         public void afterAdminAddedContact (@Observes final AdminAddedContactEvent event) {
92                 // The event must be valid
93                 if (null == event) {
94                         // Throw NPE
95                         throw new NullPointerException("event is null"); //NOI18N
96                 } else if (event.getAddedContact() == null) {
97                         // Throw again ...
98                         throw new NullPointerException("event.addedContact is null"); //NOI18N
99                 } else if (event.getAddedContact().getContactId() == null) {
100                         // ... and again
101                         throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
102                 } else if (event.getAddedContact().getContactId() < 1) {
103                         // Not valid
104                         throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N
105                 }
106
107                 // Clear this bean
108                 this.clear();
109         }
110
111         @Override
112         public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
113                 // event should not be null
114                 if (null == event) {
115                         // Throw NPE
116                         throw new NullPointerException("event is null"); //NOI18N
117                 } else if (event.getAddedUser() == null) {
118                         // Throw NPE again
119                         throw new NullPointerException("event.addedUser is null"); //NOI18N
120                 } else if (event.getAddedUser().getUserId() == null) {
121                         // userId is null
122                         throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
123                 } else if (event.getAddedUser().getUserId() < 1) {
124                         // Not avalid id
125                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
126                 }
127
128                 // Clear all data
129                 this.clear();
130         }
131
132         @Override
133         public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent event) {
134                 // event should not be null
135                 if (null == event) {
136                         // Throw NPE
137                         throw new NullPointerException("event is null"); //NOI18N
138                 } else if (event.getUpdatedContact() == null) {
139                         // Throw NPE again
140                         throw new NullPointerException("event.updatedContact is null"); //NOI18N
141                 } else if (event.getUpdatedContact().getContactId() == null) {
142                         // userId is null
143                         throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
144                 } else if (event.getUpdatedContact().getContactId() < 1) {
145                         // Not avalid id
146                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
147                 }
148
149                 // Clear all data
150                 this.clear();
151         }
152
153         @Override
154         public String unlinkCellphoneContactData () {
155                 // Is all data set
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) {
160                         // Throw NPE again
161                         throw new NullPointerException("this.beanHelper.cellphoneNumber.phoneId is null"); //NOI18N
162                 } else if (this.beanHelper.getCellPhoneNumber().getPhoneId() < 1) {
163                         // Invalid number
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) {
166                         // Throw NPE
167                         throw new NullPointerException("this.beanHelper.cellphoneNumber.cellphoneProvider is null"); //NOI18N
168                 } else if (this.beanHelper.getCellPhoneNumber().getCellphoneProvider().getProviderId() == null) {
169                         // ... throw again
170                         throw new NullPointerException("this.beanHelper.cellphoneNumber.cellphoneProvider.providerId is null"); //NOI18N
171                 } else if (this.beanHelper.getCellPhoneNumber().getCellphoneProvider().getProviderId() < 1) {
172                         // Id not valid
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) {
175                         // Throw NPE again
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) {
184                         // ... and again ...
185                         throw new NullPointerException("this.beanHelper.contact.contactId is null"); //NOI18N
186                 } else if (this.beanHelper.getContact().getContactId() < 1) {
187                         // Invalid id number
188                         throw new IllegalArgumentException(MessageFormat.format("this.beanHelper.contact.contactId={0} is invalid.", this.beanHelper.getContact().getContactId())); //NOI18N
189                 }
190
191                 // Init contact instance
192                 Contact updatedContact;
193
194                 try {
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) {
198                         // Did not work
199                         this.showFacesMessage("form_unlink_contact_cellphone:cellphoneId", ex); //NOI18N
200                         return ""; //NOI18N
201                 }
202
203                 // Fire event
204                 this.cellphoneDataUnlinkedEvent.fire(new AdminCellphoneNumberUnlinkedEvent(updatedContact, this.beanHelper.getCellPhoneNumber()));
205
206                 // All fine here
207                 return "admin_show_contact?faces-redirect=true&includeViewParams=true"; //NOI18N
208         }
209
210         /**
211          * Clears this bean
212          */
213         private void clear () {
214                 // Clear all data
215         }
216
217 }