Java tutorial
/** * Copyright © 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.ManyToMany; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.OrderBy; import javax.persistence.Table; import javax.persistence.Transient; import javax.validation.constraints.NotNull; import org.apache.commons.lang.ObjectUtils; 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; import com.sccl.attech.modules.sys.utils.DictUtils; import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; /** * ??Entity * @author sccl * @version 2013-05-15 */ @Entity @Table(name = "sys_menu") @DynamicInsert @DynamicUpdate @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) public class Menu extends IdEntity<Menu> { private static final long serialVersionUID = 1L; private Menu parent; // ?? private String parentIds; // ? private String name; // ?? private String href; // private String target; // mainFrame?_blank?_self?_parent?_top private String icon; // private Integer sort; // ? private String isShow; // ???10? private String isActiviti; // ???1?0?? private String isMobile; //?1?0?? private String permission; // ?? private String plugins;//? private List<Menu> childList = Lists.newArrayList();// ??? private List<Role> roleList = Lists.newArrayList(); // private String isShowName; private String parentName;//???? private String parentId;//??Id public Menu(String parentIds, String name, String href, String target, String icon, Integer sort, String isShow, String isActiviti, String isMobile, String permission, String plugins, String parentId) { super(); this.parentIds = parentIds; this.name = name; this.href = href; this.target = target; this.icon = icon; this.sort = sort; this.isShow = isShow; this.isActiviti = isActiviti; this.isMobile = isMobile; this.permission = permission; this.plugins = plugins; this.parentId = parentId; } public Menu() { super(); this.sort = 30; } public Menu(String id) { this(); this.id = id; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "parent_id") @NotFound(action = NotFoundAction.IGNORE) @NotNull @JsonIgnore public Menu getParent() { return parent; } public void setParent(Menu parent) { this.parent = parent; } @Length(min = 1, max = 255) public String getParentIds() { return parentIds; } public void setParentIds(String parentIds) { this.parentIds = parentIds; } @Length(min = 1, max = 100) public String getName() { return name; } public void setName(String name) { this.name = name; } @Length(min = 0, max = 255) public String getHref() { return href; } public void setHref(String href) { this.href = href; } @Length(min = 0, max = 255) public String getTarget() { return target; } public void setTarget(String target) { this.target = target; } @Length(min = 0, max = 100) public String getIcon() { return icon; } public void setIcon(String icon) { this.icon = icon; } @NotNull public Integer getSort() { return sort; } public void setSort(Integer sort) { this.sort = sort; } @Length(min = 1, max = 1) public String getIsShow() { return isShow; } public void setIsShow(String isShow) { this.isShow = isShow; } @Length(min = 1, max = 1) public String getIsActiviti() { return isActiviti; } public void setIsActiviti(String isActiviti) { this.isActiviti = isActiviti; } @Length(min = 1, max = 1) public String getIsMobile() { return isMobile; } public void setIsMobile(String isMobile) { this.isMobile = isMobile; } @Length(min = 0, max = 200) public String getPermission() { return permission; } public void setPermission(String permission) { this.permission = permission; } @OneToMany(mappedBy = "parent", fetch = FetchType.EAGER) @Where(clause = "del_flag='" + DEL_FLAG_NORMAL + "'") @OrderBy(value = "sort") @Fetch(FetchMode.SUBSELECT) @NotFound(action = NotFoundAction.IGNORE) @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) @JsonIgnore public List<Menu> getChildList() { return childList; } public void setChildList(List<Menu> childList) { this.childList = childList; } @ManyToMany(mappedBy = "menuList", 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<Role> getRoleList() { return roleList; } public void setRoleList(List<Role> roleList) { this.roleList = roleList; } @Transient public static void sortList(List<Menu> list, List<Menu> sourcelist, String parentId) { for (int i = 0; i < sourcelist.size(); i++) { Menu e = sourcelist.get(i); if (e.getParent() != null && e.getParent().getId() != null && e.getParent().getId().equals(parentId)) { list.add(e); // ??, ?? for (int j = 0; j < sourcelist.size(); j++) { Menu child = sourcelist.get(j); if (child.getParent() != null && child.getParent().getId() != null && child.getParent().getId().equals(e.getId())) { sortList(list, sourcelist, e.getId()); break; } } } } } @Transient public boolean isRoot() { return isRoot(this.id); } @Transient public static boolean isRoot(String id) { return id != null && id.equals("1"); } @Transient public String getActivitiGroupId() { return ObjectUtils.toString(getPermission()); } @Transient public String getActivitiGroupName() { return ObjectUtils.toString(getId()); } @Transient public String getIsShowName() { return DictUtils.getDictLabel(isShow, "show_hide", ""); } @Transient public void setIsShowName(String isShowName) { this.isShowName = isShowName; } @Transient public String getParentName() { if (StringUtils.isBlank(this.parentName)) { if (parent != null) { return parent.getName(); } } return parentName; } public void setParentName(String parentName) { this.parentName = parentName; } @Transient public String getParentId() { if (StringUtils.isBlank(this.parentId)) { if (parent != null) { return parent.getId(); } } return parentId; } public void setParentId(String parentId) { this.parentId = parentId; } @Length(max = 1000) public String getPlugins() { return plugins; } public void setPlugins(String plugins) { this.plugins = plugins; } /** * @see java.lang.Object#toString() */ public String toString() { return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).append("name", this.name) .append("href", this.href).append("isMobile", this.isMobile).toString(); } }