]> 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 de5c6e0561f674aa1fb060b6efa8a92ebe79d4b4..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;
@@ -28,6 +31,14 @@ 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;
@@ -52,10 +63,15 @@ public class UserWebBean implements UserWebController {
         */
        private Date birthday;
 
+       /**
+        * Cellphone number's carrier
+        */
+       private SmsProvider cellphoneCarrier;
+
        /**
         * Cellphone number
         */
-       private String cellphoneNumber;
+       private Long cellphoneNumber;
 
        /**
         * City
@@ -68,24 +84,44 @@ public class UserWebBean implements UserWebController {
        private String comment;
 
        /**
-        * Country code
+        * Country instance
         */
-       private String countryCode;
+       private Country country;
 
        /**
         * Email address
         */
        private String emailAddress;
 
+       /**
+        * Email address list
+        */
+       private List<String> emailAddressList;
+
+       /**
+        * Email address repeated
+        */
+       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
@@ -103,14 +139,19 @@ public class UserWebBean implements UserWebController {
        private Short houseNumber;
 
        /**
-        * User id
+        * Phone number area code
         */
-       private Long userId;
+       private Integer phoneAreaCode;
+
+       /**
+        * Country instance for phone number
+        */
+       private Country phoneCountry;
 
        /**
         * Phone number
         */
-       private String phoneNumber;
+       private Long phoneNumber;
 
        /**
         * Street
@@ -122,30 +163,35 @@ public class UserWebBean implements UserWebController {
         */
        private final UserSessionBeanRemote userBean;
 
+       /**
+        * User id
+        */
+       private Long userId;
+
        /**
         * User name
         */
        private String userName;
 
        /**
-        * User password (unencrypted from web form)
+        * User name list
         */
-       private String userPassword;
+       private List<String> userNameList;
 
        /**
-        * ZIP code
+        * User password (unencrypted from web form)
         */
-       private Integer zipCode;
+       private String userPassword;
 
        /**
-        * Email address list
+        * User password repeated (unencrypted from web form)
         */
-       private List<String> emailAddressList;
+       private String userPasswordRepeat;
 
        /**
-        * User name list
+        * ZIP code
         */
-       private List<String> userNameList;
+       private Integer zipCode;
 
        /**
         * Default constructor
@@ -160,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);
@@ -168,22 +214,50 @@ public class UserWebBean implements UserWebController {
        }
 
        @Override
-       public boolean isEmailAddressRegistered (String emailAddress) {
-               return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(emailAddress)));
+       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 boolean isUserNameRegistered (final String userName) {
-               return ((this.userNameList instanceof List) && (this.userNameList.contains(userName)));
-       }
+       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);
 
-       @PostConstruct
-       public void init () {
-               // Get full user name list for reducing EJB calls
-               this.userNameList = this.userBean.getUserNameList();
+               // - contact data
+               this.setEmailAddress(null);
+               this.setEmailAddressRepeat(null);
+               this.setPhoneAreaCode(null);
+               this.setCellphoneCarrier(null);
+               this.setFaxAreaCode(null);
 
-               // Get full email address list for reducing EJB calls
-               this.emailAddressList = this.userBean.getEmailAddressList();
+               // - other data
+               this.setBirthday(null);
+               this.setComment(null);
+               this.setUserName(null);
+               this.setUserPassword(null);
+               this.setUserPasswordRepeat(null);
        }
 
        @Override
@@ -191,24 +265,40 @@ public class UserWebBean implements UserWebController {
                // Copy all fields:
                // - base data
                this.setUserId(user.getUserId());
-               this.setGender(user.getUserContact().getGender());
-               this.setFirstName(user.getUserContact().getFirstName());
-               this.setFamilyName(user.getUserContact().getFamilyName());
-               this.setStreet(user.getUserContact().getStreet());
-               this.setHouseNumber(user.getUserContact().getHouseNumber());
-               this.setZipCode(user.getUserContact().getZipCode());
-               this.setCity(user.getUserContact().getCity());
-               this.setCountryCode(user.getUserContact().getCountryCode());
+               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
-               this.setPhoneNumber(user.getUserContact().getPhoneNumber());
-               this.setCellphoneNumber(user.getUserContact().getCellphoneNumber());
-               this.setFaxNumber(user.getUserContact().getFaxNumber());
-               this.setEmailAddress(user.getUserContact().getEmailAddress());
+               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().getBirthday());
-               this.setComment(user.getUserContact().getComment());
+               this.setBirthday(user.getUserContact().getContactBirthday());
+               this.setComment(user.getUserContact().getContactComment());
        }
 
        @Override
@@ -223,20 +313,87 @@ public class UserWebBean implements UserWebController {
                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());
-               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.setBirthday(this.getBirthday());
-               contact.setComment(this.getComment());
+               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.setUserContact(contact);
+               user.setUserCreated(new GregorianCalendar());
 
                // Trace message
                //this.getLogger().logTrace(MessageFormat.format("createUserInstance: user={0} - EXIT!", user));
@@ -255,12 +412,22 @@ public class UserWebBean implements UserWebController {
        }
 
        @Override
-       public String getCellphoneNumber () {
+       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;
        }
 
@@ -285,13 +452,13 @@ public class UserWebBean implements UserWebController {
        }
 
        @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
@@ -304,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;
@@ -315,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;
        }
 
@@ -355,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;
        }
 
@@ -404,6 +621,16 @@ public class UserWebBean implements UserWebController {
                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;
@@ -414,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.getUserName() != null) && (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())));
        }
 }