UpdateUserBean.java 8.49 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<AccountRole> availableAccountRoles;
    private List<AccountProperty> 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<AccountRole> roles;
    private String presentation;
    private List<AccountAttribute> accountAttributes;
    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();
        availableAccountRoles = sharedService.getAvailableUserRoles();
        availableUserProperties = sharedService.getAvailableUserProperties();
    }


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

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

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

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

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


    public List<AccountRole> getAvailableAccountRoles() {
        return availableAccountRoles;
    }

    public void setAvailableAccountRoles(List<AccountRole> availableAccountRoles) {
        this.availableAccountRoles = availableAccountRoles;
    }

    public String getPresentation() {
        return presentation;
    }

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

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

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

    public List<AccountAttribute> getAccountAttributes() {
        return accountAttributes;
    }

    public void setAccountAttributes(List<AccountAttribute> accountAttributes) {
        this.accountAttributes = accountAttributes;
    }

    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<AccountRole> getRoles() {
        return roles;
    }

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