com.vallourec.zebra.modules.sys.entity.User.java Source code

Java tutorial

Introduction

Here is the source code for com.vallourec.zebra.modules.sys.entity.User.java

Source

/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/thinkgem/ep">ep</a> All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.vallourec.zebra.modules.sys.entity;

import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.annotations.Where;
import org.hibernate.validator.constraints.Email;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import com.vallourec.zebra.core.utils.Collections3;

/**
 * Zebra user
 * 
 * @author Feng Wei
 * @version 2014-7-25
 */
@Entity
@Table(name = "sys_user")
@DynamicInsert
@DynamicUpdate
public class User extends DataEntity {

    private static final long serialVersionUID = 1L;
    public static final String OFFICE_USER = "1";
    public static final String SHOPFLOOR_USER = "2";

    private String loginName; // ??
    private String password; // ??
    private String name; // ??
    private String email; // 
    private String userType; // 
    private String loginIp; // ?IP
    private Date loginDate; // ?

    private List<Role> roleList = Lists.newArrayList(); // 

    @NotNull
    @Size(min = 2, max = 40)
    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    @Size(min = 2, max = 100)
    public String getPassword() {
        return password;
    }

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

    @Size(min = 2, max = 50)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Email
    public String getEmail() {
        return email;
    }

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

    @Size(max = 20)
    public String getUserType() {
        return userType;
    }

    public void setUserType(String userType) {
        this.userType = userType;
    }

    @Size(max = 15)
    //@Pattern(regexp = "", message = "Illegal IP address.")
    public String getLoginIp() {
        return loginIp;
    }

    public void setLoginIp(String loginIp) {
        this.loginIp = loginIp;
    }

    @Temporal(TemporalType.TIMESTAMP)
    public Date getLoginDate() {
        return loginDate;
    }

    public void setLoginDate(Date loginDate) {
        this.loginDate = loginDate;
    }

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "sys_user_role", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = {
            @JoinColumn(name = "role_id") })
    @Where(clause = DEL_NORMAL_CLAUSE)
    @OrderBy("name")
    @Fetch(FetchMode.SUBSELECT)
    @NotFound(action = NotFoundAction.IGNORE)
    @JsonIgnore
    public List<Role> getRoleList() {
        return roleList;
    }

    public void setRoleList(List<Role> roleList) {
        this.roleList = roleList;
    }

    /*************************************** 
     * transient 
     **************************************/
    @Override
    public String toString() {
        return this.name;
    }

    @Transient
    public boolean isAdmin() {
        return isAdmin(this.id);
    }

    @Transient
    public static boolean isAdmin(String id) {
        return id != null && id.equals("1");
    }

    @Transient
    public List<String> getRoleIdList() {
        List<String> roleIdList = Lists.newArrayList();
        for (Role role : roleList) {
            roleIdList.add(role.getId());
        }
        return roleIdList;
    }

    @Transient
    public void setRoleIdList(List<String> roleIdList) {
        roleList = Lists.newArrayList();
        for (String roleId : roleIdList) {
            Role role = new Role();
            role.setId(roleId);
            roleList.add(role);
        }
    }

    // ????","
    @Transient
    public String getRoleNames() {
        return Collections3.extractToString(roleList, "name", ",");
    }

    // ????","
    @Transient
    public String getRoleDescriptions() {
        return Collections3.extractToString(roleList, "description", ",");
    }
}