UpdateUserBean.java 8.35 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() {
        Person person = new Person();
        person.setActivated(activated);
        person.setAnonymous(anonymous);
        if (userAttributes != null) {
            person.setAttributes(userAttributes);
        }
        person.setCriticProvider(criticProvider);
        if (displayNameMask != null) {
            person.setDisplayNameMask(displayNameMask);
        } else {
            person.setDisplayNameMask(DisplayNameMask.PRENOMNOM);
        }
        person.setEmail(email);
        person.setFirstName(firstName);
        person.setLastName(lastName);
        if (nickName != null && !nickName.isEmpty()) {
            person.setNickName(nickName);
        }
        person.setPassword(password);
        if (presentation != null) {
            person.setPresentation(presentation);
        }
        if (roles != null) {
            person.setRoles(roles);
        }
        person.setUsername(username);
        person.setVisible(visible);
        person.setCreationDate(new Date());
        userService.addUser(person);
        return "success";
    }

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

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

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

        logger.info("userId =" + userId);
        Person person = userService.findUserById(userId);
        person.setActivated(activated);
        person.setAnonymous(anonymous);
        person.setAttributes(userAttributes);
        person.setCriticProvider(criticProvider);
        person.setDisplayNameMask(displayNameMask);
        person.setEmail(email);
        person.setFirstName(firstName);
        person.setLastName(lastName);
        person.setNickName(nickName);
        person.setPassword(password);
        person.setPresentation(presentation);
        person.setRoles(roles);
        person.setUsername(username);
        person.setVisible(visible);
        userService.updateUser(person);
        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;
    }
}