com.hongqiang.shop.modules.sys.entity.Role.java Source code

Java tutorial

Introduction

Here is the source code for com.hongqiang.shop.modules.sys.entity.Role.java

Source

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

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
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.Length;

import com.google.common.collect.Lists;
import com.hongqiang.shop.common.persistence.DataEntity;

/**
 * Entity
 * @author ThinkGem
 * @version 2013-05-15
 */
@Entity
@Table(name = "sys_role")
@DynamicInsert
@DynamicUpdate
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Role extends DataEntity {

    private static final long serialVersionUID = 1L;
    private Long id; // ?
    private Office office; // 
    private String name; // ??
    private String enname; //??
    private String roleType;//??
    private String dataScope; // ?

    private List<User> userList = Lists.newArrayList(); // 
    private List<Menu> menuList = Lists.newArrayList(); // ??
    private List<Office> officeList = Lists.newArrayList(); // ?

    // ?1?2???3??4??5?8?9
    public static final String DATA_SCOPE_ALL = "1";
    public static final String DATA_SCOPE_COMPANY_AND_CHILD = "2";
    public static final String DATA_SCOPE_COMPANY = "3";
    public static final String DATA_SCOPE_OFFICE_AND_CHILD = "4";
    public static final String DATA_SCOPE_OFFICE = "5";
    public static final String DATA_SCOPE_SELF = "8";
    public static final String DATA_SCOPE_CUSTOM = "9";

    public Role() {
        super();
        this.dataScope = DATA_SCOPE_CUSTOM;
    }

    public Role(Long id, String name) {
        this();
        this.id = id;
        this.name = name;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    //   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_sys_role")
    //   @SequenceGenerator(name = "seq_sys_role", sequenceName = "seq_sys_role")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @ManyToOne
    @JoinColumn(name = "office_id")
    @NotFound(action = NotFoundAction.IGNORE)
    public Office getOffice() {
        return office;
    }

    public void setOffice(Office office) {
        this.office = office;
    }

    @Length(min = 1, max = 100)
    public String getName() {
        return name;
    }

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

    @Length(min = 1, max = 100)
    public String getEnname() {
        return enname;
    }

    public void setEnname(String enname) {
        this.enname = enname;
    }

    @Length(min = 1, max = 100)
    public String getRoleType() {
        return roleType;
    }

    public void setRoleType(String roleType) {
        this.roleType = roleType;
    }

    public String getDataScope() {
        return dataScope;
    }

    public void setDataScope(String dataScope) {
        this.dataScope = dataScope;
    }

    @ManyToMany(mappedBy = "roleList", fetch = FetchType.LAZY)
    @Where(clause = "del_flag='" + DEL_FLAG_NORMAL + "'")
    @OrderBy("id")
    @Fetch(FetchMode.SUBSELECT)
    @NotFound(action = NotFoundAction.IGNORE)
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    public List<User> getUserList() {
        return userList;
    }

    public void setUserList(List<User> userList) {
        this.userList = userList;
    }

    @Transient
    public List<Long> getUserIdList() {
        List<Long> nameIdList = Lists.newArrayList();
        for (User user : userList) {
            nameIdList.add(user.getId());
        }
        return nameIdList;
    }

    @Transient
    public String getUserIds() {
        List<Long> nameIdList = Lists.newArrayList();
        for (User user : userList) {
            nameIdList.add(user.getId());
        }
        return StringUtils.join(nameIdList, ",");
    }

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "sys_role_menu", joinColumns = { @JoinColumn(name = "role_id") }, inverseJoinColumns = {
            @JoinColumn(name = "menu_id") })
    @Where(clause = "del_flag='" + DEL_FLAG_NORMAL + "'")
    @OrderBy("id")
    @Fetch(FetchMode.SUBSELECT)
    @NotFound(action = NotFoundAction.IGNORE)
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    public List<Menu> getMenuList() {
        return menuList;
    }

    public void setMenuList(List<Menu> menuList) {
        this.menuList = menuList;
    }

    @Transient
    public List<Long> getMenuIdList() {
        List<Long> menuIdList = Lists.newArrayList();
        for (Menu menu : menuList) {
            menuIdList.add(menu.getId());
        }
        return menuIdList;
    }

    @Transient
    public void setMenuIdList(List<Long> menuIdList) {
        menuList = Lists.newArrayList();
        for (Long menuId : menuIdList) {
            Menu menu = new Menu();
            menu.setId(menuId);
            menuList.add(menu);
        }
    }

    @Transient
    public String getMenuIds() {
        List<Long> nameIdList = Lists.newArrayList();
        for (Menu menu : menuList) {
            nameIdList.add(menu.getId());
        }
        return StringUtils.join(nameIdList, ",");
    }

    @Transient
    public void setMenuIds(String menuIds) {
        menuList = Lists.newArrayList();
        if (menuIds != null) {
            String[] ids = StringUtils.split(menuIds, ",");
            for (String menuId : ids) {
                Menu menu = new Menu();
                menu.setId(new Long(menuId));
                menuList.add(menu);
            }
        }
    }

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "sys_role_office", joinColumns = { @JoinColumn(name = "role_id") }, inverseJoinColumns = {
            @JoinColumn(name = "office_id") })
    @Where(clause = "del_flag='" + DEL_FLAG_NORMAL + "'")
    @OrderBy("id")
    @Fetch(FetchMode.SUBSELECT)
    @NotFound(action = NotFoundAction.IGNORE)
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    public List<Office> getOfficeList() {
        return officeList;
    }

    public void setOfficeList(List<Office> officeList) {
        this.officeList = officeList;
    }

    @Transient
    public List<Long> getOfficeIdList() {
        List<Long> officeIdList = Lists.newArrayList();
        for (Office office : officeList) {
            officeIdList.add(office.getId());
        }
        return officeIdList;
    }

    @Transient
    public void setOfficeIdList(List<Long> officeIdList) {
        officeList = Lists.newArrayList();
        for (Long officeId : officeIdList) {
            Office office = new Office();
            office.setId(officeId);
            officeList.add(office);
        }
    }

    @Transient
    public String getOfficeIds() {
        List<Long> nameIdList = Lists.newArrayList();
        for (Office office : officeList) {
            nameIdList.add(office.getId());
        }
        return StringUtils.join(nameIdList, ",");
    }

    @Transient
    public void setOfficeIds(String officeIds) {
        officeList = Lists.newArrayList();
        if (officeIds != null) {
            String[] ids = StringUtils.split(officeIds, ",");
            for (String officeId : ids) {
                Office office = new Office();
                office.setId(new Long(officeId));
                officeList.add(office);
            }
        }
    }

    //   @ElementCollection
    //   @CollectionTable(name = "sys_user_role", joinColumns = { @JoinColumn(name = "role_id") })
    //   @Column(name = "user_id")
    //   @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    //   public List<Long> getUserIdList() {
    //      return userIdList;
    //   }
    //
    //   public void setUserIdList(List<Long> userIdList) {
    //      this.userIdList = userIdList;
    //   }

    /**
     * ???
     */
    @Transient
    public List<String> getPermissions() {
        List<String> permissions = Lists.newArrayList();
        for (Menu menu : menuList) {
            if (menu.getPermission() != null && !"".equals(menu.getPermission())) {
                permissions.add(menu.getPermission());
            }
        }
        return permissions;
    }

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

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

    //   @Transient
    //   public String getMenuNames() {
    //      List<String> menuNameList = Lists.newArrayList();
    //      for (Menu menu : menuList) {
    //         menuNameList.add(menu.getName());
    //      }
    //      return StringUtils.join(menuNameList, ",");
    //   }
}