All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xlrit.gears.base.model.Role Maven / Gradle / Ivy

There is a newer version: 1.17.5
Show newest version
package com.xlrit.gears.base.model;

import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

import com.xlrit.gears.base.meta.*;
import com.xlrit.gears.base.util.DisplayedHelper;
import jakarta.persistence.*;
import jakarta.persistence.Id;
import lombok.Getter;
import lombok.Setter;

/*
ROLE in ROLES =
    name            : text // unique
    type            : text
    description     : text
    active          : boolean
    USERS           : multiple USER opposite of ROLES
 */
@EntityType(typeName = "ROLE", collectionName = "ROLES")
@Attributes({
	"name",
	"type",
	"description",
	"active",
	"users"
})
@Entity
@Table(name = "SYS_ROLE")
@org.hibernate.envers.Audited
public class Role implements Identifiable, Auditable {
	public static final Relation.ManyToMany USERS = Relation.manyToMany(User.class, Role::getUsers, Role::setUsers, User::getRoles, User::setRoles);

	@Getter
	@Id @Column(name = "id_", length = 26)
	private String id;

	@Getter @Setter
	@Attribute(formalName = "name", type = DataType.TEXT, label = "Name")
	@Basic @Column(name = "name_")
	private String name;

	@Getter @Setter
	@Attribute(formalName = "type", type = DataType.TEXT, label = "Type")
	@Basic @Column(name = "type_")
	private String type;

	@Getter @Setter
	@Attribute(formalName = "description", type = DataType.TEXT, label = "Description")
	@Basic @Column(name = "description_")
	private String description;

	@Getter @Setter
	@Attribute(formalName = "active", type = DataType.BOOLEAN, label = "Active")
	@Basic @Column(name = "active_")
	private Boolean active;

	@Getter @Setter
	@Basic @Column(name = "external_id_")
	private String externalId;

	@Getter @Setter
	@Basic @Column(name = "external_name_")
	private String externalName;

	@Getter @Setter
	@Attribute(formalName = "USERS", type = DataType.ENTITY, label = "Users")
	@ManyToMany(fetch = FetchType.LAZY, mappedBy = "roles")
	private List users;

	@Getter @Setter
	@Attribute(formalName = "CREATOR", type = DataType.ENTITY, label = "Creator")
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "creator_id_")
	private User creator;

	@Getter @Setter
	@Attribute(formalName = "created_at", type = DataType.DATETIME, label = "Created at")
	@Basic @Column(name = "created_at_")
	private OffsetDateTime createdAt;

	@Getter @Setter
	@Attribute(formalName = "MODIFIER", type = DataType.ENTITY, label = "Modifier")
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "modifier_id_")
	private User modifier;

	@Getter @Setter
	@Attribute(formalName = "modified_at", type = DataType.DATETIME, label = "Modified at")
	@Basic @Column(name = "modified_at_")
	private OffsetDateTime modifiedAt;

	public Role() {}

	public Role(String id) {
		this.id = id;
		this.users = new ArrayList<>();
	}

	public void setUsersBidi(Collection users) {
		USERS.set(this, new ArrayList<>(users));
	}

	public void addUsersBidi(Collection extraUsers) {
		USERS.add(this, extraUsers);
	}

	public void removeUsersBidi(Collection superfluousUsers) {
		USERS.remove(this, superfluousUsers);
	}

	@Displayed
	public String defaultDisplayed() {
		return new DisplayedHelper()
			.excludeNulls()
			.add("id", this.id)
			.add("name", this.name)
			.add("description", this.description)
			.toString();
	}

	@Override
	public boolean equals(Object obj) {
		if (this.id == null) return super.equals(obj);
		if (!(obj instanceof Role that)) return false;
		return Objects.equals(this.id, that.getId());
	}

	@Override
	public int hashCode() {
		return Objects.hash(this.id);
	}

	@Override
	public String toString() {
		return "ROLE[" + this.defaultDisplayed() + "]";
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy