com.eryansky.entity.base.Resource.java Source code

Java tutorial

Introduction

Here is the source code for com.eryansky.entity.base.Resource.java

Source

/**
 *  Copyright (c) 2012-2014 http://www.eryansky.com
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.eryansky.entity.base;

import com.eryansky.entity.base.state.ResourceType;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.google.common.collect.Lists;
import com.eryansky.common.orm.entity.BaseEntity;
import com.eryansky.common.utils.ConvertUtils;
import com.eryansky.utils.CacheConstants;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.*;

/**
 * ????Resource.
 * Author &Eryan eryanwcp@gmail.com
 * Date 2013-3-21 ?12:27:49
 *
 */
@SuppressWarnings("serial")
@Entity
@Table(name = "T_BASE_RESOURCE")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region = CacheConstants.HIBERNATE_CACHE_BASE)
//jackson??json 
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler", "fieldHandler", "parentResource", "roles",
        "roleNames", "users", "subResources", "navigation" })
public class Resource extends BaseEntity implements Serializable {

    /**
     * ???
     */
    private String name;
    /**
     * ??
     */
    private String code;
    /**
     * ?url?
     */
    private String url;
    /**
     * ?
     */
    private Integer orderNo;
    /**
     * 
     */
    private String iconCls;
    /**
     * ??
     */
    private String icon;
    /**
     * Resource
     */
    private Resource parentResource;
    /**
     * ResourceId @Transient
     */
    private Long _parentId;
    /**
     * url
     */
    private String markUrl;
    /**
     * ? ?(0) (1)
     */
    private Integer type = ResourceType.menu.getValue();
    /**
     * ???
     */
    private List<Role> roles = Lists.newArrayList();
    /**
     * ???
     */
    private List<User> users = Lists.newArrayList();
    /**
     * ?Resource?
     */
    private List<Resource> subResources = Lists.newArrayList();

    public Resource() {
    }

    @Column(name = "MARK_URL", length = 2000)
    public String getMarkUrl() {
        return markUrl;
    }

    public void setMarkUrl(String markUrl) {
        this.markUrl = markUrl;
    }

    @NotBlank(message = "{resource_name.notblank}")
    @Length(max = 20, message = "{resource_name.length}")
    @Column(name = "NAME", length = 255)
    public String getName() {
        return name;
    }

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

    @Column(name = "CODE", length = 36)
    public String getCode() {
        return code;
    }

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

    @Column(name = "URL", length = 255)
    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    @Column(name = "ORDER_NO")
    public Integer getOrderNo() {
        return orderNo;
    }

    public void setOrderNo(Integer orderNo) {
        this.orderNo = orderNo;
    }

    @Column(name = "ICON_CLS", length = 255)
    public String getIconCls() {
        return iconCls;
    }

    public void setIconCls(String iconCls) {
        this.iconCls = iconCls;
    }

    @Column(name = "ICON", length = 255)
    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    @Column(name = "TYPE")
    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
    @JoinColumn(name = "PARENT_ID")
    public Resource getParentResource() {
        return parentResource;
    }

    public void setParentResource(Resource parentResource) {
        this.parentResource = parentResource;
    }

    @ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY)
    @JoinTable(name = "T_BASE_ROLE_RESOURCE", joinColumns = {
            @JoinColumn(name = "RESOURCE_ID") }, inverseJoinColumns = { @JoinColumn(name = "ROLE_ID") })
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region = CacheConstants.HIBERNATE_CACHE_BASE)
    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    /**
     * ?,","
     * 
     * @return
     */
    @Transient
    public String getRoleNames() {
        return ConvertUtils.convertElementPropertyToString(roles, "name", ", ");
    }

    @ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY)
    @JoinTable(name = "T_BASE_USER_RESOURCE", joinColumns = {
            @JoinColumn(name = "RESOURCE_ID") }, inverseJoinColumns = { @JoinColumn(name = "USER_ID") })
    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    @OneToMany(mappedBy = "parentResource", cascade = { CascadeType.REMOVE })
    public List<Resource> getSubResources() {
        return subResources;
    }

    public void setSubResources(List<Resource> subResources) {
        this.subResources = subResources;
    }

    @Transient
    public List<Resource> getNavigation() {
        ArrayList<Resource> arrayList = new ArrayList<Resource>();
        Resource resource = this;
        arrayList.add(resource);
        while (null != resource.parentResource) {
            resource = resource.parentResource;
            arrayList.add(0, resource);
        }
        return arrayList;
    }

    @Transient
    public Long get_parentId() {
        if (parentResource != null) {
            _parentId = parentResource.getId();
        }
        return _parentId;
    }

    public void set_parentId(Long _parentId) {
        this._parentId = _parentId;
    }

    /**
     * ???
     */
    @Transient
    public String getTypeView() {
        ResourceType r = ResourceType.getResourceType(type);
        String str = "";
        if (r != null) {
            str = r.getDescription();
        }
        return str;
    }

}