UpdateUserBean.java 8.24 KB
package org.legrog.web.user;

import org.legrog.web.xyz.SharedService;
import org.legrog.entities.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Named
@SessionScoped
public class UpdateUserBean {

    Logger logger = LoggerFactory.getLogger(getClass());

    private UserService userService;
    private SharedService sharedService;

    private List<DisplayNameMask> allDisplayNameMasks;
    private List<UserRole> availableUserRoles;
    private List<UserProperty> availableUserProperties;

    @ManagedProperty("#{param.userId}")
    private int userId;
    private String username;
    private String firstName;
    private String lastName;
    private String nickName;
    private DisplayNameMask displayNameMask;
    private String email;
    private boolean anonymous;
    private String password;
    private List<UserRole> roles;
    private String presentation;
    private List<UserAttribute> userAttributes;
    private boolean criticProvider;
    private boolean visible;
    private boolean activated;

    @Inject
    public UpdateUserBean(UserService userService, SharedService sharedService) {
        this.userService = userService;
        this.sharedService = sharedService;
    }

    //no args constructor to make it proxyable
    UpdateUserBean() {
    }


    @PostConstruct
    public void init() {
        logger.info("init");
        allDisplayNameMasks = sharedService.getAllDisplayNameMasks();
        availableUserRoles = sharedService.getAvailableUserRoles();
        availableUserProperties = sharedService.getAvailableUserProperties();
    }


    public String add() {
        User user = new User();
        user.setActivated(activated);
        user.setAnonymous(anonymous);
        if (userAttributes != null) {
            user.setAttributes(userAttributes);
        }
        user.setCriticProvider(criticProvider);
        if (displayNameMask != null) {
            user.setDisplayNameMask(displayNameMask);
        } else {
            user.setDisplayNameMask(DisplayNameMask.PRENOMNOM);
        }
        user.setEmail(email);
        user.setFirstName(firstName);
        user.setLastName(lastName);
        if (nickName != null && !nickName.isEmpty()) {
            user.setNickName(nickName);
        }
        user.setPassword(password);
        if (presentation != null) {
            user.setPresentation(presentation);
        }
        if (roles != null) {
            user.setRoles(roles);
        }
        user.setUsername(username);
        user.setVisible(visible);
        user.setCreationDate(new Date());
        userService.addUser(user);
        return "success";
    }

    public String prepareUpdate(int userId) {
        logger.info("prepareUpdate");

        logger.info("userId =" + userId);
        this.userId = userId;
        if (userId != 0) {
            User user = userService.findUserById(userId);
            if (user != null) {
                activated = user.isActivated();
                anonymous = user.isAnonymous();
                userAttributes = user.getAttributes();
                criticProvider = user.isCriticProvider();
                displayNameMask = user.getDisplayNameMask();
                email = user.getEmail();
                firstName = user.getFirstName();
                lastName = user.getLastName();
                nickName = user.getNickName();
                password = user.getPassword();
                presentation = user.getPresentation();
                roles = user.getRoles();
                if (roles == null) {
                    roles = new ArrayList<UserRole>();
                }
                username = user.getUsername();
                visible = user.isVisible();
                return "updateUser.xhtml";
            }
        }
        return "listUsers.xhtml";
    }

    public String update() {
        logger.info("update");

        logger.info("userId =" + userId);
        User user = userService.findUserById(userId);
        user.setActivated(activated);
        user.setAnonymous(anonymous);
        user.setAttributes(userAttributes);
        user.setCriticProvider(criticProvider);
        user.setDisplayNameMask(displayNameMask);
        user.setEmail(email);
        user.setFirstName(firstName);
        user.setLastName(lastName);
        user.setNickName(nickName);
        user.setPassword(password);
        user.setPresentation(presentation);
        user.setRoles(roles);
        user.setUsername(username);
        user.setVisible(visible);
        userService.updateUser(user);
        return "success";
    }


    public List<UserRole> getAvailableUserRoles() {
        return availableUserRoles;
    }

    public void setAvailableUserRoles(List<UserRole> availableUserRoles) {
        this.availableUserRoles = availableUserRoles;
    }

    public String getPresentation() {
        return presentation;
    }

    public void setPresentation(String presentation) {
        this.presentation = presentation;
    }

    public List<UserProperty> getAvailableUserProperties() {
        return availableUserProperties;
    }

    public void setAvailableUserProperties(List<UserProperty> availableUserProperties) {
        this.availableUserProperties = availableUserProperties;
    }

    public List<UserAttribute> getUserAttributes() {
        return userAttributes;
    }

    public void setUserAttributes(List<UserAttribute> userAttributes) {
        this.userAttributes = userAttributes;
    }

    public boolean isCriticProvider() {
        return criticProvider;
    }

    public void setCriticProvider(boolean criticProvider) {
        this.criticProvider = criticProvider;
    }

    public boolean isVisible() {
        return visible;
    }

    public void setVisible(boolean visible) {
        this.visible = visible;
    }

    public boolean isActivated() {
        return activated;
    }

    public void setActivated(boolean activated) {
        this.activated = activated;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public SharedService getSharedService() {
        return sharedService;
    }

    public void setSharedService(SharedService sharedService) {
        this.sharedService = sharedService;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public DisplayNameMask getDisplayNameMask() {
        return displayNameMask;
    }

    public void setDisplayNameMask(DisplayNameMask displayNameMask) {
        this.displayNameMask = displayNameMask;
    }

    public List<DisplayNameMask> getAllDisplayNameMasks() {
        return allDisplayNameMasks;
    }

    public void setAllDisplayNameMasks(List<DisplayNameMask> allDisplayNameMasks) {
        this.allDisplayNameMasks = allDisplayNameMasks;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public boolean isAnonymous() {
        return anonymous;
    }

    public void setAnonymous(boolean anonymous) {
        this.anonymous = anonymous;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public List<UserRole> getRoles() {
        return roles;
    }

    public void setRoles(List<UserRole> roles) {
        this.roles = roles;
    }
}