Java tutorial
/** * Copyright © 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.green.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.google.common.collect.Lists; import com.green.common.persistence.IdEntity; /** * Entity * @author ThinkGem * @version 2013-05-15 */ @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 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") @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) public List<User> getUserList() { return userList; } public void setUserList(List<User> userList) { this.userList = userList; } @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) public List<Menu> getMenuList() { return menuList; } public void setMenuList(List<Menu> menuList) { this.menuList = menuList; } @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); } } } @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<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 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 getMenuNames() { // List<String> menuNameList = Lists.newArrayList(); // for (Menu menu : menuList) { // menuNameList.add(menu.getName()); // } // return StringUtils.join(menuNameList, ","); // } }