]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/phone/JobsPhoneWebApplicationBean.java
removed double //NOI18N (please cherry-pick)
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / phone / JobsPhoneWebApplicationBean.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.phone;
18
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;
40
41 /**
42  * Regular controller (bean) for phone numbers
43  * <p>
44  * @author Roland Haeder<roland@mxchange.org>
45  */
46 @Named ("phoneController")
47 @ApplicationScoped
48 public class JobsPhoneWebApplicationBean extends BaseJobsController implements JobsPhoneWebApplicationController {
49
50         /**
51          * Serial number
52          */
53         private static final long serialVersionUID = 491_058_674_675_690_105L;
54
55         /**
56          * All cell phone numbers
57          */
58         private final List<DialableCellphoneNumber> cellphoneNumbers;
59
60         /**
61          * All fax numbers
62          */
63         private final List<DialableFaxNumber> faxNumbers;
64
65         /**
66          * All land-line numbers
67          */
68         private final List<DialableLandLineNumber> landLineNumbers;
69
70         /**
71          * General EJB for phone numbers
72          */
73         private PhoneSessionBeanRemote phoneBean;
74
75         /**
76          * Default constructor
77          */
78         public JobsPhoneWebApplicationBean () {
79                 // Try it
80                 try {
81                         // Get initial context
82                         Context context = new InitialContext();
83
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) {
87                         // Throw it again
88                         throw new FaceletException(e);
89                 }
90
91                 // Init all lists
92                 this.cellphoneNumbers = new LinkedList<>();
93                 this.faxNumbers = new LinkedList<>();
94                 this.landLineNumbers = new LinkedList<>();
95         }
96
97         @Override
98         public void afterAdminAddedContact (@Observes final AdminAddedContactEvent event) {
99                 // The event must be valid
100                 if (null == event) {
101                         // Throw NPE
102                         throw new NullPointerException("event is null"); //NOI18N
103                 } else if (event.getAddedContact() == null) {
104                         // Throw again ...
105                         throw new NullPointerException("event.addedContact is null"); //NOI18N
106                 } else if (event.getAddedContact().getContactId() == null) {
107                         // ... and again
108                         throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
109                 } else if (event.getAddedContact().getContactId() < 1) {
110                         // Not valid
111                         throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N
112                 }
113
114                 // Update contact's cellphone, land-line and fax number
115                 this.updateContactPhoneNumbers(event.getAddedContact());
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                 // Update contact's cellphone, land-line and fax number
139                 this.updateContactPhoneNumbers(event.getAddedUser().getUserContact());
140
141                 // Clear all data
142                 this.clear();
143         }
144
145         @Override
146         public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent event) {
147                 // event should not be null
148                 if (null == event) {
149                         // Throw NPE
150                         throw new NullPointerException("event is null"); //NOI18N
151                 } else if (event.getUpdatedContact() == null) {
152                         // Throw NPE again
153                         throw new NullPointerException("event.updatedContact is null"); //NOI18N
154                 } else if (event.getUpdatedContact().getContactId() == null) {
155                         // userId is null
156                         throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
157                 } else if (event.getUpdatedContact().getContactId() < 1) {
158                         // Not avalid id
159                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
160                 }
161
162                 // Update contact's cellphone, land-line and fax number
163                 this.updateContactPhoneNumbers(event.getUpdatedContact());
164
165                 // Clear all data
166                 this.clear();
167         }
168
169         @Override
170         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
171         public List<DialableCellphoneNumber> allCellphoneNumbers () {
172                 return this.cellphoneNumbers;
173         }
174
175         @Override
176         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
177         public List<DialableFaxNumber> allFaxNumbers () {
178                 return this.faxNumbers;
179         }
180
181         @Override
182         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
183         public List<DialableLandLineNumber> allLandLineNumbers () {
184                 return this.landLineNumbers;
185         }
186
187         /**
188          * Post-construction method
189          */
190         @PostConstruct
191         public void init () {
192                 // All phone numbers
193                 this.cellphoneNumbers.addAll(this.phoneBean.allCellphoneNumbers());
194                 this.faxNumbers.addAll(this.phoneBean.allFaxNumbers());
195                 this.landLineNumbers.addAll(this.phoneBean.allLandLineNumbers());
196         }
197
198         /**
199          * Clears this bean
200          */
201         private void clear () {
202                 // Clear all data
203         }
204
205         /**
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.
208          * <p>
209          * @param cellphoneNumber Cellphone number to add
210          */
211         private void uniqueAddCellphoneNumber (final DialableCellphoneNumber cellphoneNumber) {
212                 // Make sure the parameter is valid
213                 if (null == cellphoneNumber) {
214                         // Throw NPE
215                         throw new NullPointerException("cellphoneNumber is null");
216                 } else if (cellphoneNumber.getPhoneId() == null) {
217                         // Throw again ...
218                         throw new NullPointerException("cellphoneNumber.phoneId is null");
219                 } else if (cellphoneNumber.getPhoneId() < 1) {
220                         // Not valid
221                         throw new IllegalArgumentException(MessageFormat.format("cellphoneNumber.phoneId={0} is not valid.", cellphoneNumber.getPhoneId()));
222                 }
223
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())) {
230                                         // Found it
231                                         this.cellphoneNumbers.remove(cell);
232                                         break;
233                                 }
234                         }
235                 }
236
237                 // ... then add it
238                 this.cellphoneNumbers.add(cellphoneNumber);
239         }
240
241         /**
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.
244          * <p>
245          * @param faxNumber number to add
246          */
247         private void uniqueAddFaxNumber (final DialableFaxNumber faxNumber) {
248                 // Make sure the parameter is valid
249                 if (null == faxNumber) {
250                         // Throw NPE
251                         throw new NullPointerException("faxNumber is null");
252                 } else if (faxNumber.getPhoneId() == null) {
253                         // Throw again ...
254                         throw new NullPointerException("faxNumber.phoneId is null");
255                 } else if (faxNumber.getPhoneId() < 1) {
256                         // Not valid
257                         throw new IllegalArgumentException(MessageFormat.format("faxNumber.phoneId={0} is not valid.", faxNumber.getPhoneId()));
258                 }
259
260                 // First remove it
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())) {
266                                         // Found it
267                                         this.faxNumbers.remove(fax);
268                                         break;
269                                 }
270                         }
271                 }
272
273                 // ... then add it
274                 this.faxNumbers.add(faxNumber);
275         }
276
277         /**
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.
280          * <p>
281          * @param landLineNumber Land-line number to add
282          */
283         private void uniqueAddLandLineNumber (final DialableLandLineNumber landLineNumber) {
284                 // Make sure the parameter is valid
285                 if (null == landLineNumber) {
286                         // Throw NPE
287                         throw new NullPointerException("landLineNumber is null");
288                 } else if (landLineNumber.getPhoneId() == null) {
289                         // Throw again ...
290                         throw new NullPointerException("landLineNumber.phoneId is null");
291                 } else if (landLineNumber.getPhoneId() < 1) {
292                         // Not valid
293                         throw new IllegalArgumentException(MessageFormat.format("landLineNumber.phoneId={0} is not valid.", landLineNumber.getPhoneId()));
294                 }
295
296                 // First remove it
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())) {
302                                         // Found it
303                                         this.landLineNumbers.remove(landLine);
304                                         break;
305                                 }
306                         }
307                 }
308
309                 // ... then add it
310                 this.landLineNumbers.add(landLineNumber);
311         }
312
313         /**
314          * Updates given contact's cellphone, land-line and fax number
315          * <p>
316          * @param contact Contact instance
317          */
318         private void updateContactPhoneNumbers (final Contact contact) {
319                 // Parameter must be valid
320                 if (null == contact) {
321                         // Throw NPE
322                         throw new NullPointerException("contact is null");
323                 } else if (contact.getContactId() == null) {
324                         // Throw again
325                         throw new NullPointerException("contact.contactId is null");
326                 } else if (contact.getContactId() < 1) {
327                         // Id number is not valid
328                 }
329
330                 // Is cellphone set?
331                 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
332                         // Unique-add it
333                         this.uniqueAddCellphoneNumber(contact.getContactCellphoneNumber());
334                 }
335
336                 // Is land-line set?
337                 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
338                         // Unique-add it
339                         this.uniqueAddLandLineNumber(contact.getContactLandLineNumber());
340                 }
341
342                 // Is fax set?
343                 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
344                         // Unique-add it
345                         this.uniqueAddFaxNumber(contact.getContactFaxNumber());
346                 }
347         }
348
349 }