com.sccl.attech.modules.sys.entity.Role.java Source code

Java tutorial

Introduction

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

Source

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

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.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.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import com.sccl.attech.common.persistence.IdEntity;

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

    private static final long serialVersionUID = 1L;
    private Office office; // 
    private String name; // ??
    private String dataScope; // ?
    private String officeName;//??
    private String officeId;//id???

    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(String id, String name) {
        this();
        this.id = id;
        this.name = name;
    }

    @ManyToOne
    @JoinColumn(name = "office_id")
    @JsonIgnore
    @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;
    }

    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)
    //   @JsonIgnore
    //   public List<User> getUserList() {
    //      return userList;
    //   }
    //
    //   public void setUserList(List<User> userList) {
    //      this.userList = userList;
    //   }
    @JsonIgnore
    @Transient
    public List<String> getUserIdList() {
        List<String> nameIdList = Lists.newArrayList();
        for (User user : userList) {
            nameIdList.add(user.getId());
        }
        return nameIdList;
    }

    @Transient
    public String getUserIds() {
        List<String> 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)
    @JsonIgnore
    public List<Menu> getMenuList() {
        return menuList;
    }

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

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

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

    @Transient
    public String getMenuIds() {
        List<String> 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(menuId);
                menuList.add(menu);
            }
        }
    }
    //1,10,11,12,13,14,15,16,17,18,19,2,20,21,22,23,24,25,26,27,28,29,3,30,31,32,33,34,35,36,37,38,39,4,40,41,42,43,44,45,46,47,48,49,5,50,51,52,53,54,55,56,57,58,59,6,60,61,62,63,64,65,66,67,68,69,7,70,8,9

    @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)
    @JsonIgnore
    public List<Office> getOfficeList() {
        return officeList;
    }

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

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

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

    @Transient
    public String getOfficeIds() {
        List<String> 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(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
    @JsonIgnore
    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(String id) {
        return id != null && id.equals("1");
    }

    @Transient
    public String getOfficeName() {
        if (StringUtils.isBlank(officeName)) {
            if (office != null) {
                return office.getName();
            }
        }
        return this.officeName;
    }

    public void setOfficeName(String officeName) {
        this.officeName = officeName;
    }

    @Transient
    public String getOfficeId() {

        if (StringUtils.isBlank(officeId)) {
            if (office != null) {
                return office.getId();
            }
        }
        return this.officeId;
    }

    public void setOfficeId(String officeId) {
        this.officeId = officeId;
    }

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

}