import java.util.Objects;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
+import javax.enterprise.event.Event;
+import javax.enterprise.inject.Any;
import javax.faces.view.facelets.FaceletException;
import javax.inject.Inject;
import javax.inject.Named;
import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
+import org.mxchange.jusercore.events.user.AdminAddedUserEvent;
+import org.mxchange.jusercore.events.user.AdminUserAddedEvent;
import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
import org.mxchange.jusercore.exceptions.UserNotFoundException;
import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
import org.mxchange.jusercore.model.user.UserUtils;
import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
+import org.mxchange.jusercore.model.user.status.UserAccountStatus;
/**
* A user bean (controller)
*/
private static final long serialVersionUID = 542_145_347_916L;
- /////////////////////// Properties /////////////////////
+ /**
+ * An event fired when the administrator has added a new user
+ */
+ @Inject
+ @Any
+ private Event<AdminAddedUserEvent> addedUserEvent;
+
/**
* Birth day
*/
private final UserSessionBeanRemote userBean;
/**
- * User id
+ * Regular user controller
*/
- private Long userId;
+ @Inject
+ private PizzaUserWebSessionController userController;
/**
* A list of all user profiles
*/
private String userPasswordRepeat;
- /**
- * Whether the user wants a public profile
- */
- private ProfileMode userProfileMode;
-
/**
* ZIP code
*/
private Integer zipCode;
- /**
- * Regular user controller
- */
- @Inject
- private PizzaUserWebSessionController userController;
-
/**
* Default constructor
*/
@Override
public void addUser () {
// Create new user instance
- User user = new LoginUser();
- user.setUserName(this.getUserName());
- user.setUserProfileMode(this.getUserProfileMode());
+ User localUser = new LoginUser();
+
+ // Set user name, CONFIRMED and INVISIBLE
+ localUser.setUserName(this.getUserName());
+ localUser.setUserAccountStatus(UserAccountStatus.CONFIRMED);
+ localUser.setUserProfileMode(ProfileMode.INVISIBLE);
// Generate phone number
DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
contact.setContactComment(this.getComment());
// Set contact in user
- user.setUserContact(contact);
+ localUser.setUserContact(contact);
// Init variable for password
String password = null;
// Is the user name or email address used already?
// @TODO Add password length check
- if (this.userController.isUserNameRegistered(user)) {
+ if (this.userController.isUserNameRegistered(localUser)) {
// User name is already used
- throw new FaceletException(new UserNameAlreadyRegisteredException(user));
- } else if (this.userController.isEmailAddressRegistered(user)) {
+ throw new FaceletException(new UserNameAlreadyRegisteredException(localUser));
+ } else if (this.userController.isEmailAddressRegistered(localUser)) {
// Email address is already used
- throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
- } else if ((this.getUserPassword().isEmpty()) && (this.getUserPasswordRepeat().isEmpty())) {
+ throw new FaceletException(new EmailAddressAlreadyRegisteredException(localUser));
+ } else if ((this.getUserPassword() == null && (this.getUserPasswordRepeat() == null)) || ((this.getUserPassword().isEmpty()) && (this.getUserPasswordRepeat().isEmpty()))) {
// Empty password entered, then generate one
password = UserUtils.createRandomPassword(PizzaUserWebSessionController.MINIMUM_PASSWORD_LENGTH);
} else if (!this.isSamePasswordEntered()) {
// Both passwords don't match
- throw new FaceletException(new UserPasswordMismatchException(user));
+ throw new FaceletException(new UserPasswordMismatchException(localUser));
} else {
// Both match, so get it from this bean
password = this.getUserPassword();
}
// The password should not be null and at least 5 characters long
- assert (password != null) : "password is null";
- assert (password.length() >= PizzaUserWebSessionController.MINIMUM_PASSWORD_LENGTH) : "Password is not long enough.";
+ assert (password != null) : "password is null"; //NOI18N
+ assert (password.length() >= PizzaUserWebSessionController.MINIMUM_PASSWORD_LENGTH) : "Password is not long enough."; //NOI18N
// Encrypt password and set it
- user.setUserEncryptedPassword(UserUtils.encryptPassword(password));
+ localUser.setUserEncryptedPassword(UserUtils.encryptPassword(password));
+
+ // Init updated user instance
+ User updatedUser = null;
try {
// Now, that all is set, call EJB
- this.userBean.addUser(user);
+ updatedUser = this.userBean.addUser(localUser);
} catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
// Throw again
throw new FaceletException(ex);
}
+
+ // Fire event
+ this.addedUserEvent.fire(new AdminUserAddedEvent(updatedUser));
+
+ // Add user to local list
+ this.userList.add(updatedUser);
+
+ // Clear all
+ this.clear();
}
@Override
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;
this.userPasswordRepeat = userPasswordRepeat;
}
- @Override
- public ProfileMode getUserProfileMode () {
- return this.userProfileMode;
- }
-
- @Override
- public void setUserProfileMode (final ProfileMode userProfileMode) {
- this.userProfileMode = userProfileMode;
- }
-
@Override
public Integer getZipCode () {
return this.zipCode;
this.userList = this.userBean.allUsers();
}
- /**
- * Checks if same password is entered and that they are not empty.
- * <p>
- * @return Whether the same password was entered
- */
- private boolean isSamePasswordEntered () {
- return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
- }
-
@Override
public User lookupUserById (final Long userId) throws UserNotFoundException {
+ if (null == userId) {
+ // Throw NPE
+ throw new NullPointerException("userId is null"); //NOI18N
+ } else if (userId < 1) {
+ // Not valid
+ throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
+ }
+
// Init variable
User user = null;
return user;
}
+ /**
+ * Clears this bean
+ */
+ private void clear () {
+ // Clear all
+ this.setBirthday(null);
+ this.setCellphoneCarrier(null);
+ this.setCellphoneNumber(null);
+ this.setCity(null);
+ this.setComment(null);
+ this.setCountry(null);
+ this.setEmailAddress(null);
+ this.setFamilyName(null);
+ this.setFaxAreaCode(null);
+ this.setFaxCountry(null);
+ this.setFaxNumber(null);
+ this.setFirstName(null);
+ this.setGender(null);
+ this.setHouseNumber(null);
+ this.setPhoneAreaCode(null);
+ this.setPhoneCountry(null);
+ this.setPhoneNumber(null);
+ this.setStreet(null);
+ this.setUserName(null);
+ this.setUserPassword(null);
+ this.setUserPasswordRepeat(null);
+ this.setZipCode(null);
+ }
+
+ /**
+ * Checks if same password is entered and that they are not empty.
+ * <p>
+ * @return Whether the same password was entered
+ */
+ private boolean isSamePasswordEntered () {
+ return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
+ }
+
}