com.aistor.modules.sys.entity.Role.java Source code

Java tutorial

Introduction

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

Source

/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/Dopas/dopas">Dopas</a> All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.aistor.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.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.aistor.common.persistence.BaseEntity;
import com.aistor.modules.cms.entity.Category;

/**
 * Entity
 * @author Zaric
 * @version 2013-01-15
 */
@Entity
@Table(name = "sys_role")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Role extends BaseEntity {

    private static final long serialVersionUID = 1L;
    private Long id; // ?
    private String name; // ??
    private User user; // 
    private String delFlag; // 01

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

    public Role() {
        this.delFlag = DEL_FLAG_NORMAL;
    }

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

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    //   @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;
    }

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

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

    @ManyToOne
    @JoinColumn(name = "user_id")
    @NotFound(action = NotFoundAction.IGNORE)
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Length(min = 1, max = 1)
    public String getDelFlag() {
        return delFlag;
    }

    public void setDelFlag(String delFlag) {
        this.delFlag = delFlag;
    }

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "sys_user_role", joinColumns = { @JoinColumn(name = "role_id") }, inverseJoinColumns = {
            @JoinColumn(name = "user_id") })
    @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;
    }

    @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;
    }

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

    public void setCategoryList(List<Category> categoryList) {
        this.categoryList = categoryList;
    }

    //   @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;
    //   }

    /**
     * ???Id
     */
    @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);
            }
        }
    }

    /**
     * ?Id
     */
    @Transient
    public List<Long> getCategoryIdList() {
        List<Long> categoryIdList = Lists.newArrayList();
        for (Category category : categoryList) {
            categoryIdList.add(category.getId());
        }
        return categoryIdList;
    }

    @Transient
    public void setCategoryIdList(List<Long> categoryIdList) {
        categoryList = Lists.newArrayList();
        for (Long categoryId : categoryIdList) {
            Category category = new Category();
            category.setId(categoryId);
            categoryList.add(category);
        }
    }

    @Transient
    public String getCategoryIds() {
        List<Long> nameIdList = Lists.newArrayList();
        for (Category category : categoryList) {
            nameIdList.add(category.getId());
        }
        return StringUtils.join(nameIdList, ",");
    }

    @Transient
    public void setCategoryIds(String categoryIds) {
        categoryList = Lists.newArrayList();
        if (categoryIds != null) {
            String[] ids = StringUtils.split(categoryIds, ",");
            for (String categoryId : ids) {
                Category category = new Category();
                category.setId(new Long(categoryId));
                categoryList.add(category);
            }
        }
    }

    /**
     * ???
     */
    @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, ",");
    //   }
}