]> git.mxchange.org Git - addressbook-war.git/blobdiff - src/java/org/mxchange/addressbook/beans/user/UserWebBean.java
Compare against null
[addressbook-war.git] / src / java / org / mxchange / addressbook / beans / user / UserWebBean.java
index f79582b04e4b52af390fc7797dda26ebee9f967c..c9467c045e25f55b313622ed224933afa1071775 100644 (file)
  */
 package org.mxchange.addressbook.beans.user;
 
+import java.text.MessageFormat;
+import java.util.Date;
+import java.util.GregorianCalendar;
+import java.util.List;
+import java.util.Objects;
+import javax.annotation.PostConstruct;
 import javax.enterprise.context.SessionScoped;
 import javax.faces.view.facelets.FaceletException;
 import javax.inject.Named;
@@ -25,6 +31,17 @@ import javax.naming.NamingException;
 import org.mxchange.jcontacts.contact.Contact;
 import org.mxchange.jcontacts.contact.UserContact;
 import org.mxchange.jcontacts.contact.gender.Gender;
+import org.mxchange.jcountry.data.Country;
+import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
+import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
+import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
+import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
+import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
+import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
+import org.mxchange.jphone.phonenumbers.smsprovider.SmsProvider;
+import org.mxchange.jusercore.model.user.LoginUser;
+import org.mxchange.jusercore.model.user.User;
+import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
 
 /**
  * A user bean (controller)
@@ -41,10 +58,20 @@ public class UserWebBean implements UserWebController {
        private static final long serialVersionUID = 542_145_347_916L;
 
        /////////////////////// Properties /////////////////////
+       /**
+        * Birth day
+        */
+       private Date birthday;
+
+       /**
+        * Cellphone number's carrier
+        */
+       private SmsProvider cellphoneCarrier;
+
        /**
         * Cellphone number
         */
-       private String cellphoneNumber;
+       private Long cellphoneNumber;
 
        /**
         * City
@@ -57,34 +84,44 @@ public class UserWebBean implements UserWebController {
        private String comment;
 
        /**
-        * Company name
+        * Country instance
         */
-       private String companyName;
+       private Country country;
 
        /**
-        * Country code
+        * Email address
         */
-       private String countryCode;
+       private String emailAddress;
 
        /**
-        * Remote user bean
+        * Email address list
         */
-       private final UserSessionBeanRemote userBean;
+       private List<String> emailAddressList;
 
        /**
-        * Email address
+        * Email address repeated
         */
-       private String emailAddress;
+       private String emailAddressRepeat;
 
        /**
         * Family name
         */
        private String familyName;
 
+       /**
+        * Fax number's area code
+        */
+       private Integer faxAreaCode;
+
+       /**
+        * Country instance for fax number
+        */
+       private Country faxCountry;
+
        /**
         * Fax number
         */
-       private String faxNumber;
+       private Long faxNumber;
 
        /**
         * First name
@@ -101,16 +138,56 @@ public class UserWebBean implements UserWebController {
         */
        private Short houseNumber;
 
+       /**
+        * Phone number area code
+        */
+       private Integer phoneAreaCode;
+
+       /**
+        * Country instance for phone number
+        */
+       private Country phoneCountry;
+
        /**
         * Phone number
         */
-       private String phoneNumber;
+       private Long phoneNumber;
 
        /**
         * Street
         */
        private String street;
 
+       /**
+        * Remote user bean
+        */
+       private final UserSessionBeanRemote userBean;
+
+       /**
+        * User id
+        */
+       private Long userId;
+
+       /**
+        * User name
+        */
+       private String userName;
+
+       /**
+        * User name list
+        */
+       private List<String> userNameList;
+
+       /**
+        * User password (unencrypted from web form)
+        */
+       private String userPassword;
+
+       /**
+        * User password repeated (unencrypted from web form)
+        */
+       private String userPasswordRepeat;
+
        /**
         * ZIP code
         */
@@ -129,7 +206,7 @@ public class UserWebBean implements UserWebController {
                        Context context = new InitialContext();
 
                        // Try to lookup
-                       this.userBean = (UserSessionBeanRemote) context.lookup("ejb/stateless-user");
+                       this.userBean = (UserSessionBeanRemote) context.lookup("ejb/stateless-user"); //NOI18N
                } catch (final NamingException e) {
                        // Throw again
                        throw new FaceletException(e);
@@ -137,42 +214,220 @@ public class UserWebBean implements UserWebController {
        }
 
        @Override
-       public User createCustomerInstance () {
+       public void addUserNameEmailAddress (final User user) {
+               // Make sure the entry is not added yet
+               if (this.userNameList.contains(user.getUserName())) {
+                       // Abort here
+                       throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName()));
+               } else if (this.emailAddressList.contains(user.getUserContact().getContactEmailAddress())) {
+                       // Already added
+                       throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", user.getUserContact().getContactEmailAddress()));
+               }
+
+               // Add user name
+               this.userNameList.add(user.getUserName());
+
+               // Add email addres
+               this.emailAddressList.add(user.getUserContact().getContactEmailAddress());
+       }
+
+       @Override
+       public void clearData () {
+               // Clear all data
+               // - personal data
+               this.setUserId(null);
+               this.setGender(Gender.UNKNOWN);
+               this.setFirstName(null);
+               this.setFamilyName(null);
+               this.setStreet(null);
+               this.setHouseNumber(null);
+               this.setZipCode(null);
+               this.setCity(null);
+               this.setCountry(null);
+
+               // - contact data
+               this.setEmailAddress(null);
+               this.setEmailAddressRepeat(null);
+               this.setPhoneAreaCode(null);
+               this.setCellphoneCarrier(null);
+               this.setFaxAreaCode(null);
+
+               // - other data
+               this.setBirthday(null);
+               this.setComment(null);
+               this.setUserName(null);
+               this.setUserPassword(null);
+               this.setUserPasswordRepeat(null);
+       }
+
+       @Override
+       public void copyUser (final User user) {
+               // Copy all fields:
+               // - base data
+               this.setUserId(user.getUserId());
+               this.setGender(user.getUserContact().getContactGender());
+               this.setFirstName(user.getUserContact().getContactFirstName());
+               this.setFamilyName(user.getUserContact().getContactFamilyName());
+               this.setStreet(user.getUserContact().getContactStreet());
+               this.setHouseNumber(user.getUserContact().getContactHouseNumber());
+               this.setZipCode(user.getUserContact().getContactZipCode());
+               this.setCity(user.getUserContact().getContactCity());
+               this.setCountry(user.getUserContact().getContactCountry());
+
+               // Get cellphone, phone and fax instance
+               DialableCellphoneNumber cellphone = user.getUserContact().getContactCellphoneNumber();
+               DialableFaxNumber fax = user.getUserContact().getContactFaxNumber();
+               DialableLandLineNumber phone = user.getUserContact().getContactPhoneNumber();
+
+               // - contact data
+               if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
+                       this.setPhoneCountry(phone.getPhoneCountry());
+                       this.setPhoneAreaCode(phone.getPhoneAreaCode());
+                       this.setPhoneNumber(phone.getPhoneNumber());
+               }
+               if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof SmsProvider)) {
+                       this.setCellphoneCarrier(cellphone.getCellphoneProvider());
+                       this.setCellphoneNumber(cellphone.getPhoneNumber());
+               }
+               if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
+                       this.setFaxCountry(fax.getPhoneCountry());
+                       this.setFaxAreaCode(fax.getPhoneAreaCode());
+                       this.setFaxNumber(fax.getPhoneNumber());
+               }
+               this.setEmailAddress(user.getUserContact().getContactEmailAddress());
+
+               // -- other data
+               this.setBirthday(user.getUserContact().getContactBirthday());
+               this.setComment(user.getUserContact().getContactComment());
+       }
+
+       @Override
+       public User createUserInstance () {
                // User message
-               //this.getLogger().logTrace("createInstance: CALLED!");
+               //this.getLogger().logTrace("createUserInstance: CALLED!");
 
                // Required personal data must be set
                assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
 
                // Create new user instance
-               User user = new AddressbookUser();
+               User user = new LoginUser();
+               user.setUserName(this.getUserName());
+
+               // Generate phone number
+               DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
+               DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
+               DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
 
                // Create new contact
-               Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName(), this.getCompanyName());
-               contact.setStreet(this.getStreet());
-               contact.setHouseNumber(this.getHouseNumber());
-               contact.setZipCode(this.getZipCode());
-               contact.setCity(this.getCity());
-               contact.setPhoneNumber(this.getPhoneNumber());
-               contact.setFaxNumber(this.getFaxNumber());
-               contact.setCellphoneNumber(this.getCellphoneNumber());
+               Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
+               contact.setContactStreet(this.getStreet());
+               contact.setContactHouseNumber(this.getHouseNumber());
+               contact.setContactZipCode(this.getZipCode());
+               contact.setContactCity(this.getCity());
+               contact.setContactCountry(this.getCountry());
+               contact.setContactEmailAddress(this.getEmailAddress());
+
+               // Don't set null or wrong references
+               if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) &&(this.getPhoneNumber() > 0)) {
+                       // Now the number must be given
+                       if (phone.getPhoneAreaCode() == null) {
+                               // Is null
+                               throw new NullPointerException("phone.phoneAreaCode is null");
+                       } else if (phone.getPhoneAreaCode() < 1) {
+                               // Abort here
+                               throw new IllegalArgumentException("phone.phoneAreaCode is zero or below.");
+                       } else if (phone.getPhoneNumber() == null) {
+                               // Is null
+                               throw new NullPointerException("phone.phoneNumber is null");
+                       } else if (phone.getPhoneNumber() < 1) {
+                               // Abort here
+                               throw new IllegalArgumentException("phone.phoneNumber is zero or below.");
+                       }
+
+                       // Set phone number
+                       contact.setContactPhoneNumber(phone);
+               }
+
+               // Don't set null or wrong references
+               if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
+                       // Now the number must be given
+                       if (fax.getPhoneAreaCode() == null) {
+                               // Is null
+                               throw new NullPointerException("fax.phoneAreaCode is null");
+                       } else if (fax.getPhoneAreaCode() < 1) {
+                               // Abort here
+                               throw new IllegalArgumentException("fax.phoneAreaCode is zero or below.");
+                       } else if (fax.getPhoneNumber() == null) {
+                               // Is null
+                               throw new NullPointerException("fax.phoneNumber is null");
+                       } else if (fax.getPhoneNumber() < 1) {
+                               // Abort here
+                               throw new IllegalArgumentException("fax.phoneNumber is zero or below.");
+                       }
+
+                       // Set fax number
+                       contact.setContactFaxNumber(fax);
+               }
+
+               // Is the provider set?
+               if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof SmsProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
+                       // Is the number set?
+                       if (cellphone.getPhoneNumber() == null) {
+                               // Is null
+                               throw new NullPointerException("cellphone.phoneNumber is null");
+                       } else if (cellphone.getPhoneNumber() < 1) {
+                               // Abort here
+                               throw new IllegalArgumentException("cellphone.phoneNumber is zero or below.");
+                       }
+
+                       // Set cellphone number
+                       contact.setContactCellphoneNumber(cellphone);
+               }
+
+               contact.setContactBirthday(this.getBirthday());
+               contact.setContactComment(this.getComment());
+
+               // Created timestamp and ownContact
+               contact.setContactCreated(new GregorianCalendar());
+               contact.setContactOwnContact(Boolean.TRUE);
 
                // Set contact in user
-               user.setContact(contact);
+               user.setUserContact(contact);
+               user.setUserCreated(new GregorianCalendar());
 
                // Trace message
-               //this.getLogger().logTrace(MessageFormat.format("createInstance: user={0} - EXIT!", user));
+               //this.getLogger().logTrace(MessageFormat.format("createUserInstance: user={0} - EXIT!", user));
                // Return it
                return user;
        }
 
        @Override
-       public String getCellphoneNumber () {
+       public Date getBirthday () {
+               return this.birthday;
+       }
+
+       @Override
+       public void setBirthday (final Date birthday) {
+               this.birthday = birthday;
+       }
+
+       @Override
+       public SmsProvider getCellphoneCarrier () {
+               return this.cellphoneCarrier;
+       }
+
+       @Override
+       public void setCellphoneCarrier (final SmsProvider cellphoneCarrier) {
+               this.cellphoneCarrier = cellphoneCarrier;
+       }
+
+       @Override
+       public Long getCellphoneNumber () {
                return this.cellphoneNumber;
        }
 
        @Override
-       public void setCellphoneNumber (final String cellphoneNumber) {
+       public void setCellphoneNumber (Long cellphoneNumber) {
                this.cellphoneNumber = cellphoneNumber;
        }
 
@@ -187,23 +442,23 @@ public class UserWebBean implements UserWebController {
        }
 
        @Override
-       public String getCompanyName () {
-               return this.companyName;
+       public String getComment () {
+               return this.comment;
        }
 
        @Override
-       public void setCompanyName (final String companyName) {
-               this.companyName = companyName;
+       public void setComment (final String comment) {
+               this.comment = comment;
        }
 
        @Override
-       public String getCountryCode () {
-               return this.countryCode;
+       public Country getCountry () {
+               return this.country;
        }
 
        @Override
-       public void setCountryCode (final String countryCode) {
-               this.countryCode = countryCode;
+       public void setCountry (final Country country) {
+               this.country = country;
        }
 
        @Override
@@ -216,6 +471,16 @@ public class UserWebBean implements UserWebController {
                this.emailAddress = emailAddress;
        }
 
+       @Override
+       public String getEmailAddressRepeat () {
+               return this.emailAddressRepeat;
+       }
+
+       @Override
+       public void setEmailAddressRepeat (final String emailAddressRepeat) {
+               this.emailAddressRepeat = emailAddressRepeat;
+       }
+
        @Override
        public String getFamilyName () {
                return this.familyName;
@@ -227,12 +492,32 @@ public class UserWebBean implements UserWebController {
        }
 
        @Override
-       public String getFaxNumber () {
+       public Integer getFaxAreaCode () {
+               return this.faxAreaCode;
+       }
+
+       @Override
+       public void setFaxAreaCode (final Integer faxAreaCode) {
+               this.faxAreaCode = faxAreaCode;
+       }
+
+       @Override
+       public Country getFaxCountry () {
+               return this.faxCountry;
+       }
+
+       @Override
+       public void setFaxCountry (final Country faxCountry) {
+               this.faxCountry = faxCountry;
+       }
+
+       @Override
+       public Long getFaxNumber () {
                return this.faxNumber;
        }
 
        @Override
-       public void setFaxNumber (final String faxNumber) {
+       public void setFaxNumber (final Long faxNumber) {
                this.faxNumber = faxNumber;
        }
 
@@ -267,12 +552,32 @@ public class UserWebBean implements UserWebController {
        }
 
        @Override
-       public String getPhoneNumber () {
+       public Integer getPhoneAreaCode () {
+               return this.phoneAreaCode;
+       }
+
+       @Override
+       public void setPhoneAreaCode (final Integer phoneAreaCode) {
+               this.phoneAreaCode = phoneAreaCode;
+       }
+
+       @Override
+       public Country getPhoneCountry () {
+               return this.phoneCountry;
+       }
+
+       @Override
+       public void setPhoneCountry (final Country phoneCountry) {
+               this.phoneCountry = phoneCountry;
+       }
+
+       @Override
+       public Long getPhoneNumber () {
                return this.phoneNumber;
        }
 
        @Override
-       public void setPhoneNumber (final String phoneNumber) {
+       public void setPhoneNumber (final Long phoneNumber) {
                this.phoneNumber = phoneNumber;
        }
 
@@ -286,6 +591,46 @@ public class UserWebBean implements UserWebController {
                this.street = street;
        }
 
+       @Override
+       public Long getUserId () {
+               return this.userId;
+       }
+
+       @Override
+       public void setUserId (final Long userId) {
+               this.userId = userId;
+       }
+
+       @Override
+       public String getUserName () {
+               return this.userName;
+       }
+
+       @Override
+       public void setUserName (final String userName) {
+               this.userName = userName;
+       }
+
+       @Override
+       public String getUserPassword () {
+               return this.userPassword;
+       }
+
+       @Override
+       public void setUserPassword (final String userPassword) {
+               this.userPassword = userPassword;
+       }
+
+       @Override
+       public String getUserPasswordRepeat () {
+               return this.userPasswordRepeat;
+       }
+
+       @Override
+       public void setUserPasswordRepeat (final String userPasswordRepeat) {
+               this.userPasswordRepeat = userPasswordRepeat;
+       }
+
        @Override
        public Integer getZipCode () {
                return this.zipCode;
@@ -296,8 +641,48 @@ public class UserWebBean implements UserWebController {
                this.zipCode = zipCode;
        }
 
+       @PostConstruct
+       public void init () {
+               // Get full user name list for reducing EJB calls
+               this.userNameList = this.userBean.getUserNameList();
+
+               // Get full email address list for reducing EJB calls
+               this.emailAddressList = this.userBean.getEmailAddressList();
+       }
+
+       @Override
+       public boolean isEmailAddressRegistered (final User user) {
+               return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(user.getUserContact().getContactEmailAddress())));
+       }
+
        @Override
        public boolean isRequiredPersonalDataSet () {
-               return ((this.getGender() != null) && (this.getFirstName() != null) && (this.getFamilyName() != null) && (this.getStreet() != null) && (this.getHouseNumber() != null) && (this.getZipCode() != null) && (this.getCity() != null));
+               return ((this.getUserName() != null)
+                               && (this.getGender() != null)
+                               && (this.getFirstName() != null)
+                               && (this.getFamilyName() != null)
+                               && (this.getStreet() != null)
+                               && (this.getHouseNumber() != null)
+                               && (this.getZipCode() != null)
+                               && (this.getCity() != null)
+                               && (this.getEmailAddress() != null)
+                               && (this.getEmailAddressRepeat() != null)
+                               && (this.getUserPassword() != null)
+                               && (this.getUserPasswordRepeat() != null));
+       }
+
+       @Override
+       public boolean isSameEmailAddressEntered () {
+               return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
+       }
+
+       @Override
+       public boolean isSamePasswordEntered () {
+               return (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat()));
+       }
+
+       @Override
+       public boolean isUserNameRegistered (final User user) {
+               return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName())));
        }
 }