]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/user/PizzaAdminUserWebSessionBean.java
37dfe37b008e73178ef5d6503cfded26b90fbcdd
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / user / PizzaAdminUserWebSessionBean.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.pizzaapplication.beans.user;
18
19 import java.util.Collections;
20 import java.util.Date;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Objects;
24 import javax.annotation.PostConstruct;
25 import javax.enterprise.context.SessionScoped;
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.gender.Gender;
32 import org.mxchange.jcountry.data.Country;
33 import org.mxchange.jphone.phonenumbers.smsprovider.SmsProvider;
34 import org.mxchange.jusercore.exceptions.UserNotFoundException;
35 import org.mxchange.jusercore.model.user.User;
36 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
37 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
38
39 /**
40  * A user bean (controller)
41  * <p>
42  * @author Roland Haeder<roland@mxchange.org>
43  */
44 @Named ("adminUserController")
45 @SessionScoped
46 public class PizzaAdminUserWebSessionBean implements PizzaAdminUserWebSessionController {
47
48         /**
49          * Serial number
50          */
51         private static final long serialVersionUID = 542_145_347_916L;
52
53         /////////////////////// Properties /////////////////////
54         /**
55          * Birth day
56          */
57         private Date birthday;
58
59         /**
60          * Cellphone number's carrier
61          */
62         private SmsProvider cellphoneCarrier;
63
64         /**
65          * Cellphone number
66          */
67         private Long cellphoneNumber;
68
69         /**
70          * City
71          */
72         private String city;
73
74         /**
75          * Optional comments
76          */
77         private String comment;
78
79         /**
80          * Country instance
81          */
82         private Country country;
83
84         /**
85          * Email address
86          */
87         private String emailAddress;
88
89         /**
90          * Family name
91          */
92         private String familyName;
93
94         /**
95          * Fax number's area code
96          */
97         private Integer faxAreaCode;
98
99         /**
100          * Country instance for fax number
101          */
102         private Country faxCountry;
103
104         /**
105          * Fax number
106          */
107         private Long faxNumber;
108
109         /**
110          * First name
111          */
112         private String firstName;
113
114         /**
115          * Gender instance
116          */
117         private Gender gender;
118
119         /**
120          * House number
121          */
122         private Short houseNumber;
123
124         /**
125          * Phone number area code
126          */
127         private Integer phoneAreaCode;
128
129         /**
130          * Country instance for phone number
131          */
132         private Country phoneCountry;
133
134         /**
135          * Phone number
136          */
137         private Long phoneNumber;
138
139         /**
140          * Street
141          */
142         private String street;
143
144         /**
145          * Remote user bean
146          */
147         private final UserSessionBeanRemote userBean;
148
149         /**
150          * User id
151          */
152         private Long userId;
153
154         /**
155          * A list of all user profiles
156          */
157         private List<User> userList;
158
159         /**
160          * User name
161          */
162         private String userName;
163
164         /**
165          * User password (unencrypted from web form)
166          */
167         private String userPassword;
168
169         /**
170          * User password repeated (unencrypted from web form)
171          */
172         private String userPasswordRepeat;
173
174         /**
175          * Whether the user wants a public profile
176          */
177         private ProfileMode userProfileMode;
178
179         /**
180          * ZIP code
181          */
182         private Integer zipCode;
183
184         /**
185          * Default constructor
186          */
187         public PizzaAdminUserWebSessionBean () {
188                 // Set gender to UNKNOWN
189                 this.gender = Gender.UNKNOWN;
190
191                 // Try it
192                 try {
193                         // Get initial context
194                         Context context = new InitialContext();
195
196                         // Try to lookup
197                         this.userBean = (UserSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
198                 } catch (final NamingException e) {
199                         // Throw again
200                         throw new FaceletException(e);
201                 }
202         }
203
204         @Override
205         public List<User> allUsers () {
206                 // Return it
207                 return Collections.unmodifiableList(this.userList);
208         }
209
210         @Override
211         public Date getBirthday () {
212                 return this.birthday;
213         }
214
215         @Override
216         public void setBirthday (final Date birthday) {
217                 this.birthday = birthday;
218         }
219
220         @Override
221         public SmsProvider getCellphoneCarrier () {
222                 return this.cellphoneCarrier;
223         }
224
225         @Override
226         public void setCellphoneCarrier (final SmsProvider cellphoneCarrier) {
227                 this.cellphoneCarrier = cellphoneCarrier;
228         }
229
230         @Override
231         public Long getCellphoneNumber () {
232                 return this.cellphoneNumber;
233         }
234
235         @Override
236         public void setCellphoneNumber (Long cellphoneNumber) {
237                 this.cellphoneNumber = cellphoneNumber;
238         }
239
240         @Override
241         public String getCity () {
242                 return this.city;
243         }
244
245         @Override
246         public void setCity (final String city) {
247                 this.city = city;
248         }
249
250         @Override
251         public String getComment () {
252                 return this.comment;
253         }
254
255         @Override
256         public void setComment (final String comment) {
257                 this.comment = comment;
258         }
259
260         @Override
261         public Country getCountry () {
262                 return this.country;
263         }
264
265         @Override
266         public void setCountry (final Country country) {
267                 this.country = country;
268         }
269
270         @Override
271         public String getEmailAddress () {
272                 return this.emailAddress;
273         }
274
275         @Override
276         public void setEmailAddress (final String emailAddress) {
277                 this.emailAddress = emailAddress;
278         }
279
280         @Override
281         public String getFamilyName () {
282                 return this.familyName;
283         }
284
285         @Override
286         public void setFamilyName (final String familyName) {
287                 this.familyName = familyName;
288         }
289
290         @Override
291         public Integer getFaxAreaCode () {
292                 return this.faxAreaCode;
293         }
294
295         @Override
296         public void setFaxAreaCode (final Integer faxAreaCode) {
297                 this.faxAreaCode = faxAreaCode;
298         }
299
300         @Override
301         public Country getFaxCountry () {
302                 return this.faxCountry;
303         }
304
305         @Override
306         public void setFaxCountry (final Country faxCountry) {
307                 this.faxCountry = faxCountry;
308         }
309
310         @Override
311         public Long getFaxNumber () {
312                 return this.faxNumber;
313         }
314
315         @Override
316         public void setFaxNumber (final Long faxNumber) {
317                 this.faxNumber = faxNumber;
318         }
319
320         @Override
321         public String getFirstName () {
322                 return this.firstName;
323         }
324
325         @Override
326         public void setFirstName (final String firstName) {
327                 this.firstName = firstName;
328         }
329
330         @Override
331         public Gender getGender () {
332                 return this.gender;
333         }
334
335         @Override
336         public void setGender (final Gender gender) {
337                 this.gender = gender;
338         }
339
340         @Override
341         public Short getHouseNumber () {
342                 return this.houseNumber;
343         }
344
345         @Override
346         public void setHouseNumber (final Short houseNumber) {
347                 this.houseNumber = houseNumber;
348         }
349
350         @Override
351         public Integer getPhoneAreaCode () {
352                 return this.phoneAreaCode;
353         }
354
355         @Override
356         public void setPhoneAreaCode (final Integer phoneAreaCode) {
357                 this.phoneAreaCode = phoneAreaCode;
358         }
359
360         @Override
361         public Country getPhoneCountry () {
362                 return this.phoneCountry;
363         }
364
365         @Override
366         public void setPhoneCountry (final Country phoneCountry) {
367                 this.phoneCountry = phoneCountry;
368         }
369
370         @Override
371         public Long getPhoneNumber () {
372                 return this.phoneNumber;
373         }
374
375         @Override
376         public void setPhoneNumber (final Long phoneNumber) {
377                 this.phoneNumber = phoneNumber;
378         }
379
380         @Override
381         public String getStreet () {
382                 return this.street;
383         }
384
385         @Override
386         public void setStreet (final String street) {
387                 this.street = street;
388         }
389
390         @Override
391         public Long getUserId () {
392                 return this.userId;
393         }
394
395         @Override
396         public void setUserId (final Long userId) {
397                 this.userId = userId;
398         }
399
400         @Override
401         public String getUserName () {
402                 return this.userName;
403         }
404
405         @Override
406         public void setUserName (final String userName) {
407                 this.userName = userName;
408         }
409
410         @Override
411         public String getUserPassword () {
412                 return this.userPassword;
413         }
414
415         @Override
416         public void setUserPassword (final String userPassword) {
417                 this.userPassword = userPassword;
418         }
419
420         @Override
421         public String getUserPasswordRepeat () {
422                 return this.userPasswordRepeat;
423         }
424
425         @Override
426         public void setUserPasswordRepeat (final String userPasswordRepeat) {
427                 this.userPasswordRepeat = userPasswordRepeat;
428         }
429
430         @Override
431         public ProfileMode getUserProfileMode () {
432                 return this.userProfileMode;
433         }
434
435         @Override
436         public void setUserProfileMode (final ProfileMode userProfileMode) {
437                 this.userProfileMode = userProfileMode;
438         }
439
440         @Override
441         public Integer getZipCode () {
442                 return this.zipCode;
443         }
444
445         @Override
446         public void setZipCode (final Integer zipCode) {
447                 this.zipCode = zipCode;
448         }
449
450         @Override
451         public boolean hasUsers () {
452                 return (!this.allUsers().isEmpty());
453         }
454
455         /**
456          * Post-initialization of this class
457          */
458         @PostConstruct
459         public void init () {
460                 // Initialize user list
461                 this.userList = this.userBean.allUsers();
462         }
463
464         @Override
465         public User lookupUserById (final Long userId) throws UserNotFoundException {
466                 // Init variable
467                 User user = null;
468
469                 // Try to lookup it in visible user list
470                 for (final Iterator<User> iterator = this.userList.iterator(); iterator.hasNext();) {
471                         // Get next user
472                         User next = iterator.next();
473
474                         // Is the user id found?
475                         if (Objects.equals(next.getUserId(), userId)) {
476                                 // Copy to other variable
477                                 user = next;
478                                 break;
479                         }
480                 }
481
482                 // Is it still null?
483                 if (null == user) {
484                         // Not visible for the current user
485                         throw new UserNotFoundException(userId);
486                 }
487
488                 // Return it
489                 return user;
490         }
491
492 }