2 * Copyright (C) 2015 Roland Haeder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (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 General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.addressbook.beans.user;
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import java.util.GregorianCalendar;
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.Contact;
32 import org.mxchange.jcontacts.contact.UserContact;
33 import org.mxchange.jcontacts.contact.gender.Gender;
34 import org.mxchange.jusercore.model.user.LoginUser;
35 import org.mxchange.jusercore.model.user.User;
36 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
39 * A user bean (controller)
41 * @author Roland Haeder<roland@mxchange.org>
43 @Named ("userController")
45 public class UserWebBean implements UserWebController {
50 private static final long serialVersionUID = 542_145_347_916L;
52 /////////////////////// Properties /////////////////////
56 private Date birthday;
61 private String cellphoneNumber;
71 private String comment;
76 private String countryCode;
81 private String emailAddress;
86 private List<String> emailAddressList;
89 * Email address repeated
91 private String emailAddressRepeat;
96 private String familyName;
101 private String faxNumber;
106 private String firstName;
111 private Gender gender;
116 private Short houseNumber;
121 private String phoneNumber;
126 private String street;
131 private final UserSessionBeanRemote userBean;
141 private String userName;
146 private List<String> userNameList;
149 * User password (unencrypted from web form)
151 private String userPassword;
154 * User password repeated (unencrypted from web form)
156 private String userPasswordRepeat;
161 private Integer zipCode;
164 * Default constructor
166 public UserWebBean () {
167 // Set gender to UNKNOWN
168 this.gender = Gender.UNKNOWN;
172 // Get initial context
173 Context context = new InitialContext();
176 this.userBean = (UserSessionBeanRemote) context.lookup("ejb/stateless-user"); //NOI18N
177 } catch (final NamingException e) {
179 throw new FaceletException(e);
184 public void addUserNameEmailAddress (final User user) {
185 // Make sure the entry is not added yet
186 if (this.userNameList.contains(user.getUserName())) {
188 throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName()));
189 } else if (this.emailAddressList.contains(user.getUserContact().getEmailAddress())) {
191 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", user.getUserContact().getEmailAddress()));
195 this.userNameList.add(user.getUserName());
198 this.emailAddressList.add(user.getUserContact().getEmailAddress());
202 public void clearData () {
205 this.setUserId(null);
206 this.setGender(Gender.UNKNOWN);
207 this.setFirstName(null);
208 this.setFamilyName(null);
209 this.setStreet(null);
210 this.setHouseNumber(null);
211 this.setZipCode(null);
213 this.setCountryCode(null);
216 this.setEmailAddress(null);
217 this.setEmailAddressRepeat(null);
218 this.setPhoneNumber(null);
219 this.setCellphoneNumber(null);
220 this.setFaxNumber(null);
223 this.setBirthday(null);
224 this.setComment(null);
225 this.setUserName(null);
226 this.setUserPassword(null);
227 this.setUserPasswordRepeat(null);
231 public void copyUser (final User user) {
234 this.setUserId(user.getUserId());
235 this.setGender(user.getUserContact().getGender());
236 this.setFirstName(user.getUserContact().getFirstName());
237 this.setFamilyName(user.getUserContact().getFamilyName());
238 this.setStreet(user.getUserContact().getStreet());
239 this.setHouseNumber(user.getUserContact().getHouseNumber());
240 this.setZipCode(user.getUserContact().getZipCode());
241 this.setCity(user.getUserContact().getCity());
242 this.setCountryCode(user.getUserContact().getCountryCode());
245 this.setPhoneNumber(user.getUserContact().getPhoneNumber());
246 this.setCellphoneNumber(user.getUserContact().getCellphoneNumber());
247 this.setFaxNumber(user.getUserContact().getFaxNumber());
248 this.setEmailAddress(user.getUserContact().getEmailAddress());
251 this.setBirthday(user.getUserContact().getBirthday());
252 this.setComment(user.getUserContact().getComment());
256 public User createUserInstance () {
258 //this.getLogger().logTrace("createUserInstance: CALLED!");
260 // Required personal data must be set
261 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
263 // Create new user instance
264 User user = new LoginUser();
265 user.setUserName(this.getUserName());
267 // Create new contact
268 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
269 contact.setStreet(this.getStreet());
270 contact.setHouseNumber(this.getHouseNumber());
271 contact.setZipCode(this.getZipCode());
272 contact.setCity(this.getCity());
273 contact.setCountryCode(this.getCountryCode());
274 contact.setEmailAddress(this.getEmailAddress());
275 contact.setPhoneNumber(this.getPhoneNumber());
276 contact.setFaxNumber(this.getFaxNumber());
277 contact.setCellphoneNumber(this.getCellphoneNumber());
278 contact.setBirthday(this.getBirthday());
279 contact.setComment(this.getComment());
281 // Created timestamp and ownContact
282 contact.setCreated(new GregorianCalendar());
283 contact.setOwnContact(Boolean.TRUE);
285 // Set contact in user
286 user.setUserContact(contact);
287 user.setUserCreated(new GregorianCalendar());
290 //this.getLogger().logTrace(MessageFormat.format("createUserInstance: user={0} - EXIT!", user));
296 public Date getBirthday () {
297 return this.birthday;
301 public void setBirthday (final Date birthday) {
302 this.birthday = birthday;
306 public String getCellphoneNumber () {
307 return this.cellphoneNumber;
311 public void setCellphoneNumber (final String cellphoneNumber) {
312 this.cellphoneNumber = cellphoneNumber;
316 public String getCity () {
321 public void setCity (final String city) {
326 public String getComment () {
331 public void setComment (final String comment) {
332 this.comment = comment;
336 public String getCountryCode () {
337 return this.countryCode;
341 public void setCountryCode (final String countryCode) {
342 this.countryCode = countryCode;
346 public String getEmailAddress () {
347 return this.emailAddress;
351 public void setEmailAddress (final String emailAddress) {
352 this.emailAddress = emailAddress;
356 public String getEmailAddressRepeat () {
357 return this.emailAddressRepeat;
361 public void setEmailAddressRepeat (final String emailAddressRepeat) {
362 this.emailAddressRepeat = emailAddressRepeat;
366 public String getFamilyName () {
367 return this.familyName;
371 public void setFamilyName (final String familyName) {
372 this.familyName = familyName;
376 public String getFaxNumber () {
377 return this.faxNumber;
381 public void setFaxNumber (final String faxNumber) {
382 this.faxNumber = faxNumber;
386 public String getFirstName () {
387 return this.firstName;
391 public void setFirstName (final String firstName) {
392 this.firstName = firstName;
396 public Gender getGender () {
401 public void setGender (final Gender gender) {
402 this.gender = gender;
406 public Short getHouseNumber () {
407 return this.houseNumber;
411 public void setHouseNumber (final Short houseNumber) {
412 this.houseNumber = houseNumber;
416 public String getPhoneNumber () {
417 return this.phoneNumber;
421 public void setPhoneNumber (final String phoneNumber) {
422 this.phoneNumber = phoneNumber;
426 public String getStreet () {
431 public void setStreet (final String street) {
432 this.street = street;
436 public Long getUserId () {
441 public void setUserId (final Long userId) {
442 this.userId = userId;
446 public String getUserName () {
447 return this.userName;
451 public void setUserName (final String userName) {
452 this.userName = userName;
456 public String getUserPassword () {
457 return this.userPassword;
461 public void setUserPassword (final String userPassword) {
462 this.userPassword = userPassword;
466 public String getUserPasswordRepeat () {
467 return this.userPasswordRepeat;
471 public void setUserPasswordRepeat (final String userPasswordRepeat) {
472 this.userPasswordRepeat = userPasswordRepeat;
476 public Integer getZipCode () {
481 public void setZipCode (final Integer zipCode) {
482 this.zipCode = zipCode;
486 public void init () {
487 // Get full user name list for reducing EJB calls
488 this.userNameList = this.userBean.getUserNameList();
490 // Get full email address list for reducing EJB calls
491 this.emailAddressList = this.userBean.getEmailAddressList();
495 public boolean isEmailAddressRegistered (final User user) {
496 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(user.getUserContact().getEmailAddress())));
500 public boolean isRequiredPersonalDataSet () {
501 return ((this.getUserName() != null) &&
502 (this.getGender() != null) &&
503 (this.getFirstName() != null) &&
504 (this.getFamilyName() != null) &&
505 (this.getStreet() != null) &&
506 (this.getHouseNumber() != null) &&
507 (this.getZipCode() != null) &&
508 (this.getCity() != null) &&
509 (this.getEmailAddress() != null) &&
510 (this.getEmailAddressRepeat() != null) &&
511 (this.getUserPassword() != null) &&
512 (this.getUserPasswordRepeat() != null));
516 public boolean isSameEmailAddressEntered () {
517 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
521 public boolean isSamePasswordEntered () {
522 return (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat()));
526 public boolean isUserNameRegistered (final User user) {
527 return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName())));