org.beangle.ems.security.nav.model.MenuBean.java Source code

Java tutorial

Introduction

Here is the source code for org.beangle.ems.security.nav.model.MenuBean.java

Source

/* Copyright c 2005-2012.
 * Licensed under GNU  LESSER General Public License, Version 3.
 * http://www.gnu.org/licenses
 */
package org.beangle.ems.security.nav.model;

import java.util.List;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.apache.commons.lang.StringUtils;
import org.beangle.commons.collection.CollectUtils;
import org.beangle.commons.lang.StrUtils;
import org.beangle.ems.security.Resource;
import org.beangle.ems.security.model.SubSystem;
import org.beangle.ems.security.nav.Menu;
import org.beangle.ems.security.nav.MenuProfile;
import org.beangle.model.pojo.LongIdObject;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Entity(name = "org.beangle.ems.security.nav.Menu")
@Cacheable
@Cache(region = "beangle.security", usage = CacheConcurrencyStrategy.READ_WRITE)
public class MenuBean extends LongIdObject implements Menu {

    private static final long serialVersionUID = 3864556621041443066L;
    @NotNull
    @Size(max = 32)
    @Column(unique = true)
    private String code;

    @NotNull
    @Size(max = 100)
    private String name;

    @NotNull
    @Size(max = 100)
    private String title;

    private String entry;

    private String remark;

    @ManyToMany
    @Cache(region = "beangle.security", usage = CacheConcurrencyStrategy.READ_WRITE)
    private Set<Resource> resources = CollectUtils.newHashSet();

    @NotNull
    private boolean enabled = true;

    @NotNull
    private MenuProfile profile;

    private Menu parent;

    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
    @Cache(region = "beangle.security", usage = CacheConcurrencyStrategy.READ_WRITE)
    @OrderBy("code")
    private List<Menu> children;

    /**
     * ???
     */
    private SubSystem subSystem;

    /**
     * 
     */
    @Lob
    @Basic(fetch = FetchType.EAGER)
    @Column(columnDefinition = "BLOB", length = 1048576)
    private byte[] icon;

    /**
     * ???????.
     */
    public int compareTo(Menu other) {
        return getCode().compareTo(other.getCode());
    }

    public void generateCode(String indexno) {
        if (null == parent) {
            this.code = indexno;
        } else {
            this.code = StrUtils.concat(parent.getCode(), ".", indexno);
        }
    }

    public void generateCode() {
        if (null != parent) {
            this.code = StrUtils.concat(parent.getCode(), ".", getIndexno());
        }
    }

    public String getIndexno() {
        String indexno = StringUtils.substringAfterLast(code, ".");
        if (StringUtils.isEmpty(indexno)) {
            indexno = code;
        }
        return indexno;
    }

    public int getDepth() {
        return (null == parent) ? 1 : parent.getDepth() + 1;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getName() {
        return name;
    }

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

    public String getEntry() {
        return entry;
    }

    public void setEntry(String entry) {
        this.entry = entry;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public Set<Resource> getResources() {
        return resources;
    }

    public void setResources(Set<Resource> resources) {
        this.resources = resources;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public MenuProfile getProfile() {
        return profile;
    }

    public void setProfile(MenuProfile profile) {
        this.profile = profile;
    }

    public Menu getParent() {
        return parent;
    }

    public void setParent(Menu parent) {
        this.parent = parent;
    }

    public List<Menu> getChildren() {
        return children;
    }

    public void setChildren(List<Menu> children) {
        this.children = children;
    }

    public String getDescription() {
        return StrUtils.concat("[", code, "]", title);
    }

    @Override
    public String toString() {
        return getDescription();
    }

    public SubSystem getSubSystem() {
        return subSystem;
    }

    public void setSubSystem(SubSystem subSystem) {
        this.subSystem = subSystem;
    }

    public byte[] getIcon() {
        return icon;
    }

    public void setIcon(byte[] icon) {
        this.icon = icon;
    }

}